blob: 62212231ce622f7b30f0c77a76e01497c2cbc639 [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{
495 if (tbl->target_highest_slotid == target_highest_slotid)
496 return;
497 tbl->target_highest_slotid = target_highest_slotid;
498 tbl->generation++;
499}
500
Trond Myklebustce008c42012-11-20 15:16:30 -0500501void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
502 u32 target_highest_slotid)
503{
504 spin_lock(&tbl->slot_tbl_lock);
505 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
506 spin_unlock(&tbl->slot_tbl_lock);
507}
508
Trond Myklebustda0507b2012-11-20 18:10:30 -0500509static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
510 u32 highest_slotid)
511{
512 unsigned int max_slotid, i;
513
514 if (tbl->server_highest_slotid == highest_slotid)
515 return;
516 if (tbl->highest_used_slotid > highest_slotid)
517 return;
518 max_slotid = min(tbl->max_slots - 1, highest_slotid);
519 /* Reset the seq_nr for deallocated slots */
520 for (i = tbl->server_highest_slotid + 1; i <= max_slotid; i++)
521 tbl->slots[i].seq_nr = 1;
522 tbl->server_highest_slotid = highest_slotid;
523}
524
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500525static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
526 struct nfs4_slot *slot,
527 struct nfs4_sequence_res *res)
528{
529 spin_lock(&tbl->slot_tbl_lock);
530 if (tbl->generation != slot->generation)
531 goto out;
Trond Myklebustda0507b2012-11-20 18:10:30 -0500532 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500533 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
534out:
535 spin_unlock(&tbl->slot_tbl_lock);
536}
537
Trond Myklebust14516c32010-07-31 14:29:06 -0400538static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400539{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500540 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500541 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400542 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400543 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400544
545 /*
546 * sr_status remains 1 if an RPC level error occurred. The server
547 * may or may not have processed the sequence operation..
548 * Proceed as if the server received and processed the sequence
549 * operation.
550 */
551 if (res->sr_status == 1)
552 res->sr_status = NFS_OK;
553
Bryan Schumaker468f8612011-04-18 15:57:32 -0400554 /* don't increment the sequence number if the task wasn't sent */
555 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400556 goto out;
557
Trond Myklebust933602e2012-11-16 12:12:38 -0500558 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500559 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500560
Andy Adamson691daf32009-12-04 15:55:39 -0500561 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400562 switch (res->sr_status) {
563 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400564 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500565 ++slot->seq_nr;
566 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500567 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400568 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500569 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500570 if (res->sr_status_flags != 0)
571 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500572 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400573 break;
574 case -NFS4ERR_DELAY:
575 /* The server detected a resend of the RPC call and
576 * returned NFS4ERR_DELAY as per Section 2.10.6.2
577 * of RFC5661.
578 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500579 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400580 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500581 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500582 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400583 goto out_retry;
584 default:
585 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500586 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400587 }
588out:
589 /* The session may be reset by one of the error handlers. */
590 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400591 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400592 return 1;
593out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400594 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400595 goto out;
596 rpc_delay(task, NFS4_POLL_RETRY_MAX);
597 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400598}
599
Trond Myklebust14516c32010-07-31 14:29:06 -0400600static int nfs4_sequence_done(struct rpc_task *task,
601 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400602{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500603 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400604 return 1;
605 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400606}
607
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400608/*
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500609 * nfs4_alloc_slot - efficiently look for a free slot
Benny Halevy510b8172009-04-01 09:22:14 -0400610 *
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500611 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
Benny Halevy510b8172009-04-01 09:22:14 -0400612 * If found, we mark the slot as used, update the highest_used_slotid,
613 * and respectively set up the sequence operation args.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400614 *
615 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400616 */
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500617static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400618{
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500619 struct nfs4_slot *ret = NULL;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500620 u32 slotid;
Benny Halevy510b8172009-04-01 09:22:14 -0400621
Trond Myklebust45d43c22012-02-06 19:38:51 -0500622 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400623 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
624 tbl->max_slots);
625 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
626 if (slotid >= tbl->max_slots)
627 goto out;
628 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500629 if (slotid > tbl->highest_used_slotid ||
630 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400631 tbl->highest_used_slotid = slotid;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500632 ret = &tbl->slots[slotid];
633 ret->renewal_time = jiffies;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500634 ret->generation = tbl->generation;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500635
Benny Halevy510b8172009-04-01 09:22:14 -0400636out:
637 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500638 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
639 ret ? ret->slot_nr : -1);
640 return ret;
Benny Halevy510b8172009-04-01 09:22:14 -0400641}
642
Trond Myklebust9d12b212012-01-17 22:04:25 -0500643static void nfs41_init_sequence(struct nfs4_sequence_args *args,
644 struct nfs4_sequence_res *res, int cache_reply)
645{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500646 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500647 args->sa_cache_this = 0;
648 if (cache_reply)
649 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500650 res->sr_slot = NULL;
651}
652
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000653int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 struct nfs4_sequence_args *args,
655 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 struct rpc_task *task)
657{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400658 struct nfs4_slot *slot;
659 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400660
661 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400663 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 return 0;
665
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400666 tbl = &session->fc_slot_table;
667
668 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400669 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800670 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500671 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500672 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400673 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500674 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500675 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400676 }
677
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800678 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
679 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
680 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
681 spin_unlock(&tbl->slot_tbl_lock);
682 dprintk("%s enforce FIFO order\n", __func__);
683 return -EAGAIN;
684 }
685
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500686 slot = nfs4_alloc_slot(tbl);
687 if (slot == NULL) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400688 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
689 spin_unlock(&tbl->slot_tbl_lock);
690 dprintk("<-- %s: no free slots\n", __func__);
691 return -EAGAIN;
692 }
693 spin_unlock(&tbl->slot_tbl_lock);
694
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800695 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500696
697 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400698
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500699 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
700 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400701
Benny Halevydfb4f3092010-09-24 09:17:01 -0400702 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400703 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400704 /*
705 * sr_status is only set in decode_sequence, and so will remain
706 * set to 1 if an rpc level failure occurs.
707 */
708 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400709 return 0;
710}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000711EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400712
Trond Myklebust035168a2010-06-16 09:52:26 -0400713int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 struct nfs4_sequence_args *args,
715 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716 struct rpc_task *task)
717{
Trond Myklebust035168a2010-06-16 09:52:26 -0400718 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719 int ret = 0;
720
Trond Myklebust9d12b212012-01-17 22:04:25 -0500721 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400722 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400723
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500724 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500726 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400727
Trond Myklebust9d12b212012-01-17 22:04:25 -0500728 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729out:
730 dprintk("<-- %s status=%d\n", __func__, ret);
731 return ret;
732}
733
734struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400735 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736 struct nfs4_sequence_args *seq_args;
737 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400738};
739
740static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
741{
742 struct nfs41_call_sync_data *data = calldata;
743
Trond Myklebust035168a2010-06-16 09:52:26 -0400744 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
745
746 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500747 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400748 return;
749 rpc_call_start(task);
750}
751
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800752static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
753{
754 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
755 nfs41_call_sync_prepare(task, calldata);
756}
757
Andy Adamson69ab40c2009-04-01 09:22:19 -0400758static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
759{
760 struct nfs41_call_sync_data *data = calldata;
761
Trond Myklebust14516c32010-07-31 14:29:06 -0400762 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400763}
764
Trond Myklebust17280172012-03-11 13:11:00 -0400765static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400767 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400768};
769
Trond Myklebust17280172012-03-11 13:11:00 -0400770static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800771 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
772 .rpc_call_done = nfs41_call_sync_done,
773};
774
Bryan Schumaker7c513052011-03-24 17:12:24 +0000775static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
776 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400777 struct rpc_message *msg,
778 struct nfs4_sequence_args *args,
779 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800780 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400781{
782 int ret;
783 struct rpc_task *task;
784 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400785 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400786 .seq_args = args,
787 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400788 };
789 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000790 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791 .rpc_message = msg,
792 .callback_ops = &nfs41_call_sync_ops,
793 .callback_data = &data
794 };
795
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800796 if (privileged)
797 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400798 task = rpc_run_task(&task_setup);
799 if (IS_ERR(task))
800 ret = PTR_ERR(task);
801 else {
802 ret = task->tk_status;
803 rpc_put_task(task);
804 }
805 return ret;
806}
807
Bryan Schumaker7c513052011-03-24 17:12:24 +0000808int _nfs4_call_sync_session(struct rpc_clnt *clnt,
809 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400810 struct rpc_message *msg,
811 struct nfs4_sequence_args *args,
812 struct nfs4_sequence_res *res,
813 int cache_reply)
814{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500815 nfs41_init_sequence(args, res, cache_reply);
816 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400817}
818
Trond Myklebustdf896452010-06-16 09:52:26 -0400819#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500820static inline
821void nfs41_init_sequence(struct nfs4_sequence_args *args,
822 struct nfs4_sequence_res *res, int cache_reply)
823{
824}
825
Trond Myklebust14516c32010-07-31 14:29:06 -0400826static int nfs4_sequence_done(struct rpc_task *task,
827 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400828{
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400830}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400831#endif /* CONFIG_NFS_V4_1 */
832
Bryan Schumaker7c513052011-03-24 17:12:24 +0000833int _nfs4_call_sync(struct rpc_clnt *clnt,
834 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400835 struct rpc_message *msg,
836 struct nfs4_sequence_args *args,
837 struct nfs4_sequence_res *res,
838 int cache_reply)
839{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500840 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000841 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400842}
843
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000844static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000845int nfs4_call_sync(struct rpc_clnt *clnt,
846 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000847 struct rpc_message *msg,
848 struct nfs4_sequence_args *args,
849 struct nfs4_sequence_res *res,
850 int cache_reply)
851{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000852 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
853 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000854}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400855
Trond Myklebust38478b22006-05-25 01:40:57 -0400856static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Trond Myklebust38478b22006-05-25 01:40:57 -0400858 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Trond Myklebust38478b22006-05-25 01:40:57 -0400860 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400861 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700862 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400863 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700864 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400865 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100868struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400869 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 struct nfs_openargs o_arg;
871 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100872 struct nfs_open_confirmargs c_arg;
873 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500874 struct nfs4_string owner_name;
875 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100876 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100877 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400878 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400880 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100882 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400883 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100884 int rpc_status;
885 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886};
887
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400888
889static void nfs4_init_opendata_res(struct nfs4_opendata *p)
890{
891 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400892 p->o_res.seqid = p->o_arg.seqid;
893 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400894 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400895 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400896 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500897 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400898}
899
Al Viro82a2c1b2011-06-22 18:30:55 -0400900static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400902 const struct iattr *attrs,
903 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100904{
Al Viro82a2c1b2011-06-22 18:30:55 -0400905 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100906 struct inode *dir = parent->d_inode;
907 struct nfs_server *server = NFS_SERVER(dir);
908 struct nfs4_opendata *p;
909
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400910 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100911 if (p == NULL)
912 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400913 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100914 if (p->o_arg.seqid == NULL)
915 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400916 nfs_sb_active(dentry->d_sb);
917 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100918 p->dir = parent;
919 p->owner = sp;
920 atomic_inc(&sp->so_count);
921 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 p->o_arg.open_flags = flags;
923 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700924 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
925 * will return permission denied for all bits until close */
926 if (!(flags & O_EXCL)) {
927 /* ask server to check for all possible rights as results
928 * are cached */
929 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
930 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700931 }
David Howells7539bba2006-08-22 20:06:09 -0400932 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400933 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
934 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400935 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100936 p->o_arg.server = server;
937 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400938 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100939 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500940 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500941 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400942
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100943 p->o_arg.u.attrs = &p->attrs;
944 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500945
946 verf[0] = jiffies;
947 verf[1] = current->pid;
948 memcpy(p->o_arg.u.verifier.data, verf,
949 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100950 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100951 p->c_arg.fh = &p->o_res.fh;
952 p->c_arg.stateid = &p->o_res.stateid;
953 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400954 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400955 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100956 return p;
957err_free:
958 kfree(p);
959err:
960 dput(parent);
961 return NULL;
962}
963
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400964static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100965{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400966 struct nfs4_opendata *p = container_of(kref,
967 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400968 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400969
970 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 if (p->state != NULL)
972 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400973 nfs4_put_state_owner(p->owner);
974 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400975 dput(p->dentry);
976 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500977 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400978 kfree(p);
979}
980
981static void nfs4_opendata_put(struct nfs4_opendata *p)
982{
983 if (p != NULL)
984 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100985}
986
Trond Myklebust06f814a2006-01-03 09:55:07 +0100987static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
988{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100989 int ret;
990
Trond Myklebust06f814a2006-01-03 09:55:07 +0100991 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100992 return ret;
993}
994
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400996{
997 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998
Trond Myklebust536e43d2012-01-17 22:04:26 -0500999 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 goto out;
1001 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001002 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001003 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1004 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001005 break;
1006 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001007 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1008 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001009 break;
1010 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001011 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1012 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001013 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015 return ret;
1016}
1017
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001020 if (delegation == NULL)
1021 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001024 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001026 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 return 1;
1028}
1029
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001031{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001033 case FMODE_WRITE:
1034 state->n_wronly++;
1035 break;
1036 case FMODE_READ:
1037 state->n_rdonly++;
1038 break;
1039 case FMODE_READ|FMODE_WRITE:
1040 state->n_rdwr++;
1041 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001043}
1044
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001046{
1047 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001048 nfs4_stateid_copy(&state->stateid, stateid);
1049 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001051 case FMODE_READ:
1052 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1053 break;
1054 case FMODE_WRITE:
1055 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1056 break;
1057 case FMODE_READ|FMODE_WRITE:
1058 set_bit(NFS_O_RDWR_STATE, &state->flags);
1059 }
1060}
1061
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001063{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001064 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001066 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001067}
1068
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069static 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 -07001070{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001071 /*
1072 * Protect the call to nfs4_state_set_mode_locked and
1073 * serialise the stateid update
1074 */
1075 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001076 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001077 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001078 set_bit(NFS_DELEGATED_STATE, &state->flags);
1079 }
1080 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001082 write_sequnlock(&state->seqlock);
1083 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001085 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088static 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 -05001089{
1090 struct nfs_inode *nfsi = NFS_I(state->inode);
1091 struct nfs_delegation *deleg_cur;
1092 int ret = 0;
1093
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001094 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001095
1096 rcu_read_lock();
1097 deleg_cur = rcu_dereference(nfsi->delegation);
1098 if (deleg_cur == NULL)
1099 goto no_delegation;
1100
1101 spin_lock(&deleg_cur->lock);
1102 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001104 goto no_delegation_unlock;
1105
1106 if (delegation == NULL)
1107 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001108 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001109 goto no_delegation_unlock;
1110
Trond Myklebustb7391f42008-12-23 15:21:52 -05001111 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001113 ret = 1;
1114no_delegation_unlock:
1115 spin_unlock(&deleg_cur->lock);
1116no_delegation:
1117 rcu_read_unlock();
1118
1119 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001121 ret = 1;
1122 }
1123
1124 return ret;
1125}
1126
1127
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001128static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001129{
1130 struct nfs_delegation *delegation;
1131
1132 rcu_read_lock();
1133 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001134 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001135 rcu_read_unlock();
1136 return;
1137 }
1138 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001139 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001140}
1141
Trond Myklebust6ee41262007-07-08 14:11:36 -04001142static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001143{
1144 struct nfs4_state *state = opendata->state;
1145 struct nfs_inode *nfsi = NFS_I(state->inode);
1146 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001147 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001149 nfs4_stateid stateid;
1150 int ret = -EAGAIN;
1151
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001154 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155 if (can_open_cached(state, fmode, open_mode)) {
1156 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001157 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001158 goto out_return_state;
1159 }
1160 spin_unlock(&state->owner->so_lock);
1161 }
Trond Myklebust34310432008-12-23 15:21:38 -05001162 rcu_read_lock();
1163 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001164 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001165 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001166 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001167 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001168 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001169 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001170 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001171 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001172 if (ret != 0)
1173 goto out;
1174 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001175
1176 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001178 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001179 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001180out:
1181 return ERR_PTR(ret);
1182out_return_state:
1183 atomic_inc(&state->count);
1184 return state;
1185}
1186
Andy Adamsone23008e2012-10-02 21:07:32 -04001187static void
1188nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1189{
1190 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1191 struct nfs_delegation *delegation;
1192 int delegation_flags = 0;
1193
1194 rcu_read_lock();
1195 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1196 if (delegation)
1197 delegation_flags = delegation->flags;
1198 rcu_read_unlock();
1199 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1200 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1201 "returning a delegation for "
1202 "OPEN(CLAIM_DELEGATE_CUR)\n",
1203 clp->cl_hostname);
1204 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1205 nfs_inode_set_delegation(state->inode,
1206 data->owner->so_cred,
1207 &data->o_res);
1208 else
1209 nfs_inode_reclaim_delegation(state->inode,
1210 data->owner->so_cred,
1211 &data->o_res);
1212}
1213
1214/*
1215 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1216 * and update the nfs4_state.
1217 */
1218static struct nfs4_state *
1219_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1220{
1221 struct inode *inode = data->state->inode;
1222 struct nfs4_state *state = data->state;
1223 int ret;
1224
1225 if (!data->rpc_done) {
1226 ret = data->rpc_status;
1227 goto err;
1228 }
1229
1230 ret = -ESTALE;
1231 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1232 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1233 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1234 goto err;
1235
1236 ret = -ENOMEM;
1237 state = nfs4_get_open_state(inode, data->owner);
1238 if (state == NULL)
1239 goto err;
1240
1241 ret = nfs_refresh_inode(inode, &data->f_attr);
1242 if (ret)
1243 goto err;
1244
1245 if (data->o_res.delegation_type != 0)
1246 nfs4_opendata_check_deleg(data, state);
1247 update_open_stateid(state, &data->o_res.stateid, NULL,
1248 data->o_arg.fmode);
1249
1250 return state;
1251err:
1252 return ERR_PTR(ret);
1253
1254}
1255
1256static struct nfs4_state *
1257_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001258{
1259 struct inode *inode;
1260 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001261 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001262
Trond Myklebustaac00a82007-07-05 19:02:21 -04001263 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001265 goto out;
1266 }
1267
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001268 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001269 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001270 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001271 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001272 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001273 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001274 goto err;
1275 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001276 state = nfs4_get_open_state(inode, data->owner);
1277 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001278 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001279 if (data->o_res.delegation_type != 0)
1280 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001281 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001283 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001284out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001285 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001286err_put_inode:
1287 iput(inode);
1288err:
1289 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001290}
1291
Andy Adamsone23008e2012-10-02 21:07:32 -04001292static struct nfs4_state *
1293nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1294{
1295 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1296 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1297 return _nfs4_opendata_to_nfs4_state(data);
1298}
1299
Trond Myklebust864472e2006-01-03 09:55:15 +01001300static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1301{
1302 struct nfs_inode *nfsi = NFS_I(state->inode);
1303 struct nfs_open_context *ctx;
1304
1305 spin_lock(&state->inode->i_lock);
1306 list_for_each_entry(ctx, &nfsi->open_files, list) {
1307 if (ctx->state != state)
1308 continue;
1309 get_nfs_open_context(ctx);
1310 spin_unlock(&state->inode->i_lock);
1311 return ctx;
1312 }
1313 spin_unlock(&state->inode->i_lock);
1314 return ERR_PTR(-ENOENT);
1315}
1316
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001317static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1318{
1319 struct nfs4_opendata *opendata;
1320
Al Viro3d4ff432011-06-22 18:40:12 -04001321 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001322 if (opendata == NULL)
1323 return ERR_PTR(-ENOMEM);
1324 opendata->state = state;
1325 atomic_inc(&state->count);
1326 return opendata;
1327}
1328
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001329static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001330{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001331 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001332 int ret;
1333
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 opendata->o_arg.open_flags = 0;
1335 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001336 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1337 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1338 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001339 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 if (ret != 0)
1341 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001342 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001343 if (IS_ERR(newstate))
1344 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001345 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001346 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 return 0;
1348}
1349
1350static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1351{
Trond Myklebust864472e2006-01-03 09:55:15 +01001352 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001353 int ret;
1354
1355 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001356 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001357 smp_rmb();
1358 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001359 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001360 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001361 if (ret != 0)
1362 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001363 if (newstate != state)
1364 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001365 }
1366 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001367 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001368 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001369 if (ret != 0)
1370 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001371 if (newstate != state)
1372 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001373 }
1374 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001375 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001376 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001377 if (ret != 0)
1378 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001379 if (newstate != state)
1380 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001381 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001382 /*
1383 * We may have performed cached opens for all three recoveries.
1384 * Check if we need to update the current stateid.
1385 */
1386 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001387 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001388 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001389 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001390 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001391 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001392 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001393 return 0;
1394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396/*
1397 * OPEN_RECLAIM:
1398 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001400static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001402 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001403 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001404 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 int status;
1406
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001407 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1408 if (IS_ERR(opendata))
1409 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001410 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1411 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001412 rcu_read_lock();
1413 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001414 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001415 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001416 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001417 opendata->o_arg.u.delegation_type = delegation_type;
1418 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001419 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return status;
1421}
1422
Trond Myklebust539cd032007-06-05 11:46:42 -04001423static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct nfs_server *server = NFS_SERVER(state->inode);
1426 struct nfs4_exception exception = { };
1427 int err;
1428 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001429 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001430 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001431 break;
1432 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 } while (exception.retry);
1434 return err;
1435}
1436
Trond Myklebust864472e2006-01-03 09:55:15 +01001437static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1438{
1439 struct nfs_open_context *ctx;
1440 int ret;
1441
1442 ctx = nfs4_state_find_open_context(state);
1443 if (IS_ERR(ctx))
1444 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001445 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001446 put_nfs_open_context(ctx);
1447 return ret;
1448}
1449
Trond Myklebust13437e12007-07-06 15:10:43 -04001450static 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 -07001451{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001452 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001453 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001455 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1456 if (IS_ERR(opendata))
1457 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001458 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001459 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001460 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001461 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Trond Myklebust13437e12007-07-06 15:10:43 -04001465int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001468 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int err;
1470 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001471 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 switch (err) {
1473 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001474 case -ENOENT:
1475 case -ESTALE:
1476 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001477 case -NFS4ERR_BADSESSION:
1478 case -NFS4ERR_BADSLOT:
1479 case -NFS4ERR_BAD_HIGH_SLOT:
1480 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1481 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001482 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 case -NFS4ERR_STALE_CLIENTID:
1485 case -NFS4ERR_STALE_STATEID:
1486 case -NFS4ERR_EXPIRED:
1487 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001488 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001489 goto out;
1490 case -ERESTARTSYS:
1491 /*
1492 * The show must go on: exit, but mark the
1493 * stateid as needing recovery.
1494 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001495 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001496 case -NFS4ERR_ADMIN_REVOKED:
1497 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001498 nfs_inode_find_state_and_recover(state->inode,
1499 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001500 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001501 case -EKEYEXPIRED:
1502 /*
1503 * User RPCSEC_GSS context has expired.
1504 * We cannot recover this stateid now, so
1505 * skip it and allow recovery thread to
1506 * proceed.
1507 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001508 case -ENOMEM:
1509 err = 0;
1510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 err = nfs4_handle_exception(server, err, &exception);
1513 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001514out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return err;
1516}
1517
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001518static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1519{
1520 struct nfs4_opendata *data = calldata;
1521
1522 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001523 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001524 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001525 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001526 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001527 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001528 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001529}
1530
1531static void nfs4_open_confirm_release(void *calldata)
1532{
1533 struct nfs4_opendata *data = calldata;
1534 struct nfs4_state *state = NULL;
1535
1536 /* If this request hasn't been cancelled, do nothing */
1537 if (data->cancelled == 0)
1538 goto out_free;
1539 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001540 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001541 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001542 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001543 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001544 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001545out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001546 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001547}
1548
1549static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001550 .rpc_call_done = nfs4_open_confirm_done,
1551 .rpc_release = nfs4_open_confirm_release,
1552};
1553
1554/*
1555 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1556 */
1557static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1558{
1559 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1560 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001561 struct rpc_message msg = {
1562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1563 .rpc_argp = &data->c_arg,
1564 .rpc_resp = &data->c_res,
1565 .rpc_cred = data->owner->so_cred,
1566 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001567 struct rpc_task_setup task_setup_data = {
1568 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001569 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001570 .callback_ops = &nfs4_open_confirm_ops,
1571 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001572 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001573 .flags = RPC_TASK_ASYNC,
1574 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int status;
1576
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001577 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001578 data->rpc_done = 0;
1579 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001580 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001582 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001583 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001584 status = nfs4_wait_for_completion_rpc_task(task);
1585 if (status != 0) {
1586 data->cancelled = 1;
1587 smp_wmb();
1588 } else
1589 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001590 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return status;
1592}
1593
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001594static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596 struct nfs4_opendata *data = calldata;
1597 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001598
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001599 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1600 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001601 /*
1602 * Check if we still need to send an OPEN call, or if we can use
1603 * a delegation instead.
1604 */
1605 if (data->state != NULL) {
1606 struct nfs_delegation *delegation;
1607
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001609 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001610 rcu_read_lock();
1611 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001612 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1613 can_open_delegated(delegation, data->o_arg.fmode))
1614 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001615 rcu_read_unlock();
1616 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001617 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001618 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001619 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001620 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001621 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001622 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1623 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001624 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001625 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001626 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001627 &data->o_res.seq_res,
1628 task) != 0)
1629 nfs_release_seqid(data->o_arg.seqid);
1630 else
1631 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001632 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001633unlock_no_action:
1634 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001635out_no_action:
1636 task->tk_action = NULL;
1637
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001640static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1641{
1642 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1643 nfs4_open_prepare(task, calldata);
1644}
1645
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646static void nfs4_open_done(struct rpc_task *task, void *calldata)
1647{
1648 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001650 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001651
Trond Myklebust14516c32010-07-31 14:29:06 -04001652 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1653 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001654
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001655 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001656 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1657 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001658 case S_IFREG:
1659 break;
1660 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001662 break;
1663 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001665 break;
1666 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001667 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001668 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001669 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001670 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001671 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1672 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001673 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001674 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001675}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001676
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001677static void nfs4_open_release(void *calldata)
1678{
1679 struct nfs4_opendata *data = calldata;
1680 struct nfs4_state *state = NULL;
1681
1682 /* If this request hasn't been cancelled, do nothing */
1683 if (data->cancelled == 0)
1684 goto out_free;
1685 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001686 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 goto out_free;
1688 /* In case we need an open_confirm, no cleanup! */
1689 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1690 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001691 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001692 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001693 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001695 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696}
1697
1698static const struct rpc_call_ops nfs4_open_ops = {
1699 .rpc_call_prepare = nfs4_open_prepare,
1700 .rpc_call_done = nfs4_open_done,
1701 .rpc_release = nfs4_open_release,
1702};
1703
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001704static const struct rpc_call_ops nfs4_recover_open_ops = {
1705 .rpc_call_prepare = nfs4_recover_open_prepare,
1706 .rpc_call_done = nfs4_open_done,
1707 .rpc_release = nfs4_open_release,
1708};
1709
1710static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001711{
1712 struct inode *dir = data->dir->d_inode;
1713 struct nfs_server *server = NFS_SERVER(dir);
1714 struct nfs_openargs *o_arg = &data->o_arg;
1715 struct nfs_openres *o_res = &data->o_res;
1716 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001717 struct rpc_message msg = {
1718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1719 .rpc_argp = o_arg,
1720 .rpc_resp = o_res,
1721 .rpc_cred = data->owner->so_cred,
1722 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001723 struct rpc_task_setup task_setup_data = {
1724 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001725 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001726 .callback_ops = &nfs4_open_ops,
1727 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001728 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001729 .flags = RPC_TASK_ASYNC,
1730 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001731 int status;
1732
Trond Myklebust9d12b212012-01-17 22:04:25 -05001733 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001734 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001735 data->rpc_done = 0;
1736 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001737 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001738 if (isrecover)
1739 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001740 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001741 if (IS_ERR(task))
1742 return PTR_ERR(task);
1743 status = nfs4_wait_for_completion_rpc_task(task);
1744 if (status != 0) {
1745 data->cancelled = 1;
1746 smp_wmb();
1747 } else
1748 status = data->rpc_status;
1749 rpc_put_task(task);
1750
1751 return status;
1752}
1753
1754static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1755{
1756 struct inode *dir = data->dir->d_inode;
1757 struct nfs_openres *o_res = &data->o_res;
1758 int status;
1759
1760 status = nfs4_run_open_task(data, 1);
1761 if (status != 0 || !data->rpc_done)
1762 return status;
1763
Trond Myklebust6926afd2012-01-07 13:22:46 -05001764 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1765
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001766 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1767 status = _nfs4_proc_open_confirm(data);
1768 if (status != 0)
1769 return status;
1770 }
1771
1772 return status;
1773}
1774
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001775static int nfs4_opendata_access(struct rpc_cred *cred,
1776 struct nfs4_opendata *opendata,
1777 struct nfs4_state *state, fmode_t fmode)
1778{
1779 struct nfs_access_entry cache;
1780 u32 mask;
1781
1782 /* access call failed or for some reason the server doesn't
1783 * support any access modes -- defer access call until later */
1784 if (opendata->o_res.access_supported == 0)
1785 return 0;
1786
1787 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001788 /* don't check MAY_WRITE - a newly created file may not have
1789 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001790 if (fmode & FMODE_READ)
1791 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001792 if (fmode & FMODE_EXEC)
1793 mask |= MAY_EXEC;
1794
1795 cache.cred = cred;
1796 cache.jiffies = jiffies;
1797 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1798 nfs_access_add_cache(state->inode, &cache);
1799
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001800 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001801 return 0;
1802
1803 /* even though OPEN succeeded, access is denied. Close the file */
1804 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001805 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001806}
1807
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001808/*
1809 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1810 */
1811static int _nfs4_proc_open(struct nfs4_opendata *data)
1812{
1813 struct inode *dir = data->dir->d_inode;
1814 struct nfs_server *server = NFS_SERVER(dir);
1815 struct nfs_openargs *o_arg = &data->o_arg;
1816 struct nfs_openres *o_res = &data->o_res;
1817 int status;
1818
1819 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001820 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001821 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001822 if (status != 0) {
1823 if (status == -NFS4ERR_BADNAME &&
1824 !(o_arg->open_flags & O_CREAT))
1825 return -ENOENT;
1826 return status;
1827 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001828
Trond Myklebust6926afd2012-01-07 13:22:46 -05001829 nfs_fattr_map_and_free_names(server, &data->f_attr);
1830
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001831 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001832 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001833 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1834 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001836 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001838 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001841 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Andy Adamsond83217c2011-03-01 01:34:17 +00001845static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001846{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001847 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001848 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001849
Trond Myklebusta78cb572009-08-09 15:06:19 -04001850 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001851 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001852 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001853 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001854 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1855 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001856 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001857 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001858 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001859 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001860 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001861}
1862
Andy Adamsond83217c2011-03-01 01:34:17 +00001863static int nfs4_recover_expired_lease(struct nfs_server *server)
1864{
1865 return nfs4_client_recover_expired_lease(server->nfs_client);
1866}
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868/*
1869 * OPEN_EXPIRED:
1870 * reclaim state on the server after a network partition.
1871 * Assumes caller holds the appropriate lock
1872 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001873static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001875 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001876 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001878 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1879 if (IS_ERR(opendata))
1880 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001881 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001882 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001883 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001884 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001888static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001889{
Trond Myklebust539cd032007-06-05 11:46:42 -04001890 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001891 struct nfs4_exception exception = { };
1892 int err;
1893
1894 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001895 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001896 switch (err) {
1897 default:
1898 goto out;
1899 case -NFS4ERR_GRACE:
1900 case -NFS4ERR_DELAY:
1901 nfs4_handle_exception(server, err, &exception);
1902 err = 0;
1903 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001904 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001905out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001906 return err;
1907}
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001912 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Trond Myklebust864472e2006-01-03 09:55:15 +01001914 ctx = nfs4_state_find_open_context(state);
1915 if (IS_ERR(ctx))
1916 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001917 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001918 put_nfs_open_context(ctx);
1919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001922#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001923static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001924{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001925 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001926 nfs4_stateid *stateid = &state->stateid;
1927 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001928
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001929 /* If a state reset has been done, test_stateid is unneeded */
1930 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1931 return;
1932
1933 status = nfs41_test_stateid(server, stateid);
1934 if (status != NFS_OK) {
1935 /* Free the stateid unless the server explicitly
1936 * informs us the stateid is unrecognized. */
1937 if (status != -NFS4ERR_BAD_STATEID)
1938 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001939 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001940
Bryan Schumaker69388672012-09-26 15:25:52 -04001941 write_seqlock(&state->seqlock);
1942 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1943 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001944 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1945 }
1946}
1947
1948/**
1949 * nfs41_check_open_stateid - possibly free an open stateid
1950 *
1951 * @state: NFSv4 state for an inode
1952 *
1953 * Returns NFS_OK if recovery for this stateid is now finished.
1954 * Otherwise a negative NFS4ERR value is returned.
1955 */
1956static int nfs41_check_open_stateid(struct nfs4_state *state)
1957{
1958 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001959 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001960 int status;
1961
1962 /* If a state reset has been done, test_stateid is unneeded */
1963 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1964 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1965 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1966 return -NFS4ERR_BAD_STATEID;
1967
1968 status = nfs41_test_stateid(server, stateid);
1969 if (status != NFS_OK) {
1970 /* Free the stateid unless the server explicitly
1971 * informs us the stateid is unrecognized. */
1972 if (status != -NFS4ERR_BAD_STATEID)
1973 nfs41_free_stateid(server, stateid);
1974
1975 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1976 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1977 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001978 }
1979 return status;
1980}
1981
1982static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1983{
Chuck Levereb64cf92012-07-11 16:30:05 -04001984 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001985
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001986 nfs41_clear_delegation_stateid(state);
1987 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001988 if (status != NFS_OK)
1989 status = nfs4_open_expired(sp, state);
1990 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001991}
1992#endif
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001995 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1996 * fields corresponding to attributes that were used to store the verifier.
1997 * Make sure we clobber those fields in the later setattr call
1998 */
1999static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2000{
2001 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2002 !(sattr->ia_valid & ATTR_ATIME_SET))
2003 sattr->ia_valid |= ATTR_ATIME;
2004
2005 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2006 !(sattr->ia_valid & ATTR_MTIME_SET))
2007 sattr->ia_valid |= ATTR_MTIME;
2008}
2009
2010/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002011 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 */
Andy Adamson82be4172012-05-23 05:02:35 -04002013static int _nfs4_do_open(struct inode *dir,
2014 struct dentry *dentry,
2015 fmode_t fmode,
2016 int flags,
2017 struct iattr *sattr,
2018 struct rpc_cred *cred,
2019 struct nfs4_state **res,
2020 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 struct nfs4_state_owner *sp;
2023 struct nfs4_state *state = NULL;
2024 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002025 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002026 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002030 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2031 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2033 goto out_err;
2034 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002035 status = nfs4_recover_expired_lease(server);
2036 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002037 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002038 if (dentry->d_inode != NULL)
2039 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002040 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04002041 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002042 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002043 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Andy Adamson82be4172012-05-23 05:02:35 -04002045 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2046 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2047 if (!opendata->f_attr.mdsthreshold)
2048 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002049 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002050 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002051 if (dentry->d_inode != NULL)
2052 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002053
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002054 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002056 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002058 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002059 status = PTR_ERR(state);
2060 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002061 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002062 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002063 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002064
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002065 status = nfs4_opendata_access(cred, opendata, state, fmode);
2066 if (status != 0)
2067 goto err_opendata_put;
2068
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002069 if (opendata->o_arg.open_flags & O_EXCL) {
2070 nfs4_exclusive_attrset(opendata, sattr);
2071
2072 nfs_fattr_init(opendata->o_res.f_attr);
2073 status = nfs4_do_setattr(state->inode, cred,
2074 opendata->o_res.f_attr, sattr,
2075 state);
2076 if (status == 0)
2077 nfs_setattr_update_inode(state->inode, sattr);
2078 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2079 }
Andy Adamson82be4172012-05-23 05:02:35 -04002080
2081 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2082 *ctx_th = opendata->f_attr.mdsthreshold;
2083 else
2084 kfree(opendata->f_attr.mdsthreshold);
2085 opendata->f_attr.mdsthreshold = NULL;
2086
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002087 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 *res = state;
2090 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002091err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002092 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002093 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002094err_put_state_owner:
2095 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 *res = NULL;
2098 return status;
2099}
2100
2101
Andy Adamson82be4172012-05-23 05:02:35 -04002102static struct nfs4_state *nfs4_do_open(struct inode *dir,
2103 struct dentry *dentry,
2104 fmode_t fmode,
2105 int flags,
2106 struct iattr *sattr,
2107 struct rpc_cred *cred,
2108 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 struct nfs4_exception exception = { };
2111 struct nfs4_state *res;
2112 int status;
2113
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002114 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002116 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2117 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (status == 0)
2119 break;
2120 /* NOTE: BAD_SEQID means the server and client disagree about the
2121 * book-keeping w.r.t. state-changing operations
2122 * (OPEN/CLOSE/LOCK/LOCKU...)
2123 * It is actually a sign of a bug on the client or on the server.
2124 *
2125 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002126 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 * have unhashed the old state_owner for us, and that we can
2128 * therefore safely retry using a new one. We should still warn
2129 * the user though...
2130 */
2131 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002132 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002133 " returned a bad sequence-id error!\n",
2134 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 exception.retry = 1;
2136 continue;
2137 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002138 /*
2139 * BAD_STATEID on OPEN means that the server cancelled our
2140 * state before it received the OPEN_CONFIRM.
2141 * Recover by retrying the request as per the discussion
2142 * on Page 181 of RFC3530.
2143 */
2144 if (status == -NFS4ERR_BAD_STATEID) {
2145 exception.retry = 1;
2146 continue;
2147 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002148 if (status == -EAGAIN) {
2149 /* We must have found a delegation */
2150 exception.retry = 1;
2151 continue;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2154 status, &exception));
2155 } while (exception.retry);
2156 return res;
2157}
2158
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002159static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2160 struct nfs_fattr *fattr, struct iattr *sattr,
2161 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002163 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002165 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 .iap = sattr,
2167 .server = server,
2168 .bitmask = server->attr_bitmask,
2169 };
2170 struct nfs_setattrres res = {
2171 .fattr = fattr,
2172 .server = server,
2173 };
2174 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2176 .rpc_argp = &arg,
2177 .rpc_resp = &res,
2178 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002180 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002181 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Trond Myklebust0e574af2005-10-27 22:12:38 -04002183 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Trond Myklebust4fc87962012-03-08 17:42:01 -05002185 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002186 struct nfs_lockowner lockowner = {
2187 .l_owner = current->files,
2188 .l_pid = current->tgid,
2189 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002190 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002191 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002192 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2193 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002194 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002195 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002196 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Bryan Schumaker7c513052011-03-24 17:12:24 +00002198 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002199 if (status == 0 && state != NULL)
2200 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002201 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002204static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2205 struct nfs_fattr *fattr, struct iattr *sattr,
2206 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002208 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002209 struct nfs4_exception exception = {
2210 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002211 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002212 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 int err;
2214 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002215 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2216 switch (err) {
2217 case -NFS4ERR_OPENMODE:
2218 if (state && !(state->state & FMODE_WRITE)) {
2219 err = -EBADF;
2220 if (sattr->ia_valid & ATTR_OPEN)
2221 err = -EACCES;
2222 goto out;
2223 }
2224 }
2225 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002227out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return err;
2229}
2230
2231struct nfs4_closedata {
2232 struct inode *inode;
2233 struct nfs4_state *state;
2234 struct nfs_closeargs arg;
2235 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002236 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002237 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002238 bool roc;
2239 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240};
2241
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002242static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002243{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002244 struct nfs4_closedata *calldata = data;
2245 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002246 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002247
Fred Isamanf7e89172011-01-06 11:36:32 +00002248 if (calldata->roc)
2249 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002250 nfs4_put_open_state(calldata->state);
2251 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002252 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002253 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002254 kfree(calldata);
2255}
2256
Trond Myklebust88069f72009-12-08 08:33:16 -05002257static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2258 fmode_t fmode)
2259{
2260 spin_lock(&state->owner->so_lock);
2261 if (!(fmode & FMODE_READ))
2262 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2263 if (!(fmode & FMODE_WRITE))
2264 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2265 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2266 spin_unlock(&state->owner->so_lock);
2267}
2268
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002269static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002271 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct nfs_server *server = NFS_SERVER(calldata->inode);
2274
Chuck Levera3ca5652012-03-01 17:00:40 -05002275 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002276 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2277 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /* hmm. we are done with the inode, and in the process of freeing
2279 * the state_owner. we keep this around to process errors
2280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 switch (task->tk_status) {
2282 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002283 if (calldata->roc)
2284 pnfs_roc_set_barrier(state->inode,
2285 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002286 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002287 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002288 nfs4_close_clear_stateid_flags(state,
2289 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 break;
2291 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002292 case -NFS4ERR_OLD_STATEID:
2293 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002295 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002298 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2299 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002301 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002302 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002303 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002306static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002308 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002309 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002310 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002311 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002312
Chuck Levera3ca5652012-03-01 17:00:40 -05002313 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002314 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002315 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002316
Trond Myklebust88069f72009-12-08 08:33:16 -05002317 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2318 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002319 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002320 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002321 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002322 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002323 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2324 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2325 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002326 }
2327 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002328 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2329 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2330 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002331 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002332 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002333 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002334
2335 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002336 /* Note: exit _without_ calling nfs4_close_done */
2337 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002338 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002339 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002340
Fred Isamanf7e89172011-01-06 11:36:32 +00002341 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002342 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002343 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002344 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002345 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002346 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002347
Trond Myklebust516a6af2005-10-27 22:12:41 -04002348 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002349 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002350 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002351 &calldata->arg.seq_args,
2352 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002353 task) != 0)
2354 nfs_release_seqid(calldata->arg.seqid);
2355 else
2356 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002357out:
2358 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002361static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002362 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002363 .rpc_call_done = nfs4_close_done,
2364 .rpc_release = nfs4_free_closedata,
2365};
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367/*
2368 * It is possible for data to be read/written from a mem-mapped file
2369 * after the sys_close call (which hits the vfs layer as a flush).
2370 * This means that we can't safely call nfsv4 close on a file until
2371 * the inode is cleared. This in turn means that we are not good
2372 * NFSv4 citizens - we do not indicate to the server to update the file's
2373 * share state even when we are done with one of the three share
2374 * stateid's in the inode.
2375 *
2376 * NOTE: Caller must be holding the sp->so_owner semaphore!
2377 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002378int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002380 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002382 struct nfs4_state_owner *sp = state->owner;
2383 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002384 struct rpc_message msg = {
2385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2386 .rpc_cred = state->owner->so_cred,
2387 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002388 struct rpc_task_setup task_setup_data = {
2389 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002390 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002391 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002392 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002393 .flags = RPC_TASK_ASYNC,
2394 };
Trond Myklebust95121352005-10-18 14:20:12 -07002395 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002397 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002399 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002400 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002401 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002403 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002404 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002406 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002407 if (calldata->arg.seqid == NULL)
2408 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002409 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002410 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002411 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002412 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002413 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002414 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002415 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002416
Trond Myklebust1174dd12010-12-21 10:52:24 -05002417 msg.rpc_argp = &calldata->arg;
2418 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002419 task_setup_data.callback_data = calldata;
2420 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002421 if (IS_ERR(task))
2422 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002423 status = 0;
2424 if (wait)
2425 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002426 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002427 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002428out_free_calldata:
2429 kfree(calldata);
2430out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002431 nfs4_put_open_state(state);
2432 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002433 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Trond Myklebust2b484292010-09-17 10:56:51 -04002436static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002437nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct nfs4_state *state;
2440
Trond Myklebust565277f2007-10-15 18:17:53 -04002441 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002442 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2443 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002444 if (IS_ERR(state))
2445 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002446 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002447 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
Trond Myklebust1185a552009-12-03 15:54:02 -05002450static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002451{
2452 if (ctx->state == NULL)
2453 return;
2454 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002455 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002456 else
Al Viro643168c2011-06-22 18:20:23 -04002457 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2461{
Benny Halevy43652ad2009-04-01 09:21:54 -04002462 struct nfs4_server_caps_arg args = {
2463 .fhandle = fhandle,
2464 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct nfs4_server_caps_res res = {};
2466 struct rpc_message msg = {
2467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002468 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 .rpc_resp = &res,
2470 };
2471 int status;
2472
Bryan Schumaker7c513052011-03-24 17:12:24 +00002473 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (status == 0) {
2475 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002476 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2477 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2478 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2479 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2480 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2482 server->caps |= NFS_CAP_ACLS;
2483 if (res.has_links != 0)
2484 server->caps |= NFS_CAP_HARDLINKS;
2485 if (res.has_symlinks != 0)
2486 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002487 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2488 server->caps |= NFS_CAP_FILEID;
2489 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2490 server->caps |= NFS_CAP_MODE;
2491 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2492 server->caps |= NFS_CAP_NLINK;
2493 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2494 server->caps |= NFS_CAP_OWNER;
2495 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2496 server->caps |= NFS_CAP_OWNER_GROUP;
2497 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2498 server->caps |= NFS_CAP_ATIME;
2499 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2500 server->caps |= NFS_CAP_CTIME;
2501 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2502 server->caps |= NFS_CAP_MTIME;
2503
Trond Myklebusta65318b2009-03-11 14:10:28 -04002504 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2505 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2506 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002508 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 return status;
2512}
2513
Trond Myklebust55a97592006-06-09 09:34:19 -04002514int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
2516 struct nfs4_exception exception = { };
2517 int err;
2518 do {
2519 err = nfs4_handle_exception(server,
2520 _nfs4_server_capabilities(server, fhandle),
2521 &exception);
2522 } while (exception.retry);
2523 return err;
2524}
2525
2526static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2527 struct nfs_fsinfo *info)
2528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct nfs4_lookup_root_arg args = {
2530 .bitmask = nfs4_fattr_bitmap,
2531 };
2532 struct nfs4_lookup_res res = {
2533 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002534 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 .fh = fhandle,
2536 };
2537 struct rpc_message msg = {
2538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2539 .rpc_argp = &args,
2540 .rpc_resp = &res,
2541 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002542
Trond Myklebust0e574af2005-10-27 22:12:38 -04002543 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002544 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
2547static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2548 struct nfs_fsinfo *info)
2549{
2550 struct nfs4_exception exception = { };
2551 int err;
2552 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002553 err = _nfs4_lookup_root(server, fhandle, info);
2554 switch (err) {
2555 case 0:
2556 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002557 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002558 default:
2559 err = nfs4_handle_exception(server, err, &exception);
2560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002562out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return err;
2564}
2565
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002566static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2567 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2568{
2569 struct rpc_auth *auth;
2570 int ret;
2571
2572 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002573 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002574 ret = -EIO;
2575 goto out;
2576 }
2577 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002578out:
2579 return ret;
2580}
2581
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002582static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002583 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002585 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002586 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002588 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002589 if (len < 0)
2590 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002591
2592 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002593 /* AUTH_UNIX is the default flavor if none was specified,
2594 * thus has already been tried. */
2595 if (flav_array[i] == RPC_AUTH_UNIX)
2596 continue;
2597
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002598 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002599 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002600 continue;
2601 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002602 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002603 /*
2604 * -EACCESS could mean that the user doesn't have correct permissions
2605 * to access the mount. It could also mean that we tried to mount
2606 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2607 * existing mount programs don't handle -EACCES very well so it should
2608 * be mapped to -EPERM instead.
2609 */
2610 if (status == -EACCES)
2611 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002612 return status;
2613}
2614
2615/*
2616 * get the file handle for the "/" directory on the server
2617 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002618int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2619 struct nfs_fsinfo *info)
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002620{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002621 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002622 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002623 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2624 /*
2625 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2626 * by nfs4_map_errors() as this function exits.
2627 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002628 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (status == 0)
2630 status = nfs4_server_capabilities(server, fhandle);
2631 if (status == 0)
2632 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002633 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
Bryan Schumakerbae36242012-05-10 15:07:31 -04002636static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2637 struct nfs_fsinfo *info)
2638{
2639 int error;
2640 struct nfs_fattr *fattr = info->fattr;
2641
2642 error = nfs4_server_capabilities(server, mntfh);
2643 if (error < 0) {
2644 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2645 return error;
2646 }
2647
2648 error = nfs4_proc_getattr(server, mntfh, fattr);
2649 if (error < 0) {
2650 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2651 return error;
2652 }
2653
2654 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2655 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2656 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2657
2658 return error;
2659}
2660
Manoj Naik6b97fd32006-06-09 09:34:29 -04002661/*
2662 * Get locations and (maybe) other attributes of a referral.
2663 * Note that we'll actually follow the referral later when
2664 * we detect fsid mismatch in inode revalidation
2665 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002666static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2667 const struct qstr *name, struct nfs_fattr *fattr,
2668 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002669{
2670 int status = -ENOMEM;
2671 struct page *page = NULL;
2672 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002673
2674 page = alloc_page(GFP_KERNEL);
2675 if (page == NULL)
2676 goto out;
2677 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2678 if (locations == NULL)
2679 goto out;
2680
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002681 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002682 if (status != 0)
2683 goto out;
2684 /* Make sure server returned a different fsid for the referral */
2685 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002686 dprintk("%s: server did not return a different fsid for"
2687 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002688 status = -EIO;
2689 goto out;
2690 }
Andy Adamson533eb462011-06-13 18:25:56 -04002691 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2692 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002693
Andy Adamson533eb462011-06-13 18:25:56 -04002694 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002695 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002696 memset(fhandle, 0, sizeof(struct nfs_fh));
2697out:
2698 if (page)
2699 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002700 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002701 return status;
2702}
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2705{
2706 struct nfs4_getattr_arg args = {
2707 .fh = fhandle,
2708 .bitmask = server->attr_bitmask,
2709 };
2710 struct nfs4_getattr_res res = {
2711 .fattr = fattr,
2712 .server = server,
2713 };
2714 struct rpc_message msg = {
2715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2716 .rpc_argp = &args,
2717 .rpc_resp = &res,
2718 };
2719
Trond Myklebust0e574af2005-10-27 22:12:38 -04002720 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002721 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
2724static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2725{
2726 struct nfs4_exception exception = { };
2727 int err;
2728 do {
2729 err = nfs4_handle_exception(server,
2730 _nfs4_proc_getattr(server, fhandle, fattr),
2731 &exception);
2732 } while (exception.retry);
2733 return err;
2734}
2735
2736/*
2737 * The file is not closed if it is opened due to the a request to change
2738 * the size of the file. The open call will not be needed once the
2739 * VFS layer lookup-intents are implemented.
2740 *
2741 * Close is called when the inode is destroyed.
2742 * If we haven't opened the file for O_WRONLY, we
2743 * need to in the size_change case to obtain a stateid.
2744 *
2745 * Got race?
2746 * Because OPEN is always done by name in nfsv4, it is
2747 * possible that we opened a different file by the same
2748 * name. We can recognize this race condition, but we
2749 * can't do anything about it besides returning an error.
2750 *
2751 * This will be fixed with VFS changes (lookup-intent).
2752 */
2753static int
2754nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2755 struct iattr *sattr)
2756{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002757 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002758 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002759 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 int status;
2761
Benny Halevy8a1636c2010-07-14 15:43:57 -04002762 if (pnfs_ld_layoutret_on_setattr(inode))
2763 pnfs_return_layout(inode);
2764
Trond Myklebust0e574af2005-10-27 22:12:38 -04002765 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Andy Adamson26699402012-05-30 16:12:24 -04002767 /* Deal with open(O_TRUNC) */
2768 if (sattr->ia_valid & ATTR_OPEN)
2769 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2770
2771 /* Optimization: if the end result is no change, don't RPC */
2772 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2773 return 0;
2774
Trond Myklebustd5308382005-11-04 15:33:38 -05002775 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002776 if (sattr->ia_valid & ATTR_FILE) {
2777 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002778
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002779 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002780 if (ctx) {
2781 cred = ctx->cred;
2782 state = ctx->state;
2783 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002784 }
2785
2786 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002787 if (status == 0)
2788 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return status;
2790}
2791
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002792static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2793 const struct qstr *name, struct nfs_fh *fhandle,
2794 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002795{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002796 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002797 int status;
2798 struct nfs4_lookup_arg args = {
2799 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002800 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002801 .name = name,
2802 };
2803 struct nfs4_lookup_res res = {
2804 .server = server,
2805 .fattr = fattr,
2806 .fh = fhandle,
2807 };
2808 struct rpc_message msg = {
2809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2810 .rpc_argp = &args,
2811 .rpc_resp = &res,
2812 };
2813
2814 nfs_fattr_init(fattr);
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002817 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 dprintk("NFS reply lookup: %d\n", status);
2819 return status;
2820}
2821
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002822static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002823{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002824 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002825 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002826 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2827 fattr->nlink = 2;
2828}
2829
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002830static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2831 struct qstr *name, struct nfs_fh *fhandle,
2832 struct nfs_fattr *fattr)
2833{
2834 struct nfs4_exception exception = { };
2835 struct rpc_clnt *client = *clnt;
2836 int err;
2837 do {
2838 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2839 switch (err) {
2840 case -NFS4ERR_BADNAME:
2841 err = -ENOENT;
2842 goto out;
2843 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002844 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002845 goto out;
2846 case -NFS4ERR_WRONGSEC:
2847 err = -EPERM;
2848 if (client != *clnt)
2849 goto out;
2850
2851 client = nfs4_create_sec_client(client, dir, name);
2852 if (IS_ERR(client))
2853 return PTR_ERR(client);
2854
2855 exception.retry = 1;
2856 break;
2857 default:
2858 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2859 }
2860 } while (exception.retry);
2861
2862out:
2863 if (err == 0)
2864 *clnt = client;
2865 else if (client != *clnt)
2866 rpc_shutdown_client(client);
2867
2868 return err;
2869}
2870
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002871static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002872 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002874 int status;
2875 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002876
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002877 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2878 if (client != NFS_CLIENT(dir)) {
2879 rpc_shutdown_client(client);
2880 nfs_fixup_secinfo_attributes(fattr);
2881 }
2882 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002885struct rpc_clnt *
2886nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2887 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2888{
2889 int status;
2890 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2891
2892 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2893 if (status < 0) {
2894 rpc_shutdown_client(client);
2895 return ERR_PTR(status);
2896 }
2897 return client;
2898}
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2901{
Trond Myklebust76b32992007-08-10 17:45:11 -04002902 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 struct nfs4_accessargs args = {
2904 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002905 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002907 struct nfs4_accessres res = {
2908 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002909 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 struct rpc_message msg = {
2911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2912 .rpc_argp = &args,
2913 .rpc_resp = &res,
2914 .rpc_cred = entry->cred,
2915 };
2916 int mode = entry->mask;
2917 int status;
2918
2919 /*
2920 * Determine which access bits we want to ask for...
2921 */
2922 if (mode & MAY_READ)
2923 args.access |= NFS4_ACCESS_READ;
2924 if (S_ISDIR(inode->i_mode)) {
2925 if (mode & MAY_WRITE)
2926 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2927 if (mode & MAY_EXEC)
2928 args.access |= NFS4_ACCESS_LOOKUP;
2929 } else {
2930 if (mode & MAY_WRITE)
2931 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2932 if (mode & MAY_EXEC)
2933 args.access |= NFS4_ACCESS_EXECUTE;
2934 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002935
2936 res.fattr = nfs_alloc_fattr();
2937 if (res.fattr == NULL)
2938 return -ENOMEM;
2939
Bryan Schumaker7c513052011-03-24 17:12:24 +00002940 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002942 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002943 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002945 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return status;
2947}
2948
2949static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2950{
2951 struct nfs4_exception exception = { };
2952 int err;
2953 do {
2954 err = nfs4_handle_exception(NFS_SERVER(inode),
2955 _nfs4_proc_access(inode, entry),
2956 &exception);
2957 } while (exception.retry);
2958 return err;
2959}
2960
2961/*
2962 * TODO: For the time being, we don't try to get any attributes
2963 * along with any of the zero-copy operations READ, READDIR,
2964 * READLINK, WRITE.
2965 *
2966 * In the case of the first three, we want to put the GETATTR
2967 * after the read-type operation -- this is because it is hard
2968 * to predict the length of a GETATTR response in v4, and thus
2969 * align the READ data correctly. This means that the GETATTR
2970 * may end up partially falling into the page cache, and we should
2971 * shift it into the 'tail' of the xdr_buf before processing.
2972 * To do this efficiently, we need to know the total length
2973 * of data received, which doesn't seem to be available outside
2974 * of the RPC layer.
2975 *
2976 * In the case of WRITE, we also want to put the GETATTR after
2977 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002978 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 *
2980 * Both of these changes to the XDR layer would in fact be quite
2981 * minor, but I decided to leave them for a subsequent patch.
2982 */
2983static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2984 unsigned int pgbase, unsigned int pglen)
2985{
2986 struct nfs4_readlink args = {
2987 .fh = NFS_FH(inode),
2988 .pgbase = pgbase,
2989 .pglen = pglen,
2990 .pages = &page,
2991 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002992 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct rpc_message msg = {
2994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2995 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002996 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 };
2998
Bryan Schumaker7c513052011-03-24 17:12:24 +00002999 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 -07003000}
3001
3002static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3003 unsigned int pgbase, unsigned int pglen)
3004{
3005 struct nfs4_exception exception = { };
3006 int err;
3007 do {
3008 err = nfs4_handle_exception(NFS_SERVER(inode),
3009 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3010 &exception);
3011 } while (exception.retry);
3012 return err;
3013}
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003016 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018static int
3019nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003020 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003022 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 int status = 0;
3025
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003026 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3027 if (IS_ERR(ctx))
3028 return PTR_ERR(ctx);
3029
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003030 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003031 state = nfs4_do_open(dir, dentry, ctx->mode,
3032 flags, sattr, ctx->cred,
3033 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003034 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if (IS_ERR(state)) {
3036 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003039 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003040 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003041 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003043 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return status;
3045}
3046
3047static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3048{
Trond Myklebust16e42952005-10-27 22:12:44 -04003049 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003050 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003052 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003054 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003055 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003056 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3059 .rpc_argp = &args,
3060 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003062 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003063
Bryan Schumaker7c513052011-03-24 17:12:24 +00003064 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003065 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003066 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return status;
3068}
3069
3070static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3071{
3072 struct nfs4_exception exception = { };
3073 int err;
3074 do {
3075 err = nfs4_handle_exception(NFS_SERVER(dir),
3076 _nfs4_proc_remove(dir, name),
3077 &exception);
3078 } while (exception.retry);
3079 return err;
3080}
3081
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003082static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003084 struct nfs_server *server = NFS_SERVER(dir);
3085 struct nfs_removeargs *args = msg->rpc_argp;
3086 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003088 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003090 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003093static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3094{
3095 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3096 &data->args.seq_args,
3097 &data->res.seq_res,
3098 task))
3099 return;
3100 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003103static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003105 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3106
Trond Myklebust14516c32010-07-31 14:29:06 -04003107 if (!nfs4_sequence_done(task, &res->seq_res))
3108 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003109 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003110 return 0;
3111 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003112 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
Jeff Laytond3d41522010-09-17 17:31:57 -04003115static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3116{
3117 struct nfs_server *server = NFS_SERVER(dir);
3118 struct nfs_renameargs *arg = msg->rpc_argp;
3119 struct nfs_renameres *res = msg->rpc_resp;
3120
3121 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003122 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003123 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003124}
3125
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003126static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3127{
3128 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3129 &data->args.seq_args,
3130 &data->res.seq_res,
3131 task))
3132 return;
3133 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003134}
3135
3136static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3137 struct inode *new_dir)
3138{
3139 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3140
3141 if (!nfs4_sequence_done(task, &res->seq_res))
3142 return 0;
3143 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3144 return 0;
3145
3146 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003147 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003148 return 1;
3149}
3150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3152 struct inode *new_dir, struct qstr *new_name)
3153{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003154 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003155 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 .old_dir = NFS_FH(old_dir),
3157 .new_dir = NFS_FH(new_dir),
3158 .old_name = old_name,
3159 .new_name = new_name,
3160 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003161 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003162 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003163 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 struct rpc_message msg = {
3165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3166 .rpc_argp = &arg,
3167 .rpc_resp = &res,
3168 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003169 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Bryan Schumaker7c513052011-03-24 17:12:24 +00003171 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (!status) {
3173 update_changeattr(old_dir, &res.old_cinfo);
3174 update_changeattr(new_dir, &res.new_cinfo);
3175 }
3176 return status;
3177}
3178
3179static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3180 struct inode *new_dir, struct qstr *new_name)
3181{
3182 struct nfs4_exception exception = { };
3183 int err;
3184 do {
3185 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3186 _nfs4_proc_rename(old_dir, old_name,
3187 new_dir, new_name),
3188 &exception);
3189 } while (exception.retry);
3190 return err;
3191}
3192
3193static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3194{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003195 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 struct nfs4_link_arg arg = {
3197 .fh = NFS_FH(inode),
3198 .dir_fh = NFS_FH(dir),
3199 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003200 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003202 struct nfs4_link_res res = {
3203 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003204 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3207 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003208 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003210 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Trond Myklebust136f2622010-04-16 16:22:49 -04003212 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003213 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003214 goto out;
3215
Bryan Schumaker7c513052011-03-24 17:12:24 +00003216 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003217 if (!status) {
3218 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003219 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003220 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003221out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003222 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return status;
3224}
3225
3226static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3227{
3228 struct nfs4_exception exception = { };
3229 int err;
3230 do {
3231 err = nfs4_handle_exception(NFS_SERVER(inode),
3232 _nfs4_proc_link(inode, dir, name),
3233 &exception);
3234 } while (exception.retry);
3235 return err;
3236}
3237
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003238struct nfs4_createdata {
3239 struct rpc_message msg;
3240 struct nfs4_create_arg arg;
3241 struct nfs4_create_res res;
3242 struct nfs_fh fh;
3243 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003244};
3245
3246static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3247 struct qstr *name, struct iattr *sattr, u32 ftype)
3248{
3249 struct nfs4_createdata *data;
3250
3251 data = kzalloc(sizeof(*data), GFP_KERNEL);
3252 if (data != NULL) {
3253 struct nfs_server *server = NFS_SERVER(dir);
3254
3255 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3256 data->msg.rpc_argp = &data->arg;
3257 data->msg.rpc_resp = &data->res;
3258 data->arg.dir_fh = NFS_FH(dir);
3259 data->arg.server = server;
3260 data->arg.name = name;
3261 data->arg.attrs = sattr;
3262 data->arg.ftype = ftype;
3263 data->arg.bitmask = server->attr_bitmask;
3264 data->res.server = server;
3265 data->res.fh = &data->fh;
3266 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003268 }
3269 return data;
3270}
3271
3272static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3273{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003274 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003275 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003276 if (status == 0) {
3277 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003278 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3279 }
3280 return status;
3281}
3282
3283static void nfs4_free_createdata(struct nfs4_createdata *data)
3284{
3285 kfree(data);
3286}
3287
Chuck Lever4f390c12006-08-22 20:06:22 -04003288static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003289 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003291 struct nfs4_createdata *data;
3292 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Chuck Lever94a6d752006-08-22 20:06:23 -04003294 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003295 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003296
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003297 status = -ENOMEM;
3298 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3299 if (data == NULL)
3300 goto out;
3301
3302 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3303 data->arg.u.symlink.pages = &page;
3304 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003306 status = nfs4_do_create(dir, dentry, data);
3307
3308 nfs4_free_createdata(data);
3309out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 return status;
3311}
3312
Chuck Lever4f390c12006-08-22 20:06:22 -04003313static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003314 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
3316 struct nfs4_exception exception = { };
3317 int err;
3318 do {
3319 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003320 _nfs4_proc_symlink(dir, dentry, page,
3321 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 &exception);
3323 } while (exception.retry);
3324 return err;
3325}
3326
3327static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3328 struct iattr *sattr)
3329{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003330 struct nfs4_createdata *data;
3331 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003333 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3334 if (data == NULL)
3335 goto out;
3336
3337 status = nfs4_do_create(dir, dentry, data);
3338
3339 nfs4_free_createdata(data);
3340out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return status;
3342}
3343
3344static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3345 struct iattr *sattr)
3346{
3347 struct nfs4_exception exception = { };
3348 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003349
3350 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 do {
3352 err = nfs4_handle_exception(NFS_SERVER(dir),
3353 _nfs4_proc_mkdir(dir, dentry, sattr),
3354 &exception);
3355 } while (exception.retry);
3356 return err;
3357}
3358
3359static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003360 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
3362 struct inode *dir = dentry->d_inode;
3363 struct nfs4_readdir_arg args = {
3364 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003365 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 .pgbase = 0,
3367 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003368 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003369 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 };
3371 struct nfs4_readdir_res res;
3372 struct rpc_message msg = {
3373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3374 .rpc_argp = &args,
3375 .rpc_resp = &res,
3376 .rpc_cred = cred,
3377 };
3378 int status;
3379
Harvey Harrison3110ff82008-05-02 13:42:44 -07003380 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003381 dentry->d_parent->d_name.name,
3382 dentry->d_name.name,
3383 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003384 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003386 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 -05003387 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003388 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003389 status += args.pgbase;
3390 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003391
3392 nfs_invalidate_atime(dir);
3393
Harvey Harrison3110ff82008-05-02 13:42:44 -07003394 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return status;
3396}
3397
3398static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003399 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
3401 struct nfs4_exception exception = { };
3402 int err;
3403 do {
3404 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3405 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003406 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 &exception);
3408 } while (exception.retry);
3409 return err;
3410}
3411
3412static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3413 struct iattr *sattr, dev_t rdev)
3414{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003415 struct nfs4_createdata *data;
3416 int mode = sattr->ia_mode;
3417 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003419 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3420 if (data == NULL)
3421 goto out;
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003424 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003426 data->arg.ftype = NF4BLK;
3427 data->arg.u.device.specdata1 = MAJOR(rdev);
3428 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
3430 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003431 data->arg.ftype = NF4CHR;
3432 data->arg.u.device.specdata1 = MAJOR(rdev);
3433 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003434 } else if (!S_ISSOCK(mode)) {
3435 status = -EINVAL;
3436 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003439 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003440out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003441 nfs4_free_createdata(data);
3442out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return status;
3444}
3445
3446static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3447 struct iattr *sattr, dev_t rdev)
3448{
3449 struct nfs4_exception exception = { };
3450 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003451
3452 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 do {
3454 err = nfs4_handle_exception(NFS_SERVER(dir),
3455 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3456 &exception);
3457 } while (exception.retry);
3458 return err;
3459}
3460
3461static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3462 struct nfs_fsstat *fsstat)
3463{
3464 struct nfs4_statfs_arg args = {
3465 .fh = fhandle,
3466 .bitmask = server->attr_bitmask,
3467 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003468 struct nfs4_statfs_res res = {
3469 .fsstat = fsstat,
3470 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 struct rpc_message msg = {
3472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3473 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003474 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 };
3476
Trond Myklebust0e574af2005-10-27 22:12:38 -04003477 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003478 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
3481static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3482{
3483 struct nfs4_exception exception = { };
3484 int err;
3485 do {
3486 err = nfs4_handle_exception(server,
3487 _nfs4_proc_statfs(server, fhandle, fsstat),
3488 &exception);
3489 } while (exception.retry);
3490 return err;
3491}
3492
3493static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3494 struct nfs_fsinfo *fsinfo)
3495{
3496 struct nfs4_fsinfo_arg args = {
3497 .fh = fhandle,
3498 .bitmask = server->attr_bitmask,
3499 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003500 struct nfs4_fsinfo_res res = {
3501 .fsinfo = fsinfo,
3502 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 struct rpc_message msg = {
3504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3505 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003506 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 };
3508
Bryan Schumaker7c513052011-03-24 17:12:24 +00003509 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
3512static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3513{
3514 struct nfs4_exception exception = { };
3515 int err;
3516
3517 do {
3518 err = nfs4_handle_exception(server,
3519 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3520 &exception);
3521 } while (exception.retry);
3522 return err;
3523}
3524
3525static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3526{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003527 int error;
3528
Trond Myklebust0e574af2005-10-27 22:12:38 -04003529 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003530 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003531 if (error == 0) {
3532 /* block layout checks this! */
3533 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003534 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003535 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003536
3537 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
3539
3540static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3541 struct nfs_pathconf *pathconf)
3542{
3543 struct nfs4_pathconf_arg args = {
3544 .fh = fhandle,
3545 .bitmask = server->attr_bitmask,
3546 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003547 struct nfs4_pathconf_res res = {
3548 .pathconf = pathconf,
3549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 struct rpc_message msg = {
3551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3552 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003553 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 };
3555
3556 /* None of the pathconf attributes are mandatory to implement */
3557 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3558 memset(pathconf, 0, sizeof(*pathconf));
3559 return 0;
3560 }
3561
Trond Myklebust0e574af2005-10-27 22:12:38 -04003562 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003563 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
3566static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3567 struct nfs_pathconf *pathconf)
3568{
3569 struct nfs4_exception exception = { };
3570 int err;
3571
3572 do {
3573 err = nfs4_handle_exception(server,
3574 _nfs4_proc_pathconf(server, fhandle, pathconf),
3575 &exception);
3576 } while (exception.retry);
3577 return err;
3578}
3579
Benny Halevyd20581a2011-05-22 19:52:03 +03003580void __nfs4_read_done_cb(struct nfs_read_data *data)
3581{
Fred Isamancd841602012-04-20 14:47:44 -04003582 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003583}
3584
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003585static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
Fred Isamancd841602012-04-20 14:47:44 -04003587 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003589 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003590 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003591 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003593
Benny Halevyd20581a2011-05-22 19:52:03 +03003594 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003596 renew_lease(server, data->timestamp);
3597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003600static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3601{
3602
3603 dprintk("--> %s\n", __func__);
3604
3605 if (!nfs4_sequence_done(task, &data->res.seq_res))
3606 return -EAGAIN;
3607
Benny Halevyd20581a2011-05-22 19:52:03 +03003608 return data->read_done_cb ? data->read_done_cb(task, data) :
3609 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003610}
3611
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003612static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003615 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003616 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003617 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003620static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3621{
Fred Isamancd841602012-04-20 14:47:44 -04003622 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003623 &data->args.seq_args,
3624 &data->res.seq_res,
3625 task))
3626 return;
3627 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
Fred Isamanb029bc92011-03-03 15:13:42 +00003630static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
Fred Isamancd841602012-04-20 14:47:44 -04003632 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003634 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003635 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003636 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003638 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003640 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003641 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
Fred Isamanb029bc92011-03-03 15:13:42 +00003645static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3646{
3647 if (!nfs4_sequence_done(task, &data->res.seq_res))
3648 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003649 return data->write_done_cb ? data->write_done_cb(task, data) :
3650 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003651}
3652
Trond Myklebust5a37f852012-04-28 14:55:16 -04003653static
3654bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003655{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003656 const struct nfs_pgio_header *hdr = data->header;
3657
3658 /* Don't request attributes for pNFS or O_DIRECT writes */
3659 if (data->ds_clp != NULL || hdr->dreq != NULL)
3660 return false;
3661 /* Otherwise, request attributes if and only if we don't hold
3662 * a delegation
3663 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003664 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003665}
Fred Isamana69aef12011-03-03 15:13:47 +00003666
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003667static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
Fred Isamancd841602012-04-20 14:47:44 -04003669 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003670
Trond Myklebust5a37f852012-04-28 14:55:16 -04003671 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003672 data->args.bitmask = NULL;
3673 data->res.fattr = NULL;
3674 } else
3675 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003676
Fred Isamanb029bc92011-03-03 15:13:42 +00003677 if (!data->write_done_cb)
3678 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003679 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 data->timestamp = jiffies;
3681
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003682 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003683 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
3685
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003686static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3687{
Fred Isamancd841602012-04-20 14:47:44 -04003688 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003689 &data->args.seq_args,
3690 &data->res.seq_res,
3691 task))
3692 return;
3693 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
Fred Isaman0b7c0152012-04-20 14:47:39 -04003696static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3697{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3699 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003700 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return;
3703 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003704}
3705
Fred Isaman0b7c0152012-04-20 14:47:39 -04003706static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707{
3708 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003709
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003710 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003711 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003712 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
3716
Fred Isaman0b7c0152012-04-20 14:47:39 -04003717static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003718{
3719 if (!nfs4_sequence_done(task, &data->res.seq_res))
3720 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003721 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003722}
3723
Fred Isaman0b7c0152012-04-20 14:47:39 -04003724static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003726 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003727
Fred Isaman0b7c0152012-04-20 14:47:39 -04003728 if (data->commit_done_cb == NULL)
3729 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003730 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003731 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003732 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003735struct nfs4_renewdata {
3736 struct nfs_client *client;
3737 unsigned long timestamp;
3738};
3739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740/*
3741 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3742 * standalone procedure for queueing an asynchronous RENEW.
3743 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003744static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003745{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003746 struct nfs4_renewdata *data = calldata;
3747 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003748
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003749 if (atomic_read(&clp->cl_count) > 1)
3750 nfs4_schedule_state_renewal(clp);
3751 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003752 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003753}
3754
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003755static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003757 struct nfs4_renewdata *data = calldata;
3758 struct nfs_client *clp = data->client;
3759 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
3761 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003762 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003763 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3764 return;
3765 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003766 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003767 return;
3768 }
3769 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003771 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003774static const struct rpc_call_ops nfs4_renew_ops = {
3775 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003776 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003777};
3778
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003779static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
3781 struct rpc_message msg = {
3782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3783 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003784 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003786 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003788 if (renew_flags == 0)
3789 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003790 if (!atomic_inc_not_zero(&clp->cl_count))
3791 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003792 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003793 if (data == NULL)
3794 return -ENOMEM;
3795 data->client = clp;
3796 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003798 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799}
3800
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003801static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
3803 struct rpc_message msg = {
3804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3805 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003806 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 };
3808 unsigned long now = jiffies;
3809 int status;
3810
3811 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3812 if (status < 0)
3813 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003814 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 return 0;
3816}
3817
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003818static inline int nfs4_server_supports_acls(struct nfs_server *server)
3819{
3820 return (server->caps & NFS_CAP_ACLS)
3821 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3822 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3823}
3824
Trond Myklebust21f498c2012-08-24 10:59:25 -04003825/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3826 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003827 * the stack.
3828 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003829#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003830
Neil Hormane9e3d722011-03-04 19:26:03 -05003831static int buf_to_pages_noslab(const void *buf, size_t buflen,
3832 struct page **pages, unsigned int *pgbase)
3833{
3834 struct page *newpage, **spages;
3835 int rc = 0;
3836 size_t len;
3837 spages = pages;
3838
3839 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003840 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003841 newpage = alloc_page(GFP_KERNEL);
3842
3843 if (newpage == NULL)
3844 goto unwind;
3845 memcpy(page_address(newpage), buf, len);
3846 buf += len;
3847 buflen -= len;
3848 *pages++ = newpage;
3849 rc++;
3850 } while (buflen != 0);
3851
3852 return rc;
3853
3854unwind:
3855 for(; rc > 0; rc--)
3856 __free_page(spages[rc-1]);
3857 return -ENOMEM;
3858}
3859
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860struct nfs4_cached_acl {
3861 int cached;
3862 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003863 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003864};
3865
3866static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003867{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003868 struct nfs_inode *nfsi = NFS_I(inode);
3869
3870 spin_lock(&inode->i_lock);
3871 kfree(nfsi->nfs4_acl);
3872 nfsi->nfs4_acl = acl;
3873 spin_unlock(&inode->i_lock);
3874}
3875
3876static void nfs4_zap_acl_attr(struct inode *inode)
3877{
3878 nfs4_set_cached_acl(inode, NULL);
3879}
3880
3881static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3882{
3883 struct nfs_inode *nfsi = NFS_I(inode);
3884 struct nfs4_cached_acl *acl;
3885 int ret = -ENOENT;
3886
3887 spin_lock(&inode->i_lock);
3888 acl = nfsi->nfs4_acl;
3889 if (acl == NULL)
3890 goto out;
3891 if (buf == NULL) /* user is just asking for length */
3892 goto out_len;
3893 if (acl->cached == 0)
3894 goto out;
3895 ret = -ERANGE; /* see getxattr(2) man page */
3896 if (acl->len > buflen)
3897 goto out;
3898 memcpy(buf, acl->data, acl->len);
3899out_len:
3900 ret = acl->len;
3901out:
3902 spin_unlock(&inode->i_lock);
3903 return ret;
3904}
3905
Sachin Prabhu5794d212012-04-17 14:36:40 +01003906static 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 +00003907{
3908 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003909 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003910
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003911 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003912 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003913 if (acl == NULL)
3914 goto out;
3915 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003916 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003917 } else {
3918 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3919 if (acl == NULL)
3920 goto out;
3921 acl->cached = 0;
3922 }
3923 acl->len = acl_len;
3924out:
3925 nfs4_set_cached_acl(inode, acl);
3926}
3927
Andy Adamsonbf118a32011-12-07 11:55:27 -05003928/*
3929 * The getxattr API returns the required buffer length when called with a
3930 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3931 * the required buf. On a NULL buf, we send a page of data to the server
3932 * guessing that the ACL request can be serviced by a page. If so, we cache
3933 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3934 * the cache. If not so, we throw away the page, and cache the required
3935 * length. The next getxattr call will then produce another round trip to
3936 * the server, this time with the input buf of the required size.
3937 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003938static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003939{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003940 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003941 struct nfs_getaclargs args = {
3942 .fh = NFS_FH(inode),
3943 .acl_pages = pages,
3944 .acl_len = buflen,
3945 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003946 struct nfs_getaclres res = {
3947 .acl_len = buflen,
3948 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003949 struct rpc_message msg = {
3950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3951 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003952 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003953 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003954 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3955 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003956
Andy Adamsonbf118a32011-12-07 11:55:27 -05003957 /* As long as we're doing a round trip to the server anyway,
3958 * let's be prepared for a page of acl data. */
3959 if (npages == 0)
3960 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003961 if (npages > ARRAY_SIZE(pages))
3962 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003963
Andy Adamsonbf118a32011-12-07 11:55:27 -05003964 for (i = 0; i < npages; i++) {
3965 pages[i] = alloc_page(GFP_KERNEL);
3966 if (!pages[i])
3967 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003968 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003969
3970 /* for decoding across pages */
3971 res.acl_scratch = alloc_page(GFP_KERNEL);
3972 if (!res.acl_scratch)
3973 goto out_free;
3974
Andy Adamsonbf118a32011-12-07 11:55:27 -05003975 args.acl_len = npages * PAGE_SIZE;
3976 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003977
Peng Taode040be2012-01-10 22:42:47 +08003978 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003979 __func__, buf, buflen, npages, args.acl_len);
3980 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3981 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003982 if (ret)
3983 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003984
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003985 /* Handle the case where the passed-in buffer is too short */
3986 if (res.acl_flags & NFS4_ACL_TRUNC) {
3987 /* Did the user only issue a request for the acl length? */
3988 if (buf == NULL)
3989 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003990 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003991 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003992 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003993 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3994 if (buf)
3995 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3996out_ok:
3997 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003998out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003999 for (i = 0; i < npages; i++)
4000 if (pages[i])
4001 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004002 if (res.acl_scratch)
4003 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004004 return ret;
4005}
4006
Trond Myklebust16b42892006-08-24 12:27:15 -04004007static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4008{
4009 struct nfs4_exception exception = { };
4010 ssize_t ret;
4011 do {
4012 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4013 if (ret >= 0)
4014 break;
4015 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4016 } while (exception.retry);
4017 return ret;
4018}
4019
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004020static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4021{
4022 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004023 int ret;
4024
4025 if (!nfs4_server_supports_acls(server))
4026 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004027 ret = nfs_revalidate_inode(server, inode);
4028 if (ret < 0)
4029 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004030 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4031 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004032 ret = nfs4_read_cached_acl(inode, buf, buflen);
4033 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004034 /* -ENOENT is returned if there is no ACL or if there is an ACL
4035 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004036 return ret;
4037 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004038}
4039
Trond Myklebust16b42892006-08-24 12:27:15 -04004040static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004041{
4042 struct nfs_server *server = NFS_SERVER(inode);
4043 struct page *pages[NFS4ACL_MAXPAGES];
4044 struct nfs_setaclargs arg = {
4045 .fh = NFS_FH(inode),
4046 .acl_pages = pages,
4047 .acl_len = buflen,
4048 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004049 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004050 struct rpc_message msg = {
4051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4052 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004053 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004054 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004055 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004056 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004057
4058 if (!nfs4_server_supports_acls(server))
4059 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004060 if (npages > ARRAY_SIZE(pages))
4061 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004062 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4063 if (i < 0)
4064 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004065 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004066 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004067
4068 /*
4069 * Free each page after tx, so the only ref left is
4070 * held by the network stack
4071 */
4072 for (; i > 0; i--)
4073 put_page(pages[i-1]);
4074
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004075 /*
4076 * Acl update can result in inode attribute update.
4077 * so mark the attribute cache invalid.
4078 */
4079 spin_lock(&inode->i_lock);
4080 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4081 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004082 nfs_access_zap_cache(inode);
4083 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004084 return ret;
4085}
4086
Trond Myklebust16b42892006-08-24 12:27:15 -04004087static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4088{
4089 struct nfs4_exception exception = { };
4090 int err;
4091 do {
4092 err = nfs4_handle_exception(NFS_SERVER(inode),
4093 __nfs4_proc_set_acl(inode, buf, buflen),
4094 &exception);
4095 } while (exception.retry);
4096 return err;
4097}
4098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004100nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004102 struct nfs_client *clp = server->nfs_client;
4103
4104 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return 0;
4106 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004107 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004108 case -NFS4ERR_ADMIN_REVOKED:
4109 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004110 if (state == NULL)
4111 break;
4112 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004113 case -NFS4ERR_OPENMODE:
4114 if (state == NULL)
4115 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004116 nfs4_schedule_stateid_recovery(server, state);
4117 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004118 case -NFS4ERR_EXPIRED:
4119 if (state != NULL)
4120 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004122 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004123 nfs4_schedule_lease_recovery(clp);
4124 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004125#if defined(CONFIG_NFS_V4_1)
4126 case -NFS4ERR_BADSESSION:
4127 case -NFS4ERR_BADSLOT:
4128 case -NFS4ERR_BAD_HIGH_SLOT:
4129 case -NFS4ERR_DEADSESSION:
4130 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4131 case -NFS4ERR_SEQ_FALSE_RETRY:
4132 case -NFS4ERR_SEQ_MISORDERED:
4133 dprintk("%s ERROR %d, Reset session\n", __func__,
4134 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004135 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004136 task->tk_status = 0;
4137 return -EAGAIN;
4138#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004140 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004141 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004142 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4144 task->tk_status = 0;
4145 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004146 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 case -NFS4ERR_OLD_STATEID:
4148 task->tk_status = 0;
4149 return -EAGAIN;
4150 }
4151 task->tk_status = nfs4_map_errors(task->tk_status);
4152 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004153wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004154 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004155 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4156 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4157 task->tk_status = 0;
4158 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159}
4160
Chuck Leverf0920752012-05-21 22:45:41 -04004161static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4162 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004163{
4164 __be32 verf[2];
4165
Chuck Lever2c820d92012-05-21 22:45:33 -04004166 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4167 /* An impossible timestamp guarantees this value
4168 * will never match a generated boot time. */
4169 verf[0] = 0;
4170 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4171 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004172 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4173 verf[0] = (__be32)nn->boot_time.tv_sec;
4174 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004175 }
Chuck Levercd937102012-03-02 17:14:31 -05004176 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4177}
4178
Chuck Levere984a552012-09-14 17:24:21 -04004179static unsigned int
4180nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4181 char *buf, size_t len)
4182{
4183 unsigned int result;
4184
4185 rcu_read_lock();
4186 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4187 clp->cl_ipaddr,
4188 rpc_peeraddr2str(clp->cl_rpcclient,
4189 RPC_DISPLAY_ADDR),
4190 rpc_peeraddr2str(clp->cl_rpcclient,
4191 RPC_DISPLAY_PROTO));
4192 rcu_read_unlock();
4193 return result;
4194}
4195
4196static unsigned int
4197nfs4_init_uniform_client_string(const struct nfs_client *clp,
4198 char *buf, size_t len)
4199{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004200 char *nodename = clp->cl_rpcclient->cl_nodename;
4201
4202 if (nfs4_client_id_uniquifier[0] != '\0')
4203 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004204 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4205 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004206 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004207}
4208
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004209/**
4210 * nfs4_proc_setclientid - Negotiate client ID
4211 * @clp: state data structure
4212 * @program: RPC program for NFSv4 callback service
4213 * @port: IP port number for NFS4 callback service
4214 * @cred: RPC credential to use for this call
4215 * @res: where to place the result
4216 *
4217 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4218 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004219int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4220 unsigned short port, struct rpc_cred *cred,
4221 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222{
4223 nfs4_verifier sc_verifier;
4224 struct nfs4_setclientid setclientid = {
4225 .sc_verifier = &sc_verifier,
4226 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004227 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 };
4229 struct rpc_message msg = {
4230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4231 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004232 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004233 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004235 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Chuck Leverde734832012-07-11 16:30:50 -04004237 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004238 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004239 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4240 setclientid.sc_name_len =
4241 nfs4_init_uniform_client_string(clp,
4242 setclientid.sc_name,
4243 sizeof(setclientid.sc_name));
4244 else
4245 setclientid.sc_name_len =
4246 nfs4_init_nonuniform_client_string(clp,
4247 setclientid.sc_name,
4248 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004249 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004250 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004251 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004252 sizeof(setclientid.sc_netid),
4253 rpc_peeraddr2str(clp->cl_rpcclient,
4254 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004255 rcu_read_unlock();
4256 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004257 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 clp->cl_ipaddr, port >> 8, port & 255);
4259
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004260 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4261 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4262 setclientid.sc_name_len, setclientid.sc_name);
4263 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4264 dprintk("NFS reply setclientid: %d\n", status);
4265 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266}
4267
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004268/**
4269 * nfs4_proc_setclientid_confirm - Confirm client ID
4270 * @clp: state data structure
4271 * @res: result of a previous SETCLIENTID
4272 * @cred: RPC credential to use for this call
4273 *
4274 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4275 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004276int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004277 struct nfs4_setclientid_res *arg,
4278 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
4280 struct nfs_fsinfo fsinfo;
4281 struct rpc_message msg = {
4282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004283 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004285 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 };
4287 unsigned long now;
4288 int status;
4289
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004290 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4291 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4292 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004294 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (status == 0) {
4296 spin_lock(&clp->cl_lock);
4297 clp->cl_lease_time = fsinfo.lease_time * HZ;
4298 clp->cl_last_renewal = now;
4299 spin_unlock(&clp->cl_lock);
4300 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004301 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return status;
4303}
4304
Trond Myklebustfe650402006-01-03 09:55:18 +01004305struct nfs4_delegreturndata {
4306 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004307 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004308 struct nfs_fh fh;
4309 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004310 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004311 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004312 int rpc_status;
4313};
4314
Trond Myklebustfe650402006-01-03 09:55:18 +01004315static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4316{
4317 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004318
Trond Myklebust14516c32010-07-31 14:29:06 -04004319 if (!nfs4_sequence_done(task, &data->res.seq_res))
4320 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004321
Ricardo Labiaga79708862009-12-07 09:23:21 -05004322 switch (task->tk_status) {
4323 case -NFS4ERR_STALE_STATEID:
4324 case -NFS4ERR_EXPIRED:
4325 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004326 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004327 break;
4328 default:
4329 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4330 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004331 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004332 return;
4333 }
4334 }
4335 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004336}
4337
4338static void nfs4_delegreturn_release(void *calldata)
4339{
Trond Myklebustfe650402006-01-03 09:55:18 +01004340 kfree(calldata);
4341}
4342
Andy Adamson938e1012009-04-01 09:22:28 -04004343#if defined(CONFIG_NFS_V4_1)
4344static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4345{
4346 struct nfs4_delegreturndata *d_data;
4347
4348 d_data = (struct nfs4_delegreturndata *)data;
4349
Trond Myklebust035168a2010-06-16 09:52:26 -04004350 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004351 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004352 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004353 return;
4354 rpc_call_start(task);
4355}
4356#endif /* CONFIG_NFS_V4_1 */
4357
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004358static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004359#if defined(CONFIG_NFS_V4_1)
4360 .rpc_call_prepare = nfs4_delegreturn_prepare,
4361#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004362 .rpc_call_done = nfs4_delegreturn_done,
4363 .rpc_release = nfs4_delegreturn_release,
4364};
4365
Trond Myklebuste6f81072008-01-24 18:14:34 -05004366static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004367{
4368 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004369 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004370 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004371 struct rpc_message msg = {
4372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4373 .rpc_cred = cred,
4374 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004375 struct rpc_task_setup task_setup_data = {
4376 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004377 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004378 .callback_ops = &nfs4_delegreturn_ops,
4379 .flags = RPC_TASK_ASYNC,
4380 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004381 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004382
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004383 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004384 if (data == NULL)
4385 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004386 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004387 data->args.fhandle = &data->fh;
4388 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004389 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004390 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004391 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004392 data->res.fattr = &data->fattr;
4393 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004394 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004395 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004396 data->rpc_status = 0;
4397
Trond Myklebustc970aa82007-07-14 15:39:59 -04004398 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004399 msg.rpc_argp = &data->args;
4400 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004401 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004402 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004403 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004404 if (!issync)
4405 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004406 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004407 if (status != 0)
4408 goto out;
4409 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004410 if (status == 0)
4411 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4412 else
4413 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004414out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004415 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004416 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
Trond Myklebuste6f81072008-01-24 18:14:34 -05004419int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420{
4421 struct nfs_server *server = NFS_SERVER(inode);
4422 struct nfs4_exception exception = { };
4423 int err;
4424 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004425 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 switch (err) {
4427 case -NFS4ERR_STALE_STATEID:
4428 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 case 0:
4430 return 0;
4431 }
4432 err = nfs4_handle_exception(server, err, &exception);
4433 } while (exception.retry);
4434 return err;
4435}
4436
4437#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4438#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4439
4440/*
4441 * sleep, with exponential backoff, and retry the LOCK operation.
4442 */
4443static unsigned long
4444nfs4_set_lock_task_retry(unsigned long timeout)
4445{
Jeff Laytond3103102011-12-01 22:44:39 +01004446 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 timeout <<= 1;
4448 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4449 return NFS4_LOCK_MAXTIMEOUT;
4450 return timeout;
4451}
4452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4454{
4455 struct inode *inode = state->inode;
4456 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004457 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004458 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004460 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004462 struct nfs_lockt_res res = {
4463 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 };
4465 struct rpc_message msg = {
4466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4467 .rpc_argp = &arg,
4468 .rpc_resp = &res,
4469 .rpc_cred = state->owner->so_cred,
4470 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 struct nfs4_lock_state *lsp;
4472 int status;
4473
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004474 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004475 status = nfs4_set_lock_state(state, request);
4476 if (status != 0)
4477 goto out;
4478 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004479 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004480 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004481 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004482 switch (status) {
4483 case 0:
4484 request->fl_type = F_UNLCK;
4485 break;
4486 case -NFS4ERR_DENIED:
4487 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004489 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004490out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return status;
4492}
4493
4494static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4495{
4496 struct nfs4_exception exception = { };
4497 int err;
4498
4499 do {
4500 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4501 _nfs4_proc_getlk(state, cmd, request),
4502 &exception);
4503 } while (exception.retry);
4504 return err;
4505}
4506
4507static int do_vfs_lock(struct file *file, struct file_lock *fl)
4508{
4509 int res = 0;
4510 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4511 case FL_POSIX:
4512 res = posix_lock_file_wait(file, fl);
4513 break;
4514 case FL_FLOCK:
4515 res = flock_lock_file_wait(file, fl);
4516 break;
4517 default:
4518 BUG();
4519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 return res;
4521}
4522
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004523struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004524 struct nfs_locku_args arg;
4525 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004526 struct nfs4_lock_state *lsp;
4527 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004528 struct file_lock fl;
4529 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004530 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004531};
4532
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004533static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4534 struct nfs_open_context *ctx,
4535 struct nfs4_lock_state *lsp,
4536 struct nfs_seqid *seqid)
4537{
4538 struct nfs4_unlockdata *p;
4539 struct inode *inode = lsp->ls_state->inode;
4540
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004541 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004542 if (p == NULL)
4543 return NULL;
4544 p->arg.fh = NFS_FH(inode);
4545 p->arg.fl = &p->fl;
4546 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004547 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004548 p->arg.stateid = &lsp->ls_stateid;
4549 p->lsp = lsp;
4550 atomic_inc(&lsp->ls_count);
4551 /* Ensure we don't close file until we're done freeing locks! */
4552 p->ctx = get_nfs_open_context(ctx);
4553 memcpy(&p->fl, fl, sizeof(p->fl));
4554 p->server = NFS_SERVER(inode);
4555 return p;
4556}
4557
Trond Myklebust06f814a2006-01-03 09:55:07 +01004558static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004559{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004560 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004561 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004562 nfs4_put_lock_state(calldata->lsp);
4563 put_nfs_open_context(calldata->ctx);
4564 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004565}
4566
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004567static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004568{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004569 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004570
Trond Myklebust14516c32010-07-31 14:29:06 -04004571 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4572 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004573 switch (task->tk_status) {
4574 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004575 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4576 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004577 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004578 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004579 case -NFS4ERR_BAD_STATEID:
4580 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004581 case -NFS4ERR_STALE_STATEID:
4582 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004583 break;
4584 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004585 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004586 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004587 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004588 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004589}
4590
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004591static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004592{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004593 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004595 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004596 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004597 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004598 /* Note: exit _without_ running nfs4_locku_done */
4599 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004600 return;
4601 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004602 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004603 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004604 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004605 &calldata->res.seq_res,
4606 task) != 0)
4607 nfs_release_seqid(calldata->arg.seqid);
4608 else
4609 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610}
4611
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004612static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004613 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004614 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004615 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004616};
4617
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004618static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4619 struct nfs_open_context *ctx,
4620 struct nfs4_lock_state *lsp,
4621 struct nfs_seqid *seqid)
4622{
4623 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004624 struct rpc_message msg = {
4625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4626 .rpc_cred = ctx->cred,
4627 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004628 struct rpc_task_setup task_setup_data = {
4629 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004630 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004631 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004632 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004633 .flags = RPC_TASK_ASYNC,
4634 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004635
Frank Filz137d6ac2007-07-09 15:32:29 -07004636 /* Ensure this is an unlock - when canceling a lock, the
4637 * canceled lock is passed in, and it won't be an unlock.
4638 */
4639 fl->fl_type = F_UNLCK;
4640
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004641 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4642 if (data == NULL) {
4643 nfs_free_seqid(seqid);
4644 return ERR_PTR(-ENOMEM);
4645 }
4646
Trond Myklebust9d12b212012-01-17 22:04:25 -05004647 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004648 msg.rpc_argp = &data->arg;
4649 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004650 task_setup_data.callback_data = data;
4651 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652}
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4655{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004656 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004657 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004658 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004659 struct rpc_task *task;
4660 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004661 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Trond Myklebust9b073572006-06-29 16:38:34 -04004663 status = nfs4_set_lock_state(state, request);
4664 /* Unlock _before_ we do the RPC call */
4665 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004666 down_read(&nfsi->rwsem);
4667 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4668 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004669 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004670 }
4671 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004672 if (status != 0)
4673 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004674 /* Is this a delegated lock? */
4675 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004676 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004677 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004678 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004679 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004680 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004681 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004682 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683 status = PTR_ERR(task);
4684 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004685 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004686 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004687 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004688out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004689 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004690 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004693struct nfs4_lockdata {
4694 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004695 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004696 struct nfs4_lock_state *lsp;
4697 struct nfs_open_context *ctx;
4698 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004699 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004700 int rpc_status;
4701 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004702 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004703};
4704
4705static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004706 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4707 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004708{
4709 struct nfs4_lockdata *p;
4710 struct inode *inode = lsp->ls_state->inode;
4711 struct nfs_server *server = NFS_SERVER(inode);
4712
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004713 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004714 if (p == NULL)
4715 return NULL;
4716
4717 p->arg.fh = NFS_FH(inode);
4718 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004719 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004720 if (p->arg.open_seqid == NULL)
4721 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004722 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004723 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004724 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004725 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004726 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004727 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004728 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004729 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004731 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004732 atomic_inc(&lsp->ls_count);
4733 p->ctx = get_nfs_open_context(ctx);
4734 memcpy(&p->fl, fl, sizeof(p->fl));
4735 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004736out_free_seqid:
4737 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004738out_free:
4739 kfree(p);
4740 return NULL;
4741}
4742
4743static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4744{
4745 struct nfs4_lockdata *data = calldata;
4746 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Harvey Harrison3110ff82008-05-02 13:42:44 -07004748 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004749 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4750 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004751 /* Do we need to do an open_to_lock_owner? */
4752 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004753 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004754 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004755 data->arg.open_stateid = &state->stateid;
4756 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004757 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004758 } else
4759 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004760 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004761 if (nfs4_setup_sequence(data->server,
4762 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004763 &data->res.seq_res,
4764 task) == 0) {
4765 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004766 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004767 }
4768 nfs_release_seqid(data->arg.open_seqid);
4769out_release_lock_seqid:
4770 nfs_release_seqid(data->arg.lock_seqid);
4771 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004772}
4773
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004774static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4775{
4776 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4777 nfs4_lock_prepare(task, calldata);
4778}
4779
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004780static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4781{
4782 struct nfs4_lockdata *data = calldata;
4783
Harvey Harrison3110ff82008-05-02 13:42:44 -07004784 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004785
Trond Myklebust14516c32010-07-31 14:29:06 -04004786 if (!nfs4_sequence_done(task, &data->res.seq_res))
4787 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004788
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004789 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004790 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004791 if (data->rpc_status == 0)
4792 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4793 else
4794 goto out;
4795 }
4796 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004797 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004798 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004799 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004800 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004801out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004802 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004803}
4804
4805static void nfs4_lock_release(void *calldata)
4806{
4807 struct nfs4_lockdata *data = calldata;
4808
Harvey Harrison3110ff82008-05-02 13:42:44 -07004809 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004810 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004811 if (data->cancelled != 0) {
4812 struct rpc_task *task;
4813 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4814 data->arg.lock_seqid);
4815 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004816 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004817 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818 } else
4819 nfs_free_seqid(data->arg.lock_seqid);
4820 nfs4_put_lock_state(data->lsp);
4821 put_nfs_open_context(data->ctx);
4822 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004823 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004824}
4825
4826static const struct rpc_call_ops nfs4_lock_ops = {
4827 .rpc_call_prepare = nfs4_lock_prepare,
4828 .rpc_call_done = nfs4_lock_done,
4829 .rpc_release = nfs4_lock_release,
4830};
4831
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004832static const struct rpc_call_ops nfs4_recover_lock_ops = {
4833 .rpc_call_prepare = nfs4_recover_lock_prepare,
4834 .rpc_call_done = nfs4_lock_done,
4835 .rpc_release = nfs4_lock_release,
4836};
4837
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004838static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4839{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004840 switch (error) {
4841 case -NFS4ERR_ADMIN_REVOKED:
4842 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004843 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004844 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004845 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004846 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004847 break;
4848 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004849 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004850 case -NFS4ERR_EXPIRED:
4851 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004852 };
4853}
4854
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004855static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004856{
4857 struct nfs4_lockdata *data;
4858 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004859 struct rpc_message msg = {
4860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4861 .rpc_cred = state->owner->so_cred,
4862 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004863 struct rpc_task_setup task_setup_data = {
4864 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004865 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004866 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004867 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004868 .flags = RPC_TASK_ASYNC,
4869 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004870 int ret;
4871
Harvey Harrison3110ff82008-05-02 13:42:44 -07004872 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004873 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004874 fl->fl_u.nfs4_fl.owner,
4875 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004876 if (data == NULL)
4877 return -ENOMEM;
4878 if (IS_SETLKW(cmd))
4879 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004880 if (recovery_type > NFS_LOCK_NEW) {
4881 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004882 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004883 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4884 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004885 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004886 msg.rpc_argp = &data->arg;
4887 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004888 task_setup_data.callback_data = data;
4889 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004890 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004891 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004892 ret = nfs4_wait_for_completion_rpc_task(task);
4893 if (ret == 0) {
4894 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004895 if (ret)
4896 nfs4_handle_setlk_error(data->server, data->lsp,
4897 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004898 } else
4899 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004900 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004901 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004902 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903}
4904
4905static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4906{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004907 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004908 struct nfs4_exception exception = {
4909 .inode = state->inode,
4910 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004911 int err;
4912
4913 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004914 /* Cache the lock if possible... */
4915 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4916 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004917 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004918 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004919 break;
4920 nfs4_handle_exception(server, err, &exception);
4921 } while (exception.retry);
4922 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923}
4924
4925static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4926{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004927 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004928 struct nfs4_exception exception = {
4929 .inode = state->inode,
4930 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004931 int err;
4932
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004933 err = nfs4_set_lock_state(state, request);
4934 if (err != 0)
4935 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004936 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004937 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4938 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004939 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004940 switch (err) {
4941 default:
4942 goto out;
4943 case -NFS4ERR_GRACE:
4944 case -NFS4ERR_DELAY:
4945 nfs4_handle_exception(server, err, &exception);
4946 err = 0;
4947 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004948 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004949out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951}
4952
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004953#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004954/**
4955 * nfs41_check_expired_locks - possibly free a lock stateid
4956 *
4957 * @state: NFSv4 state for an inode
4958 *
4959 * Returns NFS_OK if recovery for this stateid is now finished.
4960 * Otherwise a negative NFS4ERR value is returned.
4961 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004962static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004963{
Chuck Levereb64cf92012-07-11 16:30:05 -04004964 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004965 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004966 struct nfs_server *server = NFS_SERVER(state->inode);
4967
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004968 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004969 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004970 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4971 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004972 /* Free the stateid unless the server
4973 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004974 if (status != -NFS4ERR_BAD_STATEID)
4975 nfs41_free_stateid(server,
4976 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004977 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004978 ret = status;
4979 }
4980 }
4981 };
4982
4983 return ret;
4984}
4985
4986static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4987{
4988 int status = NFS_OK;
4989
4990 if (test_bit(LK_STATE_IN_USE, &state->flags))
4991 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004992 if (status != NFS_OK)
4993 status = nfs4_lock_expired(state, request);
4994 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004995}
4996#endif
4997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4999{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005000 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005001 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005002 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005004 if ((fl_flags & FL_POSIX) &&
5005 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5006 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005007 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005008 status = nfs4_set_lock_state(state, request);
5009 if (status != 0)
5010 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005011 request->fl_flags |= FL_ACCESS;
5012 status = do_vfs_lock(request->fl_file, request);
5013 if (status < 0)
5014 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005015 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005016 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005017 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005018 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005019 request->fl_flags = fl_flags & ~FL_SLEEP;
5020 status = do_vfs_lock(request->fl_file, request);
5021 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005022 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005023 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005024 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04005025 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005026 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005027 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005028 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005029 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5030 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005031out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005032 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005033out:
5034 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 return status;
5036}
5037
5038static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5039{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005040 struct nfs4_exception exception = {
5041 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005042 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 int err;
5045
5046 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005047 err = _nfs4_proc_setlk(state, cmd, request);
5048 if (err == -NFS4ERR_DENIED)
5049 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005051 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 } while (exception.retry);
5053 return err;
5054}
5055
5056static int
5057nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5058{
5059 struct nfs_open_context *ctx;
5060 struct nfs4_state *state;
5061 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5062 int status;
5063
5064 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005065 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 state = ctx->state;
5067
5068 if (request->fl_start < 0 || request->fl_end < 0)
5069 return -EINVAL;
5070
Trond Myklebustd9531262009-07-21 19:22:38 -04005071 if (IS_GETLK(cmd)) {
5072 if (state != NULL)
5073 return nfs4_proc_getlk(state, F_GETLK, request);
5074 return 0;
5075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
5077 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5078 return -EINVAL;
5079
Trond Myklebustd9531262009-07-21 19:22:38 -04005080 if (request->fl_type == F_UNLCK) {
5081 if (state != NULL)
5082 return nfs4_proc_unlck(state, cmd, request);
5083 return 0;
5084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Trond Myklebustd9531262009-07-21 19:22:38 -04005086 if (state == NULL)
5087 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005088 /*
5089 * Don't rely on the VFS having checked the file open mode,
5090 * since it won't do this for flock() locks.
5091 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005092 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005093 case F_RDLCK:
5094 if (!(filp->f_mode & FMODE_READ))
5095 return -EBADF;
5096 break;
5097 case F_WRLCK:
5098 if (!(filp->f_mode & FMODE_WRITE))
5099 return -EBADF;
5100 }
5101
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 do {
5103 status = nfs4_proc_setlk(state, cmd, request);
5104 if ((status != -EAGAIN) || IS_SETLK(cmd))
5105 break;
5106 timeout = nfs4_set_lock_task_retry(timeout);
5107 status = -ERESTARTSYS;
5108 if (signalled())
5109 break;
5110 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return status;
5112}
5113
Trond Myklebust888e6942005-11-04 15:38:11 -05005114int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5115{
5116 struct nfs_server *server = NFS_SERVER(state->inode);
5117 struct nfs4_exception exception = { };
5118 int err;
5119
5120 err = nfs4_set_lock_state(state, fl);
5121 if (err != 0)
5122 goto out;
5123 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005124 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005125 switch (err) {
5126 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005127 printk(KERN_ERR "NFS: %s: unhandled error "
5128 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005129 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005130 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005131 goto out;
5132 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005133 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005134 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005135 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005136 nfs4_schedule_lease_recovery(server->nfs_client);
5137 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005138 case -NFS4ERR_BADSESSION:
5139 case -NFS4ERR_BADSLOT:
5140 case -NFS4ERR_BAD_HIGH_SLOT:
5141 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5142 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005143 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005144 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005145 case -ERESTARTSYS:
5146 /*
5147 * The show must go on: exit, but mark the
5148 * stateid as needing recovery.
5149 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005150 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005151 case -NFS4ERR_ADMIN_REVOKED:
5152 case -NFS4ERR_BAD_STATEID:
5153 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005154 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005155 err = 0;
5156 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005157 case -EKEYEXPIRED:
5158 /*
5159 * User RPCSEC_GSS context has expired.
5160 * We cannot recover this stateid now, so
5161 * skip it and allow recovery thread to
5162 * proceed.
5163 */
5164 err = 0;
5165 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005166 case -ENOMEM:
5167 case -NFS4ERR_DENIED:
5168 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5169 err = 0;
5170 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005171 case -NFS4ERR_DELAY:
5172 break;
5173 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005174 err = nfs4_handle_exception(server, err, &exception);
5175 } while (exception.retry);
5176out:
5177 return err;
5178}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005179
Trond Myklebustcf470c32012-03-07 13:49:12 -05005180struct nfs_release_lockowner_data {
5181 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005182 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005183 struct nfs_release_lockowner_args args;
5184};
5185
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005186static void nfs4_release_lockowner_release(void *calldata)
5187{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005188 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005189 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005190 kfree(calldata);
5191}
5192
Trond Myklebust17280172012-03-11 13:11:00 -04005193static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005194 .rpc_release = nfs4_release_lockowner_release,
5195};
5196
Trond Myklebustcf470c32012-03-07 13:49:12 -05005197int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005198{
5199 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005200 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005201 struct rpc_message msg = {
5202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5203 };
5204
5205 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005206 return -EINVAL;
5207 data = kmalloc(sizeof(*data), GFP_NOFS);
5208 if (!data)
5209 return -ENOMEM;
5210 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005211 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005212 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5213 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5214 data->args.lock_owner.s_dev = server->s_dev;
5215 msg.rpc_argp = &data->args;
5216 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5217 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005218}
5219
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005220#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5221
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005222static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5223 const void *buf, size_t buflen,
5224 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005225{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005226 if (strcmp(key, "") != 0)
5227 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005228
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005229 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005230}
5231
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005232static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5233 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005234{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005235 if (strcmp(key, "") != 0)
5236 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005237
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005238 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005239}
5240
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005241static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5242 size_t list_len, const char *name,
5243 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005244{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005245 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005246
J. Bruce Fields096455a2006-03-20 23:23:42 -05005247 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5248 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005249
5250 if (list && len <= list_len)
5251 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005252 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005253}
5254
Andy Adamson533eb462011-06-13 18:25:56 -04005255/*
5256 * nfs_fhget will use either the mounted_on_fileid or the fileid
5257 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005258static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5259{
Andy Adamson533eb462011-06-13 18:25:56 -04005260 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5261 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5262 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005263 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005264 return;
5265
5266 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005267 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005268 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5269 fattr->nlink = 2;
5270}
5271
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005272static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5273 const struct qstr *name,
5274 struct nfs4_fs_locations *fs_locations,
5275 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005276{
5277 struct nfs_server *server = NFS_SERVER(dir);
5278 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005279 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005280 };
5281 struct nfs4_fs_locations_arg args = {
5282 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005283 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005284 .page = page,
5285 .bitmask = bitmask,
5286 };
Benny Halevy22958462009-04-01 09:22:02 -04005287 struct nfs4_fs_locations_res res = {
5288 .fs_locations = fs_locations,
5289 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005290 struct rpc_message msg = {
5291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5292 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005293 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005294 };
5295 int status;
5296
Harvey Harrison3110ff82008-05-02 13:42:44 -07005297 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005298
5299 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5300 * is not supported */
5301 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5302 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5303 else
5304 bitmask[0] |= FATTR4_WORD0_FILEID;
5305
Trond Myklebustc228fd32007-01-13 02:28:11 -05005306 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005307 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005308 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005309 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005310 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005311 return status;
5312}
5313
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005314int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5315 const struct qstr *name,
5316 struct nfs4_fs_locations *fs_locations,
5317 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005318{
5319 struct nfs4_exception exception = { };
5320 int err;
5321 do {
5322 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005323 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005324 &exception);
5325 } while (exception.retry);
5326 return err;
5327}
5328
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005329static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5330{
5331 int status;
5332 struct nfs4_secinfo_arg args = {
5333 .dir_fh = NFS_FH(dir),
5334 .name = name,
5335 };
5336 struct nfs4_secinfo_res res = {
5337 .flavors = flavors,
5338 };
5339 struct rpc_message msg = {
5340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5341 .rpc_argp = &args,
5342 .rpc_resp = &res,
5343 };
5344
5345 dprintk("NFS call secinfo %s\n", name->name);
5346 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5347 dprintk("NFS reply secinfo: %d\n", status);
5348 return status;
5349}
5350
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005351int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5352 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005353{
5354 struct nfs4_exception exception = { };
5355 int err;
5356 do {
5357 err = nfs4_handle_exception(NFS_SERVER(dir),
5358 _nfs4_proc_secinfo(dir, name, flavors),
5359 &exception);
5360 } while (exception.retry);
5361 return err;
5362}
5363
Andy Adamson557134a2009-04-01 09:21:53 -04005364#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005365/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005366 * Check the exchange flags returned by the server for invalid flags, having
5367 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5368 * DS flags set.
5369 */
5370static int nfs4_check_cl_exchange_flags(u32 flags)
5371{
5372 if (flags & ~EXCHGID4_FLAG_MASK_R)
5373 goto out_inval;
5374 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5375 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5376 goto out_inval;
5377 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5378 goto out_inval;
5379 return NFS_OK;
5380out_inval:
5381 return -NFS4ERR_INVAL;
5382}
5383
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005384static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005385nfs41_same_server_scope(struct nfs41_server_scope *a,
5386 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005387{
5388 if (a->server_scope_sz == b->server_scope_sz &&
5389 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5390 return true;
5391
5392 return false;
5393}
5394
Andy Adamson357f54d2010-12-14 10:11:57 -05005395/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005396 * nfs4_proc_bind_conn_to_session()
5397 *
5398 * The 4.1 client currently uses the same TCP connection for the
5399 * fore and backchannel.
5400 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005401int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005402{
5403 int status;
5404 struct nfs41_bind_conn_to_session_res res;
5405 struct rpc_message msg = {
5406 .rpc_proc =
5407 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5408 .rpc_argp = clp,
5409 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005410 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005411 };
5412
5413 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005414
5415 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5416 if (unlikely(res.session == NULL)) {
5417 status = -ENOMEM;
5418 goto out;
5419 }
5420
5421 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5422 if (status == 0) {
5423 if (memcmp(res.session->sess_id.data,
5424 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5425 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5426 status = -EIO;
5427 goto out_session;
5428 }
5429 if (res.dir != NFS4_CDFS4_BOTH) {
5430 dprintk("NFS: %s: Unexpected direction from server\n",
5431 __func__);
5432 status = -EIO;
5433 goto out_session;
5434 }
5435 if (res.use_conn_in_rdma_mode) {
5436 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5437 __func__);
5438 status = -EIO;
5439 goto out_session;
5440 }
5441 }
5442out_session:
5443 kfree(res.session);
5444out:
5445 dprintk("<-- %s status= %d\n", __func__, status);
5446 return status;
5447}
5448
5449/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005450 * nfs4_proc_exchange_id()
5451 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005452 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5453 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005454 * Since the clientid has expired, all compounds using sessions
5455 * associated with the stale clientid will be returning
5456 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5457 * be in some phase of session reset.
5458 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005459int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005460{
5461 nfs4_verifier verifier;
5462 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005463 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005464 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005465 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005466 };
5467 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005468 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005469 };
5470 int status;
5471 struct rpc_message msg = {
5472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5473 .rpc_argp = &args,
5474 .rpc_resp = &res,
5475 .rpc_cred = cred,
5476 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005477
Chuck Leverf0920752012-05-21 22:45:41 -04005478 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005479 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5480 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005481 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5482 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5483 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005484
Chuck Leveracdeb692012-05-21 22:46:16 -04005485 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005486 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005487 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005488 status = -ENOMEM;
5489 goto out;
5490 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005491
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005492 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005493 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005494 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005495 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005496 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005497 }
5498
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005499 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005500 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005501 status = -ENOMEM;
5502 goto out_server_scope;
5503 }
5504
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005505 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005506 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005507 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005508
Chuck Lever177313f2012-05-21 22:44:58 -04005509 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005510 clp->cl_clientid = res.clientid;
5511 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5512 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5513 clp->cl_seqid = res.seqid;
5514
Chuck Leveracdeb692012-05-21 22:46:16 -04005515 kfree(clp->cl_serverowner);
5516 clp->cl_serverowner = res.server_owner;
5517 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005518
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005519 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005520 kfree(clp->cl_implid);
5521 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005522
Chuck Lever177313f2012-05-21 22:44:58 -04005523 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005524 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005525 res.server_scope)) {
5526 dprintk("%s: server_scope mismatch detected\n",
5527 __func__);
5528 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005529 kfree(clp->cl_serverscope);
5530 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005531 }
5532
Chuck Lever177313f2012-05-21 22:44:58 -04005533 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005534 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005535 goto out;
5536 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005537 } else
5538 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005539
Chuck Leveracdeb692012-05-21 22:46:16 -04005540out_server_owner:
5541 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005542out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005543 kfree(res.server_scope);
5544out:
Chuck Lever177313f2012-05-21 22:44:58 -04005545 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005546 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005547 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005548 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005549 clp->cl_implid->date.seconds,
5550 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005551 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005552 return status;
5553}
5554
Trond Myklebust66245532012-05-25 17:18:09 -04005555static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5556 struct rpc_cred *cred)
5557{
5558 struct rpc_message msg = {
5559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5560 .rpc_argp = clp,
5561 .rpc_cred = cred,
5562 };
5563 int status;
5564
5565 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5566 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005567 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005568 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5569 return status;
5570}
5571
5572static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5573 struct rpc_cred *cred)
5574{
5575 unsigned int loop;
5576 int ret;
5577
5578 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5579 ret = _nfs4_proc_destroy_clientid(clp, cred);
5580 switch (ret) {
5581 case -NFS4ERR_DELAY:
5582 case -NFS4ERR_CLIENTID_BUSY:
5583 ssleep(1);
5584 break;
5585 default:
5586 return ret;
5587 }
5588 }
5589 return 0;
5590}
5591
5592int nfs4_destroy_clientid(struct nfs_client *clp)
5593{
5594 struct rpc_cred *cred;
5595 int ret = 0;
5596
5597 if (clp->cl_mvops->minor_version < 1)
5598 goto out;
5599 if (clp->cl_exchange_flags == 0)
5600 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005601 if (clp->cl_preserve_clid)
5602 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005603 cred = nfs4_get_exchange_id_cred(clp);
5604 ret = nfs4_proc_destroy_clientid(clp, cred);
5605 if (cred)
5606 put_rpccred(cred);
5607 switch (ret) {
5608 case 0:
5609 case -NFS4ERR_STALE_CLIENTID:
5610 clp->cl_exchange_flags = 0;
5611 }
5612out:
5613 return ret;
5614}
5615
Andy Adamson2050f0c2009-04-01 09:22:30 -04005616struct nfs4_get_lease_time_data {
5617 struct nfs4_get_lease_time_args *args;
5618 struct nfs4_get_lease_time_res *res;
5619 struct nfs_client *clp;
5620};
5621
5622static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5623 void *calldata)
5624{
5625 int ret;
5626 struct nfs4_get_lease_time_data *data =
5627 (struct nfs4_get_lease_time_data *)calldata;
5628
5629 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005630 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005631 /* just setup sequence, do not trigger session recovery
5632 since we're invoked within one */
5633 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005634 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005635 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005636
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005637 if (ret != -EAGAIN)
5638 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005639 dprintk("<-- %s\n", __func__);
5640}
5641
5642/*
5643 * Called from nfs4_state_manager thread for session setup, so don't recover
5644 * from sequence operation or clientid errors.
5645 */
5646static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5647{
5648 struct nfs4_get_lease_time_data *data =
5649 (struct nfs4_get_lease_time_data *)calldata;
5650
5651 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005652 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5653 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005654 switch (task->tk_status) {
5655 case -NFS4ERR_DELAY:
5656 case -NFS4ERR_GRACE:
5657 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5658 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5659 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005660 /* fall through */
5661 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005662 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005663 return;
5664 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005665 dprintk("<-- %s\n", __func__);
5666}
5667
Trond Myklebust17280172012-03-11 13:11:00 -04005668static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005669 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5670 .rpc_call_done = nfs4_get_lease_time_done,
5671};
5672
5673int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5674{
5675 struct rpc_task *task;
5676 struct nfs4_get_lease_time_args args;
5677 struct nfs4_get_lease_time_res res = {
5678 .lr_fsinfo = fsinfo,
5679 };
5680 struct nfs4_get_lease_time_data data = {
5681 .args = &args,
5682 .res = &res,
5683 .clp = clp,
5684 };
5685 struct rpc_message msg = {
5686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5687 .rpc_argp = &args,
5688 .rpc_resp = &res,
5689 };
5690 struct rpc_task_setup task_setup = {
5691 .rpc_client = clp->cl_rpcclient,
5692 .rpc_message = &msg,
5693 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005694 .callback_data = &data,
5695 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005696 };
5697 int status;
5698
Trond Myklebust9d12b212012-01-17 22:04:25 -05005699 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005700 dprintk("--> %s\n", __func__);
5701 task = rpc_run_task(&task_setup);
5702
5703 if (IS_ERR(task))
5704 status = PTR_ERR(task);
5705 else {
5706 status = task->tk_status;
5707 rpc_put_task(task);
5708 }
5709 dprintk("<-- %s return %d\n", __func__, status);
5710
5711 return status;
5712}
5713
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005714struct nfs4_slot *nfs4_alloc_slots(struct nfs4_slot_table *table,
5715 u32 max_slots, gfp_t gfp_flags)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005716{
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005717 struct nfs4_slot *tbl;
5718 u32 i;
5719
5720 tbl = kmalloc_array(max_slots, sizeof(*tbl), gfp_flags);
5721 if (tbl != NULL) {
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005722 for (i = 0; i < max_slots; i++) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005723 tbl[i].table = table;
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005724 tbl[i].slot_nr = i;
5725 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005726 }
5727 return tbl;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005728}
5729
5730static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5731 struct nfs4_slot *new,
5732 u32 max_slots,
5733 u32 ivalue)
5734{
5735 struct nfs4_slot *old = NULL;
5736 u32 i;
5737
5738 spin_lock(&tbl->slot_tbl_lock);
5739 if (new) {
5740 old = tbl->slots;
5741 tbl->slots = new;
5742 tbl->max_slots = max_slots;
5743 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005744 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust464ee9f2012-11-20 12:49:27 -05005745 tbl->target_highest_slotid = max_slots - 1;
Trond Myklebustda0507b2012-11-20 18:10:30 -05005746 tbl->server_highest_slotid = max_slots - 1;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005747 for (i = 0; i < tbl->max_slots; i++)
5748 tbl->slots[i].seq_nr = ivalue;
5749 spin_unlock(&tbl->slot_tbl_lock);
5750 kfree(old);
5751}
5752
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005753/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005754 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005755 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005756static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5757 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005758{
Andy Adamson104aeba2010-01-14 17:45:10 -05005759 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005760 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005761
Andy Adamson104aeba2010-01-14 17:45:10 -05005762 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5763 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005764
Andy Adamson104aeba2010-01-14 17:45:10 -05005765 /* Does the newly negotiated max_reqs match the existing slot table? */
5766 if (max_reqs != tbl->max_slots) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005767 new = nfs4_alloc_slots(tbl, max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005768 if (!new)
5769 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005770 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005771 ret = 0;
5772
5773 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005774 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5775 tbl, tbl->slots, tbl->max_slots);
5776out:
5777 dprintk("<-- %s: return %d\n", __func__, ret);
5778 return ret;
5779}
5780
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005781/* Destroy the slot table */
5782static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5783{
5784 if (session->fc_slot_table.slots != NULL) {
5785 kfree(session->fc_slot_table.slots);
5786 session->fc_slot_table.slots = NULL;
5787 }
5788 if (session->bc_slot_table.slots != NULL) {
5789 kfree(session->bc_slot_table.slots);
5790 session->bc_slot_table.slots = NULL;
5791 }
5792 return;
5793}
5794
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005795/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005796 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005797 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005798static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005799{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005800 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005801 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005802
Andy Adamsonaacd5532011-11-09 13:58:21 -05005803 dprintk("--> %s\n", __func__);
5804 /* Fore channel */
5805 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005806 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005807 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5808 if (status) /* -ENOMEM */
5809 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005810 /* Back channel */
5811 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005812 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005813 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5814 if (status && tbl->slots == NULL)
5815 /* Fore and back channel share a connection so get
5816 * both slot tables or neither */
5817 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005818 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005819}
5820
5821struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5822{
5823 struct nfs4_session *session;
5824 struct nfs4_slot_table *tbl;
5825
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005826 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005827 if (!session)
5828 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005829
Andy Adamson557134a2009-04-01 09:21:53 -04005830 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005831 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005832 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005833 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005834 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005835
5836 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005837 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005838 spin_lock_init(&tbl->slot_tbl_lock);
5839 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005840 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005841
Trond Myklebust1055d762010-06-16 09:52:27 -04005842 session->session_state = 1<<NFS4_SESSION_INITING;
5843
Andy Adamson557134a2009-04-01 09:21:53 -04005844 session->clp = clp;
5845 return session;
5846}
5847
5848void nfs4_destroy_session(struct nfs4_session *session)
5849{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005850 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005851 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005852
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005853 cred = nfs4_get_exchange_id_cred(session->clp);
5854 nfs4_proc_destroy_session(session, cred);
5855 if (cred)
5856 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005857
5858 rcu_read_lock();
5859 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5860 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005861 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005862 __func__, xprt);
5863 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005864 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005865 kfree(session);
5866}
5867
Andy Adamsonfc931582009-04-01 09:22:31 -04005868/*
5869 * Initialize the values to be used by the client in CREATE_SESSION
5870 * If nfs4_init_session set the fore channel request and response sizes,
5871 * use them.
5872 *
5873 * Set the back channel max_resp_sz_cached to zero to force the client to
5874 * always set csa_cachethis to FALSE because the current implementation
5875 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5876 */
5877static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5878{
5879 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005880 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5881 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005882
5883 if (mxrqst_sz == 0)
5884 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5885 if (mxresp_sz == 0)
5886 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5887 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005888 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5889 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005890 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005891 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005892
5893 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005894 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005895 __func__,
5896 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005897 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005898
5899 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005900 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5901 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5902 args->bc_attrs.max_resp_sz_cached = 0;
5903 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5904 args->bc_attrs.max_reqs = 1;
5905
5906 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5907 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5908 __func__,
5909 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5910 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5911 args->bc_attrs.max_reqs);
5912}
5913
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005914static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005915{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005916 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5917 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5918
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005919 if (rcvd->max_resp_sz > sent->max_resp_sz)
5920 return -EINVAL;
5921 /*
5922 * Our requested max_ops is the minimum we need; we're not
5923 * prepared to break up compounds into smaller pieces than that.
5924 * So, no point even trying to continue if the server won't
5925 * cooperate:
5926 */
5927 if (rcvd->max_ops < sent->max_ops)
5928 return -EINVAL;
5929 if (rcvd->max_reqs == 0)
5930 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005931 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5932 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005933 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005934}
5935
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005936static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5937{
5938 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5939 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005940
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005941 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5942 return -EINVAL;
5943 if (rcvd->max_resp_sz < sent->max_resp_sz)
5944 return -EINVAL;
5945 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5946 return -EINVAL;
5947 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005948 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005949 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005950 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005951 return -EINVAL;
5952 return 0;
5953}
Andy Adamson8d353012009-04-01 09:22:32 -04005954
Andy Adamson8d353012009-04-01 09:22:32 -04005955static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5956 struct nfs4_session *session)
5957{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005958 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005959
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005960 ret = nfs4_verify_fore_channel_attrs(args, session);
5961 if (ret)
5962 return ret;
5963 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005964}
5965
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005966static int _nfs4_proc_create_session(struct nfs_client *clp,
5967 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005968{
5969 struct nfs4_session *session = clp->cl_session;
5970 struct nfs41_create_session_args args = {
5971 .client = clp,
5972 .cb_program = NFS4_CALLBACK,
5973 };
5974 struct nfs41_create_session_res res = {
5975 .client = clp,
5976 };
5977 struct rpc_message msg = {
5978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5979 .rpc_argp = &args,
5980 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005981 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005982 };
5983 int status;
5984
5985 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005986 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005987
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005988 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005989
Trond Myklebust43095d32012-11-20 11:13:12 -05005990 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005991 /* Verify the session's negotiated channel_attrs values */
5992 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005993 /* Increment the clientid slot sequence id */
5994 clp->cl_seqid++;
5995 }
5996
5997 return status;
5998}
5999
6000/*
6001 * Issues a CREATE_SESSION operation to the server.
6002 * It is the responsibility of the caller to verify the session is
6003 * expired before calling this routine.
6004 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006005int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006006{
6007 int status;
6008 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006009 struct nfs4_session *session = clp->cl_session;
6010
6011 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6012
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006013 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006014 if (status)
6015 goto out;
6016
Andy Adamsonaacd5532011-11-09 13:58:21 -05006017 /* Init or reset the session slot tables */
6018 status = nfs4_setup_session_slot_tables(session);
6019 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006020 if (status)
6021 goto out;
6022
6023 ptr = (unsigned *)&session->sess_id.data[0];
6024 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6025 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006026out:
6027 dprintk("<-- %s\n", __func__);
6028 return status;
6029}
6030
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006031/*
6032 * Issue the over-the-wire RPC DESTROY_SESSION.
6033 * The caller must serialize access to this routine.
6034 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006035int nfs4_proc_destroy_session(struct nfs4_session *session,
6036 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006037{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006038 struct rpc_message msg = {
6039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6040 .rpc_argp = session,
6041 .rpc_cred = cred,
6042 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006043 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006044
6045 dprintk("--> nfs4_proc_destroy_session\n");
6046
6047 /* session is still being setup */
6048 if (session->clp->cl_cons_state != NFS_CS_READY)
6049 return status;
6050
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006051 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006052
6053 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006054 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006055 "Session has been destroyed regardless...\n", status);
6056
6057 dprintk("<-- nfs4_proc_destroy_session\n");
6058 return status;
6059}
6060
Trond Myklebust7b38c362012-05-23 13:23:31 -04006061/*
6062 * With sessions, the client is not marked ready until after a
6063 * successful EXCHANGE_ID and CREATE_SESSION.
6064 *
6065 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6066 * other versions of NFS can be tried.
6067 */
6068static int nfs41_check_session_ready(struct nfs_client *clp)
6069{
6070 int ret;
6071
6072 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6073 ret = nfs4_client_recover_expired_lease(clp);
6074 if (ret)
6075 return ret;
6076 }
6077 if (clp->cl_cons_state < NFS_CS_READY)
6078 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006079 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006080 return 0;
6081}
6082
Trond Myklebustfccba802009-07-21 16:48:07 -04006083int nfs4_init_session(struct nfs_server *server)
6084{
6085 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006086 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006087 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6088 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006089
6090 if (!nfs4_has_session(clp))
6091 return 0;
6092
Trond Myklebustae72ae672012-11-20 11:02:55 -05006093 if (server->rsize != 0)
6094 target_max_resp_sz = server->rsize;
6095 target_max_resp_sz += nfs41_maxread_overhead;
6096
6097 if (server->wsize != 0)
6098 target_max_rqst_sz = server->wsize;
6099 target_max_rqst_sz += nfs41_maxwrite_overhead;
6100
Trond Myklebust1055d762010-06-16 09:52:27 -04006101 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006102 spin_lock(&clp->cl_lock);
6103 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006104 /* Initialise targets and channel attributes */
6105 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6106 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6107 session->fc_target_max_resp_sz = target_max_resp_sz;
6108 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006109 } else {
6110 /* Just adjust the targets */
6111 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6112 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6113 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6114 }
6115 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6116 session->fc_target_max_resp_sz = target_max_resp_sz;
6117 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6118 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006119 }
6120 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006121
Trond Myklebust688a9022012-11-20 10:53:39 -05006122 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6123 nfs4_schedule_lease_recovery(clp);
6124
Trond Myklebust7b38c362012-05-23 13:23:31 -04006125 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006126}
6127
Trond Myklebust7b38c362012-05-23 13:23:31 -04006128int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006129{
6130 struct nfs4_session *session = clp->cl_session;
6131 int ret;
6132
Trond Myklebust7b38c362012-05-23 13:23:31 -04006133 spin_lock(&clp->cl_lock);
6134 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6135 /*
6136 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6137 * DS lease to be equal to the MDS lease.
6138 */
6139 clp->cl_lease_time = lease_time;
6140 clp->cl_last_renewal = jiffies;
6141 }
6142 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006143
Trond Myklebust7b38c362012-05-23 13:23:31 -04006144 ret = nfs41_check_session_ready(clp);
6145 if (ret)
6146 return ret;
6147 /* Test for the DS role */
6148 if (!is_ds_client(clp))
6149 return -ENODEV;
6150 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006151}
6152EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6153
6154
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006155/*
6156 * Renew the cl_session lease.
6157 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006158struct nfs4_sequence_data {
6159 struct nfs_client *clp;
6160 struct nfs4_sequence_args args;
6161 struct nfs4_sequence_res res;
6162};
6163
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006164static void nfs41_sequence_release(void *data)
6165{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006166 struct nfs4_sequence_data *calldata = data;
6167 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006168
Alexandros Batsakis71358402010-02-05 03:45:05 -08006169 if (atomic_read(&clp->cl_count) > 1)
6170 nfs4_schedule_state_renewal(clp);
6171 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006172 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006173}
6174
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006175static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6176{
6177 switch(task->tk_status) {
6178 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006179 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6180 return -EAGAIN;
6181 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006182 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006183 }
6184 return 0;
6185}
6186
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006187static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006188{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006189 struct nfs4_sequence_data *calldata = data;
6190 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006191
Trond Myklebust14516c32010-07-31 14:29:06 -04006192 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6193 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006194
6195 if (task->tk_status < 0) {
6196 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006197 if (atomic_read(&clp->cl_count) == 1)
6198 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006199
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006200 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6201 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006202 return;
6203 }
6204 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006205 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006206out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006207 dprintk("<-- %s\n", __func__);
6208}
6209
6210static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6211{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006212 struct nfs4_sequence_data *calldata = data;
6213 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006214 struct nfs4_sequence_args *args;
6215 struct nfs4_sequence_res *res;
6216
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006217 args = task->tk_msg.rpc_argp;
6218 res = task->tk_msg.rpc_resp;
6219
Trond Myklebust9d12b212012-01-17 22:04:25 -05006220 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006221 return;
6222 rpc_call_start(task);
6223}
6224
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006225static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6226{
6227 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6228 nfs41_sequence_prepare(task, data);
6229}
6230
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006231static const struct rpc_call_ops nfs41_sequence_ops = {
6232 .rpc_call_done = nfs41_sequence_call_done,
6233 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006234 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006235};
6236
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006237static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6238 .rpc_call_done = nfs41_sequence_call_done,
6239 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6240 .rpc_release = nfs41_sequence_release,
6241};
6242
6243static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6244 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006245{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006246 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006247 struct rpc_message msg = {
6248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6249 .rpc_cred = cred,
6250 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006251 struct rpc_task_setup task_setup_data = {
6252 .rpc_client = clp->cl_rpcclient,
6253 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006254 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006255 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6256 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006257
Alexandros Batsakis71358402010-02-05 03:45:05 -08006258 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006259 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006260 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006261 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006262 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006263 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006264 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006265 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006266 msg.rpc_argp = &calldata->args;
6267 msg.rpc_resp = &calldata->res;
6268 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006269 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006270
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006271 return rpc_run_task(&task_setup_data);
6272}
6273
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006274static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006275{
6276 struct rpc_task *task;
6277 int ret = 0;
6278
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006279 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6280 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006281 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006282 if (IS_ERR(task))
6283 ret = PTR_ERR(task);
6284 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006285 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006286 dprintk("<-- %s status=%d\n", __func__, ret);
6287 return ret;
6288}
6289
6290static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6291{
6292 struct rpc_task *task;
6293 int ret;
6294
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006295 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006296 if (IS_ERR(task)) {
6297 ret = PTR_ERR(task);
6298 goto out;
6299 }
6300 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006301 if (!ret) {
6302 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6303
6304 if (task->tk_status == 0)
6305 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006306 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006307 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006308 rpc_put_task(task);
6309out:
6310 dprintk("<-- %s status=%d\n", __func__, ret);
6311 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006312}
6313
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006314struct nfs4_reclaim_complete_data {
6315 struct nfs_client *clp;
6316 struct nfs41_reclaim_complete_args arg;
6317 struct nfs41_reclaim_complete_res res;
6318};
6319
6320static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6321{
6322 struct nfs4_reclaim_complete_data *calldata = data;
6323
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006324 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006325 if (nfs41_setup_sequence(calldata->clp->cl_session,
6326 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006327 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006328 return;
6329
6330 rpc_call_start(task);
6331}
6332
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006333static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6334{
6335 switch(task->tk_status) {
6336 case 0:
6337 case -NFS4ERR_COMPLETE_ALREADY:
6338 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6339 break;
6340 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006341 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006342 /* fall through */
6343 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006344 return -EAGAIN;
6345 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006346 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006347 }
6348 return 0;
6349}
6350
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006351static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6352{
6353 struct nfs4_reclaim_complete_data *calldata = data;
6354 struct nfs_client *clp = calldata->clp;
6355 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6356
6357 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006358 if (!nfs41_sequence_done(task, res))
6359 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006360
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006361 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6362 rpc_restart_call_prepare(task);
6363 return;
6364 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006365 dprintk("<-- %s\n", __func__);
6366}
6367
6368static void nfs4_free_reclaim_complete_data(void *data)
6369{
6370 struct nfs4_reclaim_complete_data *calldata = data;
6371
6372 kfree(calldata);
6373}
6374
6375static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6376 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6377 .rpc_call_done = nfs4_reclaim_complete_done,
6378 .rpc_release = nfs4_free_reclaim_complete_data,
6379};
6380
6381/*
6382 * Issue a global reclaim complete.
6383 */
6384static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6385{
6386 struct nfs4_reclaim_complete_data *calldata;
6387 struct rpc_task *task;
6388 struct rpc_message msg = {
6389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6390 };
6391 struct rpc_task_setup task_setup_data = {
6392 .rpc_client = clp->cl_rpcclient,
6393 .rpc_message = &msg,
6394 .callback_ops = &nfs4_reclaim_complete_call_ops,
6395 .flags = RPC_TASK_ASYNC,
6396 };
6397 int status = -ENOMEM;
6398
6399 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006400 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006401 if (calldata == NULL)
6402 goto out;
6403 calldata->clp = clp;
6404 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006405
Trond Myklebust9d12b212012-01-17 22:04:25 -05006406 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006407 msg.rpc_argp = &calldata->arg;
6408 msg.rpc_resp = &calldata->res;
6409 task_setup_data.callback_data = calldata;
6410 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006411 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006412 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006413 goto out;
6414 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006415 status = nfs4_wait_for_completion_rpc_task(task);
6416 if (status == 0)
6417 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006418 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006419 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006420out:
6421 dprintk("<-- %s status=%d\n", __func__, status);
6422 return status;
6423}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006424
6425static void
6426nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6427{
6428 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006429 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006430
6431 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006432 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6433 * right now covering the LAYOUTGET we are about to send.
6434 * However, that is not so catastrophic, and there seems
6435 * to be no way to prevent it completely.
6436 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006437 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006438 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006439 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006440 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6441 NFS_I(lgp->args.inode)->layout,
6442 lgp->args.ctx->state)) {
6443 rpc_exit(task, NFS4_OK);
6444 return;
6445 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006446 rpc_call_start(task);
6447}
6448
6449static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6450{
6451 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006452 struct inode *inode = lgp->args.inode;
6453 struct nfs_server *server = NFS_SERVER(inode);
6454 struct pnfs_layout_hdr *lo;
6455 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006456
6457 dprintk("--> %s\n", __func__);
6458
6459 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006460 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006461
6462 switch (task->tk_status) {
6463 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006464 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006465 case -NFS4ERR_LAYOUTTRYLATER:
6466 case -NFS4ERR_RECALLCONFLICT:
6467 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006468 break;
6469 case -NFS4ERR_EXPIRED:
6470 case -NFS4ERR_BAD_STATEID:
6471 spin_lock(&inode->i_lock);
6472 lo = NFS_I(inode)->layout;
6473 if (!lo || list_empty(&lo->plh_segs)) {
6474 spin_unlock(&inode->i_lock);
6475 /* If the open stateid was bad, then recover it. */
6476 state = lgp->args.ctx->state;
6477 } else {
6478 LIST_HEAD(head);
6479
6480 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6481 spin_unlock(&inode->i_lock);
6482 /* Mark the bad layout state as invalid, then
6483 * retry using the open stateid. */
6484 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006485 }
6486 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006487 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6488 rpc_restart_call_prepare(task);
6489out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006490 dprintk("<-- %s\n", __func__);
6491}
6492
Idan Kedar85541162012-08-02 11:47:10 +03006493static size_t max_response_pages(struct nfs_server *server)
6494{
6495 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6496 return nfs_page_array_len(0, max_resp_sz);
6497}
6498
6499static void nfs4_free_pages(struct page **pages, size_t size)
6500{
6501 int i;
6502
6503 if (!pages)
6504 return;
6505
6506 for (i = 0; i < size; i++) {
6507 if (!pages[i])
6508 break;
6509 __free_page(pages[i]);
6510 }
6511 kfree(pages);
6512}
6513
6514static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6515{
6516 struct page **pages;
6517 int i;
6518
6519 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6520 if (!pages) {
6521 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6522 return NULL;
6523 }
6524
6525 for (i = 0; i < size; i++) {
6526 pages[i] = alloc_page(gfp_flags);
6527 if (!pages[i]) {
6528 dprintk("%s: failed to allocate page\n", __func__);
6529 nfs4_free_pages(pages, size);
6530 return NULL;
6531 }
6532 }
6533
6534 return pages;
6535}
6536
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006537static void nfs4_layoutget_release(void *calldata)
6538{
6539 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006540 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6541 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006542
6543 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006544 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006545 put_nfs_open_context(lgp->args.ctx);
6546 kfree(calldata);
6547 dprintk("<-- %s\n", __func__);
6548}
6549
6550static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6551 .rpc_call_prepare = nfs4_layoutget_prepare,
6552 .rpc_call_done = nfs4_layoutget_done,
6553 .rpc_release = nfs4_layoutget_release,
6554};
6555
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006556struct pnfs_layout_segment *
6557nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006558{
6559 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006560 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006561 struct rpc_task *task;
6562 struct rpc_message msg = {
6563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6564 .rpc_argp = &lgp->args,
6565 .rpc_resp = &lgp->res,
6566 };
6567 struct rpc_task_setup task_setup_data = {
6568 .rpc_client = server->client,
6569 .rpc_message = &msg,
6570 .callback_ops = &nfs4_layoutget_call_ops,
6571 .callback_data = lgp,
6572 .flags = RPC_TASK_ASYNC,
6573 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006574 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006575 int status = 0;
6576
6577 dprintk("--> %s\n", __func__);
6578
Idan Kedar85541162012-08-02 11:47:10 +03006579 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6580 if (!lgp->args.layout.pages) {
6581 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006582 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006583 }
6584 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6585
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006586 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006587 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006588 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006589 task = rpc_run_task(&task_setup_data);
6590 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006591 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006592 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006593 if (status == 0)
6594 status = task->tk_status;
6595 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006596 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006597 rpc_put_task(task);
6598 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006599 if (status)
6600 return ERR_PTR(status);
6601 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006602}
6603
Benny Halevycbe82602011-05-22 19:52:37 +03006604static void
6605nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6606{
6607 struct nfs4_layoutreturn *lrp = calldata;
6608
6609 dprintk("--> %s\n", __func__);
6610 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006611 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006612 return;
6613 rpc_call_start(task);
6614}
6615
6616static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6617{
6618 struct nfs4_layoutreturn *lrp = calldata;
6619 struct nfs_server *server;
6620
6621 dprintk("--> %s\n", __func__);
6622
6623 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6624 return;
6625
6626 server = NFS_SERVER(lrp->args.inode);
6627 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006628 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006629 return;
6630 }
Benny Halevycbe82602011-05-22 19:52:37 +03006631 dprintk("<-- %s\n", __func__);
6632}
6633
6634static void nfs4_layoutreturn_release(void *calldata)
6635{
6636 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006637 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006638
6639 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006640 spin_lock(&lo->plh_inode->i_lock);
6641 if (lrp->res.lrs_present)
6642 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6643 lo->plh_block_lgets--;
6644 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006645 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006646 kfree(calldata);
6647 dprintk("<-- %s\n", __func__);
6648}
6649
6650static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6651 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6652 .rpc_call_done = nfs4_layoutreturn_done,
6653 .rpc_release = nfs4_layoutreturn_release,
6654};
6655
6656int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6657{
6658 struct rpc_task *task;
6659 struct rpc_message msg = {
6660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6661 .rpc_argp = &lrp->args,
6662 .rpc_resp = &lrp->res,
6663 };
6664 struct rpc_task_setup task_setup_data = {
6665 .rpc_client = lrp->clp->cl_rpcclient,
6666 .rpc_message = &msg,
6667 .callback_ops = &nfs4_layoutreturn_call_ops,
6668 .callback_data = lrp,
6669 };
6670 int status;
6671
6672 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006673 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006674 task = rpc_run_task(&task_setup_data);
6675 if (IS_ERR(task))
6676 return PTR_ERR(task);
6677 status = task->tk_status;
6678 dprintk("<-- %s status=%d\n", __func__, status);
6679 rpc_put_task(task);
6680 return status;
6681}
6682
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006683/*
6684 * Retrieve the list of Data Server devices from the MDS.
6685 */
6686static int _nfs4_getdevicelist(struct nfs_server *server,
6687 const struct nfs_fh *fh,
6688 struct pnfs_devicelist *devlist)
6689{
6690 struct nfs4_getdevicelist_args args = {
6691 .fh = fh,
6692 .layoutclass = server->pnfs_curr_ld->id,
6693 };
6694 struct nfs4_getdevicelist_res res = {
6695 .devlist = devlist,
6696 };
6697 struct rpc_message msg = {
6698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6699 .rpc_argp = &args,
6700 .rpc_resp = &res,
6701 };
6702 int status;
6703
6704 dprintk("--> %s\n", __func__);
6705 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6706 &res.seq_res, 0);
6707 dprintk("<-- %s status=%d\n", __func__, status);
6708 return status;
6709}
6710
6711int nfs4_proc_getdevicelist(struct nfs_server *server,
6712 const struct nfs_fh *fh,
6713 struct pnfs_devicelist *devlist)
6714{
6715 struct nfs4_exception exception = { };
6716 int err;
6717
6718 do {
6719 err = nfs4_handle_exception(server,
6720 _nfs4_getdevicelist(server, fh, devlist),
6721 &exception);
6722 } while (exception.retry);
6723
6724 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6725 err, devlist->num_devs);
6726
6727 return err;
6728}
6729EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6730
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006731static int
6732_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6733{
6734 struct nfs4_getdeviceinfo_args args = {
6735 .pdev = pdev,
6736 };
6737 struct nfs4_getdeviceinfo_res res = {
6738 .pdev = pdev,
6739 };
6740 struct rpc_message msg = {
6741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6742 .rpc_argp = &args,
6743 .rpc_resp = &res,
6744 };
6745 int status;
6746
6747 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006748 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006749 dprintk("<-- %s status=%d\n", __func__, status);
6750
6751 return status;
6752}
6753
6754int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6755{
6756 struct nfs4_exception exception = { };
6757 int err;
6758
6759 do {
6760 err = nfs4_handle_exception(server,
6761 _nfs4_proc_getdeviceinfo(server, pdev),
6762 &exception);
6763 } while (exception.retry);
6764 return err;
6765}
6766EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6767
Andy Adamson863a3c62011-03-23 13:27:54 +00006768static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6769{
6770 struct nfs4_layoutcommit_data *data = calldata;
6771 struct nfs_server *server = NFS_SERVER(data->args.inode);
6772
6773 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006774 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006775 return;
6776 rpc_call_start(task);
6777}
6778
6779static void
6780nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6781{
6782 struct nfs4_layoutcommit_data *data = calldata;
6783 struct nfs_server *server = NFS_SERVER(data->args.inode);
6784
6785 if (!nfs4_sequence_done(task, &data->res.seq_res))
6786 return;
6787
6788 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006789 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6790 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6791 case -NFS4ERR_BADLAYOUT: /* no layout */
6792 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006793 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006794 break;
6795 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006796 nfs_post_op_update_inode_force_wcc(data->args.inode,
6797 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006798 break;
6799 default:
6800 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6801 rpc_restart_call_prepare(task);
6802 return;
6803 }
6804 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006805}
6806
6807static void nfs4_layoutcommit_release(void *calldata)
6808{
6809 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006810 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006811 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006812
Andy Adamsondb29c082011-07-30 20:52:38 -04006813 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006814 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006815 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6816 list_del_init(&lseg->pls_lc_list);
6817 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6818 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006819 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006820 }
Peng Tao92407e72011-10-23 20:21:17 -07006821
6822 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6823 smp_mb__after_clear_bit();
6824 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6825
Andy Adamson863a3c62011-03-23 13:27:54 +00006826 put_rpccred(data->cred);
6827 kfree(data);
6828}
6829
6830static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6831 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6832 .rpc_call_done = nfs4_layoutcommit_done,
6833 .rpc_release = nfs4_layoutcommit_release,
6834};
6835
6836int
Andy Adamsonef311532011-03-12 02:58:10 -05006837nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006838{
6839 struct rpc_message msg = {
6840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6841 .rpc_argp = &data->args,
6842 .rpc_resp = &data->res,
6843 .rpc_cred = data->cred,
6844 };
6845 struct rpc_task_setup task_setup_data = {
6846 .task = &data->task,
6847 .rpc_client = NFS_CLIENT(data->args.inode),
6848 .rpc_message = &msg,
6849 .callback_ops = &nfs4_layoutcommit_ops,
6850 .callback_data = data,
6851 .flags = RPC_TASK_ASYNC,
6852 };
6853 struct rpc_task *task;
6854 int status = 0;
6855
6856 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6857 "lbw: %llu inode %lu\n",
6858 data->task.tk_pid, sync,
6859 data->args.lastbytewritten,
6860 data->args.inode->i_ino);
6861
Trond Myklebust9d12b212012-01-17 22:04:25 -05006862 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006863 task = rpc_run_task(&task_setup_data);
6864 if (IS_ERR(task))
6865 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006866 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006867 goto out;
6868 status = nfs4_wait_for_completion_rpc_task(task);
6869 if (status != 0)
6870 goto out;
6871 status = task->tk_status;
6872out:
6873 dprintk("%s: status %d\n", __func__, status);
6874 rpc_put_task(task);
6875 return status;
6876}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006877
6878static int
6879_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6880 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6881{
6882 struct nfs41_secinfo_no_name_args args = {
6883 .style = SECINFO_STYLE_CURRENT_FH,
6884 };
6885 struct nfs4_secinfo_res res = {
6886 .flavors = flavors,
6887 };
6888 struct rpc_message msg = {
6889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6890 .rpc_argp = &args,
6891 .rpc_resp = &res,
6892 };
6893 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6894}
6895
6896static int
6897nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6898 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6899{
6900 struct nfs4_exception exception = { };
6901 int err;
6902 do {
6903 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6904 switch (err) {
6905 case 0:
6906 case -NFS4ERR_WRONGSEC:
6907 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006908 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006909 default:
6910 err = nfs4_handle_exception(server, err, &exception);
6911 }
6912 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006913out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006914 return err;
6915}
6916
6917static int
6918nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6919 struct nfs_fsinfo *info)
6920{
6921 int err;
6922 struct page *page;
6923 rpc_authflavor_t flavor;
6924 struct nfs4_secinfo_flavors *flavors;
6925
6926 page = alloc_page(GFP_KERNEL);
6927 if (!page) {
6928 err = -ENOMEM;
6929 goto out;
6930 }
6931
6932 flavors = page_address(page);
6933 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6934
6935 /*
6936 * Fall back on "guess and check" method if
6937 * the server doesn't support SECINFO_NO_NAME
6938 */
6939 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6940 err = nfs4_find_root_sec(server, fhandle, info);
6941 goto out_freepage;
6942 }
6943 if (err)
6944 goto out_freepage;
6945
6946 flavor = nfs_find_best_sec(flavors);
6947 if (err == 0)
6948 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6949
6950out_freepage:
6951 put_page(page);
6952 if (err == -EACCES)
6953 return -EPERM;
6954out:
6955 return err;
6956}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006957
6958static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006959{
6960 int status;
6961 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006962 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006963 };
6964 struct nfs41_test_stateid_res res;
6965 struct rpc_message msg = {
6966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6967 .rpc_argp = &args,
6968 .rpc_resp = &res,
6969 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006970
Chuck Lever38527b12012-07-11 16:30:23 -04006971 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006972 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006973 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006974 if (status != NFS_OK) {
6975 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006976 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006977 }
6978 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006979 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006980}
6981
Chuck Lever38527b12012-07-11 16:30:23 -04006982/**
6983 * nfs41_test_stateid - perform a TEST_STATEID operation
6984 *
6985 * @server: server / transport on which to perform the operation
6986 * @stateid: state ID to test
6987 *
6988 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6989 * Otherwise a negative NFS4ERR value is returned if the operation
6990 * failed or the state ID is not currently valid.
6991 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006992static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006993{
6994 struct nfs4_exception exception = { };
6995 int err;
6996 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006997 err = _nfs41_test_stateid(server, stateid);
6998 if (err != -NFS4ERR_DELAY)
6999 break;
7000 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007001 } while (exception.retry);
7002 return err;
7003}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007004
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007005static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007006{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007007 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007008 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007009 };
7010 struct nfs41_free_stateid_res res;
7011 struct rpc_message msg = {
7012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7013 .rpc_argp = &args,
7014 .rpc_resp = &res,
7015 };
Chuck Lever38527b12012-07-11 16:30:23 -04007016 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007017
Chuck Lever38527b12012-07-11 16:30:23 -04007018 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007019 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04007020 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04007021 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04007022 dprintk("NFS reply free_stateid: %d\n", status);
7023 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007024}
7025
Chuck Lever38527b12012-07-11 16:30:23 -04007026/**
7027 * nfs41_free_stateid - perform a FREE_STATEID operation
7028 *
7029 * @server: server / transport on which to perform the operation
7030 * @stateid: state ID to release
7031 *
7032 * Returns NFS_OK if the server freed "stateid". Otherwise a
7033 * negative NFS4ERR value is returned.
7034 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007035static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007036{
7037 struct nfs4_exception exception = { };
7038 int err;
7039 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007040 err = _nfs4_free_stateid(server, stateid);
7041 if (err != -NFS4ERR_DELAY)
7042 break;
7043 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007044 } while (exception.retry);
7045 return err;
7046}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007047
7048static bool nfs41_match_stateid(const nfs4_stateid *s1,
7049 const nfs4_stateid *s2)
7050{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007051 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007052 return false;
7053
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007054 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007055 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007056 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007057 return true;
7058
7059 return false;
7060}
7061
Andy Adamson557134a2009-04-01 09:21:53 -04007062#endif /* CONFIG_NFS_V4_1 */
7063
Trond Myklebust36281ca2012-03-04 18:13:56 -05007064static bool nfs4_match_stateid(const nfs4_stateid *s1,
7065 const nfs4_stateid *s2)
7066{
Trond Myklebustf597c532012-03-04 18:13:56 -05007067 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007068}
7069
7070
Trond Myklebust17280172012-03-11 13:11:00 -04007071static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007072 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007073 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 .recover_open = nfs4_open_reclaim,
7075 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007076 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007077 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007078 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079};
7080
Andy Adamson591d71c2009-04-01 09:22:47 -04007081#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007082static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007083 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7084 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7085 .recover_open = nfs4_open_reclaim,
7086 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007087 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007088 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007089 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007090 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007091};
7092#endif /* CONFIG_NFS_V4_1 */
7093
Trond Myklebust17280172012-03-11 13:11:00 -04007094static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007095 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007096 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 .recover_open = nfs4_open_expired,
7098 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007099 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007100 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101};
7102
Andy Adamson591d71c2009-04-01 09:22:47 -04007103#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007104static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007105 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7106 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007107 .recover_open = nfs41_open_expired,
7108 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007109 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007110 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007111};
7112#endif /* CONFIG_NFS_V4_1 */
7113
Trond Myklebust17280172012-03-11 13:11:00 -04007114static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007115 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007116 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007117 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007118};
7119
7120#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007121static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007122 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007123 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007124 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007125};
7126#endif
7127
Trond Myklebust97dc1352010-06-16 09:52:26 -04007128static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7129 .minor_version = 0,
7130 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007131 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007132 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007133 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7134 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7135 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007136};
7137
7138#if defined(CONFIG_NFS_V4_1)
7139static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7140 .minor_version = 1,
7141 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007142 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007143 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007144 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7145 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7146 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007147};
7148#endif
7149
Trond Myklebust97dc1352010-06-16 09:52:26 -04007150const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7151 [0] = &nfs_v4_0_minor_ops,
7152#if defined(CONFIG_NFS_V4_1)
7153 [1] = &nfs_v4_1_minor_ops,
7154#endif
7155};
7156
Bryan Schumaker73a79702012-07-16 16:39:12 -04007157const struct inode_operations nfs4_dir_inode_operations = {
7158 .create = nfs_create,
7159 .lookup = nfs_lookup,
7160 .atomic_open = nfs_atomic_open,
7161 .link = nfs_link,
7162 .unlink = nfs_unlink,
7163 .symlink = nfs_symlink,
7164 .mkdir = nfs_mkdir,
7165 .rmdir = nfs_rmdir,
7166 .mknod = nfs_mknod,
7167 .rename = nfs_rename,
7168 .permission = nfs_permission,
7169 .getattr = nfs_getattr,
7170 .setattr = nfs_setattr,
7171 .getxattr = generic_getxattr,
7172 .setxattr = generic_setxattr,
7173 .listxattr = generic_listxattr,
7174 .removexattr = generic_removexattr,
7175};
7176
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007177static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007178 .permission = nfs_permission,
7179 .getattr = nfs_getattr,
7180 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007181 .getxattr = generic_getxattr,
7182 .setxattr = generic_setxattr,
7183 .listxattr = generic_listxattr,
7184 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007185};
7186
David Howells509de812006-08-22 20:06:11 -04007187const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 .version = 4, /* protocol version */
7189 .dentry_ops = &nfs4_dentry_operations,
7190 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007191 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007192 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007194 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007195 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 .getattr = nfs4_proc_getattr,
7197 .setattr = nfs4_proc_setattr,
7198 .lookup = nfs4_proc_lookup,
7199 .access = nfs4_proc_access,
7200 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 .create = nfs4_proc_create,
7202 .remove = nfs4_proc_remove,
7203 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007204 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 .unlink_done = nfs4_proc_unlink_done,
7206 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007207 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007208 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007209 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 .link = nfs4_proc_link,
7211 .symlink = nfs4_proc_symlink,
7212 .mkdir = nfs4_proc_mkdir,
7213 .rmdir = nfs4_proc_remove,
7214 .readdir = nfs4_proc_readdir,
7215 .mknod = nfs4_proc_mknod,
7216 .statfs = nfs4_proc_statfs,
7217 .fsinfo = nfs4_proc_fsinfo,
7218 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007219 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 .decode_dirent = nfs4_decode_dirent,
7221 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007222 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007223 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007224 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007226 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007227 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007228 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007230 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007231 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007233 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007234 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007235 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007236 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007237 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007238 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007239 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007240 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007241 .create_server = nfs4_create_server,
7242 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243};
7244
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007245static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7246 .prefix = XATTR_NAME_NFSV4_ACL,
7247 .list = nfs4_xattr_list_nfs4_acl,
7248 .get = nfs4_xattr_get_nfs4_acl,
7249 .set = nfs4_xattr_set_nfs4_acl,
7250};
7251
7252const struct xattr_handler *nfs4_xattr_handlers[] = {
7253 &nfs4_xattr_nfs4_acl_handler,
7254 NULL
7255};
7256
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257/*
7258 * Local variables:
7259 * c-basic-offset: 8
7260 * End:
7261 */