blob: 52435ec441932b878e2824a9cd5ad3a24714328e [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 Myklebustda0507b2012-11-20 18:10:30 -0500501static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
502 u32 highest_slotid)
503{
504 unsigned int max_slotid, i;
505
506 if (tbl->server_highest_slotid == highest_slotid)
507 return;
508 if (tbl->highest_used_slotid > highest_slotid)
509 return;
510 max_slotid = min(tbl->max_slots - 1, highest_slotid);
511 /* Reset the seq_nr for deallocated slots */
512 for (i = tbl->server_highest_slotid + 1; i <= max_slotid; i++)
513 tbl->slots[i].seq_nr = 1;
514 tbl->server_highest_slotid = highest_slotid;
515}
516
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500517static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
518 struct nfs4_slot *slot,
519 struct nfs4_sequence_res *res)
520{
521 spin_lock(&tbl->slot_tbl_lock);
522 if (tbl->generation != slot->generation)
523 goto out;
Trond Myklebustda0507b2012-11-20 18:10:30 -0500524 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500525 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
526out:
527 spin_unlock(&tbl->slot_tbl_lock);
528}
529
Trond Myklebust14516c32010-07-31 14:29:06 -0400530static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400531{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500532 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500533 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400534 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400535 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400536
537 /*
538 * sr_status remains 1 if an RPC level error occurred. The server
539 * may or may not have processed the sequence operation..
540 * Proceed as if the server received and processed the sequence
541 * operation.
542 */
543 if (res->sr_status == 1)
544 res->sr_status = NFS_OK;
545
Bryan Schumaker468f8612011-04-18 15:57:32 -0400546 /* don't increment the sequence number if the task wasn't sent */
547 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400548 goto out;
549
Trond Myklebust933602e2012-11-16 12:12:38 -0500550 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500551 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500552
Andy Adamson691daf32009-12-04 15:55:39 -0500553 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400554 switch (res->sr_status) {
555 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400556 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500557 ++slot->seq_nr;
558 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500559 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400560 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500561 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500562 if (res->sr_status_flags != 0)
563 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500564 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400565 break;
566 case -NFS4ERR_DELAY:
567 /* The server detected a resend of the RPC call and
568 * returned NFS4ERR_DELAY as per Section 2.10.6.2
569 * of RFC5661.
570 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500571 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400572 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500573 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500574 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400575 goto out_retry;
576 default:
577 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500578 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400579 }
580out:
581 /* The session may be reset by one of the error handlers. */
582 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400583 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400584 return 1;
585out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400586 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400587 goto out;
588 rpc_delay(task, NFS4_POLL_RETRY_MAX);
589 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400590}
591
Trond Myklebust14516c32010-07-31 14:29:06 -0400592static int nfs4_sequence_done(struct rpc_task *task,
593 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400594{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500595 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400596 return 1;
597 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400598}
599
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400600/*
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500601 * nfs4_alloc_slot - efficiently look for a free slot
Benny Halevy510b8172009-04-01 09:22:14 -0400602 *
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500603 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
Benny Halevy510b8172009-04-01 09:22:14 -0400604 * If found, we mark the slot as used, update the highest_used_slotid,
605 * and respectively set up the sequence operation args.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606 *
607 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400608 */
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500609static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400610{
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500611 struct nfs4_slot *ret = NULL;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500612 u32 slotid;
Benny Halevy510b8172009-04-01 09:22:14 -0400613
Trond Myklebust45d43c22012-02-06 19:38:51 -0500614 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400615 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
616 tbl->max_slots);
617 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
618 if (slotid >= tbl->max_slots)
619 goto out;
620 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500621 if (slotid > tbl->highest_used_slotid ||
622 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400623 tbl->highest_used_slotid = slotid;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500624 ret = &tbl->slots[slotid];
625 ret->renewal_time = jiffies;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500626 ret->generation = tbl->generation;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500627
Benny Halevy510b8172009-04-01 09:22:14 -0400628out:
629 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500630 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
631 ret ? ret->slot_nr : -1);
632 return ret;
Benny Halevy510b8172009-04-01 09:22:14 -0400633}
634
Trond Myklebust9d12b212012-01-17 22:04:25 -0500635static void nfs41_init_sequence(struct nfs4_sequence_args *args,
636 struct nfs4_sequence_res *res, int cache_reply)
637{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500638 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500639 args->sa_cache_this = 0;
640 if (cache_reply)
641 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500642 res->sr_slot = NULL;
643}
644
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000645int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400646 struct nfs4_sequence_args *args,
647 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648 struct rpc_task *task)
649{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400650 struct nfs4_slot *slot;
651 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652
653 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400655 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 return 0;
657
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400658 tbl = &session->fc_slot_table;
659
660 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400661 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800662 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500663 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500664 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400665 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500666 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500667 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400668 }
669
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800670 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
671 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
672 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
673 spin_unlock(&tbl->slot_tbl_lock);
674 dprintk("%s enforce FIFO order\n", __func__);
675 return -EAGAIN;
676 }
677
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500678 slot = nfs4_alloc_slot(tbl);
679 if (slot == NULL) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400680 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
681 spin_unlock(&tbl->slot_tbl_lock);
682 dprintk("<-- %s: no free slots\n", __func__);
683 return -EAGAIN;
684 }
685 spin_unlock(&tbl->slot_tbl_lock);
686
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800687 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500688
689 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400690
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500691 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
692 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400693
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400695 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400696 /*
697 * sr_status is only set in decode_sequence, and so will remain
698 * set to 1 if an rpc level failure occurs.
699 */
700 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400701 return 0;
702}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000703EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704
Trond Myklebust035168a2010-06-16 09:52:26 -0400705int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 struct nfs4_sequence_args *args,
707 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 struct rpc_task *task)
709{
Trond Myklebust035168a2010-06-16 09:52:26 -0400710 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711 int ret = 0;
712
Trond Myklebust9d12b212012-01-17 22:04:25 -0500713 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400715
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500716 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400717 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500718 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400719
Trond Myklebust9d12b212012-01-17 22:04:25 -0500720 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721out:
722 dprintk("<-- %s status=%d\n", __func__, ret);
723 return ret;
724}
725
726struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400727 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 struct nfs4_sequence_args *seq_args;
729 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730};
731
732static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
733{
734 struct nfs41_call_sync_data *data = calldata;
735
Trond Myklebust035168a2010-06-16 09:52:26 -0400736 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
737
738 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500739 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 return;
741 rpc_call_start(task);
742}
743
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800744static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
745{
746 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
747 nfs41_call_sync_prepare(task, calldata);
748}
749
Andy Adamson69ab40c2009-04-01 09:22:19 -0400750static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
751{
752 struct nfs41_call_sync_data *data = calldata;
753
Trond Myklebust14516c32010-07-31 14:29:06 -0400754 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400755}
756
Trond Myklebust17280172012-03-11 13:11:00 -0400757static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400758 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400759 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760};
761
Trond Myklebust17280172012-03-11 13:11:00 -0400762static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800763 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
764 .rpc_call_done = nfs41_call_sync_done,
765};
766
Bryan Schumaker7c513052011-03-24 17:12:24 +0000767static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
768 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400769 struct rpc_message *msg,
770 struct nfs4_sequence_args *args,
771 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800772 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400773{
774 int ret;
775 struct rpc_task *task;
776 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400777 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400778 .seq_args = args,
779 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400780 };
781 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000782 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400783 .rpc_message = msg,
784 .callback_ops = &nfs41_call_sync_ops,
785 .callback_data = &data
786 };
787
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800788 if (privileged)
789 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400790 task = rpc_run_task(&task_setup);
791 if (IS_ERR(task))
792 ret = PTR_ERR(task);
793 else {
794 ret = task->tk_status;
795 rpc_put_task(task);
796 }
797 return ret;
798}
799
Bryan Schumaker7c513052011-03-24 17:12:24 +0000800int _nfs4_call_sync_session(struct rpc_clnt *clnt,
801 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400802 struct rpc_message *msg,
803 struct nfs4_sequence_args *args,
804 struct nfs4_sequence_res *res,
805 int cache_reply)
806{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500807 nfs41_init_sequence(args, res, cache_reply);
808 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400809}
810
Trond Myklebustdf896452010-06-16 09:52:26 -0400811#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500812static inline
813void nfs41_init_sequence(struct nfs4_sequence_args *args,
814 struct nfs4_sequence_res *res, int cache_reply)
815{
816}
817
Trond Myklebust14516c32010-07-31 14:29:06 -0400818static int nfs4_sequence_done(struct rpc_task *task,
819 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400820{
Trond Myklebust14516c32010-07-31 14:29:06 -0400821 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400822}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400823#endif /* CONFIG_NFS_V4_1 */
824
Bryan Schumaker7c513052011-03-24 17:12:24 +0000825int _nfs4_call_sync(struct rpc_clnt *clnt,
826 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400827 struct rpc_message *msg,
828 struct nfs4_sequence_args *args,
829 struct nfs4_sequence_res *res,
830 int cache_reply)
831{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500832 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000833 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400834}
835
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000836static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000837int nfs4_call_sync(struct rpc_clnt *clnt,
838 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000839 struct rpc_message *msg,
840 struct nfs4_sequence_args *args,
841 struct nfs4_sequence_res *res,
842 int cache_reply)
843{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000844 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
845 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000846}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400847
Trond Myklebust38478b22006-05-25 01:40:57 -0400848static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Trond Myklebust38478b22006-05-25 01:40:57 -0400850 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Trond Myklebust38478b22006-05-25 01:40:57 -0400852 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400853 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700854 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400855 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700856 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400857 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400861 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100862 struct nfs_openargs o_arg;
863 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100864 struct nfs_open_confirmargs c_arg;
865 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500866 struct nfs4_string owner_name;
867 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100868 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100869 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400870 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100871 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400872 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100874 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400875 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100876 int rpc_status;
877 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100878};
879
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400880
881static void nfs4_init_opendata_res(struct nfs4_opendata *p)
882{
883 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400884 p->o_res.seqid = p->o_arg.seqid;
885 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400886 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400887 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400888 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500889 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400890}
891
Al Viro82a2c1b2011-06-22 18:30:55 -0400892static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400894 const struct iattr *attrs,
895 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100896{
Al Viro82a2c1b2011-06-22 18:30:55 -0400897 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100898 struct inode *dir = parent->d_inode;
899 struct nfs_server *server = NFS_SERVER(dir);
900 struct nfs4_opendata *p;
901
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400902 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100903 if (p == NULL)
904 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400905 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100906 if (p->o_arg.seqid == NULL)
907 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400908 nfs_sb_active(dentry->d_sb);
909 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100910 p->dir = parent;
911 p->owner = sp;
912 atomic_inc(&sp->so_count);
913 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 p->o_arg.open_flags = flags;
915 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700916 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
917 * will return permission denied for all bits until close */
918 if (!(flags & O_EXCL)) {
919 /* ask server to check for all possible rights as results
920 * are cached */
921 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
922 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700923 }
David Howells7539bba2006-08-22 20:06:09 -0400924 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400925 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
926 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400927 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100928 p->o_arg.server = server;
929 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400930 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100931 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500932 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500933 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400934
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100935 p->o_arg.u.attrs = &p->attrs;
936 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500937
938 verf[0] = jiffies;
939 verf[1] = current->pid;
940 memcpy(p->o_arg.u.verifier.data, verf,
941 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100942 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100943 p->c_arg.fh = &p->o_res.fh;
944 p->c_arg.stateid = &p->o_res.stateid;
945 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400946 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400947 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100948 return p;
949err_free:
950 kfree(p);
951err:
952 dput(parent);
953 return NULL;
954}
955
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400956static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100957{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400958 struct nfs4_opendata *p = container_of(kref,
959 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400960 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400961
962 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963 if (p->state != NULL)
964 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400965 nfs4_put_state_owner(p->owner);
966 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400967 dput(p->dentry);
968 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500969 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400970 kfree(p);
971}
972
973static void nfs4_opendata_put(struct nfs4_opendata *p)
974{
975 if (p != NULL)
976 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100977}
978
Trond Myklebust06f814a2006-01-03 09:55:07 +0100979static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
980{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100981 int ret;
982
Trond Myklebust06f814a2006-01-03 09:55:07 +0100983 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100984 return ret;
985}
986
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400988{
989 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990
Trond Myklebust536e43d2012-01-17 22:04:26 -0500991 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 goto out;
993 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400994 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500995 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
996 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400997 break;
998 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500999 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1000 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001 break;
1002 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001003 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1004 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001005 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001007 return ret;
1008}
1009
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001012 if (delegation == NULL)
1013 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001016 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001018 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019 return 1;
1020}
1021
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001023{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001025 case FMODE_WRITE:
1026 state->n_wronly++;
1027 break;
1028 case FMODE_READ:
1029 state->n_rdonly++;
1030 break;
1031 case FMODE_READ|FMODE_WRITE:
1032 state->n_rdwr++;
1033 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001038{
1039 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001040 nfs4_stateid_copy(&state->stateid, stateid);
1041 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001043 case FMODE_READ:
1044 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1045 break;
1046 case FMODE_WRITE:
1047 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1048 break;
1049 case FMODE_READ|FMODE_WRITE:
1050 set_bit(NFS_O_RDWR_STATE, &state->flags);
1051 }
1052}
1053
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001055{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001056 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001058 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001059}
1060
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061static 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 -07001062{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001063 /*
1064 * Protect the call to nfs4_state_set_mode_locked and
1065 * serialise the stateid update
1066 */
1067 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001068 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001069 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001070 set_bit(NFS_DELEGATED_STATE, &state->flags);
1071 }
1072 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001074 write_sequnlock(&state->seqlock);
1075 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001077 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001080static 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 -05001081{
1082 struct nfs_inode *nfsi = NFS_I(state->inode);
1083 struct nfs_delegation *deleg_cur;
1084 int ret = 0;
1085
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001087
1088 rcu_read_lock();
1089 deleg_cur = rcu_dereference(nfsi->delegation);
1090 if (deleg_cur == NULL)
1091 goto no_delegation;
1092
1093 spin_lock(&deleg_cur->lock);
1094 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001096 goto no_delegation_unlock;
1097
1098 if (delegation == NULL)
1099 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001100 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001101 goto no_delegation_unlock;
1102
Trond Myklebustb7391f42008-12-23 15:21:52 -05001103 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001105 ret = 1;
1106no_delegation_unlock:
1107 spin_unlock(&deleg_cur->lock);
1108no_delegation:
1109 rcu_read_unlock();
1110
1111 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001113 ret = 1;
1114 }
1115
1116 return ret;
1117}
1118
1119
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121{
1122 struct nfs_delegation *delegation;
1123
1124 rcu_read_lock();
1125 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 rcu_read_unlock();
1128 return;
1129 }
1130 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001131 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001132}
1133
Trond Myklebust6ee41262007-07-08 14:11:36 -04001134static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001135{
1136 struct nfs4_state *state = opendata->state;
1137 struct nfs_inode *nfsi = NFS_I(state->inode);
1138 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001139 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001141 nfs4_stateid stateid;
1142 int ret = -EAGAIN;
1143
Trond Myklebustaac00a82007-07-05 19:02:21 -04001144 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001145 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001146 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001147 if (can_open_cached(state, fmode, open_mode)) {
1148 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001149 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001150 goto out_return_state;
1151 }
1152 spin_unlock(&state->owner->so_lock);
1153 }
Trond Myklebust34310432008-12-23 15:21:38 -05001154 rcu_read_lock();
1155 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001156 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001157 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001158 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001159 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001160 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001161 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001162 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001163 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001164 if (ret != 0)
1165 goto out;
1166 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001167
1168 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001169 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001170 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001171 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001172out:
1173 return ERR_PTR(ret);
1174out_return_state:
1175 atomic_inc(&state->count);
1176 return state;
1177}
1178
Andy Adamsone23008e2012-10-02 21:07:32 -04001179static void
1180nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1181{
1182 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1183 struct nfs_delegation *delegation;
1184 int delegation_flags = 0;
1185
1186 rcu_read_lock();
1187 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1188 if (delegation)
1189 delegation_flags = delegation->flags;
1190 rcu_read_unlock();
1191 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1192 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1193 "returning a delegation for "
1194 "OPEN(CLAIM_DELEGATE_CUR)\n",
1195 clp->cl_hostname);
1196 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1197 nfs_inode_set_delegation(state->inode,
1198 data->owner->so_cred,
1199 &data->o_res);
1200 else
1201 nfs_inode_reclaim_delegation(state->inode,
1202 data->owner->so_cred,
1203 &data->o_res);
1204}
1205
1206/*
1207 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1208 * and update the nfs4_state.
1209 */
1210static struct nfs4_state *
1211_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1212{
1213 struct inode *inode = data->state->inode;
1214 struct nfs4_state *state = data->state;
1215 int ret;
1216
1217 if (!data->rpc_done) {
1218 ret = data->rpc_status;
1219 goto err;
1220 }
1221
1222 ret = -ESTALE;
1223 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1224 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1225 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1226 goto err;
1227
1228 ret = -ENOMEM;
1229 state = nfs4_get_open_state(inode, data->owner);
1230 if (state == NULL)
1231 goto err;
1232
1233 ret = nfs_refresh_inode(inode, &data->f_attr);
1234 if (ret)
1235 goto err;
1236
1237 if (data->o_res.delegation_type != 0)
1238 nfs4_opendata_check_deleg(data, state);
1239 update_open_stateid(state, &data->o_res.stateid, NULL,
1240 data->o_arg.fmode);
1241
1242 return state;
1243err:
1244 return ERR_PTR(ret);
1245
1246}
1247
1248static struct nfs4_state *
1249_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001250{
1251 struct inode *inode;
1252 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001253 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001254
Trond Myklebustaac00a82007-07-05 19:02:21 -04001255 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001256 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001257 goto out;
1258 }
1259
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001260 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001261 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001262 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001263 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001264 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001265 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001266 goto err;
1267 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001268 state = nfs4_get_open_state(inode, data->owner);
1269 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001270 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001271 if (data->o_res.delegation_type != 0)
1272 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001273 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001274 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001275 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001276out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001277 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001278err_put_inode:
1279 iput(inode);
1280err:
1281 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001282}
1283
Andy Adamsone23008e2012-10-02 21:07:32 -04001284static struct nfs4_state *
1285nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1286{
1287 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1288 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1289 return _nfs4_opendata_to_nfs4_state(data);
1290}
1291
Trond Myklebust864472e2006-01-03 09:55:15 +01001292static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1293{
1294 struct nfs_inode *nfsi = NFS_I(state->inode);
1295 struct nfs_open_context *ctx;
1296
1297 spin_lock(&state->inode->i_lock);
1298 list_for_each_entry(ctx, &nfsi->open_files, list) {
1299 if (ctx->state != state)
1300 continue;
1301 get_nfs_open_context(ctx);
1302 spin_unlock(&state->inode->i_lock);
1303 return ctx;
1304 }
1305 spin_unlock(&state->inode->i_lock);
1306 return ERR_PTR(-ENOENT);
1307}
1308
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001309static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1310{
1311 struct nfs4_opendata *opendata;
1312
Al Viro3d4ff432011-06-22 18:40:12 -04001313 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001314 if (opendata == NULL)
1315 return ERR_PTR(-ENOMEM);
1316 opendata->state = state;
1317 atomic_inc(&state->count);
1318 return opendata;
1319}
1320
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001322{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001323 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 int ret;
1325
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326 opendata->o_arg.open_flags = 0;
1327 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001328 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1329 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1330 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001331 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001332 if (ret != 0)
1333 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001334 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001335 if (IS_ERR(newstate))
1336 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001337 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001338 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 return 0;
1340}
1341
1342static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1343{
Trond Myklebust864472e2006-01-03 09:55:15 +01001344 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001345 int ret;
1346
1347 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001348 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001349 smp_rmb();
1350 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001351 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001352 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001353 if (ret != 0)
1354 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001355 if (newstate != state)
1356 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001357 }
1358 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001359 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001360 ret = nfs4_open_recover_helper(opendata, 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_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001367 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001368 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &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 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001374 /*
1375 * We may have performed cached opens for all three recoveries.
1376 * Check if we need to update the current stateid.
1377 */
1378 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001379 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001380 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001381 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001382 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001383 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001384 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001385 return 0;
1386}
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/*
1389 * OPEN_RECLAIM:
1390 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001392static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001394 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001395 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001396 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int status;
1398
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001399 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1400 if (IS_ERR(opendata))
1401 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001402 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1403 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001404 rcu_read_lock();
1405 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001406 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001407 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001408 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001409 opendata->o_arg.u.delegation_type = delegation_type;
1410 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001411 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return status;
1413}
1414
Trond Myklebust539cd032007-06-05 11:46:42 -04001415static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct nfs_server *server = NFS_SERVER(state->inode);
1418 struct nfs4_exception exception = { };
1419 int err;
1420 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001421 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001422 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001423 break;
1424 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 } while (exception.retry);
1426 return err;
1427}
1428
Trond Myklebust864472e2006-01-03 09:55:15 +01001429static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1430{
1431 struct nfs_open_context *ctx;
1432 int ret;
1433
1434 ctx = nfs4_state_find_open_context(state);
1435 if (IS_ERR(ctx))
1436 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001437 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001438 put_nfs_open_context(ctx);
1439 return ret;
1440}
1441
Trond Myklebust13437e12007-07-06 15:10:43 -04001442static 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 -07001443{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001444 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001445 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001447 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1448 if (IS_ERR(opendata))
1449 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001450 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001451 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001452 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001453 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001454 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Trond Myklebust13437e12007-07-06 15:10:43 -04001457int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001460 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int err;
1462 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001463 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 switch (err) {
1465 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001466 case -ENOENT:
1467 case -ESTALE:
1468 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001469 case -NFS4ERR_BADSESSION:
1470 case -NFS4ERR_BADSLOT:
1471 case -NFS4ERR_BAD_HIGH_SLOT:
1472 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1473 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001474 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001475 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 case -NFS4ERR_STALE_CLIENTID:
1477 case -NFS4ERR_STALE_STATEID:
1478 case -NFS4ERR_EXPIRED:
1479 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001480 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001481 goto out;
1482 case -ERESTARTSYS:
1483 /*
1484 * The show must go on: exit, but mark the
1485 * stateid as needing recovery.
1486 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001487 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001488 case -NFS4ERR_ADMIN_REVOKED:
1489 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001490 nfs_inode_find_state_and_recover(state->inode,
1491 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001492 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001493 case -EKEYEXPIRED:
1494 /*
1495 * User RPCSEC_GSS context has expired.
1496 * We cannot recover this stateid now, so
1497 * skip it and allow recovery thread to
1498 * proceed.
1499 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001500 case -ENOMEM:
1501 err = 0;
1502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504 err = nfs4_handle_exception(server, err, &exception);
1505 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001506out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return err;
1508}
1509
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001510static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1511{
1512 struct nfs4_opendata *data = calldata;
1513
1514 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001515 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001516 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001517 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001518 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001519 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001520 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001521}
1522
1523static void nfs4_open_confirm_release(void *calldata)
1524{
1525 struct nfs4_opendata *data = calldata;
1526 struct nfs4_state *state = NULL;
1527
1528 /* If this request hasn't been cancelled, do nothing */
1529 if (data->cancelled == 0)
1530 goto out_free;
1531 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001532 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001533 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001534 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001535 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001536 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001537out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001538 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001539}
1540
1541static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001542 .rpc_call_done = nfs4_open_confirm_done,
1543 .rpc_release = nfs4_open_confirm_release,
1544};
1545
1546/*
1547 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1548 */
1549static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1550{
1551 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1552 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001553 struct rpc_message msg = {
1554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1555 .rpc_argp = &data->c_arg,
1556 .rpc_resp = &data->c_res,
1557 .rpc_cred = data->owner->so_cred,
1558 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001559 struct rpc_task_setup task_setup_data = {
1560 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001561 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001562 .callback_ops = &nfs4_open_confirm_ops,
1563 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001564 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001565 .flags = RPC_TASK_ASYNC,
1566 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int status;
1568
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001569 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001570 data->rpc_done = 0;
1571 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001572 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001573 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001574 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001575 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001576 status = nfs4_wait_for_completion_rpc_task(task);
1577 if (status != 0) {
1578 data->cancelled = 1;
1579 smp_wmb();
1580 } else
1581 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001582 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return status;
1584}
1585
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001588 struct nfs4_opendata *data = calldata;
1589 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001590
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1592 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001593 /*
1594 * Check if we still need to send an OPEN call, or if we can use
1595 * a delegation instead.
1596 */
1597 if (data->state != NULL) {
1598 struct nfs_delegation *delegation;
1599
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001600 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001601 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001602 rcu_read_lock();
1603 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001604 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1605 can_open_delegated(delegation, data->o_arg.fmode))
1606 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001607 rcu_read_unlock();
1608 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001609 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001610 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001611 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001612 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001613 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001614 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1615 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001616 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001617 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001618 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001619 &data->o_res.seq_res,
1620 task) != 0)
1621 nfs_release_seqid(data->o_arg.seqid);
1622 else
1623 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001624 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001625unlock_no_action:
1626 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001627out_no_action:
1628 task->tk_action = NULL;
1629
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001632static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1633{
1634 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1635 nfs4_open_prepare(task, calldata);
1636}
1637
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638static void nfs4_open_done(struct rpc_task *task, void *calldata)
1639{
1640 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001643
Trond Myklebust14516c32010-07-31 14:29:06 -04001644 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1645 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001646
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001647 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001648 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1649 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001650 case S_IFREG:
1651 break;
1652 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001653 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001654 break;
1655 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001657 break;
1658 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001660 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001661 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001662 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001663 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1664 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001665 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001666 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001667}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001668
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669static void nfs4_open_release(void *calldata)
1670{
1671 struct nfs4_opendata *data = calldata;
1672 struct nfs4_state *state = NULL;
1673
1674 /* If this request hasn't been cancelled, do nothing */
1675 if (data->cancelled == 0)
1676 goto out_free;
1677 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001678 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679 goto out_free;
1680 /* In case we need an open_confirm, no cleanup! */
1681 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1682 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001684 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001685 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001687 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001688}
1689
1690static const struct rpc_call_ops nfs4_open_ops = {
1691 .rpc_call_prepare = nfs4_open_prepare,
1692 .rpc_call_done = nfs4_open_done,
1693 .rpc_release = nfs4_open_release,
1694};
1695
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001696static const struct rpc_call_ops nfs4_recover_open_ops = {
1697 .rpc_call_prepare = nfs4_recover_open_prepare,
1698 .rpc_call_done = nfs4_open_done,
1699 .rpc_release = nfs4_open_release,
1700};
1701
1702static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703{
1704 struct inode *dir = data->dir->d_inode;
1705 struct nfs_server *server = NFS_SERVER(dir);
1706 struct nfs_openargs *o_arg = &data->o_arg;
1707 struct nfs_openres *o_res = &data->o_res;
1708 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001709 struct rpc_message msg = {
1710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1711 .rpc_argp = o_arg,
1712 .rpc_resp = o_res,
1713 .rpc_cred = data->owner->so_cred,
1714 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001715 struct rpc_task_setup task_setup_data = {
1716 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001717 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001718 .callback_ops = &nfs4_open_ops,
1719 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001720 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001721 .flags = RPC_TASK_ASYNC,
1722 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001723 int status;
1724
Trond Myklebust9d12b212012-01-17 22:04:25 -05001725 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001726 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001727 data->rpc_done = 0;
1728 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001729 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001730 if (isrecover)
1731 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001732 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001733 if (IS_ERR(task))
1734 return PTR_ERR(task);
1735 status = nfs4_wait_for_completion_rpc_task(task);
1736 if (status != 0) {
1737 data->cancelled = 1;
1738 smp_wmb();
1739 } else
1740 status = data->rpc_status;
1741 rpc_put_task(task);
1742
1743 return status;
1744}
1745
1746static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1747{
1748 struct inode *dir = data->dir->d_inode;
1749 struct nfs_openres *o_res = &data->o_res;
1750 int status;
1751
1752 status = nfs4_run_open_task(data, 1);
1753 if (status != 0 || !data->rpc_done)
1754 return status;
1755
Trond Myklebust6926afd2012-01-07 13:22:46 -05001756 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1757
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001758 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1759 status = _nfs4_proc_open_confirm(data);
1760 if (status != 0)
1761 return status;
1762 }
1763
1764 return status;
1765}
1766
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001767static int nfs4_opendata_access(struct rpc_cred *cred,
1768 struct nfs4_opendata *opendata,
1769 struct nfs4_state *state, fmode_t fmode)
1770{
1771 struct nfs_access_entry cache;
1772 u32 mask;
1773
1774 /* access call failed or for some reason the server doesn't
1775 * support any access modes -- defer access call until later */
1776 if (opendata->o_res.access_supported == 0)
1777 return 0;
1778
1779 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001780 /* don't check MAY_WRITE - a newly created file may not have
1781 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001782 if (fmode & FMODE_READ)
1783 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001784 if (fmode & FMODE_EXEC)
1785 mask |= MAY_EXEC;
1786
1787 cache.cred = cred;
1788 cache.jiffies = jiffies;
1789 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1790 nfs_access_add_cache(state->inode, &cache);
1791
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001792 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001793 return 0;
1794
1795 /* even though OPEN succeeded, access is denied. Close the file */
1796 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001797 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001798}
1799
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001800/*
1801 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1802 */
1803static int _nfs4_proc_open(struct nfs4_opendata *data)
1804{
1805 struct inode *dir = data->dir->d_inode;
1806 struct nfs_server *server = NFS_SERVER(dir);
1807 struct nfs_openargs *o_arg = &data->o_arg;
1808 struct nfs_openres *o_res = &data->o_res;
1809 int status;
1810
1811 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001812 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001814 if (status != 0) {
1815 if (status == -NFS4ERR_BADNAME &&
1816 !(o_arg->open_flags & O_CREAT))
1817 return -ENOENT;
1818 return status;
1819 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001820
Trond Myklebust6926afd2012-01-07 13:22:46 -05001821 nfs_fattr_map_and_free_names(server, &data->f_attr);
1822
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001823 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001824 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001825 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1826 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001828 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001830 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001833 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Andy Adamsond83217c2011-03-01 01:34:17 +00001837static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001838{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001839 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001840 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001841
Trond Myklebusta78cb572009-08-09 15:06:19 -04001842 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001843 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001844 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001845 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001846 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1847 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001848 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001849 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001850 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001851 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001852 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001853}
1854
Andy Adamsond83217c2011-03-01 01:34:17 +00001855static int nfs4_recover_expired_lease(struct nfs_server *server)
1856{
1857 return nfs4_client_recover_expired_lease(server->nfs_client);
1858}
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860/*
1861 * OPEN_EXPIRED:
1862 * reclaim state on the server after a network partition.
1863 * Assumes caller holds the appropriate lock
1864 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001865static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001867 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001868 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001870 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1871 if (IS_ERR(opendata))
1872 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001873 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001874 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001875 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001876 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001877 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001880static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001881{
Trond Myklebust539cd032007-06-05 11:46:42 -04001882 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001883 struct nfs4_exception exception = { };
1884 int err;
1885
1886 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001887 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001888 switch (err) {
1889 default:
1890 goto out;
1891 case -NFS4ERR_GRACE:
1892 case -NFS4ERR_DELAY:
1893 nfs4_handle_exception(server, err, &exception);
1894 err = 0;
1895 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001896 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001897out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001898 return err;
1899}
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001904 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Trond Myklebust864472e2006-01-03 09:55:15 +01001906 ctx = nfs4_state_find_open_context(state);
1907 if (IS_ERR(ctx))
1908 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001909 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001910 put_nfs_open_context(ctx);
1911 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001914#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001915static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001916{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001917 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001918 nfs4_stateid *stateid = &state->stateid;
1919 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001920
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001921 /* If a state reset has been done, test_stateid is unneeded */
1922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1923 return;
1924
1925 status = nfs41_test_stateid(server, stateid);
1926 if (status != NFS_OK) {
1927 /* Free the stateid unless the server explicitly
1928 * informs us the stateid is unrecognized. */
1929 if (status != -NFS4ERR_BAD_STATEID)
1930 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001931 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001932
Bryan Schumaker69388672012-09-26 15:25:52 -04001933 write_seqlock(&state->seqlock);
1934 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1935 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001936 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1937 }
1938}
1939
1940/**
1941 * nfs41_check_open_stateid - possibly free an open stateid
1942 *
1943 * @state: NFSv4 state for an inode
1944 *
1945 * Returns NFS_OK if recovery for this stateid is now finished.
1946 * Otherwise a negative NFS4ERR value is returned.
1947 */
1948static int nfs41_check_open_stateid(struct nfs4_state *state)
1949{
1950 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001951 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001952 int status;
1953
1954 /* If a state reset has been done, test_stateid is unneeded */
1955 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1956 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1957 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1958 return -NFS4ERR_BAD_STATEID;
1959
1960 status = nfs41_test_stateid(server, stateid);
1961 if (status != NFS_OK) {
1962 /* Free the stateid unless the server explicitly
1963 * informs us the stateid is unrecognized. */
1964 if (status != -NFS4ERR_BAD_STATEID)
1965 nfs41_free_stateid(server, stateid);
1966
1967 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1968 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1969 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001970 }
1971 return status;
1972}
1973
1974static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1975{
Chuck Levereb64cf92012-07-11 16:30:05 -04001976 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001977
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001978 nfs41_clear_delegation_stateid(state);
1979 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001980 if (status != NFS_OK)
1981 status = nfs4_open_expired(sp, state);
1982 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001983}
1984#endif
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001987 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1988 * fields corresponding to attributes that were used to store the verifier.
1989 * Make sure we clobber those fields in the later setattr call
1990 */
1991static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1992{
1993 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1994 !(sattr->ia_valid & ATTR_ATIME_SET))
1995 sattr->ia_valid |= ATTR_ATIME;
1996
1997 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1998 !(sattr->ia_valid & ATTR_MTIME_SET))
1999 sattr->ia_valid |= ATTR_MTIME;
2000}
2001
2002/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002003 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 */
Andy Adamson82be4172012-05-23 05:02:35 -04002005static int _nfs4_do_open(struct inode *dir,
2006 struct dentry *dentry,
2007 fmode_t fmode,
2008 int flags,
2009 struct iattr *sattr,
2010 struct rpc_cred *cred,
2011 struct nfs4_state **res,
2012 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 struct nfs4_state_owner *sp;
2015 struct nfs4_state *state = NULL;
2016 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002017 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002018 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002022 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2023 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2025 goto out_err;
2026 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002027 status = nfs4_recover_expired_lease(server);
2028 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002029 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002030 if (dentry->d_inode != NULL)
2031 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002032 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04002033 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002034 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002035 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Andy Adamson82be4172012-05-23 05:02:35 -04002037 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2038 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2039 if (!opendata->f_attr.mdsthreshold)
2040 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002041 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002042 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002043 if (dentry->d_inode != NULL)
2044 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002048 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002050 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002051 status = PTR_ERR(state);
2052 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002053 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002054 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002055 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002056
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002057 status = nfs4_opendata_access(cred, opendata, state, fmode);
2058 if (status != 0)
2059 goto err_opendata_put;
2060
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002061 if (opendata->o_arg.open_flags & O_EXCL) {
2062 nfs4_exclusive_attrset(opendata, sattr);
2063
2064 nfs_fattr_init(opendata->o_res.f_attr);
2065 status = nfs4_do_setattr(state->inode, cred,
2066 opendata->o_res.f_attr, sattr,
2067 state);
2068 if (status == 0)
2069 nfs_setattr_update_inode(state->inode, sattr);
2070 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2071 }
Andy Adamson82be4172012-05-23 05:02:35 -04002072
2073 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2074 *ctx_th = opendata->f_attr.mdsthreshold;
2075 else
2076 kfree(opendata->f_attr.mdsthreshold);
2077 opendata->f_attr.mdsthreshold = NULL;
2078
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002079 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 *res = state;
2082 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002083err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002084 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002085 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002086err_put_state_owner:
2087 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 *res = NULL;
2090 return status;
2091}
2092
2093
Andy Adamson82be4172012-05-23 05:02:35 -04002094static struct nfs4_state *nfs4_do_open(struct inode *dir,
2095 struct dentry *dentry,
2096 fmode_t fmode,
2097 int flags,
2098 struct iattr *sattr,
2099 struct rpc_cred *cred,
2100 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 struct nfs4_exception exception = { };
2103 struct nfs4_state *res;
2104 int status;
2105
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002106 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002108 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2109 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (status == 0)
2111 break;
2112 /* NOTE: BAD_SEQID means the server and client disagree about the
2113 * book-keeping w.r.t. state-changing operations
2114 * (OPEN/CLOSE/LOCK/LOCKU...)
2115 * It is actually a sign of a bug on the client or on the server.
2116 *
2117 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002118 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 * have unhashed the old state_owner for us, and that we can
2120 * therefore safely retry using a new one. We should still warn
2121 * the user though...
2122 */
2123 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002124 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002125 " returned a bad sequence-id error!\n",
2126 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 exception.retry = 1;
2128 continue;
2129 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002130 /*
2131 * BAD_STATEID on OPEN means that the server cancelled our
2132 * state before it received the OPEN_CONFIRM.
2133 * Recover by retrying the request as per the discussion
2134 * on Page 181 of RFC3530.
2135 */
2136 if (status == -NFS4ERR_BAD_STATEID) {
2137 exception.retry = 1;
2138 continue;
2139 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002140 if (status == -EAGAIN) {
2141 /* We must have found a delegation */
2142 exception.retry = 1;
2143 continue;
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2146 status, &exception));
2147 } while (exception.retry);
2148 return res;
2149}
2150
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002151static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2152 struct nfs_fattr *fattr, struct iattr *sattr,
2153 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002155 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002157 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 .iap = sattr,
2159 .server = server,
2160 .bitmask = server->attr_bitmask,
2161 };
2162 struct nfs_setattrres res = {
2163 .fattr = fattr,
2164 .server = server,
2165 };
2166 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2168 .rpc_argp = &arg,
2169 .rpc_resp = &res,
2170 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002172 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002173 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Trond Myklebust0e574af2005-10-27 22:12:38 -04002175 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Trond Myklebust4fc87962012-03-08 17:42:01 -05002177 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002178 struct nfs_lockowner lockowner = {
2179 .l_owner = current->files,
2180 .l_pid = current->tgid,
2181 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002182 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002183 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002184 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2185 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002186 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002187 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002188 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Bryan Schumaker7c513052011-03-24 17:12:24 +00002190 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002191 if (status == 0 && state != NULL)
2192 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002193 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002196static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2197 struct nfs_fattr *fattr, struct iattr *sattr,
2198 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002200 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002201 struct nfs4_exception exception = {
2202 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002203 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002204 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 int err;
2206 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002207 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2208 switch (err) {
2209 case -NFS4ERR_OPENMODE:
2210 if (state && !(state->state & FMODE_WRITE)) {
2211 err = -EBADF;
2212 if (sattr->ia_valid & ATTR_OPEN)
2213 err = -EACCES;
2214 goto out;
2215 }
2216 }
2217 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return err;
2221}
2222
2223struct nfs4_closedata {
2224 struct inode *inode;
2225 struct nfs4_state *state;
2226 struct nfs_closeargs arg;
2227 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002228 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002229 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002230 bool roc;
2231 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232};
2233
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002234static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002235{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002236 struct nfs4_closedata *calldata = data;
2237 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002238 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002239
Fred Isamanf7e89172011-01-06 11:36:32 +00002240 if (calldata->roc)
2241 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002242 nfs4_put_open_state(calldata->state);
2243 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002244 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002245 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002246 kfree(calldata);
2247}
2248
Trond Myklebust88069f72009-12-08 08:33:16 -05002249static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2250 fmode_t fmode)
2251{
2252 spin_lock(&state->owner->so_lock);
2253 if (!(fmode & FMODE_READ))
2254 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2255 if (!(fmode & FMODE_WRITE))
2256 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2257 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2258 spin_unlock(&state->owner->so_lock);
2259}
2260
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002261static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002263 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 struct nfs_server *server = NFS_SERVER(calldata->inode);
2266
Chuck Levera3ca5652012-03-01 17:00:40 -05002267 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002268 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2269 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /* hmm. we are done with the inode, and in the process of freeing
2271 * the state_owner. we keep this around to process errors
2272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 switch (task->tk_status) {
2274 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002275 if (calldata->roc)
2276 pnfs_roc_set_barrier(state->inode,
2277 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002278 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002279 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002280 nfs4_close_clear_stateid_flags(state,
2281 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 break;
2283 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002284 case -NFS4ERR_OLD_STATEID:
2285 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002287 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002290 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2291 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002293 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002294 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002295 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002298static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002300 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002301 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002302 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002303 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002304
Chuck Levera3ca5652012-03-01 17:00:40 -05002305 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002306 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002307 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002308
Trond Myklebust88069f72009-12-08 08:33:16 -05002309 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2310 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002311 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002312 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002313 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002314 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002315 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2316 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2317 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002318 }
2319 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002320 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2321 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2322 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002323 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002324 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002325 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002326
2327 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002328 /* Note: exit _without_ calling nfs4_close_done */
2329 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002330 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002331 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002332
Fred Isamanf7e89172011-01-06 11:36:32 +00002333 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002334 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002335 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002336 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002337 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002338 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002339
Trond Myklebust516a6af2005-10-27 22:12:41 -04002340 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002341 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002342 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002343 &calldata->arg.seq_args,
2344 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002345 task) != 0)
2346 nfs_release_seqid(calldata->arg.seqid);
2347 else
2348 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002349out:
2350 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002353static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002354 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002355 .rpc_call_done = nfs4_close_done,
2356 .rpc_release = nfs4_free_closedata,
2357};
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359/*
2360 * It is possible for data to be read/written from a mem-mapped file
2361 * after the sys_close call (which hits the vfs layer as a flush).
2362 * This means that we can't safely call nfsv4 close on a file until
2363 * the inode is cleared. This in turn means that we are not good
2364 * NFSv4 citizens - we do not indicate to the server to update the file's
2365 * share state even when we are done with one of the three share
2366 * stateid's in the inode.
2367 *
2368 * NOTE: Caller must be holding the sp->so_owner semaphore!
2369 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002370int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002372 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002374 struct nfs4_state_owner *sp = state->owner;
2375 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002376 struct rpc_message msg = {
2377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2378 .rpc_cred = state->owner->so_cred,
2379 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002380 struct rpc_task_setup task_setup_data = {
2381 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002382 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002383 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002384 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002385 .flags = RPC_TASK_ASYNC,
2386 };
Trond Myklebust95121352005-10-18 14:20:12 -07002387 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002389 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002391 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002392 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002393 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002395 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002396 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002398 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002399 if (calldata->arg.seqid == NULL)
2400 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002401 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002402 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002403 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002404 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002405 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002406 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002407 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002408
Trond Myklebust1174dd12010-12-21 10:52:24 -05002409 msg.rpc_argp = &calldata->arg;
2410 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002411 task_setup_data.callback_data = calldata;
2412 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002413 if (IS_ERR(task))
2414 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002415 status = 0;
2416 if (wait)
2417 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002418 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002419 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002420out_free_calldata:
2421 kfree(calldata);
2422out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002423 nfs4_put_open_state(state);
2424 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002425 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Trond Myklebust2b484292010-09-17 10:56:51 -04002428static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002429nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 struct nfs4_state *state;
2432
Trond Myklebust565277f2007-10-15 18:17:53 -04002433 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002434 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2435 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002436 if (IS_ERR(state))
2437 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002438 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002439 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Trond Myklebust1185a552009-12-03 15:54:02 -05002442static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002443{
2444 if (ctx->state == NULL)
2445 return;
2446 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002447 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002448 else
Al Viro643168c2011-06-22 18:20:23 -04002449 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2453{
Benny Halevy43652ad2009-04-01 09:21:54 -04002454 struct nfs4_server_caps_arg args = {
2455 .fhandle = fhandle,
2456 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct nfs4_server_caps_res res = {};
2458 struct rpc_message msg = {
2459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002460 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 .rpc_resp = &res,
2462 };
2463 int status;
2464
Bryan Schumaker7c513052011-03-24 17:12:24 +00002465 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (status == 0) {
2467 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002468 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2469 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2470 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2471 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2472 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2474 server->caps |= NFS_CAP_ACLS;
2475 if (res.has_links != 0)
2476 server->caps |= NFS_CAP_HARDLINKS;
2477 if (res.has_symlinks != 0)
2478 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002479 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2480 server->caps |= NFS_CAP_FILEID;
2481 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2482 server->caps |= NFS_CAP_MODE;
2483 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2484 server->caps |= NFS_CAP_NLINK;
2485 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2486 server->caps |= NFS_CAP_OWNER;
2487 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2488 server->caps |= NFS_CAP_OWNER_GROUP;
2489 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2490 server->caps |= NFS_CAP_ATIME;
2491 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2492 server->caps |= NFS_CAP_CTIME;
2493 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2494 server->caps |= NFS_CAP_MTIME;
2495
Trond Myklebusta65318b2009-03-11 14:10:28 -04002496 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2497 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2498 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002500 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 return status;
2504}
2505
Trond Myklebust55a97592006-06-09 09:34:19 -04002506int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 struct nfs4_exception exception = { };
2509 int err;
2510 do {
2511 err = nfs4_handle_exception(server,
2512 _nfs4_server_capabilities(server, fhandle),
2513 &exception);
2514 } while (exception.retry);
2515 return err;
2516}
2517
2518static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2519 struct nfs_fsinfo *info)
2520{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 struct nfs4_lookup_root_arg args = {
2522 .bitmask = nfs4_fattr_bitmap,
2523 };
2524 struct nfs4_lookup_res res = {
2525 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002526 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 .fh = fhandle,
2528 };
2529 struct rpc_message msg = {
2530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2531 .rpc_argp = &args,
2532 .rpc_resp = &res,
2533 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002534
Trond Myklebust0e574af2005-10-27 22:12:38 -04002535 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002536 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
2539static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2540 struct nfs_fsinfo *info)
2541{
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002545 err = _nfs4_lookup_root(server, fhandle, info);
2546 switch (err) {
2547 case 0:
2548 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002549 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002550 default:
2551 err = nfs4_handle_exception(server, err, &exception);
2552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002554out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return err;
2556}
2557
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002558static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2559 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2560{
2561 struct rpc_auth *auth;
2562 int ret;
2563
2564 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002565 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002566 ret = -EIO;
2567 goto out;
2568 }
2569 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002570out:
2571 return ret;
2572}
2573
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002574static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002575 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002577 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002578 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002580 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002581 if (len < 0)
2582 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002583
2584 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002585 /* AUTH_UNIX is the default flavor if none was specified,
2586 * thus has already been tried. */
2587 if (flav_array[i] == RPC_AUTH_UNIX)
2588 continue;
2589
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002590 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002591 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002592 continue;
2593 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002594 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002595 /*
2596 * -EACCESS could mean that the user doesn't have correct permissions
2597 * to access the mount. It could also mean that we tried to mount
2598 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2599 * existing mount programs don't handle -EACCES very well so it should
2600 * be mapped to -EPERM instead.
2601 */
2602 if (status == -EACCES)
2603 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002604 return status;
2605}
2606
2607/*
2608 * get the file handle for the "/" directory on the server
2609 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002610int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2611 struct nfs_fsinfo *info)
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002612{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002613 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002614 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002615 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2616 /*
2617 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2618 * by nfs4_map_errors() as this function exits.
2619 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002620 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (status == 0)
2622 status = nfs4_server_capabilities(server, fhandle);
2623 if (status == 0)
2624 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002625 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Bryan Schumakerbae36242012-05-10 15:07:31 -04002628static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2629 struct nfs_fsinfo *info)
2630{
2631 int error;
2632 struct nfs_fattr *fattr = info->fattr;
2633
2634 error = nfs4_server_capabilities(server, mntfh);
2635 if (error < 0) {
2636 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2637 return error;
2638 }
2639
2640 error = nfs4_proc_getattr(server, mntfh, fattr);
2641 if (error < 0) {
2642 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2643 return error;
2644 }
2645
2646 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2647 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2648 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2649
2650 return error;
2651}
2652
Manoj Naik6b97fd32006-06-09 09:34:29 -04002653/*
2654 * Get locations and (maybe) other attributes of a referral.
2655 * Note that we'll actually follow the referral later when
2656 * we detect fsid mismatch in inode revalidation
2657 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002658static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2659 const struct qstr *name, struct nfs_fattr *fattr,
2660 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002661{
2662 int status = -ENOMEM;
2663 struct page *page = NULL;
2664 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002665
2666 page = alloc_page(GFP_KERNEL);
2667 if (page == NULL)
2668 goto out;
2669 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2670 if (locations == NULL)
2671 goto out;
2672
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002673 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002674 if (status != 0)
2675 goto out;
2676 /* Make sure server returned a different fsid for the referral */
2677 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002678 dprintk("%s: server did not return a different fsid for"
2679 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002680 status = -EIO;
2681 goto out;
2682 }
Andy Adamson533eb462011-06-13 18:25:56 -04002683 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2684 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002685
Andy Adamson533eb462011-06-13 18:25:56 -04002686 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002687 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002688 memset(fhandle, 0, sizeof(struct nfs_fh));
2689out:
2690 if (page)
2691 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002692 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002693 return status;
2694}
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2697{
2698 struct nfs4_getattr_arg args = {
2699 .fh = fhandle,
2700 .bitmask = server->attr_bitmask,
2701 };
2702 struct nfs4_getattr_res res = {
2703 .fattr = fattr,
2704 .server = server,
2705 };
2706 struct rpc_message msg = {
2707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2708 .rpc_argp = &args,
2709 .rpc_resp = &res,
2710 };
2711
Trond Myklebust0e574af2005-10-27 22:12:38 -04002712 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002713 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
2716static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2717{
2718 struct nfs4_exception exception = { };
2719 int err;
2720 do {
2721 err = nfs4_handle_exception(server,
2722 _nfs4_proc_getattr(server, fhandle, fattr),
2723 &exception);
2724 } while (exception.retry);
2725 return err;
2726}
2727
2728/*
2729 * The file is not closed if it is opened due to the a request to change
2730 * the size of the file. The open call will not be needed once the
2731 * VFS layer lookup-intents are implemented.
2732 *
2733 * Close is called when the inode is destroyed.
2734 * If we haven't opened the file for O_WRONLY, we
2735 * need to in the size_change case to obtain a stateid.
2736 *
2737 * Got race?
2738 * Because OPEN is always done by name in nfsv4, it is
2739 * possible that we opened a different file by the same
2740 * name. We can recognize this race condition, but we
2741 * can't do anything about it besides returning an error.
2742 *
2743 * This will be fixed with VFS changes (lookup-intent).
2744 */
2745static int
2746nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2747 struct iattr *sattr)
2748{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002749 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002750 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002751 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 int status;
2753
Benny Halevy8a1636c2010-07-14 15:43:57 -04002754 if (pnfs_ld_layoutret_on_setattr(inode))
2755 pnfs_return_layout(inode);
2756
Trond Myklebust0e574af2005-10-27 22:12:38 -04002757 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Andy Adamson26699402012-05-30 16:12:24 -04002759 /* Deal with open(O_TRUNC) */
2760 if (sattr->ia_valid & ATTR_OPEN)
2761 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2762
2763 /* Optimization: if the end result is no change, don't RPC */
2764 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2765 return 0;
2766
Trond Myklebustd5308382005-11-04 15:33:38 -05002767 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002768 if (sattr->ia_valid & ATTR_FILE) {
2769 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002770
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002771 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002772 if (ctx) {
2773 cred = ctx->cred;
2774 state = ctx->state;
2775 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002776 }
2777
2778 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002779 if (status == 0)
2780 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return status;
2782}
2783
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002784static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2785 const struct qstr *name, struct nfs_fh *fhandle,
2786 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002787{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002788 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002789 int status;
2790 struct nfs4_lookup_arg args = {
2791 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002792 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002793 .name = name,
2794 };
2795 struct nfs4_lookup_res res = {
2796 .server = server,
2797 .fattr = fattr,
2798 .fh = fhandle,
2799 };
2800 struct rpc_message msg = {
2801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2802 .rpc_argp = &args,
2803 .rpc_resp = &res,
2804 };
2805
2806 nfs_fattr_init(fattr);
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002809 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 dprintk("NFS reply lookup: %d\n", status);
2811 return status;
2812}
2813
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002814static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002815{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002816 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002817 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002818 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2819 fattr->nlink = 2;
2820}
2821
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002822static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2823 struct qstr *name, struct nfs_fh *fhandle,
2824 struct nfs_fattr *fattr)
2825{
2826 struct nfs4_exception exception = { };
2827 struct rpc_clnt *client = *clnt;
2828 int err;
2829 do {
2830 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2831 switch (err) {
2832 case -NFS4ERR_BADNAME:
2833 err = -ENOENT;
2834 goto out;
2835 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002836 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002837 goto out;
2838 case -NFS4ERR_WRONGSEC:
2839 err = -EPERM;
2840 if (client != *clnt)
2841 goto out;
2842
2843 client = nfs4_create_sec_client(client, dir, name);
2844 if (IS_ERR(client))
2845 return PTR_ERR(client);
2846
2847 exception.retry = 1;
2848 break;
2849 default:
2850 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2851 }
2852 } while (exception.retry);
2853
2854out:
2855 if (err == 0)
2856 *clnt = client;
2857 else if (client != *clnt)
2858 rpc_shutdown_client(client);
2859
2860 return err;
2861}
2862
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002863static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002864 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002866 int status;
2867 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002868
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002869 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2870 if (client != NFS_CLIENT(dir)) {
2871 rpc_shutdown_client(client);
2872 nfs_fixup_secinfo_attributes(fattr);
2873 }
2874 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002877struct rpc_clnt *
2878nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2879 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2880{
2881 int status;
2882 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2883
2884 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2885 if (status < 0) {
2886 rpc_shutdown_client(client);
2887 return ERR_PTR(status);
2888 }
2889 return client;
2890}
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2893{
Trond Myklebust76b32992007-08-10 17:45:11 -04002894 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 struct nfs4_accessargs args = {
2896 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002897 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002899 struct nfs4_accessres res = {
2900 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002901 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 struct rpc_message msg = {
2903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2904 .rpc_argp = &args,
2905 .rpc_resp = &res,
2906 .rpc_cred = entry->cred,
2907 };
2908 int mode = entry->mask;
2909 int status;
2910
2911 /*
2912 * Determine which access bits we want to ask for...
2913 */
2914 if (mode & MAY_READ)
2915 args.access |= NFS4_ACCESS_READ;
2916 if (S_ISDIR(inode->i_mode)) {
2917 if (mode & MAY_WRITE)
2918 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2919 if (mode & MAY_EXEC)
2920 args.access |= NFS4_ACCESS_LOOKUP;
2921 } else {
2922 if (mode & MAY_WRITE)
2923 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2924 if (mode & MAY_EXEC)
2925 args.access |= NFS4_ACCESS_EXECUTE;
2926 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002927
2928 res.fattr = nfs_alloc_fattr();
2929 if (res.fattr == NULL)
2930 return -ENOMEM;
2931
Bryan Schumaker7c513052011-03-24 17:12:24 +00002932 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002934 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002935 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002937 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return status;
2939}
2940
2941static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2942{
2943 struct nfs4_exception exception = { };
2944 int err;
2945 do {
2946 err = nfs4_handle_exception(NFS_SERVER(inode),
2947 _nfs4_proc_access(inode, entry),
2948 &exception);
2949 } while (exception.retry);
2950 return err;
2951}
2952
2953/*
2954 * TODO: For the time being, we don't try to get any attributes
2955 * along with any of the zero-copy operations READ, READDIR,
2956 * READLINK, WRITE.
2957 *
2958 * In the case of the first three, we want to put the GETATTR
2959 * after the read-type operation -- this is because it is hard
2960 * to predict the length of a GETATTR response in v4, and thus
2961 * align the READ data correctly. This means that the GETATTR
2962 * may end up partially falling into the page cache, and we should
2963 * shift it into the 'tail' of the xdr_buf before processing.
2964 * To do this efficiently, we need to know the total length
2965 * of data received, which doesn't seem to be available outside
2966 * of the RPC layer.
2967 *
2968 * In the case of WRITE, we also want to put the GETATTR after
2969 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002970 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 *
2972 * Both of these changes to the XDR layer would in fact be quite
2973 * minor, but I decided to leave them for a subsequent patch.
2974 */
2975static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2976 unsigned int pgbase, unsigned int pglen)
2977{
2978 struct nfs4_readlink args = {
2979 .fh = NFS_FH(inode),
2980 .pgbase = pgbase,
2981 .pglen = pglen,
2982 .pages = &page,
2983 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002984 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 struct rpc_message msg = {
2986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2987 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002988 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 };
2990
Bryan Schumaker7c513052011-03-24 17:12:24 +00002991 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 -07002992}
2993
2994static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2995 unsigned int pgbase, unsigned int pglen)
2996{
2997 struct nfs4_exception exception = { };
2998 int err;
2999 do {
3000 err = nfs4_handle_exception(NFS_SERVER(inode),
3001 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3002 &exception);
3003 } while (exception.retry);
3004 return err;
3005}
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003008 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010static int
3011nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003012 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003014 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 int status = 0;
3017
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003018 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3019 if (IS_ERR(ctx))
3020 return PTR_ERR(ctx);
3021
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003022 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003023 state = nfs4_do_open(dir, dentry, ctx->mode,
3024 flags, sattr, ctx->cred,
3025 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003026 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (IS_ERR(state)) {
3028 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003029 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003031 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003032 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003033 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003035 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return status;
3037}
3038
3039static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3040{
Trond Myklebust16e42952005-10-27 22:12:44 -04003041 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003042 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003044 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003046 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003047 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3051 .rpc_argp = &args,
3052 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003054 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003055
Bryan Schumaker7c513052011-03-24 17:12:24 +00003056 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003057 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003058 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return status;
3060}
3061
3062static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3063{
3064 struct nfs4_exception exception = { };
3065 int err;
3066 do {
3067 err = nfs4_handle_exception(NFS_SERVER(dir),
3068 _nfs4_proc_remove(dir, name),
3069 &exception);
3070 } while (exception.retry);
3071 return err;
3072}
3073
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003074static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003076 struct nfs_server *server = NFS_SERVER(dir);
3077 struct nfs_removeargs *args = msg->rpc_argp;
3078 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003080 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003082 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003085static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3086{
3087 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3088 &data->args.seq_args,
3089 &data->res.seq_res,
3090 task))
3091 return;
3092 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003095static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003097 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3098
Trond Myklebust14516c32010-07-31 14:29:06 -04003099 if (!nfs4_sequence_done(task, &res->seq_res))
3100 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003101 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003102 return 0;
3103 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003104 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105}
3106
Jeff Laytond3d41522010-09-17 17:31:57 -04003107static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3108{
3109 struct nfs_server *server = NFS_SERVER(dir);
3110 struct nfs_renameargs *arg = msg->rpc_argp;
3111 struct nfs_renameres *res = msg->rpc_resp;
3112
3113 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003114 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003115 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003116}
3117
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003118static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3119{
3120 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3121 &data->args.seq_args,
3122 &data->res.seq_res,
3123 task))
3124 return;
3125 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003126}
3127
3128static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3129 struct inode *new_dir)
3130{
3131 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3132
3133 if (!nfs4_sequence_done(task, &res->seq_res))
3134 return 0;
3135 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3136 return 0;
3137
3138 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003139 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003140 return 1;
3141}
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3144 struct inode *new_dir, struct qstr *new_name)
3145{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003146 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003147 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 .old_dir = NFS_FH(old_dir),
3149 .new_dir = NFS_FH(new_dir),
3150 .old_name = old_name,
3151 .new_name = new_name,
3152 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003153 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003154 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003155 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct rpc_message msg = {
3157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3158 .rpc_argp = &arg,
3159 .rpc_resp = &res,
3160 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003161 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Bryan Schumaker7c513052011-03-24 17:12:24 +00003163 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (!status) {
3165 update_changeattr(old_dir, &res.old_cinfo);
3166 update_changeattr(new_dir, &res.new_cinfo);
3167 }
3168 return status;
3169}
3170
3171static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3172 struct inode *new_dir, struct qstr *new_name)
3173{
3174 struct nfs4_exception exception = { };
3175 int err;
3176 do {
3177 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3178 _nfs4_proc_rename(old_dir, old_name,
3179 new_dir, new_name),
3180 &exception);
3181 } while (exception.retry);
3182 return err;
3183}
3184
3185static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3186{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003187 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 struct nfs4_link_arg arg = {
3189 .fh = NFS_FH(inode),
3190 .dir_fh = NFS_FH(dir),
3191 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003192 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003194 struct nfs4_link_res res = {
3195 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003196 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 struct rpc_message msg = {
3198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3199 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003200 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003202 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Trond Myklebust136f2622010-04-16 16:22:49 -04003204 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003205 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003206 goto out;
3207
Bryan Schumaker7c513052011-03-24 17:12:24 +00003208 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003209 if (!status) {
3210 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003211 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003212 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003213out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003214 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 return status;
3216}
3217
3218static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3219{
3220 struct nfs4_exception exception = { };
3221 int err;
3222 do {
3223 err = nfs4_handle_exception(NFS_SERVER(inode),
3224 _nfs4_proc_link(inode, dir, name),
3225 &exception);
3226 } while (exception.retry);
3227 return err;
3228}
3229
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003230struct nfs4_createdata {
3231 struct rpc_message msg;
3232 struct nfs4_create_arg arg;
3233 struct nfs4_create_res res;
3234 struct nfs_fh fh;
3235 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003236};
3237
3238static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3239 struct qstr *name, struct iattr *sattr, u32 ftype)
3240{
3241 struct nfs4_createdata *data;
3242
3243 data = kzalloc(sizeof(*data), GFP_KERNEL);
3244 if (data != NULL) {
3245 struct nfs_server *server = NFS_SERVER(dir);
3246
3247 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3248 data->msg.rpc_argp = &data->arg;
3249 data->msg.rpc_resp = &data->res;
3250 data->arg.dir_fh = NFS_FH(dir);
3251 data->arg.server = server;
3252 data->arg.name = name;
3253 data->arg.attrs = sattr;
3254 data->arg.ftype = ftype;
3255 data->arg.bitmask = server->attr_bitmask;
3256 data->res.server = server;
3257 data->res.fh = &data->fh;
3258 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003259 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003260 }
3261 return data;
3262}
3263
3264static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3265{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003266 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003267 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003268 if (status == 0) {
3269 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003270 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3271 }
3272 return status;
3273}
3274
3275static void nfs4_free_createdata(struct nfs4_createdata *data)
3276{
3277 kfree(data);
3278}
3279
Chuck Lever4f390c12006-08-22 20:06:22 -04003280static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003281 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003283 struct nfs4_createdata *data;
3284 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Chuck Lever94a6d752006-08-22 20:06:23 -04003286 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003287 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003288
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003289 status = -ENOMEM;
3290 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3291 if (data == NULL)
3292 goto out;
3293
3294 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3295 data->arg.u.symlink.pages = &page;
3296 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003298 status = nfs4_do_create(dir, dentry, data);
3299
3300 nfs4_free_createdata(data);
3301out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return status;
3303}
3304
Chuck Lever4f390c12006-08-22 20:06:22 -04003305static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003306 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
3308 struct nfs4_exception exception = { };
3309 int err;
3310 do {
3311 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003312 _nfs4_proc_symlink(dir, dentry, page,
3313 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 &exception);
3315 } while (exception.retry);
3316 return err;
3317}
3318
3319static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3320 struct iattr *sattr)
3321{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003322 struct nfs4_createdata *data;
3323 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003325 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3326 if (data == NULL)
3327 goto out;
3328
3329 status = nfs4_do_create(dir, dentry, data);
3330
3331 nfs4_free_createdata(data);
3332out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return status;
3334}
3335
3336static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3337 struct iattr *sattr)
3338{
3339 struct nfs4_exception exception = { };
3340 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003341
3342 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 do {
3344 err = nfs4_handle_exception(NFS_SERVER(dir),
3345 _nfs4_proc_mkdir(dir, dentry, sattr),
3346 &exception);
3347 } while (exception.retry);
3348 return err;
3349}
3350
3351static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003352 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
3354 struct inode *dir = dentry->d_inode;
3355 struct nfs4_readdir_arg args = {
3356 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003357 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 .pgbase = 0,
3359 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003360 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003361 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 };
3363 struct nfs4_readdir_res res;
3364 struct rpc_message msg = {
3365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3366 .rpc_argp = &args,
3367 .rpc_resp = &res,
3368 .rpc_cred = cred,
3369 };
3370 int status;
3371
Harvey Harrison3110ff82008-05-02 13:42:44 -07003372 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003373 dentry->d_parent->d_name.name,
3374 dentry->d_name.name,
3375 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003376 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003378 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 -05003379 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003380 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003381 status += args.pgbase;
3382 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003383
3384 nfs_invalidate_atime(dir);
3385
Harvey Harrison3110ff82008-05-02 13:42:44 -07003386 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return status;
3388}
3389
3390static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003391 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 struct nfs4_exception exception = { };
3394 int err;
3395 do {
3396 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3397 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003398 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 &exception);
3400 } while (exception.retry);
3401 return err;
3402}
3403
3404static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3405 struct iattr *sattr, dev_t rdev)
3406{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003407 struct nfs4_createdata *data;
3408 int mode = sattr->ia_mode;
3409 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003411 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3412 if (data == NULL)
3413 goto out;
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003416 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003418 data->arg.ftype = NF4BLK;
3419 data->arg.u.device.specdata1 = MAJOR(rdev);
3420 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
3422 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003423 data->arg.ftype = NF4CHR;
3424 data->arg.u.device.specdata1 = MAJOR(rdev);
3425 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003426 } else if (!S_ISSOCK(mode)) {
3427 status = -EINVAL;
3428 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003431 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003432out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003433 nfs4_free_createdata(data);
3434out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return status;
3436}
3437
3438static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3439 struct iattr *sattr, dev_t rdev)
3440{
3441 struct nfs4_exception exception = { };
3442 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003443
3444 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 do {
3446 err = nfs4_handle_exception(NFS_SERVER(dir),
3447 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3448 &exception);
3449 } while (exception.retry);
3450 return err;
3451}
3452
3453static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3454 struct nfs_fsstat *fsstat)
3455{
3456 struct nfs4_statfs_arg args = {
3457 .fh = fhandle,
3458 .bitmask = server->attr_bitmask,
3459 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003460 struct nfs4_statfs_res res = {
3461 .fsstat = fsstat,
3462 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 struct rpc_message msg = {
3464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3465 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003466 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 };
3468
Trond Myklebust0e574af2005-10-27 22:12:38 -04003469 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003470 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
3473static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3474{
3475 struct nfs4_exception exception = { };
3476 int err;
3477 do {
3478 err = nfs4_handle_exception(server,
3479 _nfs4_proc_statfs(server, fhandle, fsstat),
3480 &exception);
3481 } while (exception.retry);
3482 return err;
3483}
3484
3485static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3486 struct nfs_fsinfo *fsinfo)
3487{
3488 struct nfs4_fsinfo_arg args = {
3489 .fh = fhandle,
3490 .bitmask = server->attr_bitmask,
3491 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003492 struct nfs4_fsinfo_res res = {
3493 .fsinfo = fsinfo,
3494 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 struct rpc_message msg = {
3496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3497 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003498 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 };
3500
Bryan Schumaker7c513052011-03-24 17:12:24 +00003501 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
3504static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3505{
3506 struct nfs4_exception exception = { };
3507 int err;
3508
3509 do {
3510 err = nfs4_handle_exception(server,
3511 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3512 &exception);
3513 } while (exception.retry);
3514 return err;
3515}
3516
3517static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3518{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003519 int error;
3520
Trond Myklebust0e574af2005-10-27 22:12:38 -04003521 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003522 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003523 if (error == 0) {
3524 /* block layout checks this! */
3525 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003526 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003527 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003528
3529 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530}
3531
3532static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3533 struct nfs_pathconf *pathconf)
3534{
3535 struct nfs4_pathconf_arg args = {
3536 .fh = fhandle,
3537 .bitmask = server->attr_bitmask,
3538 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003539 struct nfs4_pathconf_res res = {
3540 .pathconf = pathconf,
3541 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 struct rpc_message msg = {
3543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3544 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003545 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 };
3547
3548 /* None of the pathconf attributes are mandatory to implement */
3549 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3550 memset(pathconf, 0, sizeof(*pathconf));
3551 return 0;
3552 }
3553
Trond Myklebust0e574af2005-10-27 22:12:38 -04003554 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003555 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
3557
3558static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3559 struct nfs_pathconf *pathconf)
3560{
3561 struct nfs4_exception exception = { };
3562 int err;
3563
3564 do {
3565 err = nfs4_handle_exception(server,
3566 _nfs4_proc_pathconf(server, fhandle, pathconf),
3567 &exception);
3568 } while (exception.retry);
3569 return err;
3570}
3571
Benny Halevyd20581a2011-05-22 19:52:03 +03003572void __nfs4_read_done_cb(struct nfs_read_data *data)
3573{
Fred Isamancd841602012-04-20 14:47:44 -04003574 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003575}
3576
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003577static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Fred Isamancd841602012-04-20 14:47:44 -04003579 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003581 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003582 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003583 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003585
Benny Halevyd20581a2011-05-22 19:52:03 +03003586 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003588 renew_lease(server, data->timestamp);
3589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003592static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3593{
3594
3595 dprintk("--> %s\n", __func__);
3596
3597 if (!nfs4_sequence_done(task, &data->res.seq_res))
3598 return -EAGAIN;
3599
Benny Halevyd20581a2011-05-22 19:52:03 +03003600 return data->read_done_cb ? data->read_done_cb(task, data) :
3601 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003602}
3603
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003604static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003607 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003608 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003609 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003612static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3613{
Fred Isamancd841602012-04-20 14:47:44 -04003614 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003615 &data->args.seq_args,
3616 &data->res.seq_res,
3617 task))
3618 return;
3619 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
Fred Isamanb029bc92011-03-03 15:13:42 +00003622static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
Fred Isamancd841602012-04-20 14:47:44 -04003624 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003626 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003627 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003628 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003630 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003632 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003633 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635}
3636
Fred Isamanb029bc92011-03-03 15:13:42 +00003637static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3638{
3639 if (!nfs4_sequence_done(task, &data->res.seq_res))
3640 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003641 return data->write_done_cb ? data->write_done_cb(task, data) :
3642 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003643}
3644
Trond Myklebust5a37f852012-04-28 14:55:16 -04003645static
3646bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003647{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003648 const struct nfs_pgio_header *hdr = data->header;
3649
3650 /* Don't request attributes for pNFS or O_DIRECT writes */
3651 if (data->ds_clp != NULL || hdr->dreq != NULL)
3652 return false;
3653 /* Otherwise, request attributes if and only if we don't hold
3654 * a delegation
3655 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003656 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003657}
Fred Isamana69aef12011-03-03 15:13:47 +00003658
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003659static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Fred Isamancd841602012-04-20 14:47:44 -04003661 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003662
Trond Myklebust5a37f852012-04-28 14:55:16 -04003663 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003664 data->args.bitmask = NULL;
3665 data->res.fattr = NULL;
3666 } else
3667 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003668
Fred Isamanb029bc92011-03-03 15:13:42 +00003669 if (!data->write_done_cb)
3670 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003671 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 data->timestamp = jiffies;
3673
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003674 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003675 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003678static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3679{
Fred Isamancd841602012-04-20 14:47:44 -04003680 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003681 &data->args.seq_args,
3682 &data->res.seq_res,
3683 task))
3684 return;
3685 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686}
3687
Fred Isaman0b7c0152012-04-20 14:47:39 -04003688static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3691 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003692 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return;
3695 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003696}
3697
Fred Isaman0b7c0152012-04-20 14:47:39 -04003698static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003701
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003702 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003703 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003704 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
Fred Isaman0b7c0152012-04-20 14:47:39 -04003709static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003710{
3711 if (!nfs4_sequence_done(task, &data->res.seq_res))
3712 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003713 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003714}
3715
Fred Isaman0b7c0152012-04-20 14:47:39 -04003716static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003718 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003719
Fred Isaman0b7c0152012-04-20 14:47:39 -04003720 if (data->commit_done_cb == NULL)
3721 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003722 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003723 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003724 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003727struct nfs4_renewdata {
3728 struct nfs_client *client;
3729 unsigned long timestamp;
3730};
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732/*
3733 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3734 * standalone procedure for queueing an asynchronous RENEW.
3735 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003736static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003737{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003738 struct nfs4_renewdata *data = calldata;
3739 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003740
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003741 if (atomic_read(&clp->cl_count) > 1)
3742 nfs4_schedule_state_renewal(clp);
3743 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003744 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003745}
3746
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003747static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003749 struct nfs4_renewdata *data = calldata;
3750 struct nfs_client *clp = data->client;
3751 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
3753 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003754 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003755 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3756 return;
3757 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003758 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003759 return;
3760 }
3761 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003763 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003766static const struct rpc_call_ops nfs4_renew_ops = {
3767 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003768 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003769};
3770
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003771static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
3773 struct rpc_message msg = {
3774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3775 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003776 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003778 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003780 if (renew_flags == 0)
3781 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003782 if (!atomic_inc_not_zero(&clp->cl_count))
3783 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003784 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003785 if (data == NULL)
3786 return -ENOMEM;
3787 data->client = clp;
3788 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003790 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791}
3792
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003793static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
3795 struct rpc_message msg = {
3796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3797 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003798 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 };
3800 unsigned long now = jiffies;
3801 int status;
3802
3803 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3804 if (status < 0)
3805 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003806 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 return 0;
3808}
3809
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003810static inline int nfs4_server_supports_acls(struct nfs_server *server)
3811{
3812 return (server->caps & NFS_CAP_ACLS)
3813 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3814 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3815}
3816
Trond Myklebust21f498c2012-08-24 10:59:25 -04003817/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3818 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003819 * the stack.
3820 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003821#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003822
Neil Hormane9e3d722011-03-04 19:26:03 -05003823static int buf_to_pages_noslab(const void *buf, size_t buflen,
3824 struct page **pages, unsigned int *pgbase)
3825{
3826 struct page *newpage, **spages;
3827 int rc = 0;
3828 size_t len;
3829 spages = pages;
3830
3831 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003832 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003833 newpage = alloc_page(GFP_KERNEL);
3834
3835 if (newpage == NULL)
3836 goto unwind;
3837 memcpy(page_address(newpage), buf, len);
3838 buf += len;
3839 buflen -= len;
3840 *pages++ = newpage;
3841 rc++;
3842 } while (buflen != 0);
3843
3844 return rc;
3845
3846unwind:
3847 for(; rc > 0; rc--)
3848 __free_page(spages[rc-1]);
3849 return -ENOMEM;
3850}
3851
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003852struct nfs4_cached_acl {
3853 int cached;
3854 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003855 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003856};
3857
3858static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003859{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860 struct nfs_inode *nfsi = NFS_I(inode);
3861
3862 spin_lock(&inode->i_lock);
3863 kfree(nfsi->nfs4_acl);
3864 nfsi->nfs4_acl = acl;
3865 spin_unlock(&inode->i_lock);
3866}
3867
3868static void nfs4_zap_acl_attr(struct inode *inode)
3869{
3870 nfs4_set_cached_acl(inode, NULL);
3871}
3872
3873static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3874{
3875 struct nfs_inode *nfsi = NFS_I(inode);
3876 struct nfs4_cached_acl *acl;
3877 int ret = -ENOENT;
3878
3879 spin_lock(&inode->i_lock);
3880 acl = nfsi->nfs4_acl;
3881 if (acl == NULL)
3882 goto out;
3883 if (buf == NULL) /* user is just asking for length */
3884 goto out_len;
3885 if (acl->cached == 0)
3886 goto out;
3887 ret = -ERANGE; /* see getxattr(2) man page */
3888 if (acl->len > buflen)
3889 goto out;
3890 memcpy(buf, acl->data, acl->len);
3891out_len:
3892 ret = acl->len;
3893out:
3894 spin_unlock(&inode->i_lock);
3895 return ret;
3896}
3897
Sachin Prabhu5794d212012-04-17 14:36:40 +01003898static 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 +00003899{
3900 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003901 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003902
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003903 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003904 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003905 if (acl == NULL)
3906 goto out;
3907 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003908 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003909 } else {
3910 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3911 if (acl == NULL)
3912 goto out;
3913 acl->cached = 0;
3914 }
3915 acl->len = acl_len;
3916out:
3917 nfs4_set_cached_acl(inode, acl);
3918}
3919
Andy Adamsonbf118a32011-12-07 11:55:27 -05003920/*
3921 * The getxattr API returns the required buffer length when called with a
3922 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3923 * the required buf. On a NULL buf, we send a page of data to the server
3924 * guessing that the ACL request can be serviced by a page. If so, we cache
3925 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3926 * the cache. If not so, we throw away the page, and cache the required
3927 * length. The next getxattr call will then produce another round trip to
3928 * the server, this time with the input buf of the required size.
3929 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003930static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003931{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003932 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003933 struct nfs_getaclargs args = {
3934 .fh = NFS_FH(inode),
3935 .acl_pages = pages,
3936 .acl_len = buflen,
3937 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003938 struct nfs_getaclres res = {
3939 .acl_len = buflen,
3940 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003941 struct rpc_message msg = {
3942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3943 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003944 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003945 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003946 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3947 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003948
Andy Adamsonbf118a32011-12-07 11:55:27 -05003949 /* As long as we're doing a round trip to the server anyway,
3950 * let's be prepared for a page of acl data. */
3951 if (npages == 0)
3952 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003953 if (npages > ARRAY_SIZE(pages))
3954 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003955
Andy Adamsonbf118a32011-12-07 11:55:27 -05003956 for (i = 0; i < npages; i++) {
3957 pages[i] = alloc_page(GFP_KERNEL);
3958 if (!pages[i])
3959 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003960 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003961
3962 /* for decoding across pages */
3963 res.acl_scratch = alloc_page(GFP_KERNEL);
3964 if (!res.acl_scratch)
3965 goto out_free;
3966
Andy Adamsonbf118a32011-12-07 11:55:27 -05003967 args.acl_len = npages * PAGE_SIZE;
3968 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003969
Peng Taode040be2012-01-10 22:42:47 +08003970 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003971 __func__, buf, buflen, npages, args.acl_len);
3972 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3973 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003974 if (ret)
3975 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003976
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003977 /* Handle the case where the passed-in buffer is too short */
3978 if (res.acl_flags & NFS4_ACL_TRUNC) {
3979 /* Did the user only issue a request for the acl length? */
3980 if (buf == NULL)
3981 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003982 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003983 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003984 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003985 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3986 if (buf)
3987 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3988out_ok:
3989 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003990out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003991 for (i = 0; i < npages; i++)
3992 if (pages[i])
3993 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003994 if (res.acl_scratch)
3995 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003996 return ret;
3997}
3998
Trond Myklebust16b42892006-08-24 12:27:15 -04003999static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4000{
4001 struct nfs4_exception exception = { };
4002 ssize_t ret;
4003 do {
4004 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4005 if (ret >= 0)
4006 break;
4007 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4008 } while (exception.retry);
4009 return ret;
4010}
4011
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004012static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4013{
4014 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004015 int ret;
4016
4017 if (!nfs4_server_supports_acls(server))
4018 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004019 ret = nfs_revalidate_inode(server, inode);
4020 if (ret < 0)
4021 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004022 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4023 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004024 ret = nfs4_read_cached_acl(inode, buf, buflen);
4025 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004026 /* -ENOENT is returned if there is no ACL or if there is an ACL
4027 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004028 return ret;
4029 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004030}
4031
Trond Myklebust16b42892006-08-24 12:27:15 -04004032static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004033{
4034 struct nfs_server *server = NFS_SERVER(inode);
4035 struct page *pages[NFS4ACL_MAXPAGES];
4036 struct nfs_setaclargs arg = {
4037 .fh = NFS_FH(inode),
4038 .acl_pages = pages,
4039 .acl_len = buflen,
4040 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004041 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004042 struct rpc_message msg = {
4043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4044 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004045 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004046 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004047 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004048 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004049
4050 if (!nfs4_server_supports_acls(server))
4051 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004052 if (npages > ARRAY_SIZE(pages))
4053 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004054 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4055 if (i < 0)
4056 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004057 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004058 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004059
4060 /*
4061 * Free each page after tx, so the only ref left is
4062 * held by the network stack
4063 */
4064 for (; i > 0; i--)
4065 put_page(pages[i-1]);
4066
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004067 /*
4068 * Acl update can result in inode attribute update.
4069 * so mark the attribute cache invalid.
4070 */
4071 spin_lock(&inode->i_lock);
4072 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4073 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004074 nfs_access_zap_cache(inode);
4075 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004076 return ret;
4077}
4078
Trond Myklebust16b42892006-08-24 12:27:15 -04004079static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4080{
4081 struct nfs4_exception exception = { };
4082 int err;
4083 do {
4084 err = nfs4_handle_exception(NFS_SERVER(inode),
4085 __nfs4_proc_set_acl(inode, buf, buflen),
4086 &exception);
4087 } while (exception.retry);
4088 return err;
4089}
4090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004092nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004094 struct nfs_client *clp = server->nfs_client;
4095
4096 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return 0;
4098 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004099 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004100 case -NFS4ERR_ADMIN_REVOKED:
4101 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004102 if (state == NULL)
4103 break;
4104 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004105 case -NFS4ERR_OPENMODE:
4106 if (state == NULL)
4107 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004108 nfs4_schedule_stateid_recovery(server, state);
4109 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004110 case -NFS4ERR_EXPIRED:
4111 if (state != NULL)
4112 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004114 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004115 nfs4_schedule_lease_recovery(clp);
4116 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004117#if defined(CONFIG_NFS_V4_1)
4118 case -NFS4ERR_BADSESSION:
4119 case -NFS4ERR_BADSLOT:
4120 case -NFS4ERR_BAD_HIGH_SLOT:
4121 case -NFS4ERR_DEADSESSION:
4122 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4123 case -NFS4ERR_SEQ_FALSE_RETRY:
4124 case -NFS4ERR_SEQ_MISORDERED:
4125 dprintk("%s ERROR %d, Reset session\n", __func__,
4126 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004127 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004128 task->tk_status = 0;
4129 return -EAGAIN;
4130#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004132 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004133 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004134 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4136 task->tk_status = 0;
4137 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004138 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 case -NFS4ERR_OLD_STATEID:
4140 task->tk_status = 0;
4141 return -EAGAIN;
4142 }
4143 task->tk_status = nfs4_map_errors(task->tk_status);
4144 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004145wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004146 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004147 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4148 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4149 task->tk_status = 0;
4150 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151}
4152
Chuck Leverf0920752012-05-21 22:45:41 -04004153static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4154 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004155{
4156 __be32 verf[2];
4157
Chuck Lever2c820d92012-05-21 22:45:33 -04004158 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4159 /* An impossible timestamp guarantees this value
4160 * will never match a generated boot time. */
4161 verf[0] = 0;
4162 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4163 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004164 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4165 verf[0] = (__be32)nn->boot_time.tv_sec;
4166 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004167 }
Chuck Levercd937102012-03-02 17:14:31 -05004168 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4169}
4170
Chuck Levere984a552012-09-14 17:24:21 -04004171static unsigned int
4172nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4173 char *buf, size_t len)
4174{
4175 unsigned int result;
4176
4177 rcu_read_lock();
4178 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4179 clp->cl_ipaddr,
4180 rpc_peeraddr2str(clp->cl_rpcclient,
4181 RPC_DISPLAY_ADDR),
4182 rpc_peeraddr2str(clp->cl_rpcclient,
4183 RPC_DISPLAY_PROTO));
4184 rcu_read_unlock();
4185 return result;
4186}
4187
4188static unsigned int
4189nfs4_init_uniform_client_string(const struct nfs_client *clp,
4190 char *buf, size_t len)
4191{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004192 char *nodename = clp->cl_rpcclient->cl_nodename;
4193
4194 if (nfs4_client_id_uniquifier[0] != '\0')
4195 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004196 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4197 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004198 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004199}
4200
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004201/**
4202 * nfs4_proc_setclientid - Negotiate client ID
4203 * @clp: state data structure
4204 * @program: RPC program for NFSv4 callback service
4205 * @port: IP port number for NFS4 callback service
4206 * @cred: RPC credential to use for this call
4207 * @res: where to place the result
4208 *
4209 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4210 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004211int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4212 unsigned short port, struct rpc_cred *cred,
4213 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 nfs4_verifier sc_verifier;
4216 struct nfs4_setclientid setclientid = {
4217 .sc_verifier = &sc_verifier,
4218 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004219 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 };
4221 struct rpc_message msg = {
4222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4223 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004224 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004225 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004227 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Chuck Leverde734832012-07-11 16:30:50 -04004229 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004230 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004231 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4232 setclientid.sc_name_len =
4233 nfs4_init_uniform_client_string(clp,
4234 setclientid.sc_name,
4235 sizeof(setclientid.sc_name));
4236 else
4237 setclientid.sc_name_len =
4238 nfs4_init_nonuniform_client_string(clp,
4239 setclientid.sc_name,
4240 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004241 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004242 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004243 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004244 sizeof(setclientid.sc_netid),
4245 rpc_peeraddr2str(clp->cl_rpcclient,
4246 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004247 rcu_read_unlock();
4248 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004249 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 clp->cl_ipaddr, port >> 8, port & 255);
4251
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004252 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4253 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4254 setclientid.sc_name_len, setclientid.sc_name);
4255 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4256 dprintk("NFS reply setclientid: %d\n", status);
4257 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004260/**
4261 * nfs4_proc_setclientid_confirm - Confirm client ID
4262 * @clp: state data structure
4263 * @res: result of a previous SETCLIENTID
4264 * @cred: RPC credential to use for this call
4265 *
4266 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4267 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004268int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004269 struct nfs4_setclientid_res *arg,
4270 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
4272 struct nfs_fsinfo fsinfo;
4273 struct rpc_message msg = {
4274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004275 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004277 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 };
4279 unsigned long now;
4280 int status;
4281
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004282 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4283 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4284 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004286 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 if (status == 0) {
4288 spin_lock(&clp->cl_lock);
4289 clp->cl_lease_time = fsinfo.lease_time * HZ;
4290 clp->cl_last_renewal = now;
4291 spin_unlock(&clp->cl_lock);
4292 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004293 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return status;
4295}
4296
Trond Myklebustfe650402006-01-03 09:55:18 +01004297struct nfs4_delegreturndata {
4298 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004299 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004300 struct nfs_fh fh;
4301 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004302 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004303 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004304 int rpc_status;
4305};
4306
Trond Myklebustfe650402006-01-03 09:55:18 +01004307static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4308{
4309 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004310
Trond Myklebust14516c32010-07-31 14:29:06 -04004311 if (!nfs4_sequence_done(task, &data->res.seq_res))
4312 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004313
Ricardo Labiaga79708862009-12-07 09:23:21 -05004314 switch (task->tk_status) {
4315 case -NFS4ERR_STALE_STATEID:
4316 case -NFS4ERR_EXPIRED:
4317 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004318 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004319 break;
4320 default:
4321 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4322 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004323 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004324 return;
4325 }
4326 }
4327 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004328}
4329
4330static void nfs4_delegreturn_release(void *calldata)
4331{
Trond Myklebustfe650402006-01-03 09:55:18 +01004332 kfree(calldata);
4333}
4334
Andy Adamson938e1012009-04-01 09:22:28 -04004335#if defined(CONFIG_NFS_V4_1)
4336static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4337{
4338 struct nfs4_delegreturndata *d_data;
4339
4340 d_data = (struct nfs4_delegreturndata *)data;
4341
Trond Myklebust035168a2010-06-16 09:52:26 -04004342 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004343 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004344 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004345 return;
4346 rpc_call_start(task);
4347}
4348#endif /* CONFIG_NFS_V4_1 */
4349
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004350static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004351#if defined(CONFIG_NFS_V4_1)
4352 .rpc_call_prepare = nfs4_delegreturn_prepare,
4353#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004354 .rpc_call_done = nfs4_delegreturn_done,
4355 .rpc_release = nfs4_delegreturn_release,
4356};
4357
Trond Myklebuste6f81072008-01-24 18:14:34 -05004358static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004359{
4360 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004361 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004362 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004363 struct rpc_message msg = {
4364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4365 .rpc_cred = cred,
4366 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004367 struct rpc_task_setup task_setup_data = {
4368 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004369 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004370 .callback_ops = &nfs4_delegreturn_ops,
4371 .flags = RPC_TASK_ASYNC,
4372 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004373 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004374
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004375 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004376 if (data == NULL)
4377 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004378 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004379 data->args.fhandle = &data->fh;
4380 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004381 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004382 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004383 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004384 data->res.fattr = &data->fattr;
4385 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004386 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004387 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004388 data->rpc_status = 0;
4389
Trond Myklebustc970aa82007-07-14 15:39:59 -04004390 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004391 msg.rpc_argp = &data->args;
4392 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004393 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004394 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004395 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004396 if (!issync)
4397 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004398 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004399 if (status != 0)
4400 goto out;
4401 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004402 if (status == 0)
4403 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4404 else
4405 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004406out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004407 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004408 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409}
4410
Trond Myklebuste6f81072008-01-24 18:14:34 -05004411int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
4413 struct nfs_server *server = NFS_SERVER(inode);
4414 struct nfs4_exception exception = { };
4415 int err;
4416 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004417 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 switch (err) {
4419 case -NFS4ERR_STALE_STATEID:
4420 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 case 0:
4422 return 0;
4423 }
4424 err = nfs4_handle_exception(server, err, &exception);
4425 } while (exception.retry);
4426 return err;
4427}
4428
4429#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4430#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4431
4432/*
4433 * sleep, with exponential backoff, and retry the LOCK operation.
4434 */
4435static unsigned long
4436nfs4_set_lock_task_retry(unsigned long timeout)
4437{
Jeff Laytond3103102011-12-01 22:44:39 +01004438 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 timeout <<= 1;
4440 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4441 return NFS4_LOCK_MAXTIMEOUT;
4442 return timeout;
4443}
4444
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4446{
4447 struct inode *inode = state->inode;
4448 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004449 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004450 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004452 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004454 struct nfs_lockt_res res = {
4455 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 };
4457 struct rpc_message msg = {
4458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4459 .rpc_argp = &arg,
4460 .rpc_resp = &res,
4461 .rpc_cred = state->owner->so_cred,
4462 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 struct nfs4_lock_state *lsp;
4464 int status;
4465
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004466 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004467 status = nfs4_set_lock_state(state, request);
4468 if (status != 0)
4469 goto out;
4470 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004471 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004472 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004473 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004474 switch (status) {
4475 case 0:
4476 request->fl_type = F_UNLCK;
4477 break;
4478 case -NFS4ERR_DENIED:
4479 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004481 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004482out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 return status;
4484}
4485
4486static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4487{
4488 struct nfs4_exception exception = { };
4489 int err;
4490
4491 do {
4492 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4493 _nfs4_proc_getlk(state, cmd, request),
4494 &exception);
4495 } while (exception.retry);
4496 return err;
4497}
4498
4499static int do_vfs_lock(struct file *file, struct file_lock *fl)
4500{
4501 int res = 0;
4502 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4503 case FL_POSIX:
4504 res = posix_lock_file_wait(file, fl);
4505 break;
4506 case FL_FLOCK:
4507 res = flock_lock_file_wait(file, fl);
4508 break;
4509 default:
4510 BUG();
4511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return res;
4513}
4514
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004515struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004516 struct nfs_locku_args arg;
4517 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004518 struct nfs4_lock_state *lsp;
4519 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004520 struct file_lock fl;
4521 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004522 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004523};
4524
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004525static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4526 struct nfs_open_context *ctx,
4527 struct nfs4_lock_state *lsp,
4528 struct nfs_seqid *seqid)
4529{
4530 struct nfs4_unlockdata *p;
4531 struct inode *inode = lsp->ls_state->inode;
4532
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004533 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004534 if (p == NULL)
4535 return NULL;
4536 p->arg.fh = NFS_FH(inode);
4537 p->arg.fl = &p->fl;
4538 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004539 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004540 p->arg.stateid = &lsp->ls_stateid;
4541 p->lsp = lsp;
4542 atomic_inc(&lsp->ls_count);
4543 /* Ensure we don't close file until we're done freeing locks! */
4544 p->ctx = get_nfs_open_context(ctx);
4545 memcpy(&p->fl, fl, sizeof(p->fl));
4546 p->server = NFS_SERVER(inode);
4547 return p;
4548}
4549
Trond Myklebust06f814a2006-01-03 09:55:07 +01004550static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004551{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004552 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004553 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004554 nfs4_put_lock_state(calldata->lsp);
4555 put_nfs_open_context(calldata->ctx);
4556 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004557}
4558
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004559static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004560{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004561 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004562
Trond Myklebust14516c32010-07-31 14:29:06 -04004563 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4564 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004565 switch (task->tk_status) {
4566 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004567 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4568 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004569 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004570 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004571 case -NFS4ERR_BAD_STATEID:
4572 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004573 case -NFS4ERR_STALE_STATEID:
4574 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004575 break;
4576 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004577 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004578 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004579 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004580 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004581}
4582
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004583static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004584{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004585 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004587 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004588 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004589 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004590 /* Note: exit _without_ running nfs4_locku_done */
4591 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004592 return;
4593 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004594 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004595 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004596 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004597 &calldata->res.seq_res,
4598 task) != 0)
4599 nfs_release_seqid(calldata->arg.seqid);
4600 else
4601 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602}
4603
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004604static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004605 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004606 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004607 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004608};
4609
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4611 struct nfs_open_context *ctx,
4612 struct nfs4_lock_state *lsp,
4613 struct nfs_seqid *seqid)
4614{
4615 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004616 struct rpc_message msg = {
4617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4618 .rpc_cred = ctx->cred,
4619 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004620 struct rpc_task_setup task_setup_data = {
4621 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004622 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004623 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004624 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004625 .flags = RPC_TASK_ASYNC,
4626 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004627
Frank Filz137d6ac2007-07-09 15:32:29 -07004628 /* Ensure this is an unlock - when canceling a lock, the
4629 * canceled lock is passed in, and it won't be an unlock.
4630 */
4631 fl->fl_type = F_UNLCK;
4632
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004633 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4634 if (data == NULL) {
4635 nfs_free_seqid(seqid);
4636 return ERR_PTR(-ENOMEM);
4637 }
4638
Trond Myklebust9d12b212012-01-17 22:04:25 -05004639 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004640 msg.rpc_argp = &data->arg;
4641 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004642 task_setup_data.callback_data = data;
4643 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004644}
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4647{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004648 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004649 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004650 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004651 struct rpc_task *task;
4652 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004653 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Trond Myklebust9b073572006-06-29 16:38:34 -04004655 status = nfs4_set_lock_state(state, request);
4656 /* Unlock _before_ we do the RPC call */
4657 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004658 down_read(&nfsi->rwsem);
4659 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4660 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004661 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004662 }
4663 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004664 if (status != 0)
4665 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004666 /* Is this a delegated lock? */
4667 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004668 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004669 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004670 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004671 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004672 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004673 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004674 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675 status = PTR_ERR(task);
4676 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004677 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004679 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004680out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004681 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004682 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004685struct nfs4_lockdata {
4686 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004687 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688 struct nfs4_lock_state *lsp;
4689 struct nfs_open_context *ctx;
4690 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004691 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004692 int rpc_status;
4693 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004694 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695};
4696
4697static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004698 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4699 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004700{
4701 struct nfs4_lockdata *p;
4702 struct inode *inode = lsp->ls_state->inode;
4703 struct nfs_server *server = NFS_SERVER(inode);
4704
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004705 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004706 if (p == NULL)
4707 return NULL;
4708
4709 p->arg.fh = NFS_FH(inode);
4710 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004711 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004712 if (p->arg.open_seqid == NULL)
4713 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004714 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004715 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004716 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004718 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004719 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004720 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004721 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004722 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004723 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004724 atomic_inc(&lsp->ls_count);
4725 p->ctx = get_nfs_open_context(ctx);
4726 memcpy(&p->fl, fl, sizeof(p->fl));
4727 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004728out_free_seqid:
4729 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730out_free:
4731 kfree(p);
4732 return NULL;
4733}
4734
4735static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4736{
4737 struct nfs4_lockdata *data = calldata;
4738 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Harvey Harrison3110ff82008-05-02 13:42:44 -07004740 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004741 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4742 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004743 /* Do we need to do an open_to_lock_owner? */
4744 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004745 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004746 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004747 data->arg.open_stateid = &state->stateid;
4748 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004749 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004750 } else
4751 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004752 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004753 if (nfs4_setup_sequence(data->server,
4754 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004755 &data->res.seq_res,
4756 task) == 0) {
4757 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004758 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004759 }
4760 nfs_release_seqid(data->arg.open_seqid);
4761out_release_lock_seqid:
4762 nfs_release_seqid(data->arg.lock_seqid);
4763 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764}
4765
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004766static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4767{
4768 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4769 nfs4_lock_prepare(task, calldata);
4770}
4771
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004772static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4773{
4774 struct nfs4_lockdata *data = calldata;
4775
Harvey Harrison3110ff82008-05-02 13:42:44 -07004776 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004777
Trond Myklebust14516c32010-07-31 14:29:06 -04004778 if (!nfs4_sequence_done(task, &data->res.seq_res))
4779 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004780
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004781 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004782 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004783 if (data->rpc_status == 0)
4784 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4785 else
4786 goto out;
4787 }
4788 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004789 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004790 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004791 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004792 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004793out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004794 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004795}
4796
4797static void nfs4_lock_release(void *calldata)
4798{
4799 struct nfs4_lockdata *data = calldata;
4800
Harvey Harrison3110ff82008-05-02 13:42:44 -07004801 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004802 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004803 if (data->cancelled != 0) {
4804 struct rpc_task *task;
4805 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4806 data->arg.lock_seqid);
4807 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004808 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004809 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004810 } else
4811 nfs_free_seqid(data->arg.lock_seqid);
4812 nfs4_put_lock_state(data->lsp);
4813 put_nfs_open_context(data->ctx);
4814 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004815 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004816}
4817
4818static const struct rpc_call_ops nfs4_lock_ops = {
4819 .rpc_call_prepare = nfs4_lock_prepare,
4820 .rpc_call_done = nfs4_lock_done,
4821 .rpc_release = nfs4_lock_release,
4822};
4823
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004824static const struct rpc_call_ops nfs4_recover_lock_ops = {
4825 .rpc_call_prepare = nfs4_recover_lock_prepare,
4826 .rpc_call_done = nfs4_lock_done,
4827 .rpc_release = nfs4_lock_release,
4828};
4829
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004830static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4831{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004832 switch (error) {
4833 case -NFS4ERR_ADMIN_REVOKED:
4834 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004835 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004836 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004837 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004838 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004839 break;
4840 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004841 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004842 case -NFS4ERR_EXPIRED:
4843 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004844 };
4845}
4846
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004847static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004848{
4849 struct nfs4_lockdata *data;
4850 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004851 struct rpc_message msg = {
4852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4853 .rpc_cred = state->owner->so_cred,
4854 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004855 struct rpc_task_setup task_setup_data = {
4856 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004857 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004858 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004859 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004860 .flags = RPC_TASK_ASYNC,
4861 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004862 int ret;
4863
Harvey Harrison3110ff82008-05-02 13:42:44 -07004864 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004865 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004866 fl->fl_u.nfs4_fl.owner,
4867 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004868 if (data == NULL)
4869 return -ENOMEM;
4870 if (IS_SETLKW(cmd))
4871 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004872 if (recovery_type > NFS_LOCK_NEW) {
4873 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004874 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004875 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4876 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004877 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004878 msg.rpc_argp = &data->arg;
4879 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004880 task_setup_data.callback_data = data;
4881 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004882 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004883 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004884 ret = nfs4_wait_for_completion_rpc_task(task);
4885 if (ret == 0) {
4886 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004887 if (ret)
4888 nfs4_handle_setlk_error(data->server, data->lsp,
4889 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004890 } else
4891 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004892 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004893 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004894 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895}
4896
4897static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4898{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004899 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004900 struct nfs4_exception exception = {
4901 .inode = state->inode,
4902 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004903 int err;
4904
4905 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004906 /* Cache the lock if possible... */
4907 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4908 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004909 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004910 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004911 break;
4912 nfs4_handle_exception(server, err, &exception);
4913 } while (exception.retry);
4914 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915}
4916
4917static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4918{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004919 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004920 struct nfs4_exception exception = {
4921 .inode = state->inode,
4922 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004923 int err;
4924
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004925 err = nfs4_set_lock_state(state, request);
4926 if (err != 0)
4927 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004928 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004929 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4930 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004931 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004932 switch (err) {
4933 default:
4934 goto out;
4935 case -NFS4ERR_GRACE:
4936 case -NFS4ERR_DELAY:
4937 nfs4_handle_exception(server, err, &exception);
4938 err = 0;
4939 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004940 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004941out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004942 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943}
4944
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004945#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004946/**
4947 * nfs41_check_expired_locks - possibly free a lock stateid
4948 *
4949 * @state: NFSv4 state for an inode
4950 *
4951 * Returns NFS_OK if recovery for this stateid is now finished.
4952 * Otherwise a negative NFS4ERR value is returned.
4953 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004954static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004955{
Chuck Levereb64cf92012-07-11 16:30:05 -04004956 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004957 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004958 struct nfs_server *server = NFS_SERVER(state->inode);
4959
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004960 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004961 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004962 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4963 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004964 /* Free the stateid unless the server
4965 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004966 if (status != -NFS4ERR_BAD_STATEID)
4967 nfs41_free_stateid(server,
4968 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004969 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004970 ret = status;
4971 }
4972 }
4973 };
4974
4975 return ret;
4976}
4977
4978static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4979{
4980 int status = NFS_OK;
4981
4982 if (test_bit(LK_STATE_IN_USE, &state->flags))
4983 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004984 if (status != NFS_OK)
4985 status = nfs4_lock_expired(state, request);
4986 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004987}
4988#endif
4989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4991{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004992 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004993 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004994 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004996 if ((fl_flags & FL_POSIX) &&
4997 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4998 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004999 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005000 status = nfs4_set_lock_state(state, request);
5001 if (status != 0)
5002 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005003 request->fl_flags |= FL_ACCESS;
5004 status = do_vfs_lock(request->fl_file, request);
5005 if (status < 0)
5006 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005007 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005008 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005009 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005010 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005011 request->fl_flags = fl_flags & ~FL_SLEEP;
5012 status = do_vfs_lock(request->fl_file, request);
5013 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005014 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005015 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005016 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04005017 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005018 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005019 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005020 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005021 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5022 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005023out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005024 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005025out:
5026 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 return status;
5028}
5029
5030static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5031{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005032 struct nfs4_exception exception = {
5033 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005034 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005035 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 int err;
5037
5038 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005039 err = _nfs4_proc_setlk(state, cmd, request);
5040 if (err == -NFS4ERR_DENIED)
5041 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005043 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 } while (exception.retry);
5045 return err;
5046}
5047
5048static int
5049nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5050{
5051 struct nfs_open_context *ctx;
5052 struct nfs4_state *state;
5053 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5054 int status;
5055
5056 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005057 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 state = ctx->state;
5059
5060 if (request->fl_start < 0 || request->fl_end < 0)
5061 return -EINVAL;
5062
Trond Myklebustd9531262009-07-21 19:22:38 -04005063 if (IS_GETLK(cmd)) {
5064 if (state != NULL)
5065 return nfs4_proc_getlk(state, F_GETLK, request);
5066 return 0;
5067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
5069 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5070 return -EINVAL;
5071
Trond Myklebustd9531262009-07-21 19:22:38 -04005072 if (request->fl_type == F_UNLCK) {
5073 if (state != NULL)
5074 return nfs4_proc_unlck(state, cmd, request);
5075 return 0;
5076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
Trond Myklebustd9531262009-07-21 19:22:38 -04005078 if (state == NULL)
5079 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005080 /*
5081 * Don't rely on the VFS having checked the file open mode,
5082 * since it won't do this for flock() locks.
5083 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005084 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005085 case F_RDLCK:
5086 if (!(filp->f_mode & FMODE_READ))
5087 return -EBADF;
5088 break;
5089 case F_WRLCK:
5090 if (!(filp->f_mode & FMODE_WRITE))
5091 return -EBADF;
5092 }
5093
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 do {
5095 status = nfs4_proc_setlk(state, cmd, request);
5096 if ((status != -EAGAIN) || IS_SETLK(cmd))
5097 break;
5098 timeout = nfs4_set_lock_task_retry(timeout);
5099 status = -ERESTARTSYS;
5100 if (signalled())
5101 break;
5102 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 return status;
5104}
5105
Trond Myklebust888e6942005-11-04 15:38:11 -05005106int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5107{
5108 struct nfs_server *server = NFS_SERVER(state->inode);
5109 struct nfs4_exception exception = { };
5110 int err;
5111
5112 err = nfs4_set_lock_state(state, fl);
5113 if (err != 0)
5114 goto out;
5115 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005116 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005117 switch (err) {
5118 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005119 printk(KERN_ERR "NFS: %s: unhandled error "
5120 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005121 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005122 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005123 goto out;
5124 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005125 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005126 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005127 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005128 nfs4_schedule_lease_recovery(server->nfs_client);
5129 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005130 case -NFS4ERR_BADSESSION:
5131 case -NFS4ERR_BADSLOT:
5132 case -NFS4ERR_BAD_HIGH_SLOT:
5133 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5134 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005135 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005136 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005137 case -ERESTARTSYS:
5138 /*
5139 * The show must go on: exit, but mark the
5140 * stateid as needing recovery.
5141 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005142 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005143 case -NFS4ERR_ADMIN_REVOKED:
5144 case -NFS4ERR_BAD_STATEID:
5145 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005146 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005147 err = 0;
5148 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005149 case -EKEYEXPIRED:
5150 /*
5151 * User RPCSEC_GSS context has expired.
5152 * We cannot recover this stateid now, so
5153 * skip it and allow recovery thread to
5154 * proceed.
5155 */
5156 err = 0;
5157 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005158 case -ENOMEM:
5159 case -NFS4ERR_DENIED:
5160 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5161 err = 0;
5162 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005163 case -NFS4ERR_DELAY:
5164 break;
5165 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005166 err = nfs4_handle_exception(server, err, &exception);
5167 } while (exception.retry);
5168out:
5169 return err;
5170}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005171
Trond Myklebustcf470c32012-03-07 13:49:12 -05005172struct nfs_release_lockowner_data {
5173 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005174 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005175 struct nfs_release_lockowner_args args;
5176};
5177
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005178static void nfs4_release_lockowner_release(void *calldata)
5179{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005180 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005181 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005182 kfree(calldata);
5183}
5184
Trond Myklebust17280172012-03-11 13:11:00 -04005185static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005186 .rpc_release = nfs4_release_lockowner_release,
5187};
5188
Trond Myklebustcf470c32012-03-07 13:49:12 -05005189int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005190{
5191 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005192 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005193 struct rpc_message msg = {
5194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5195 };
5196
5197 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005198 return -EINVAL;
5199 data = kmalloc(sizeof(*data), GFP_NOFS);
5200 if (!data)
5201 return -ENOMEM;
5202 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005203 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005204 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5205 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5206 data->args.lock_owner.s_dev = server->s_dev;
5207 msg.rpc_argp = &data->args;
5208 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5209 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005210}
5211
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005212#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5213
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005214static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5215 const void *buf, size_t buflen,
5216 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005217{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005218 if (strcmp(key, "") != 0)
5219 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005220
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005221 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005222}
5223
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005224static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5225 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005226{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005227 if (strcmp(key, "") != 0)
5228 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005229
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005230 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005231}
5232
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005233static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5234 size_t list_len, const char *name,
5235 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005236{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005237 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005238
J. Bruce Fields096455a2006-03-20 23:23:42 -05005239 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5240 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005241
5242 if (list && len <= list_len)
5243 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005244 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005245}
5246
Andy Adamson533eb462011-06-13 18:25:56 -04005247/*
5248 * nfs_fhget will use either the mounted_on_fileid or the fileid
5249 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005250static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5251{
Andy Adamson533eb462011-06-13 18:25:56 -04005252 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5253 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5254 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005255 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005256 return;
5257
5258 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005259 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005260 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5261 fattr->nlink = 2;
5262}
5263
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005264static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5265 const struct qstr *name,
5266 struct nfs4_fs_locations *fs_locations,
5267 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005268{
5269 struct nfs_server *server = NFS_SERVER(dir);
5270 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005271 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005272 };
5273 struct nfs4_fs_locations_arg args = {
5274 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005275 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005276 .page = page,
5277 .bitmask = bitmask,
5278 };
Benny Halevy22958462009-04-01 09:22:02 -04005279 struct nfs4_fs_locations_res res = {
5280 .fs_locations = fs_locations,
5281 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005282 struct rpc_message msg = {
5283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5284 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005285 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005286 };
5287 int status;
5288
Harvey Harrison3110ff82008-05-02 13:42:44 -07005289 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005290
5291 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5292 * is not supported */
5293 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5294 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5295 else
5296 bitmask[0] |= FATTR4_WORD0_FILEID;
5297
Trond Myklebustc228fd32007-01-13 02:28:11 -05005298 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005299 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005300 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005301 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005302 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005303 return status;
5304}
5305
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005306int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5307 const struct qstr *name,
5308 struct nfs4_fs_locations *fs_locations,
5309 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005310{
5311 struct nfs4_exception exception = { };
5312 int err;
5313 do {
5314 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005315 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005316 &exception);
5317 } while (exception.retry);
5318 return err;
5319}
5320
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005321static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5322{
5323 int status;
5324 struct nfs4_secinfo_arg args = {
5325 .dir_fh = NFS_FH(dir),
5326 .name = name,
5327 };
5328 struct nfs4_secinfo_res res = {
5329 .flavors = flavors,
5330 };
5331 struct rpc_message msg = {
5332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5333 .rpc_argp = &args,
5334 .rpc_resp = &res,
5335 };
5336
5337 dprintk("NFS call secinfo %s\n", name->name);
5338 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5339 dprintk("NFS reply secinfo: %d\n", status);
5340 return status;
5341}
5342
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005343int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5344 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005345{
5346 struct nfs4_exception exception = { };
5347 int err;
5348 do {
5349 err = nfs4_handle_exception(NFS_SERVER(dir),
5350 _nfs4_proc_secinfo(dir, name, flavors),
5351 &exception);
5352 } while (exception.retry);
5353 return err;
5354}
5355
Andy Adamson557134a2009-04-01 09:21:53 -04005356#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005357/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005358 * Check the exchange flags returned by the server for invalid flags, having
5359 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5360 * DS flags set.
5361 */
5362static int nfs4_check_cl_exchange_flags(u32 flags)
5363{
5364 if (flags & ~EXCHGID4_FLAG_MASK_R)
5365 goto out_inval;
5366 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5367 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5368 goto out_inval;
5369 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5370 goto out_inval;
5371 return NFS_OK;
5372out_inval:
5373 return -NFS4ERR_INVAL;
5374}
5375
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005376static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005377nfs41_same_server_scope(struct nfs41_server_scope *a,
5378 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005379{
5380 if (a->server_scope_sz == b->server_scope_sz &&
5381 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5382 return true;
5383
5384 return false;
5385}
5386
Andy Adamson357f54d2010-12-14 10:11:57 -05005387/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005388 * nfs4_proc_bind_conn_to_session()
5389 *
5390 * The 4.1 client currently uses the same TCP connection for the
5391 * fore and backchannel.
5392 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005393int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005394{
5395 int status;
5396 struct nfs41_bind_conn_to_session_res res;
5397 struct rpc_message msg = {
5398 .rpc_proc =
5399 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5400 .rpc_argp = clp,
5401 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005402 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005403 };
5404
5405 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005406
5407 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5408 if (unlikely(res.session == NULL)) {
5409 status = -ENOMEM;
5410 goto out;
5411 }
5412
5413 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5414 if (status == 0) {
5415 if (memcmp(res.session->sess_id.data,
5416 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5417 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5418 status = -EIO;
5419 goto out_session;
5420 }
5421 if (res.dir != NFS4_CDFS4_BOTH) {
5422 dprintk("NFS: %s: Unexpected direction from server\n",
5423 __func__);
5424 status = -EIO;
5425 goto out_session;
5426 }
5427 if (res.use_conn_in_rdma_mode) {
5428 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5429 __func__);
5430 status = -EIO;
5431 goto out_session;
5432 }
5433 }
5434out_session:
5435 kfree(res.session);
5436out:
5437 dprintk("<-- %s status= %d\n", __func__, status);
5438 return status;
5439}
5440
5441/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005442 * nfs4_proc_exchange_id()
5443 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005444 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5445 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005446 * Since the clientid has expired, all compounds using sessions
5447 * associated with the stale clientid will be returning
5448 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5449 * be in some phase of session reset.
5450 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005451int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005452{
5453 nfs4_verifier verifier;
5454 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005455 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005456 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005457 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005458 };
5459 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005460 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005461 };
5462 int status;
5463 struct rpc_message msg = {
5464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5465 .rpc_argp = &args,
5466 .rpc_resp = &res,
5467 .rpc_cred = cred,
5468 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005469
Chuck Leverf0920752012-05-21 22:45:41 -04005470 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005471 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5472 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005473 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5474 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5475 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005476
Chuck Leveracdeb692012-05-21 22:46:16 -04005477 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005478 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005479 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005480 status = -ENOMEM;
5481 goto out;
5482 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005483
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005484 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005485 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005486 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005487 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005488 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005489 }
5490
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005491 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005492 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005493 status = -ENOMEM;
5494 goto out_server_scope;
5495 }
5496
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005497 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005498 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005499 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005500
Chuck Lever177313f2012-05-21 22:44:58 -04005501 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005502 clp->cl_clientid = res.clientid;
5503 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5504 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5505 clp->cl_seqid = res.seqid;
5506
Chuck Leveracdeb692012-05-21 22:46:16 -04005507 kfree(clp->cl_serverowner);
5508 clp->cl_serverowner = res.server_owner;
5509 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005510
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005511 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005512 kfree(clp->cl_implid);
5513 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005514
Chuck Lever177313f2012-05-21 22:44:58 -04005515 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005516 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005517 res.server_scope)) {
5518 dprintk("%s: server_scope mismatch detected\n",
5519 __func__);
5520 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005521 kfree(clp->cl_serverscope);
5522 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005523 }
5524
Chuck Lever177313f2012-05-21 22:44:58 -04005525 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005526 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005527 goto out;
5528 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005529 } else
5530 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005531
Chuck Leveracdeb692012-05-21 22:46:16 -04005532out_server_owner:
5533 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005534out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005535 kfree(res.server_scope);
5536out:
Chuck Lever177313f2012-05-21 22:44:58 -04005537 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005538 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005539 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005540 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005541 clp->cl_implid->date.seconds,
5542 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005543 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005544 return status;
5545}
5546
Trond Myklebust66245532012-05-25 17:18:09 -04005547static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5548 struct rpc_cred *cred)
5549{
5550 struct rpc_message msg = {
5551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5552 .rpc_argp = clp,
5553 .rpc_cred = cred,
5554 };
5555 int status;
5556
5557 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5558 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005559 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005560 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5561 return status;
5562}
5563
5564static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5565 struct rpc_cred *cred)
5566{
5567 unsigned int loop;
5568 int ret;
5569
5570 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5571 ret = _nfs4_proc_destroy_clientid(clp, cred);
5572 switch (ret) {
5573 case -NFS4ERR_DELAY:
5574 case -NFS4ERR_CLIENTID_BUSY:
5575 ssleep(1);
5576 break;
5577 default:
5578 return ret;
5579 }
5580 }
5581 return 0;
5582}
5583
5584int nfs4_destroy_clientid(struct nfs_client *clp)
5585{
5586 struct rpc_cred *cred;
5587 int ret = 0;
5588
5589 if (clp->cl_mvops->minor_version < 1)
5590 goto out;
5591 if (clp->cl_exchange_flags == 0)
5592 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005593 if (clp->cl_preserve_clid)
5594 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005595 cred = nfs4_get_exchange_id_cred(clp);
5596 ret = nfs4_proc_destroy_clientid(clp, cred);
5597 if (cred)
5598 put_rpccred(cred);
5599 switch (ret) {
5600 case 0:
5601 case -NFS4ERR_STALE_CLIENTID:
5602 clp->cl_exchange_flags = 0;
5603 }
5604out:
5605 return ret;
5606}
5607
Andy Adamson2050f0c2009-04-01 09:22:30 -04005608struct nfs4_get_lease_time_data {
5609 struct nfs4_get_lease_time_args *args;
5610 struct nfs4_get_lease_time_res *res;
5611 struct nfs_client *clp;
5612};
5613
5614static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5615 void *calldata)
5616{
5617 int ret;
5618 struct nfs4_get_lease_time_data *data =
5619 (struct nfs4_get_lease_time_data *)calldata;
5620
5621 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005622 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005623 /* just setup sequence, do not trigger session recovery
5624 since we're invoked within one */
5625 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005626 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005627 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005628
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005629 if (ret != -EAGAIN)
5630 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005631 dprintk("<-- %s\n", __func__);
5632}
5633
5634/*
5635 * Called from nfs4_state_manager thread for session setup, so don't recover
5636 * from sequence operation or clientid errors.
5637 */
5638static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5639{
5640 struct nfs4_get_lease_time_data *data =
5641 (struct nfs4_get_lease_time_data *)calldata;
5642
5643 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005644 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5645 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005646 switch (task->tk_status) {
5647 case -NFS4ERR_DELAY:
5648 case -NFS4ERR_GRACE:
5649 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5650 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5651 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005652 /* fall through */
5653 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005654 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005655 return;
5656 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005657 dprintk("<-- %s\n", __func__);
5658}
5659
Trond Myklebust17280172012-03-11 13:11:00 -04005660static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005661 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5662 .rpc_call_done = nfs4_get_lease_time_done,
5663};
5664
5665int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5666{
5667 struct rpc_task *task;
5668 struct nfs4_get_lease_time_args args;
5669 struct nfs4_get_lease_time_res res = {
5670 .lr_fsinfo = fsinfo,
5671 };
5672 struct nfs4_get_lease_time_data data = {
5673 .args = &args,
5674 .res = &res,
5675 .clp = clp,
5676 };
5677 struct rpc_message msg = {
5678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5679 .rpc_argp = &args,
5680 .rpc_resp = &res,
5681 };
5682 struct rpc_task_setup task_setup = {
5683 .rpc_client = clp->cl_rpcclient,
5684 .rpc_message = &msg,
5685 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005686 .callback_data = &data,
5687 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005688 };
5689 int status;
5690
Trond Myklebust9d12b212012-01-17 22:04:25 -05005691 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005692 dprintk("--> %s\n", __func__);
5693 task = rpc_run_task(&task_setup);
5694
5695 if (IS_ERR(task))
5696 status = PTR_ERR(task);
5697 else {
5698 status = task->tk_status;
5699 rpc_put_task(task);
5700 }
5701 dprintk("<-- %s return %d\n", __func__, status);
5702
5703 return status;
5704}
5705
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005706struct nfs4_slot *nfs4_alloc_slots(struct nfs4_slot_table *table,
5707 u32 max_slots, gfp_t gfp_flags)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005708{
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005709 struct nfs4_slot *tbl;
5710 u32 i;
5711
5712 tbl = kmalloc_array(max_slots, sizeof(*tbl), gfp_flags);
5713 if (tbl != NULL) {
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005714 for (i = 0; i < max_slots; i++) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005715 tbl[i].table = table;
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005716 tbl[i].slot_nr = i;
5717 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005718 }
5719 return tbl;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005720}
5721
5722static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5723 struct nfs4_slot *new,
5724 u32 max_slots,
5725 u32 ivalue)
5726{
5727 struct nfs4_slot *old = NULL;
5728 u32 i;
5729
5730 spin_lock(&tbl->slot_tbl_lock);
5731 if (new) {
5732 old = tbl->slots;
5733 tbl->slots = new;
5734 tbl->max_slots = max_slots;
5735 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005736 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust464ee9f2012-11-20 12:49:27 -05005737 tbl->target_highest_slotid = max_slots - 1;
Trond Myklebustda0507b2012-11-20 18:10:30 -05005738 tbl->server_highest_slotid = max_slots - 1;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005739 for (i = 0; i < tbl->max_slots; i++)
5740 tbl->slots[i].seq_nr = ivalue;
5741 spin_unlock(&tbl->slot_tbl_lock);
5742 kfree(old);
5743}
5744
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005745/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005746 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005747 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005748static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5749 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005750{
Andy Adamson104aeba2010-01-14 17:45:10 -05005751 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005752 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005753
Andy Adamson104aeba2010-01-14 17:45:10 -05005754 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5755 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005756
Andy Adamson104aeba2010-01-14 17:45:10 -05005757 /* Does the newly negotiated max_reqs match the existing slot table? */
5758 if (max_reqs != tbl->max_slots) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005759 new = nfs4_alloc_slots(tbl, max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005760 if (!new)
5761 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005762 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005763 ret = 0;
5764
5765 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005766 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5767 tbl, tbl->slots, tbl->max_slots);
5768out:
5769 dprintk("<-- %s: return %d\n", __func__, ret);
5770 return ret;
5771}
5772
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005773/* Destroy the slot table */
5774static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5775{
5776 if (session->fc_slot_table.slots != NULL) {
5777 kfree(session->fc_slot_table.slots);
5778 session->fc_slot_table.slots = NULL;
5779 }
5780 if (session->bc_slot_table.slots != NULL) {
5781 kfree(session->bc_slot_table.slots);
5782 session->bc_slot_table.slots = NULL;
5783 }
5784 return;
5785}
5786
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005787/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005788 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005789 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005790static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005791{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005792 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005793 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005794
Andy Adamsonaacd5532011-11-09 13:58:21 -05005795 dprintk("--> %s\n", __func__);
5796 /* Fore channel */
5797 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005798 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005799 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5800 if (status) /* -ENOMEM */
5801 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005802 /* Back channel */
5803 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005804 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005805 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5806 if (status && tbl->slots == NULL)
5807 /* Fore and back channel share a connection so get
5808 * both slot tables or neither */
5809 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005810 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005811}
5812
5813struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5814{
5815 struct nfs4_session *session;
5816 struct nfs4_slot_table *tbl;
5817
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005818 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005819 if (!session)
5820 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005821
Andy Adamson557134a2009-04-01 09:21:53 -04005822 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005823 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005824 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005825 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005826 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005827
5828 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005829 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005830 spin_lock_init(&tbl->slot_tbl_lock);
5831 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005832 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005833
Trond Myklebust1055d762010-06-16 09:52:27 -04005834 session->session_state = 1<<NFS4_SESSION_INITING;
5835
Andy Adamson557134a2009-04-01 09:21:53 -04005836 session->clp = clp;
5837 return session;
5838}
5839
5840void nfs4_destroy_session(struct nfs4_session *session)
5841{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005842 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005843 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005844
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005845 cred = nfs4_get_exchange_id_cred(session->clp);
5846 nfs4_proc_destroy_session(session, cred);
5847 if (cred)
5848 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005849
5850 rcu_read_lock();
5851 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5852 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005853 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005854 __func__, xprt);
5855 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005856 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005857 kfree(session);
5858}
5859
Andy Adamsonfc931582009-04-01 09:22:31 -04005860/*
5861 * Initialize the values to be used by the client in CREATE_SESSION
5862 * If nfs4_init_session set the fore channel request and response sizes,
5863 * use them.
5864 *
5865 * Set the back channel max_resp_sz_cached to zero to force the client to
5866 * always set csa_cachethis to FALSE because the current implementation
5867 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5868 */
5869static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5870{
5871 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005872 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5873 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005874
5875 if (mxrqst_sz == 0)
5876 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5877 if (mxresp_sz == 0)
5878 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5879 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005880 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5881 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005882 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005883 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005884
5885 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005886 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005887 __func__,
5888 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005889 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005890
5891 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005892 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5893 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5894 args->bc_attrs.max_resp_sz_cached = 0;
5895 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5896 args->bc_attrs.max_reqs = 1;
5897
5898 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5899 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5900 __func__,
5901 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5902 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5903 args->bc_attrs.max_reqs);
5904}
5905
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005906static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005907{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005908 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5909 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5910
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005911 if (rcvd->max_resp_sz > sent->max_resp_sz)
5912 return -EINVAL;
5913 /*
5914 * Our requested max_ops is the minimum we need; we're not
5915 * prepared to break up compounds into smaller pieces than that.
5916 * So, no point even trying to continue if the server won't
5917 * cooperate:
5918 */
5919 if (rcvd->max_ops < sent->max_ops)
5920 return -EINVAL;
5921 if (rcvd->max_reqs == 0)
5922 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005923 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5924 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005925 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005926}
5927
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005928static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5929{
5930 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5931 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005932
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005933 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5934 return -EINVAL;
5935 if (rcvd->max_resp_sz < sent->max_resp_sz)
5936 return -EINVAL;
5937 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5938 return -EINVAL;
5939 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005940 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005941 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005942 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005943 return -EINVAL;
5944 return 0;
5945}
Andy Adamson8d353012009-04-01 09:22:32 -04005946
Andy Adamson8d353012009-04-01 09:22:32 -04005947static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5948 struct nfs4_session *session)
5949{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005950 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005951
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005952 ret = nfs4_verify_fore_channel_attrs(args, session);
5953 if (ret)
5954 return ret;
5955 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005956}
5957
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005958static int _nfs4_proc_create_session(struct nfs_client *clp,
5959 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005960{
5961 struct nfs4_session *session = clp->cl_session;
5962 struct nfs41_create_session_args args = {
5963 .client = clp,
5964 .cb_program = NFS4_CALLBACK,
5965 };
5966 struct nfs41_create_session_res res = {
5967 .client = clp,
5968 };
5969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5971 .rpc_argp = &args,
5972 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005973 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005974 };
5975 int status;
5976
5977 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005978 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005979
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005980 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005981
Trond Myklebust43095d32012-11-20 11:13:12 -05005982 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005983 /* Verify the session's negotiated channel_attrs values */
5984 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005985 /* Increment the clientid slot sequence id */
5986 clp->cl_seqid++;
5987 }
5988
5989 return status;
5990}
5991
5992/*
5993 * Issues a CREATE_SESSION operation to the server.
5994 * It is the responsibility of the caller to verify the session is
5995 * expired before calling this routine.
5996 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005997int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005998{
5999 int status;
6000 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006001 struct nfs4_session *session = clp->cl_session;
6002
6003 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6004
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006005 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006006 if (status)
6007 goto out;
6008
Andy Adamsonaacd5532011-11-09 13:58:21 -05006009 /* Init or reset the session slot tables */
6010 status = nfs4_setup_session_slot_tables(session);
6011 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006012 if (status)
6013 goto out;
6014
6015 ptr = (unsigned *)&session->sess_id.data[0];
6016 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6017 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006018out:
6019 dprintk("<-- %s\n", __func__);
6020 return status;
6021}
6022
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006023/*
6024 * Issue the over-the-wire RPC DESTROY_SESSION.
6025 * The caller must serialize access to this routine.
6026 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006027int nfs4_proc_destroy_session(struct nfs4_session *session,
6028 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006029{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006030 struct rpc_message msg = {
6031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6032 .rpc_argp = session,
6033 .rpc_cred = cred,
6034 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006035 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006036
6037 dprintk("--> nfs4_proc_destroy_session\n");
6038
6039 /* session is still being setup */
6040 if (session->clp->cl_cons_state != NFS_CS_READY)
6041 return status;
6042
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006043 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006044
6045 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006046 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006047 "Session has been destroyed regardless...\n", status);
6048
6049 dprintk("<-- nfs4_proc_destroy_session\n");
6050 return status;
6051}
6052
Trond Myklebust7b38c362012-05-23 13:23:31 -04006053/*
6054 * With sessions, the client is not marked ready until after a
6055 * successful EXCHANGE_ID and CREATE_SESSION.
6056 *
6057 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6058 * other versions of NFS can be tried.
6059 */
6060static int nfs41_check_session_ready(struct nfs_client *clp)
6061{
6062 int ret;
6063
6064 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6065 ret = nfs4_client_recover_expired_lease(clp);
6066 if (ret)
6067 return ret;
6068 }
6069 if (clp->cl_cons_state < NFS_CS_READY)
6070 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006071 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006072 return 0;
6073}
6074
Trond Myklebustfccba802009-07-21 16:48:07 -04006075int nfs4_init_session(struct nfs_server *server)
6076{
6077 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006078 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006079 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6080 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006081
6082 if (!nfs4_has_session(clp))
6083 return 0;
6084
Trond Myklebustae72ae672012-11-20 11:02:55 -05006085 if (server->rsize != 0)
6086 target_max_resp_sz = server->rsize;
6087 target_max_resp_sz += nfs41_maxread_overhead;
6088
6089 if (server->wsize != 0)
6090 target_max_rqst_sz = server->wsize;
6091 target_max_rqst_sz += nfs41_maxwrite_overhead;
6092
Trond Myklebust1055d762010-06-16 09:52:27 -04006093 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006094 spin_lock(&clp->cl_lock);
6095 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006096 /* Initialise targets and channel attributes */
6097 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6098 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6099 session->fc_target_max_resp_sz = target_max_resp_sz;
6100 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006101 } else {
6102 /* Just adjust the targets */
6103 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6104 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6105 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6106 }
6107 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6108 session->fc_target_max_resp_sz = target_max_resp_sz;
6109 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6110 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006111 }
6112 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006113
Trond Myklebust688a9022012-11-20 10:53:39 -05006114 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6115 nfs4_schedule_lease_recovery(clp);
6116
Trond Myklebust7b38c362012-05-23 13:23:31 -04006117 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006118}
6119
Trond Myklebust7b38c362012-05-23 13:23:31 -04006120int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006121{
6122 struct nfs4_session *session = clp->cl_session;
6123 int ret;
6124
Trond Myklebust7b38c362012-05-23 13:23:31 -04006125 spin_lock(&clp->cl_lock);
6126 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6127 /*
6128 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6129 * DS lease to be equal to the MDS lease.
6130 */
6131 clp->cl_lease_time = lease_time;
6132 clp->cl_last_renewal = jiffies;
6133 }
6134 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006135
Trond Myklebust7b38c362012-05-23 13:23:31 -04006136 ret = nfs41_check_session_ready(clp);
6137 if (ret)
6138 return ret;
6139 /* Test for the DS role */
6140 if (!is_ds_client(clp))
6141 return -ENODEV;
6142 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006143}
6144EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6145
6146
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006147/*
6148 * Renew the cl_session lease.
6149 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006150struct nfs4_sequence_data {
6151 struct nfs_client *clp;
6152 struct nfs4_sequence_args args;
6153 struct nfs4_sequence_res res;
6154};
6155
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006156static void nfs41_sequence_release(void *data)
6157{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006158 struct nfs4_sequence_data *calldata = data;
6159 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006160
Alexandros Batsakis71358402010-02-05 03:45:05 -08006161 if (atomic_read(&clp->cl_count) > 1)
6162 nfs4_schedule_state_renewal(clp);
6163 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006164 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006165}
6166
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006167static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6168{
6169 switch(task->tk_status) {
6170 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006171 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6172 return -EAGAIN;
6173 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006174 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006175 }
6176 return 0;
6177}
6178
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006179static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006180{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006181 struct nfs4_sequence_data *calldata = data;
6182 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006183
Trond Myklebust14516c32010-07-31 14:29:06 -04006184 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6185 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006186
6187 if (task->tk_status < 0) {
6188 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006189 if (atomic_read(&clp->cl_count) == 1)
6190 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006191
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006192 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6193 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006194 return;
6195 }
6196 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006197 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006198out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006199 dprintk("<-- %s\n", __func__);
6200}
6201
6202static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6203{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006204 struct nfs4_sequence_data *calldata = data;
6205 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006206 struct nfs4_sequence_args *args;
6207 struct nfs4_sequence_res *res;
6208
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006209 args = task->tk_msg.rpc_argp;
6210 res = task->tk_msg.rpc_resp;
6211
Trond Myklebust9d12b212012-01-17 22:04:25 -05006212 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006213 return;
6214 rpc_call_start(task);
6215}
6216
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006217static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6218{
6219 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6220 nfs41_sequence_prepare(task, data);
6221}
6222
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006223static const struct rpc_call_ops nfs41_sequence_ops = {
6224 .rpc_call_done = nfs41_sequence_call_done,
6225 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006226 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006227};
6228
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006229static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6230 .rpc_call_done = nfs41_sequence_call_done,
6231 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6232 .rpc_release = nfs41_sequence_release,
6233};
6234
6235static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6236 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006237{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006238 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006239 struct rpc_message msg = {
6240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6241 .rpc_cred = cred,
6242 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006243 struct rpc_task_setup task_setup_data = {
6244 .rpc_client = clp->cl_rpcclient,
6245 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006246 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006247 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6248 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006249
Alexandros Batsakis71358402010-02-05 03:45:05 -08006250 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006251 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006252 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006253 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006254 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006255 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006256 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006257 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006258 msg.rpc_argp = &calldata->args;
6259 msg.rpc_resp = &calldata->res;
6260 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006261 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006262
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006263 return rpc_run_task(&task_setup_data);
6264}
6265
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006266static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006267{
6268 struct rpc_task *task;
6269 int ret = 0;
6270
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006271 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6272 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006273 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006274 if (IS_ERR(task))
6275 ret = PTR_ERR(task);
6276 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006277 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006278 dprintk("<-- %s status=%d\n", __func__, ret);
6279 return ret;
6280}
6281
6282static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6283{
6284 struct rpc_task *task;
6285 int ret;
6286
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006287 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006288 if (IS_ERR(task)) {
6289 ret = PTR_ERR(task);
6290 goto out;
6291 }
6292 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006293 if (!ret) {
6294 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6295
6296 if (task->tk_status == 0)
6297 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006298 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006299 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006300 rpc_put_task(task);
6301out:
6302 dprintk("<-- %s status=%d\n", __func__, ret);
6303 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006304}
6305
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006306struct nfs4_reclaim_complete_data {
6307 struct nfs_client *clp;
6308 struct nfs41_reclaim_complete_args arg;
6309 struct nfs41_reclaim_complete_res res;
6310};
6311
6312static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6313{
6314 struct nfs4_reclaim_complete_data *calldata = data;
6315
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006316 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006317 if (nfs41_setup_sequence(calldata->clp->cl_session,
6318 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006319 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006320 return;
6321
6322 rpc_call_start(task);
6323}
6324
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006325static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6326{
6327 switch(task->tk_status) {
6328 case 0:
6329 case -NFS4ERR_COMPLETE_ALREADY:
6330 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6331 break;
6332 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006333 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006334 /* fall through */
6335 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006336 return -EAGAIN;
6337 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006338 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006339 }
6340 return 0;
6341}
6342
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006343static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6344{
6345 struct nfs4_reclaim_complete_data *calldata = data;
6346 struct nfs_client *clp = calldata->clp;
6347 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6348
6349 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006350 if (!nfs41_sequence_done(task, res))
6351 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006352
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006353 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6354 rpc_restart_call_prepare(task);
6355 return;
6356 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006357 dprintk("<-- %s\n", __func__);
6358}
6359
6360static void nfs4_free_reclaim_complete_data(void *data)
6361{
6362 struct nfs4_reclaim_complete_data *calldata = data;
6363
6364 kfree(calldata);
6365}
6366
6367static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6368 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6369 .rpc_call_done = nfs4_reclaim_complete_done,
6370 .rpc_release = nfs4_free_reclaim_complete_data,
6371};
6372
6373/*
6374 * Issue a global reclaim complete.
6375 */
6376static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6377{
6378 struct nfs4_reclaim_complete_data *calldata;
6379 struct rpc_task *task;
6380 struct rpc_message msg = {
6381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6382 };
6383 struct rpc_task_setup task_setup_data = {
6384 .rpc_client = clp->cl_rpcclient,
6385 .rpc_message = &msg,
6386 .callback_ops = &nfs4_reclaim_complete_call_ops,
6387 .flags = RPC_TASK_ASYNC,
6388 };
6389 int status = -ENOMEM;
6390
6391 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006392 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006393 if (calldata == NULL)
6394 goto out;
6395 calldata->clp = clp;
6396 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006397
Trond Myklebust9d12b212012-01-17 22:04:25 -05006398 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006399 msg.rpc_argp = &calldata->arg;
6400 msg.rpc_resp = &calldata->res;
6401 task_setup_data.callback_data = calldata;
6402 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006403 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006404 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006405 goto out;
6406 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006407 status = nfs4_wait_for_completion_rpc_task(task);
6408 if (status == 0)
6409 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006410 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006411 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006412out:
6413 dprintk("<-- %s status=%d\n", __func__, status);
6414 return status;
6415}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006416
6417static void
6418nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6419{
6420 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006421 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006422
6423 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006424 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6425 * right now covering the LAYOUTGET we are about to send.
6426 * However, that is not so catastrophic, and there seems
6427 * to be no way to prevent it completely.
6428 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006429 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006430 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006431 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006432 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6433 NFS_I(lgp->args.inode)->layout,
6434 lgp->args.ctx->state)) {
6435 rpc_exit(task, NFS4_OK);
6436 return;
6437 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006438 rpc_call_start(task);
6439}
6440
6441static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6442{
6443 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006444 struct inode *inode = lgp->args.inode;
6445 struct nfs_server *server = NFS_SERVER(inode);
6446 struct pnfs_layout_hdr *lo;
6447 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006448
6449 dprintk("--> %s\n", __func__);
6450
6451 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006452 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006453
6454 switch (task->tk_status) {
6455 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006456 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006457 case -NFS4ERR_LAYOUTTRYLATER:
6458 case -NFS4ERR_RECALLCONFLICT:
6459 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006460 break;
6461 case -NFS4ERR_EXPIRED:
6462 case -NFS4ERR_BAD_STATEID:
6463 spin_lock(&inode->i_lock);
6464 lo = NFS_I(inode)->layout;
6465 if (!lo || list_empty(&lo->plh_segs)) {
6466 spin_unlock(&inode->i_lock);
6467 /* If the open stateid was bad, then recover it. */
6468 state = lgp->args.ctx->state;
6469 } else {
6470 LIST_HEAD(head);
6471
6472 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6473 spin_unlock(&inode->i_lock);
6474 /* Mark the bad layout state as invalid, then
6475 * retry using the open stateid. */
6476 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006477 }
6478 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006479 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6480 rpc_restart_call_prepare(task);
6481out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006482 dprintk("<-- %s\n", __func__);
6483}
6484
Idan Kedar85541162012-08-02 11:47:10 +03006485static size_t max_response_pages(struct nfs_server *server)
6486{
6487 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6488 return nfs_page_array_len(0, max_resp_sz);
6489}
6490
6491static void nfs4_free_pages(struct page **pages, size_t size)
6492{
6493 int i;
6494
6495 if (!pages)
6496 return;
6497
6498 for (i = 0; i < size; i++) {
6499 if (!pages[i])
6500 break;
6501 __free_page(pages[i]);
6502 }
6503 kfree(pages);
6504}
6505
6506static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6507{
6508 struct page **pages;
6509 int i;
6510
6511 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6512 if (!pages) {
6513 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6514 return NULL;
6515 }
6516
6517 for (i = 0; i < size; i++) {
6518 pages[i] = alloc_page(gfp_flags);
6519 if (!pages[i]) {
6520 dprintk("%s: failed to allocate page\n", __func__);
6521 nfs4_free_pages(pages, size);
6522 return NULL;
6523 }
6524 }
6525
6526 return pages;
6527}
6528
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006529static void nfs4_layoutget_release(void *calldata)
6530{
6531 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006532 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6533 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006534
6535 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006536 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006537 put_nfs_open_context(lgp->args.ctx);
6538 kfree(calldata);
6539 dprintk("<-- %s\n", __func__);
6540}
6541
6542static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6543 .rpc_call_prepare = nfs4_layoutget_prepare,
6544 .rpc_call_done = nfs4_layoutget_done,
6545 .rpc_release = nfs4_layoutget_release,
6546};
6547
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006548struct pnfs_layout_segment *
6549nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006550{
6551 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006552 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006553 struct rpc_task *task;
6554 struct rpc_message msg = {
6555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6556 .rpc_argp = &lgp->args,
6557 .rpc_resp = &lgp->res,
6558 };
6559 struct rpc_task_setup task_setup_data = {
6560 .rpc_client = server->client,
6561 .rpc_message = &msg,
6562 .callback_ops = &nfs4_layoutget_call_ops,
6563 .callback_data = lgp,
6564 .flags = RPC_TASK_ASYNC,
6565 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006566 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006567 int status = 0;
6568
6569 dprintk("--> %s\n", __func__);
6570
Idan Kedar85541162012-08-02 11:47:10 +03006571 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6572 if (!lgp->args.layout.pages) {
6573 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006574 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006575 }
6576 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6577
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006578 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006579 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006580 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006581 task = rpc_run_task(&task_setup_data);
6582 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006583 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006584 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006585 if (status == 0)
6586 status = task->tk_status;
6587 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006588 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006589 rpc_put_task(task);
6590 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006591 if (status)
6592 return ERR_PTR(status);
6593 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006594}
6595
Benny Halevycbe82602011-05-22 19:52:37 +03006596static void
6597nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6598{
6599 struct nfs4_layoutreturn *lrp = calldata;
6600
6601 dprintk("--> %s\n", __func__);
6602 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006603 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006604 return;
6605 rpc_call_start(task);
6606}
6607
6608static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6609{
6610 struct nfs4_layoutreturn *lrp = calldata;
6611 struct nfs_server *server;
6612
6613 dprintk("--> %s\n", __func__);
6614
6615 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6616 return;
6617
6618 server = NFS_SERVER(lrp->args.inode);
6619 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006620 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006621 return;
6622 }
Benny Halevycbe82602011-05-22 19:52:37 +03006623 dprintk("<-- %s\n", __func__);
6624}
6625
6626static void nfs4_layoutreturn_release(void *calldata)
6627{
6628 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006629 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006630
6631 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006632 spin_lock(&lo->plh_inode->i_lock);
6633 if (lrp->res.lrs_present)
6634 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6635 lo->plh_block_lgets--;
6636 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006637 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006638 kfree(calldata);
6639 dprintk("<-- %s\n", __func__);
6640}
6641
6642static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6643 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6644 .rpc_call_done = nfs4_layoutreturn_done,
6645 .rpc_release = nfs4_layoutreturn_release,
6646};
6647
6648int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6649{
6650 struct rpc_task *task;
6651 struct rpc_message msg = {
6652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6653 .rpc_argp = &lrp->args,
6654 .rpc_resp = &lrp->res,
6655 };
6656 struct rpc_task_setup task_setup_data = {
6657 .rpc_client = lrp->clp->cl_rpcclient,
6658 .rpc_message = &msg,
6659 .callback_ops = &nfs4_layoutreturn_call_ops,
6660 .callback_data = lrp,
6661 };
6662 int status;
6663
6664 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006665 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006666 task = rpc_run_task(&task_setup_data);
6667 if (IS_ERR(task))
6668 return PTR_ERR(task);
6669 status = task->tk_status;
6670 dprintk("<-- %s status=%d\n", __func__, status);
6671 rpc_put_task(task);
6672 return status;
6673}
6674
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006675/*
6676 * Retrieve the list of Data Server devices from the MDS.
6677 */
6678static int _nfs4_getdevicelist(struct nfs_server *server,
6679 const struct nfs_fh *fh,
6680 struct pnfs_devicelist *devlist)
6681{
6682 struct nfs4_getdevicelist_args args = {
6683 .fh = fh,
6684 .layoutclass = server->pnfs_curr_ld->id,
6685 };
6686 struct nfs4_getdevicelist_res res = {
6687 .devlist = devlist,
6688 };
6689 struct rpc_message msg = {
6690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6691 .rpc_argp = &args,
6692 .rpc_resp = &res,
6693 };
6694 int status;
6695
6696 dprintk("--> %s\n", __func__);
6697 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6698 &res.seq_res, 0);
6699 dprintk("<-- %s status=%d\n", __func__, status);
6700 return status;
6701}
6702
6703int nfs4_proc_getdevicelist(struct nfs_server *server,
6704 const struct nfs_fh *fh,
6705 struct pnfs_devicelist *devlist)
6706{
6707 struct nfs4_exception exception = { };
6708 int err;
6709
6710 do {
6711 err = nfs4_handle_exception(server,
6712 _nfs4_getdevicelist(server, fh, devlist),
6713 &exception);
6714 } while (exception.retry);
6715
6716 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6717 err, devlist->num_devs);
6718
6719 return err;
6720}
6721EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6722
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006723static int
6724_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6725{
6726 struct nfs4_getdeviceinfo_args args = {
6727 .pdev = pdev,
6728 };
6729 struct nfs4_getdeviceinfo_res res = {
6730 .pdev = pdev,
6731 };
6732 struct rpc_message msg = {
6733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6734 .rpc_argp = &args,
6735 .rpc_resp = &res,
6736 };
6737 int status;
6738
6739 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006740 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006741 dprintk("<-- %s status=%d\n", __func__, status);
6742
6743 return status;
6744}
6745
6746int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6747{
6748 struct nfs4_exception exception = { };
6749 int err;
6750
6751 do {
6752 err = nfs4_handle_exception(server,
6753 _nfs4_proc_getdeviceinfo(server, pdev),
6754 &exception);
6755 } while (exception.retry);
6756 return err;
6757}
6758EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6759
Andy Adamson863a3c62011-03-23 13:27:54 +00006760static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6761{
6762 struct nfs4_layoutcommit_data *data = calldata;
6763 struct nfs_server *server = NFS_SERVER(data->args.inode);
6764
6765 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006766 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006767 return;
6768 rpc_call_start(task);
6769}
6770
6771static void
6772nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6773{
6774 struct nfs4_layoutcommit_data *data = calldata;
6775 struct nfs_server *server = NFS_SERVER(data->args.inode);
6776
6777 if (!nfs4_sequence_done(task, &data->res.seq_res))
6778 return;
6779
6780 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006781 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6782 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6783 case -NFS4ERR_BADLAYOUT: /* no layout */
6784 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006785 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006786 break;
6787 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006788 nfs_post_op_update_inode_force_wcc(data->args.inode,
6789 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006790 break;
6791 default:
6792 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6793 rpc_restart_call_prepare(task);
6794 return;
6795 }
6796 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006797}
6798
6799static void nfs4_layoutcommit_release(void *calldata)
6800{
6801 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006802 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006803 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006804
Andy Adamsondb29c082011-07-30 20:52:38 -04006805 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006806 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006807 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6808 list_del_init(&lseg->pls_lc_list);
6809 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6810 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006811 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006812 }
Peng Tao92407e72011-10-23 20:21:17 -07006813
6814 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6815 smp_mb__after_clear_bit();
6816 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6817
Andy Adamson863a3c62011-03-23 13:27:54 +00006818 put_rpccred(data->cred);
6819 kfree(data);
6820}
6821
6822static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6823 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6824 .rpc_call_done = nfs4_layoutcommit_done,
6825 .rpc_release = nfs4_layoutcommit_release,
6826};
6827
6828int
Andy Adamsonef311532011-03-12 02:58:10 -05006829nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006830{
6831 struct rpc_message msg = {
6832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6833 .rpc_argp = &data->args,
6834 .rpc_resp = &data->res,
6835 .rpc_cred = data->cred,
6836 };
6837 struct rpc_task_setup task_setup_data = {
6838 .task = &data->task,
6839 .rpc_client = NFS_CLIENT(data->args.inode),
6840 .rpc_message = &msg,
6841 .callback_ops = &nfs4_layoutcommit_ops,
6842 .callback_data = data,
6843 .flags = RPC_TASK_ASYNC,
6844 };
6845 struct rpc_task *task;
6846 int status = 0;
6847
6848 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6849 "lbw: %llu inode %lu\n",
6850 data->task.tk_pid, sync,
6851 data->args.lastbytewritten,
6852 data->args.inode->i_ino);
6853
Trond Myklebust9d12b212012-01-17 22:04:25 -05006854 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006855 task = rpc_run_task(&task_setup_data);
6856 if (IS_ERR(task))
6857 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006858 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006859 goto out;
6860 status = nfs4_wait_for_completion_rpc_task(task);
6861 if (status != 0)
6862 goto out;
6863 status = task->tk_status;
6864out:
6865 dprintk("%s: status %d\n", __func__, status);
6866 rpc_put_task(task);
6867 return status;
6868}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006869
6870static int
6871_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6872 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6873{
6874 struct nfs41_secinfo_no_name_args args = {
6875 .style = SECINFO_STYLE_CURRENT_FH,
6876 };
6877 struct nfs4_secinfo_res res = {
6878 .flavors = flavors,
6879 };
6880 struct rpc_message msg = {
6881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6882 .rpc_argp = &args,
6883 .rpc_resp = &res,
6884 };
6885 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6886}
6887
6888static int
6889nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6890 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6891{
6892 struct nfs4_exception exception = { };
6893 int err;
6894 do {
6895 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6896 switch (err) {
6897 case 0:
6898 case -NFS4ERR_WRONGSEC:
6899 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006900 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006901 default:
6902 err = nfs4_handle_exception(server, err, &exception);
6903 }
6904 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006905out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006906 return err;
6907}
6908
6909static int
6910nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6911 struct nfs_fsinfo *info)
6912{
6913 int err;
6914 struct page *page;
6915 rpc_authflavor_t flavor;
6916 struct nfs4_secinfo_flavors *flavors;
6917
6918 page = alloc_page(GFP_KERNEL);
6919 if (!page) {
6920 err = -ENOMEM;
6921 goto out;
6922 }
6923
6924 flavors = page_address(page);
6925 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6926
6927 /*
6928 * Fall back on "guess and check" method if
6929 * the server doesn't support SECINFO_NO_NAME
6930 */
6931 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6932 err = nfs4_find_root_sec(server, fhandle, info);
6933 goto out_freepage;
6934 }
6935 if (err)
6936 goto out_freepage;
6937
6938 flavor = nfs_find_best_sec(flavors);
6939 if (err == 0)
6940 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6941
6942out_freepage:
6943 put_page(page);
6944 if (err == -EACCES)
6945 return -EPERM;
6946out:
6947 return err;
6948}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006949
6950static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006951{
6952 int status;
6953 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006954 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006955 };
6956 struct nfs41_test_stateid_res res;
6957 struct rpc_message msg = {
6958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6959 .rpc_argp = &args,
6960 .rpc_resp = &res,
6961 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006962
Chuck Lever38527b12012-07-11 16:30:23 -04006963 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006964 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006965 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006966 if (status != NFS_OK) {
6967 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006968 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006969 }
6970 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006971 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006972}
6973
Chuck Lever38527b12012-07-11 16:30:23 -04006974/**
6975 * nfs41_test_stateid - perform a TEST_STATEID operation
6976 *
6977 * @server: server / transport on which to perform the operation
6978 * @stateid: state ID to test
6979 *
6980 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6981 * Otherwise a negative NFS4ERR value is returned if the operation
6982 * failed or the state ID is not currently valid.
6983 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006984static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006985{
6986 struct nfs4_exception exception = { };
6987 int err;
6988 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006989 err = _nfs41_test_stateid(server, stateid);
6990 if (err != -NFS4ERR_DELAY)
6991 break;
6992 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006993 } while (exception.retry);
6994 return err;
6995}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006996
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006997static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006998{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006999 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007000 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007001 };
7002 struct nfs41_free_stateid_res res;
7003 struct rpc_message msg = {
7004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7005 .rpc_argp = &args,
7006 .rpc_resp = &res,
7007 };
Chuck Lever38527b12012-07-11 16:30:23 -04007008 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007009
Chuck Lever38527b12012-07-11 16:30:23 -04007010 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007011 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04007012 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04007013 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04007014 dprintk("NFS reply free_stateid: %d\n", status);
7015 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007016}
7017
Chuck Lever38527b12012-07-11 16:30:23 -04007018/**
7019 * nfs41_free_stateid - perform a FREE_STATEID operation
7020 *
7021 * @server: server / transport on which to perform the operation
7022 * @stateid: state ID to release
7023 *
7024 * Returns NFS_OK if the server freed "stateid". Otherwise a
7025 * negative NFS4ERR value is returned.
7026 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007027static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007028{
7029 struct nfs4_exception exception = { };
7030 int err;
7031 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007032 err = _nfs4_free_stateid(server, stateid);
7033 if (err != -NFS4ERR_DELAY)
7034 break;
7035 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007036 } while (exception.retry);
7037 return err;
7038}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007039
7040static bool nfs41_match_stateid(const nfs4_stateid *s1,
7041 const nfs4_stateid *s2)
7042{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007043 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007044 return false;
7045
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007046 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007047 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007048 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007049 return true;
7050
7051 return false;
7052}
7053
Andy Adamson557134a2009-04-01 09:21:53 -04007054#endif /* CONFIG_NFS_V4_1 */
7055
Trond Myklebust36281ca2012-03-04 18:13:56 -05007056static bool nfs4_match_stateid(const nfs4_stateid *s1,
7057 const nfs4_stateid *s2)
7058{
Trond Myklebustf597c532012-03-04 18:13:56 -05007059 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007060}
7061
7062
Trond Myklebust17280172012-03-11 13:11:00 -04007063static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007064 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007065 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 .recover_open = nfs4_open_reclaim,
7067 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007068 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007069 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007070 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071};
7072
Andy Adamson591d71c2009-04-01 09:22:47 -04007073#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007074static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007075 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7076 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7077 .recover_open = nfs4_open_reclaim,
7078 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007079 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007080 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007081 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007082 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007083};
7084#endif /* CONFIG_NFS_V4_1 */
7085
Trond Myklebust17280172012-03-11 13:11:00 -04007086static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007087 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007088 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 .recover_open = nfs4_open_expired,
7090 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007091 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007092 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093};
7094
Andy Adamson591d71c2009-04-01 09:22:47 -04007095#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007096static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007097 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7098 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007099 .recover_open = nfs41_open_expired,
7100 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007101 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007102 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007103};
7104#endif /* CONFIG_NFS_V4_1 */
7105
Trond Myklebust17280172012-03-11 13:11:00 -04007106static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007107 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007108 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007109 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007110};
7111
7112#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007113static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007114 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007115 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007116 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007117};
7118#endif
7119
Trond Myklebust97dc1352010-06-16 09:52:26 -04007120static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7121 .minor_version = 0,
7122 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007123 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007124 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007125 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7126 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7127 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007128};
7129
7130#if defined(CONFIG_NFS_V4_1)
7131static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7132 .minor_version = 1,
7133 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007134 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007135 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007136 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7137 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7138 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007139};
7140#endif
7141
Trond Myklebust97dc1352010-06-16 09:52:26 -04007142const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7143 [0] = &nfs_v4_0_minor_ops,
7144#if defined(CONFIG_NFS_V4_1)
7145 [1] = &nfs_v4_1_minor_ops,
7146#endif
7147};
7148
Bryan Schumaker73a79702012-07-16 16:39:12 -04007149const struct inode_operations nfs4_dir_inode_operations = {
7150 .create = nfs_create,
7151 .lookup = nfs_lookup,
7152 .atomic_open = nfs_atomic_open,
7153 .link = nfs_link,
7154 .unlink = nfs_unlink,
7155 .symlink = nfs_symlink,
7156 .mkdir = nfs_mkdir,
7157 .rmdir = nfs_rmdir,
7158 .mknod = nfs_mknod,
7159 .rename = nfs_rename,
7160 .permission = nfs_permission,
7161 .getattr = nfs_getattr,
7162 .setattr = nfs_setattr,
7163 .getxattr = generic_getxattr,
7164 .setxattr = generic_setxattr,
7165 .listxattr = generic_listxattr,
7166 .removexattr = generic_removexattr,
7167};
7168
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007169static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007170 .permission = nfs_permission,
7171 .getattr = nfs_getattr,
7172 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007173 .getxattr = generic_getxattr,
7174 .setxattr = generic_setxattr,
7175 .listxattr = generic_listxattr,
7176 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007177};
7178
David Howells509de812006-08-22 20:06:11 -04007179const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 .version = 4, /* protocol version */
7181 .dentry_ops = &nfs4_dentry_operations,
7182 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007183 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007184 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007186 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007187 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 .getattr = nfs4_proc_getattr,
7189 .setattr = nfs4_proc_setattr,
7190 .lookup = nfs4_proc_lookup,
7191 .access = nfs4_proc_access,
7192 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 .create = nfs4_proc_create,
7194 .remove = nfs4_proc_remove,
7195 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007196 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 .unlink_done = nfs4_proc_unlink_done,
7198 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007199 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007200 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007201 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 .link = nfs4_proc_link,
7203 .symlink = nfs4_proc_symlink,
7204 .mkdir = nfs4_proc_mkdir,
7205 .rmdir = nfs4_proc_remove,
7206 .readdir = nfs4_proc_readdir,
7207 .mknod = nfs4_proc_mknod,
7208 .statfs = nfs4_proc_statfs,
7209 .fsinfo = nfs4_proc_fsinfo,
7210 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007211 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 .decode_dirent = nfs4_decode_dirent,
7213 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007214 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007215 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007216 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007218 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007219 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007220 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007222 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007223 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007225 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007226 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007227 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007228 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007229 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007230 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007231 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007232 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007233 .create_server = nfs4_create_server,
7234 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235};
7236
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007237static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7238 .prefix = XATTR_NAME_NFSV4_ACL,
7239 .list = nfs4_xattr_list_nfs4_acl,
7240 .get = nfs4_xattr_get_nfs4_acl,
7241 .set = nfs4_xattr_set_nfs4_acl,
7242};
7243
7244const struct xattr_handler *nfs4_xattr_handlers[] = {
7245 &nfs4_xattr_nfs4_acl_handler,
7246 NULL
7247};
7248
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249/*
7250 * Local variables:
7251 * c-basic-offset: 8
7252 * End:
7253 */