blob: e9488f5e1037986686357ea017f2c99ad1d11969 [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>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
81static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040084 struct nfs4_state *state, struct nfs4_label *ilabel,
85 struct nfs4_label *olabel);
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:
Weston Andros Adamson30005122013-02-28 20:30:10 -050097 case -NFS4ERR_LAYOUTTRYLATER:
98 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -040099 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000100 case -NFS4ERR_WRONGSEC:
101 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800102 case -NFS4ERR_BADOWNER:
103 case -NFS4ERR_BADNAME:
104 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400105 case -NFS4ERR_SHARE_DENIED:
106 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400107 case -NFS4ERR_MINOR_VERS_MISMATCH:
108 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400109 case -NFS4ERR_ACCESS:
110 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400111 case -NFS4ERR_FILE_OPEN:
112 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700115 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400116 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400118 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121/*
122 * This is our standard bitmap for GETATTR requests.
123 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400124const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 FATTR4_WORD0_TYPE
126 | FATTR4_WORD0_CHANGE
127 | FATTR4_WORD0_SIZE
128 | FATTR4_WORD0_FSID
129 | FATTR4_WORD0_FILEID,
130 FATTR4_WORD1_MODE
131 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER
133 | FATTR4_WORD1_OWNER_GROUP
134 | FATTR4_WORD1_RAWDEV
135 | FATTR4_WORD1_SPACE_USED
136 | FATTR4_WORD1_TIME_ACCESS
137 | FATTR4_WORD1_TIME_METADATA
138 | FATTR4_WORD1_TIME_MODIFY
139};
140
Trond Myklebust1549210f2012-06-05 09:16:47 -0400141static const u32 nfs4_pnfs_open_bitmap[3] = {
142 FATTR4_WORD0_TYPE
143 | FATTR4_WORD0_CHANGE
144 | FATTR4_WORD0_SIZE
145 | FATTR4_WORD0_FSID
146 | FATTR4_WORD0_FILEID,
147 FATTR4_WORD1_MODE
148 | FATTR4_WORD1_NUMLINKS
149 | FATTR4_WORD1_OWNER
150 | FATTR4_WORD1_OWNER_GROUP
151 | FATTR4_WORD1_RAWDEV
152 | FATTR4_WORD1_SPACE_USED
153 | FATTR4_WORD1_TIME_ACCESS
154 | FATTR4_WORD1_TIME_METADATA
155 | FATTR4_WORD1_TIME_MODIFY,
156 FATTR4_WORD2_MDSTHRESHOLD
157};
158
Andy Adamsone23008e2012-10-02 21:07:32 -0400159static const u32 nfs4_open_noattr_bitmap[3] = {
160 FATTR4_WORD0_TYPE
161 | FATTR4_WORD0_CHANGE
162 | FATTR4_WORD0_FILEID,
163};
164
David Quigleya09df2c2013-05-22 12:50:41 -0400165const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 FATTR4_WORD0_FILES_AVAIL
167 | FATTR4_WORD0_FILES_FREE
168 | FATTR4_WORD0_FILES_TOTAL,
169 FATTR4_WORD1_SPACE_AVAIL
170 | FATTR4_WORD1_SPACE_FREE
171 | FATTR4_WORD1_SPACE_TOTAL
172};
173
David Quigleya09df2c2013-05-22 12:50:41 -0400174const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 FATTR4_WORD0_MAXLINK
176 | FATTR4_WORD0_MAXNAME,
177 0
178};
179
Fred Isamandae100c2011-07-30 20:52:37 -0400180const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 | FATTR4_WORD0_MAXREAD
182 | FATTR4_WORD0_MAXWRITE
183 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700184 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400185 | FATTR4_WORD1_FS_LAYOUT_TYPES,
186 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
David Quigleya09df2c2013-05-22 12:50:41 -0400189const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID
195 | FATTR4_WORD0_FS_LOCATIONS,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400206};
207
Al Virobc4785c2006-10-19 23:28:51 -0700208static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct nfs4_readdir_arg *readdir)
210{
Al Viro0dbb4c62006-10-19 23:28:49 -0700211 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000214 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
216 return;
217 }
218
219 readdir->cookie = 0;
220 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
221 if (cookie == 2)
222 return;
223
224 /*
225 * NFSv4 servers do not return entries for '.' and '..'
226 * Therefore, we fake these entries here. We let '.'
227 * have cookie 0 and '..' have cookie 1. Note that
228 * when talking to the server, we always send cookie 0
229 * instead of 1 or 2.
230 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800231 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (cookie == 0) {
234 *p++ = xdr_one; /* next */
235 *p++ = xdr_zero; /* cookie, first word */
236 *p++ = xdr_one; /* cookie, second word */
237 *p++ = xdr_one; /* entry len */
238 memcpy(p, ".\0\0\0", 4); /* entry */
239 p++;
240 *p++ = xdr_one; /* bitmap length */
241 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
242 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400243 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 *p++ = xdr_one; /* next */
247 *p++ = xdr_zero; /* cookie, first word */
248 *p++ = xdr_two; /* cookie, second word */
249 *p++ = xdr_two; /* entry len */
250 memcpy(p, "..\0\0", 4); /* entry */
251 p++;
252 *p++ = xdr_one; /* bitmap length */
253 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
254 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400255 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 readdir->pgbase = (char *)p - (char *)start;
258 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800259 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Trond Myklebust65de8722008-12-23 15:21:44 -0500262static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
263{
264 int res = 0;
265
266 might_sleep();
267
268 if (*timeout <= 0)
269 *timeout = NFS4_POLL_RETRY_MIN;
270 if (*timeout > NFS4_POLL_RETRY_MAX)
271 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100272 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500273 if (fatal_signal_pending(current))
274 res = -ERESTARTSYS;
275 *timeout <<= 1;
276 return res;
277}
278
279/* This is the error handling routine for processes that are allowed
280 * to sleep.
281 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800282static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500283{
284 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500285 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500286 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500287 int ret = errorcode;
288
289 exception->retry = 0;
290 switch(errorcode) {
291 case 0:
292 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500293 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400294 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400295 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500296 exception->retry = 1;
297 return 0;
298 }
299 if (state == NULL)
300 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400301 ret = nfs4_schedule_stateid_recovery(server, state);
302 if (ret < 0)
303 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500304 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500305 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500306 case -NFS4ERR_ADMIN_REVOKED:
307 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400308 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
309 nfs_remove_bad_delegation(inode);
310 exception->retry = 1;
311 break;
312 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500313 if (state == NULL)
314 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400315 ret = nfs4_schedule_stateid_recovery(server, state);
316 if (ret < 0)
317 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500318 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400319 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400320 if (state != NULL) {
321 ret = nfs4_schedule_stateid_recovery(server, state);
322 if (ret < 0)
323 break;
324 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500325 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500326 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500327 nfs4_schedule_lease_recovery(clp);
328 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500329#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400330 case -NFS4ERR_BADSESSION:
331 case -NFS4ERR_BADSLOT:
332 case -NFS4ERR_BAD_HIGH_SLOT:
333 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
334 case -NFS4ERR_DEADSESSION:
335 case -NFS4ERR_SEQ_FALSE_RETRY:
336 case -NFS4ERR_SEQ_MISORDERED:
337 dprintk("%s ERROR: %d Reset session\n", __func__,
338 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400339 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400340 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500341#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500343 if (exception->timeout > HZ) {
344 /* We have retried a decent amount, time to
345 * fail
346 */
347 ret = -EBUSY;
348 break;
349 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500350 case -NFS4ERR_GRACE:
351 case -NFS4ERR_DELAY:
352 ret = nfs4_delay(server->client, &exception->timeout);
353 if (ret != 0)
354 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400355 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 case -NFS4ERR_OLD_STATEID:
357 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800358 break;
359 case -NFS4ERR_BADOWNER:
360 /* The following works around a Linux server bug! */
361 case -NFS4ERR_BADNAME:
362 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
363 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
364 exception->retry = 1;
365 printk(KERN_WARNING "NFS: v4 server %s "
366 "does not accept raw "
367 "uid/gids. "
368 "Reenabling the idmapper.\n",
369 server->nfs_client->cl_hostname);
370 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500371 }
372 /* We failed to handle the error */
373 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500374wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500375 ret = nfs4_wait_clnt_recover(clp);
376 if (ret == 0)
377 exception->retry = 1;
378 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500379}
380
381
Trond Myklebust452e9352010-07-31 14:29:06 -0400382static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_lock(&clp->cl_lock);
385 if (time_before(clp->cl_last_renewal,timestamp))
386 clp->cl_last_renewal = timestamp;
387 spin_unlock(&clp->cl_lock);
388}
389
Trond Myklebust452e9352010-07-31 14:29:06 -0400390static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
391{
392 do_renew_lease(server->nfs_client, timestamp);
393}
394
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400395#if defined(CONFIG_NFS_V4_1)
396
Trond Myklebustd185a332010-06-16 09:52:25 -0400397static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400398{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500399 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400400 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500401 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400402
Benny Halevydfb4f3092010-09-24 09:17:01 -0400403 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400404 /* just wake up the next guy waiting since
405 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500406 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 return;
Andy Adamson13615872009-04-01 09:22:17 -0400408 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500409 tbl = res->sr_slot->table;
410 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500411
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500412 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500413 /* Be nice to the server: try to ensure that the last transmitted
414 * value for highest_user_slotid <= target_highest_slotid
415 */
416 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
417 send_new_highest_used_slotid = true;
418
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500419 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
420 send_new_highest_used_slotid = false;
421 goto out_unlock;
422 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500423 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500424
425 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
426 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500427out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500428 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400429 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500430 if (send_new_highest_used_slotid)
431 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400432}
433
Trond Myklebust14516c32010-07-31 14:29:06 -0400434static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400435{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500436 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500437 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400438 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500439 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500440 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441
Bryan Schumaker468f8612011-04-18 15:57:32 -0400442 /* don't increment the sequence number if the task wasn't sent */
443 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444 goto out;
445
Trond Myklebust933602e2012-11-16 12:12:38 -0500446 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500447 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500448
Trond Myklebustac20d162012-12-15 15:36:07 -0500449 if (slot->interrupted) {
450 slot->interrupted = 0;
451 interrupted = true;
452 }
453
Andy Adamson691daf32009-12-04 15:55:39 -0500454 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400455 switch (res->sr_status) {
456 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400457 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500458 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500459 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500460 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500461 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500462 if (res->sr_status_flags != 0)
463 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500464 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400465 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500466 case 1:
467 /*
468 * sr_status remains 1 if an RPC level error occurred.
469 * The server may or may not have processed the sequence
470 * operation..
471 * Mark the slot as having hosted an interrupted RPC call.
472 */
473 slot->interrupted = 1;
474 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400475 case -NFS4ERR_DELAY:
476 /* The server detected a resend of the RPC call and
477 * returned NFS4ERR_DELAY as per Section 2.10.6.2
478 * of RFC5661.
479 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500480 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400481 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500482 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500483 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400484 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500485 case -NFS4ERR_BADSLOT:
486 /*
487 * The slot id we used was probably retired. Try again
488 * using a different slot id.
489 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500490 goto retry_nowait;
491 case -NFS4ERR_SEQ_MISORDERED:
492 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500493 * Was the last operation on this sequence interrupted?
494 * If so, retry after bumping the sequence number.
495 */
496 if (interrupted) {
497 ++slot->seq_nr;
498 goto retry_nowait;
499 }
500 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500501 * Could this slot have been previously retired?
502 * If so, then the server may be expecting seq_nr = 1!
503 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500504 if (slot->seq_nr != 1) {
505 slot->seq_nr = 1;
506 goto retry_nowait;
507 }
508 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500509 case -NFS4ERR_SEQ_FALSE_RETRY:
510 ++slot->seq_nr;
511 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400512 default:
513 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500514 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400515 }
516out:
517 /* The session may be reset by one of the error handlers. */
518 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400519 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500520 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500521retry_nowait:
522 if (rpc_restart_call_prepare(task)) {
523 task->tk_status = 0;
524 ret = 0;
525 }
526 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400527out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400528 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400529 goto out;
530 rpc_delay(task, NFS4_POLL_RETRY_MAX);
531 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400532}
533
Trond Myklebust14516c32010-07-31 14:29:06 -0400534static int nfs4_sequence_done(struct rpc_task *task,
535 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400536{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500537 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400538 return 1;
539 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400540}
541
Trond Myklebust9d12b212012-01-17 22:04:25 -0500542static void nfs41_init_sequence(struct nfs4_sequence_args *args,
543 struct nfs4_sequence_res *res, int cache_reply)
544{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500545 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500546 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400547 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500548 if (cache_reply)
549 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500550 res->sr_slot = NULL;
551}
552
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400553static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
554{
555 args->sa_privileged = 1;
556}
557
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000558int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400559 struct nfs4_sequence_args *args,
560 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561 struct rpc_task *task)
562{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400563 struct nfs4_slot *slot;
564 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400565
566 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400568 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400569 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400570
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400571 tbl = &session->fc_slot_table;
572
Trond Myklebust69d206b2012-11-22 13:21:02 -0500573 task->tk_timeout = 0;
574
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400575 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400576 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400577 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500578 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500579 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400580 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400581 }
582
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500583 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500584 if (IS_ERR(slot)) {
585 /* If out of memory, try again in 1/4 second */
586 if (slot == ERR_PTR(-ENOMEM))
587 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400588 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400589 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400590 }
591 spin_unlock(&tbl->slot_tbl_lock);
592
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500593 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500595 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
596 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400597
Benny Halevydfb4f3092010-09-24 09:17:01 -0400598 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500599 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400600 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400601 /*
602 * sr_status is only set in decode_sequence, and so will remain
603 * set to 1 if an rpc level failure occurs.
604 */
605 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400606out_success:
607 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400609out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400610 /* Privileged tasks are queued with top priority */
611 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400612 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
613 NULL, RPC_PRIORITY_PRIVILEGED);
614 else
615 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400616 spin_unlock(&tbl->slot_tbl_lock);
617 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000619EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620
Trond Myklebust035168a2010-06-16 09:52:26 -0400621int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 struct nfs4_sequence_args *args,
623 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 struct rpc_task *task)
625{
Trond Myklebust035168a2010-06-16 09:52:26 -0400626 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627 int ret = 0;
628
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400629 if (session == NULL) {
630 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400632 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400633
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500634 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400635 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500636 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400637
Trond Myklebust9d12b212012-01-17 22:04:25 -0500638 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639out:
640 dprintk("<-- %s status=%d\n", __func__, ret);
641 return ret;
642}
643
644struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400645 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400646 struct nfs4_sequence_args *seq_args;
647 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648};
649
650static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
651{
652 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400653 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654
Trond Myklebust035168a2010-06-16 09:52:26 -0400655 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
656
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400657 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658}
659
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
661{
662 struct nfs41_call_sync_data *data = calldata;
663
Trond Myklebust14516c32010-07-31 14:29:06 -0400664 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400665}
666
Trond Myklebust17280172012-03-11 13:11:00 -0400667static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400669 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670};
671
Bryan Schumaker7c513052011-03-24 17:12:24 +0000672static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
673 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674 struct rpc_message *msg,
675 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400676 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677{
678 int ret;
679 struct rpc_task *task;
680 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400681 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .seq_args = args,
683 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 };
685 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000686 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 .rpc_message = msg,
688 .callback_ops = &nfs41_call_sync_ops,
689 .callback_data = &data
690 };
691
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 task = rpc_run_task(&task_setup);
693 if (IS_ERR(task))
694 ret = PTR_ERR(task);
695 else {
696 ret = task->tk_status;
697 rpc_put_task(task);
698 }
699 return ret;
700}
701
Trond Myklebustdf896452010-06-16 09:52:26 -0400702#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400703static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500704void nfs41_init_sequence(struct nfs4_sequence_args *args,
705 struct nfs4_sequence_res *res, int cache_reply)
706{
707}
708
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400709static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
710{
711}
712
713
Trond Myklebust14516c32010-07-31 14:29:06 -0400714static int nfs4_sequence_done(struct rpc_task *task,
715 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400716{
Trond Myklebust14516c32010-07-31 14:29:06 -0400717 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400718}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719#endif /* CONFIG_NFS_V4_1 */
720
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400721static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000722int _nfs4_call_sync(struct rpc_clnt *clnt,
723 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400726 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400727{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000728 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729}
730
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400731static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732int nfs4_call_sync(struct rpc_clnt *clnt,
733 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000734 struct rpc_message *msg,
735 struct nfs4_sequence_args *args,
736 struct nfs4_sequence_res *res,
737 int cache_reply)
738{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400739 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000740 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400741 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000742}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400743
Trond Myklebust38478b22006-05-25 01:40:57 -0400744static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Trond Myklebust38478b22006-05-25 01:40:57 -0400746 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Trond Myklebust38478b22006-05-25 01:40:57 -0400748 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400749 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700750 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400751 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700752 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000753 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400754 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400758 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 struct nfs_openargs o_arg;
760 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100761 struct nfs_open_confirmargs c_arg;
762 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500763 struct nfs4_string owner_name;
764 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400766 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400768 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400770 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100772 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400773 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400774 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100775 int rpc_status;
776 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777};
778
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400779static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
780 int err, struct nfs4_exception *exception)
781{
782 if (err != -EINVAL)
783 return false;
784 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
785 return false;
786 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
787 exception->retry = 1;
788 return true;
789}
790
791static enum open_claim_type4
792nfs4_map_atomic_open_claim(struct nfs_server *server,
793 enum open_claim_type4 claim)
794{
795 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
796 return claim;
797 switch (claim) {
798 default:
799 return claim;
800 case NFS4_OPEN_CLAIM_FH:
801 return NFS4_OPEN_CLAIM_NULL;
802 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
803 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
804 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
805 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
806 }
807}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400808
809static void nfs4_init_opendata_res(struct nfs4_opendata *p)
810{
811 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400812 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400813 p->o_res.seqid = p->o_arg.seqid;
814 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400815 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400816 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400817 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500818 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400819}
820
Al Viro82a2c1b2011-06-22 18:30:55 -0400821static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400823 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400824 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400825 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400826 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827{
Al Viro82a2c1b2011-06-22 18:30:55 -0400828 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 struct inode *dir = parent->d_inode;
830 struct nfs_server *server = NFS_SERVER(dir);
831 struct nfs4_opendata *p;
832
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400833 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 if (p == NULL)
835 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400836
837 p->f_label = nfs4_label_alloc(server, gfp_mask);
838 if (IS_ERR(p->f_label))
839 goto err_free_p;
840
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400841 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100842 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400843 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 nfs_sb_active(dentry->d_sb);
845 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846 p->dir = parent;
847 p->owner = sp;
848 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 p->o_arg.open_flags = flags;
850 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700851 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
852 * will return permission denied for all bits until close */
853 if (!(flags & O_EXCL)) {
854 /* ask server to check for all possible rights as results
855 * are cached */
856 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
857 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700858 }
David Howells7539bba2006-08-22 20:06:09 -0400859 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400860 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
861 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400862 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863 p->o_arg.server = server;
864 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400865 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400866 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400867 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
868 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400869 case NFS4_OPEN_CLAIM_NULL:
870 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
871 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
872 p->o_arg.fh = NFS_FH(dir);
873 break;
874 case NFS4_OPEN_CLAIM_PREVIOUS:
875 case NFS4_OPEN_CLAIM_FH:
876 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
877 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
878 p->o_arg.fh = NFS_FH(dentry->d_inode);
879 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500880 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500881 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400882
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100883 p->o_arg.u.attrs = &p->attrs;
884 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500885
886 verf[0] = jiffies;
887 verf[1] = current->pid;
888 memcpy(p->o_arg.u.verifier.data, verf,
889 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100890 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100891 p->c_arg.fh = &p->o_res.fh;
892 p->c_arg.stateid = &p->o_res.stateid;
893 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400894 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400895 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100896 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400897
898err_free_label:
899 nfs4_label_free(p->f_label);
900err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100901 kfree(p);
902err:
903 dput(parent);
904 return NULL;
905}
906
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400907static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100908{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400909 struct nfs4_opendata *p = container_of(kref,
910 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400911 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400912
913 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400914 if (p->state != NULL)
915 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400916 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400917
918 nfs4_label_free(p->f_label);
919
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400920 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400921 dput(p->dentry);
922 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500923 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400924 kfree(p);
925}
926
927static void nfs4_opendata_put(struct nfs4_opendata *p)
928{
929 if (p != NULL)
930 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100931}
932
Trond Myklebust06f814a2006-01-03 09:55:07 +0100933static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
934{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100935 int ret;
936
Trond Myklebust06f814a2006-01-03 09:55:07 +0100937 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100938 return ret;
939}
940
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942{
943 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944
Trond Myklebust536e43d2012-01-17 22:04:26 -0500945 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 goto out;
947 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400948 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500949 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
950 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400951 break;
952 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500953 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
954 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400955 break;
956 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500957 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
958 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400959 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400961 return ret;
962}
963
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500966 if (delegation == NULL)
967 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500970 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500972 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
973 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500974 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 return 1;
976}
977
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100979{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100981 case FMODE_WRITE:
982 state->n_wronly++;
983 break;
984 case FMODE_READ:
985 state->n_rdonly++;
986 break;
987 case FMODE_READ|FMODE_WRITE:
988 state->n_rdwr++;
989 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400994{
995 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500996 nfs4_stateid_copy(&state->stateid, stateid);
997 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400998 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001000 case FMODE_READ:
1001 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1002 break;
1003 case FMODE_WRITE:
1004 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1005 break;
1006 case FMODE_READ|FMODE_WRITE:
1007 set_bit(NFS_O_RDWR_STATE, &state->flags);
1008 }
1009}
1010
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001012{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001015 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001016}
1017
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001020 /*
1021 * Protect the call to nfs4_state_set_mode_locked and
1022 * serialise the stateid update
1023 */
1024 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001025 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001026 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001027 set_bit(NFS_DELEGATED_STATE, &state->flags);
1028 }
1029 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001031 write_sequnlock(&state->seqlock);
1032 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001034 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001038{
1039 struct nfs_inode *nfsi = NFS_I(state->inode);
1040 struct nfs_delegation *deleg_cur;
1041 int ret = 0;
1042
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001044
1045 rcu_read_lock();
1046 deleg_cur = rcu_dereference(nfsi->delegation);
1047 if (deleg_cur == NULL)
1048 goto no_delegation;
1049
1050 spin_lock(&deleg_cur->lock);
1051 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001052 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001054 goto no_delegation_unlock;
1055
1056 if (delegation == NULL)
1057 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001058 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001059 goto no_delegation_unlock;
1060
Trond Myklebustb7391f42008-12-23 15:21:52 -05001061 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001063 ret = 1;
1064no_delegation_unlock:
1065 spin_unlock(&deleg_cur->lock);
1066no_delegation:
1067 rcu_read_unlock();
1068
1069 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001071 ret = 1;
1072 }
1073
1074 return ret;
1075}
1076
1077
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001078static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079{
1080 struct nfs_delegation *delegation;
1081
1082 rcu_read_lock();
1083 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 rcu_read_unlock();
1086 return;
1087 }
1088 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001089 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001090}
1091
Trond Myklebust6ee41262007-07-08 14:11:36 -04001092static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001093{
1094 struct nfs4_state *state = opendata->state;
1095 struct nfs_inode *nfsi = NFS_I(state->inode);
1096 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001097 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 nfs4_stateid stateid;
1100 int ret = -EAGAIN;
1101
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001104 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 if (can_open_cached(state, fmode, open_mode)) {
1106 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001107 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 goto out_return_state;
1109 }
1110 spin_unlock(&state->owner->so_lock);
1111 }
Trond Myklebust34310432008-12-23 15:21:38 -05001112 rcu_read_lock();
1113 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001114 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001115 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001116 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001117 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001119 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001120 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001121 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001122 if (!opendata->is_recover) {
1123 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1124 if (ret != 0)
1125 goto out;
1126 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001128
1129 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001130 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001131 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001132 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001133out:
1134 return ERR_PTR(ret);
1135out_return_state:
1136 atomic_inc(&state->count);
1137 return state;
1138}
1139
Andy Adamsone23008e2012-10-02 21:07:32 -04001140static void
1141nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1142{
1143 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1144 struct nfs_delegation *delegation;
1145 int delegation_flags = 0;
1146
1147 rcu_read_lock();
1148 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1149 if (delegation)
1150 delegation_flags = delegation->flags;
1151 rcu_read_unlock();
1152 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1153 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1154 "returning a delegation for "
1155 "OPEN(CLAIM_DELEGATE_CUR)\n",
1156 clp->cl_hostname);
1157 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1158 nfs_inode_set_delegation(state->inode,
1159 data->owner->so_cred,
1160 &data->o_res);
1161 else
1162 nfs_inode_reclaim_delegation(state->inode,
1163 data->owner->so_cred,
1164 &data->o_res);
1165}
1166
1167/*
1168 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1169 * and update the nfs4_state.
1170 */
1171static struct nfs4_state *
1172_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1173{
1174 struct inode *inode = data->state->inode;
1175 struct nfs4_state *state = data->state;
1176 int ret;
1177
1178 if (!data->rpc_done) {
1179 ret = data->rpc_status;
1180 goto err;
1181 }
1182
1183 ret = -ESTALE;
1184 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1185 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1186 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1187 goto err;
1188
1189 ret = -ENOMEM;
1190 state = nfs4_get_open_state(inode, data->owner);
1191 if (state == NULL)
1192 goto err;
1193
1194 ret = nfs_refresh_inode(inode, &data->f_attr);
1195 if (ret)
1196 goto err;
1197
1198 if (data->o_res.delegation_type != 0)
1199 nfs4_opendata_check_deleg(data, state);
1200 update_open_stateid(state, &data->o_res.stateid, NULL,
1201 data->o_arg.fmode);
1202
1203 return state;
1204err:
1205 return ERR_PTR(ret);
1206
1207}
1208
1209static struct nfs4_state *
1210_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211{
1212 struct inode *inode;
1213 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001214 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001215
Trond Myklebustaac00a82007-07-05 19:02:21 -04001216 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001217 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001218 goto out;
1219 }
1220
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001221 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001222 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001223 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001224 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001225 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001226 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001227 goto err;
1228 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001229 state = nfs4_get_open_state(inode, data->owner);
1230 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001231 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001232 if (data->o_res.delegation_type != 0)
1233 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001234 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001235 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001236 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001237out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001238 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001239 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001240err_put_inode:
1241 iput(inode);
1242err:
1243 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001244}
1245
Andy Adamsone23008e2012-10-02 21:07:32 -04001246static struct nfs4_state *
1247nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1248{
1249 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1250 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1251 return _nfs4_opendata_to_nfs4_state(data);
1252}
1253
Trond Myklebust864472e2006-01-03 09:55:15 +01001254static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1255{
1256 struct nfs_inode *nfsi = NFS_I(state->inode);
1257 struct nfs_open_context *ctx;
1258
1259 spin_lock(&state->inode->i_lock);
1260 list_for_each_entry(ctx, &nfsi->open_files, list) {
1261 if (ctx->state != state)
1262 continue;
1263 get_nfs_open_context(ctx);
1264 spin_unlock(&state->inode->i_lock);
1265 return ctx;
1266 }
1267 spin_unlock(&state->inode->i_lock);
1268 return ERR_PTR(-ENOENT);
1269}
1270
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001271static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1272 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001273{
1274 struct nfs4_opendata *opendata;
1275
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001276 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001277 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001278 if (opendata == NULL)
1279 return ERR_PTR(-ENOMEM);
1280 opendata->state = state;
1281 atomic_inc(&state->count);
1282 return opendata;
1283}
1284
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001285static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001286{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 int ret;
1289
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290 opendata->o_arg.open_flags = 0;
1291 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1293 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1294 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001295 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 if (ret != 0)
1297 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001298 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001299 if (IS_ERR(newstate))
1300 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001301 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001302 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 return 0;
1304}
1305
1306static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1307{
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001309 int ret;
1310
1311 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001312 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001313 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 smp_rmb();
1315 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001316 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001317 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 if (ret != 0)
1319 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001320 if (newstate != state)
1321 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 }
1323 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001324 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001325 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001326 if (ret != 0)
1327 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001328 if (newstate != state)
1329 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001330 }
1331 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001332 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001333 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 if (ret != 0)
1335 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001336 if (newstate != state)
1337 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001339 /*
1340 * We may have performed cached opens for all three recoveries.
1341 * Check if we need to update the current stateid.
1342 */
1343 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001344 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001345 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001346 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001347 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001348 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001349 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001350 return 0;
1351}
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353/*
1354 * OPEN_RECLAIM:
1355 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001357static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001359 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001360 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001361 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 int status;
1363
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001364 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1365 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001366 if (IS_ERR(opendata))
1367 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001368 rcu_read_lock();
1369 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001370 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001371 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001372 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001373 opendata->o_arg.u.delegation_type = delegation_type;
1374 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001375 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return status;
1377}
1378
Trond Myklebust539cd032007-06-05 11:46:42 -04001379static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 struct nfs_server *server = NFS_SERVER(state->inode);
1382 struct nfs4_exception exception = { };
1383 int err;
1384 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001385 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001386 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1387 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001388 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001389 break;
1390 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 } while (exception.retry);
1392 return err;
1393}
1394
Trond Myklebust864472e2006-01-03 09:55:15 +01001395static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1396{
1397 struct nfs_open_context *ctx;
1398 int ret;
1399
1400 ctx = nfs4_state_find_open_context(state);
1401 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001402 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001403 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001404 put_nfs_open_context(ctx);
1405 return ret;
1406}
1407
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001408static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001410 switch (err) {
1411 default:
1412 printk(KERN_ERR "NFS: %s: unhandled error "
1413 "%d.\n", __func__, err);
1414 case 0:
1415 case -ENOENT:
1416 case -ESTALE:
1417 break;
1418 case -NFS4ERR_BADSESSION:
1419 case -NFS4ERR_BADSLOT:
1420 case -NFS4ERR_BAD_HIGH_SLOT:
1421 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1422 case -NFS4ERR_DEADSESSION:
1423 set_bit(NFS_DELEGATED_STATE, &state->flags);
1424 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1425 return -EAGAIN;
1426 case -NFS4ERR_STALE_CLIENTID:
1427 case -NFS4ERR_STALE_STATEID:
1428 set_bit(NFS_DELEGATED_STATE, &state->flags);
1429 case -NFS4ERR_EXPIRED:
1430 /* Don't recall a delegation if it was lost */
1431 nfs4_schedule_lease_recovery(server->nfs_client);
1432 return -EAGAIN;
1433 case -NFS4ERR_DELEG_REVOKED:
1434 case -NFS4ERR_ADMIN_REVOKED:
1435 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001436 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001437 nfs_inode_find_state_and_recover(state->inode,
1438 stateid);
1439 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001440 return 0;
1441 case -NFS4ERR_DELAY:
1442 case -NFS4ERR_GRACE:
1443 set_bit(NFS_DELEGATED_STATE, &state->flags);
1444 ssleep(1);
1445 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001446 case -ENOMEM:
1447 case -NFS4ERR_DENIED:
1448 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1449 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return err;
1452}
1453
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001454int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1455{
1456 struct nfs_server *server = NFS_SERVER(state->inode);
1457 struct nfs4_opendata *opendata;
1458 int err;
1459
1460 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1461 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1462 if (IS_ERR(opendata))
1463 return PTR_ERR(opendata);
1464 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1465 err = nfs4_open_recover(opendata, state);
1466 nfs4_opendata_put(opendata);
1467 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1468}
1469
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001470static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1471{
1472 struct nfs4_opendata *data = calldata;
1473
1474 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001475 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001476 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001477 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001478 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001479 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001480 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001481}
1482
1483static void nfs4_open_confirm_release(void *calldata)
1484{
1485 struct nfs4_opendata *data = calldata;
1486 struct nfs4_state *state = NULL;
1487
1488 /* If this request hasn't been cancelled, do nothing */
1489 if (data->cancelled == 0)
1490 goto out_free;
1491 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001492 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001493 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001494 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001495 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001496 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001497out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001498 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001499}
1500
1501static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001502 .rpc_call_done = nfs4_open_confirm_done,
1503 .rpc_release = nfs4_open_confirm_release,
1504};
1505
1506/*
1507 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1508 */
1509static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1510{
1511 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1512 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001513 struct rpc_message msg = {
1514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1515 .rpc_argp = &data->c_arg,
1516 .rpc_resp = &data->c_res,
1517 .rpc_cred = data->owner->so_cred,
1518 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001519 struct rpc_task_setup task_setup_data = {
1520 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001521 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001522 .callback_ops = &nfs4_open_confirm_ops,
1523 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001524 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001525 .flags = RPC_TASK_ASYNC,
1526 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int status;
1528
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001529 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001530 data->rpc_done = 0;
1531 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001532 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001533 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001534 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001535 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001536 status = nfs4_wait_for_completion_rpc_task(task);
1537 if (status != 0) {
1538 data->cancelled = 1;
1539 smp_wmb();
1540 } else
1541 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001542 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return status;
1544}
1545
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 struct nfs4_opendata *data = calldata;
1549 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001550 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001551
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001553 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001554 /*
1555 * Check if we still need to send an OPEN call, or if we can use
1556 * a delegation instead.
1557 */
1558 if (data->state != NULL) {
1559 struct nfs_delegation *delegation;
1560
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001561 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001562 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 rcu_read_lock();
1564 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001565 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001566 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001567 can_open_delegated(delegation, data->o_arg.fmode))
1568 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001569 rcu_read_unlock();
1570 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001571 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001572 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001573 switch (data->o_arg.claim) {
1574 case NFS4_OPEN_CLAIM_PREVIOUS:
1575 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1576 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001577 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001578 case NFS4_OPEN_CLAIM_FH:
1579 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001580 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1581 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001582 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001583 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001584 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001585 &data->o_res.seq_res,
1586 task) != 0)
1587 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001588
1589 /* Set the create mode (note dependency on the session type) */
1590 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1591 if (data->o_arg.open_flags & O_EXCL) {
1592 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1593 if (nfs4_has_persistent_session(clp))
1594 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1595 else if (clp->cl_mvops->minor_version > 0)
1596 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1597 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001598 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001599unlock_no_action:
1600 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001601out_no_action:
1602 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001603out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001604 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001605}
1606
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001607static void nfs4_open_done(struct rpc_task *task, void *calldata)
1608{
1609 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001612
Trond Myklebust14516c32010-07-31 14:29:06 -04001613 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1614 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001615
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001616 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001617 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1618 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001619 case S_IFREG:
1620 break;
1621 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001623 break;
1624 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001626 break;
1627 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001628 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001629 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001630 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001631 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001632 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1633 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001634 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001635 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001637
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638static void nfs4_open_release(void *calldata)
1639{
1640 struct nfs4_opendata *data = calldata;
1641 struct nfs4_state *state = NULL;
1642
1643 /* If this request hasn't been cancelled, do nothing */
1644 if (data->cancelled == 0)
1645 goto out_free;
1646 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001647 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001648 goto out_free;
1649 /* In case we need an open_confirm, no cleanup! */
1650 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1651 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001652 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001653 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001654 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001655out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001656 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001657}
1658
1659static const struct rpc_call_ops nfs4_open_ops = {
1660 .rpc_call_prepare = nfs4_open_prepare,
1661 .rpc_call_done = nfs4_open_done,
1662 .rpc_release = nfs4_open_release,
1663};
1664
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001665static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666{
1667 struct inode *dir = data->dir->d_inode;
1668 struct nfs_server *server = NFS_SERVER(dir);
1669 struct nfs_openargs *o_arg = &data->o_arg;
1670 struct nfs_openres *o_res = &data->o_res;
1671 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001672 struct rpc_message msg = {
1673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1674 .rpc_argp = o_arg,
1675 .rpc_resp = o_res,
1676 .rpc_cred = data->owner->so_cred,
1677 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001678 struct rpc_task_setup task_setup_data = {
1679 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001680 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001681 .callback_ops = &nfs4_open_ops,
1682 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001683 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001684 .flags = RPC_TASK_ASYNC,
1685 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686 int status;
1687
Trond Myklebust9d12b212012-01-17 22:04:25 -05001688 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001689 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001690 data->rpc_done = 0;
1691 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001692 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001693 data->is_recover = 0;
1694 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001695 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001696 data->is_recover = 1;
1697 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001698 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001699 if (IS_ERR(task))
1700 return PTR_ERR(task);
1701 status = nfs4_wait_for_completion_rpc_task(task);
1702 if (status != 0) {
1703 data->cancelled = 1;
1704 smp_wmb();
1705 } else
1706 status = data->rpc_status;
1707 rpc_put_task(task);
1708
1709 return status;
1710}
1711
1712static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1713{
1714 struct inode *dir = data->dir->d_inode;
1715 struct nfs_openres *o_res = &data->o_res;
1716 int status;
1717
1718 status = nfs4_run_open_task(data, 1);
1719 if (status != 0 || !data->rpc_done)
1720 return status;
1721
Trond Myklebust6926afd2012-01-07 13:22:46 -05001722 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1723
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001724 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1725 status = _nfs4_proc_open_confirm(data);
1726 if (status != 0)
1727 return status;
1728 }
1729
1730 return status;
1731}
1732
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001733static int nfs4_opendata_access(struct rpc_cred *cred,
1734 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001735 struct nfs4_state *state, fmode_t fmode,
1736 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001737{
1738 struct nfs_access_entry cache;
1739 u32 mask;
1740
1741 /* access call failed or for some reason the server doesn't
1742 * support any access modes -- defer access call until later */
1743 if (opendata->o_res.access_supported == 0)
1744 return 0;
1745
1746 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001747 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001748 * write mode bits, but POSIX allows the creating process to write.
1749 * use openflags to check for exec, because fmode won't
1750 * always have FMODE_EXEC set when file open for exec. */
1751 if (openflags & __FMODE_EXEC) {
1752 /* ONLY check for exec rights */
1753 mask = MAY_EXEC;
1754 } else if (fmode & FMODE_READ)
1755 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001756
1757 cache.cred = cred;
1758 cache.jiffies = jiffies;
1759 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1760 nfs_access_add_cache(state->inode, &cache);
1761
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001762 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001763 return 0;
1764
1765 /* even though OPEN succeeded, access is denied. Close the file */
1766 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001767 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001768}
1769
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001770/*
1771 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1772 */
1773static int _nfs4_proc_open(struct nfs4_opendata *data)
1774{
1775 struct inode *dir = data->dir->d_inode;
1776 struct nfs_server *server = NFS_SERVER(dir);
1777 struct nfs_openargs *o_arg = &data->o_arg;
1778 struct nfs_openres *o_res = &data->o_res;
1779 int status;
1780
1781 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001782 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001784 if (status != 0) {
1785 if (status == -NFS4ERR_BADNAME &&
1786 !(o_arg->open_flags & O_CREAT))
1787 return -ENOENT;
1788 return status;
1789 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001790
Trond Myklebust6926afd2012-01-07 13:22:46 -05001791 nfs_fattr_map_and_free_names(server, &data->f_attr);
1792
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001793 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001794 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001795 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1796 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001798 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001800 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001803 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Andy Adamsond83217c2011-03-01 01:34:17 +00001807static int nfs4_recover_expired_lease(struct nfs_server *server)
1808{
1809 return nfs4_client_recover_expired_lease(server->nfs_client);
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/*
1813 * OPEN_EXPIRED:
1814 * reclaim state on the server after a network partition.
1815 * Assumes caller holds the appropriate lock
1816 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001817static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001819 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001820 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001822 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001823 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001824 if (IS_ERR(opendata))
1825 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001826 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001827 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001828 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001830 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001833static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001834{
Trond Myklebust539cd032007-06-05 11:46:42 -04001835 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001836 struct nfs4_exception exception = { };
1837 int err;
1838
1839 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001840 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001841 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1842 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001843 switch (err) {
1844 default:
1845 goto out;
1846 case -NFS4ERR_GRACE:
1847 case -NFS4ERR_DELAY:
1848 nfs4_handle_exception(server, err, &exception);
1849 err = 0;
1850 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001851 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001852out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001853 return err;
1854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1857{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Trond Myklebust864472e2006-01-03 09:55:15 +01001861 ctx = nfs4_state_find_open_context(state);
1862 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001863 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001864 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001865 put_nfs_open_context(ctx);
1866 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001869#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001870static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001871{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001872 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001873 nfs4_stateid *stateid = &state->stateid;
1874 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001875
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001876 /* If a state reset has been done, test_stateid is unneeded */
1877 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1878 return;
1879
1880 status = nfs41_test_stateid(server, stateid);
1881 if (status != NFS_OK) {
1882 /* Free the stateid unless the server explicitly
1883 * informs us the stateid is unrecognized. */
1884 if (status != -NFS4ERR_BAD_STATEID)
1885 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001886 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001887
Bryan Schumaker69388672012-09-26 15:25:52 -04001888 write_seqlock(&state->seqlock);
1889 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1890 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001891 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1892 }
1893}
1894
1895/**
1896 * nfs41_check_open_stateid - possibly free an open stateid
1897 *
1898 * @state: NFSv4 state for an inode
1899 *
1900 * Returns NFS_OK if recovery for this stateid is now finished.
1901 * Otherwise a negative NFS4ERR value is returned.
1902 */
1903static int nfs41_check_open_stateid(struct nfs4_state *state)
1904{
1905 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001906 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001907 int status;
1908
1909 /* If a state reset has been done, test_stateid is unneeded */
1910 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1911 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1912 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1913 return -NFS4ERR_BAD_STATEID;
1914
1915 status = nfs41_test_stateid(server, stateid);
1916 if (status != NFS_OK) {
1917 /* Free the stateid unless the server explicitly
1918 * informs us the stateid is unrecognized. */
1919 if (status != -NFS4ERR_BAD_STATEID)
1920 nfs41_free_stateid(server, stateid);
1921
1922 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1923 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1924 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001925 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001926 }
1927 return status;
1928}
1929
1930static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1931{
Chuck Levereb64cf92012-07-11 16:30:05 -04001932 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001933
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001934 nfs41_clear_delegation_stateid(state);
1935 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001936 if (status != NFS_OK)
1937 status = nfs4_open_expired(sp, state);
1938 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001939}
1940#endif
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001943 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1944 * fields corresponding to attributes that were used to store the verifier.
1945 * Make sure we clobber those fields in the later setattr call
1946 */
1947static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1948{
1949 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1950 !(sattr->ia_valid & ATTR_ATIME_SET))
1951 sattr->ia_valid |= ATTR_ATIME;
1952
1953 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1954 !(sattr->ia_valid & ATTR_MTIME_SET))
1955 sattr->ia_valid |= ATTR_MTIME;
1956}
1957
Trond Myklebustc21443c2013-02-07 14:26:21 -05001958static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1959 fmode_t fmode,
1960 int flags,
1961 struct nfs4_state **res)
1962{
1963 struct nfs4_state_owner *sp = opendata->owner;
1964 struct nfs_server *server = sp->so_server;
1965 struct nfs4_state *state;
1966 unsigned int seq;
1967 int ret;
1968
1969 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1970
1971 ret = _nfs4_proc_open(opendata);
1972 if (ret != 0)
1973 goto out;
1974
1975 state = nfs4_opendata_to_nfs4_state(opendata);
1976 ret = PTR_ERR(state);
1977 if (IS_ERR(state))
1978 goto out;
1979 if (server->caps & NFS_CAP_POSIX_LOCK)
1980 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1981
1982 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1983 if (ret != 0)
1984 goto out;
1985
Trond Myklebust91876b12013-03-28 14:01:33 -04001986 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001987 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001988 *res = state;
1989out:
1990 return ret;
1991}
1992
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001993/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
Andy Adamson82be4172012-05-23 05:02:35 -04001996static int _nfs4_do_open(struct inode *dir,
1997 struct dentry *dentry,
1998 fmode_t fmode,
1999 int flags,
2000 struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002001 struct nfs4_label *label,
Andy Adamson82be4172012-05-23 05:02:35 -04002002 struct rpc_cred *cred,
2003 struct nfs4_state **res,
2004 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 struct nfs4_state_owner *sp;
2007 struct nfs4_state *state = NULL;
2008 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002009 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002010 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002011 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002012 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002016 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2017 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2019 goto out_err;
2020 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002021 status = nfs4_recover_expired_lease(server);
2022 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002023 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002024 if (dentry->d_inode != NULL)
2025 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002026 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002027 if (dentry->d_inode)
2028 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002029 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002030 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002031 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002032 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
David Quigley14c43f72013-05-22 12:50:43 -04002034 if (label) {
2035 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2036 if (IS_ERR(olabel)) {
2037 status = PTR_ERR(olabel);
2038 goto err_opendata_put;
2039 }
2040 }
2041
Andy Adamson82be4172012-05-23 05:02:35 -04002042 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2043 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2044 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002045 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002046 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002047 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002048 if (dentry->d_inode != NULL)
2049 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002050
Trond Myklebustc21443c2013-02-07 14:26:21 -05002051 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002052 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002053 goto err_free_label;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002054
Trond Myklebust549b19c2013-04-16 18:42:34 -04002055 if ((opendata->o_arg.open_flags & O_EXCL) &&
2056 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002057 nfs4_exclusive_attrset(opendata, sattr);
2058
2059 nfs_fattr_init(opendata->o_res.f_attr);
2060 status = nfs4_do_setattr(state->inode, cred,
2061 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002062 state, label, olabel);
2063 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002064 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002065 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2066 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002067 }
Andy Adamson82be4172012-05-23 05:02:35 -04002068
2069 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2070 *ctx_th = opendata->f_attr.mdsthreshold;
2071 else
2072 kfree(opendata->f_attr.mdsthreshold);
2073 opendata->f_attr.mdsthreshold = NULL;
2074
David Quigley14c43f72013-05-22 12:50:43 -04002075 nfs4_label_free(olabel);
2076
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002077 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 *res = state;
2080 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002081err_free_label:
2082 nfs4_label_free(olabel);
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,
David Quigley1775fd32013-05-22 12:50:42 -04002099 struct nfs4_label *label,
Andy Adamson82be4172012-05-23 05:02:35 -04002100 struct rpc_cred *cred,
2101 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002103 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 struct nfs4_exception exception = { };
2105 struct nfs4_state *res;
2106 int status;
2107
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002108 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 do {
David Quigley1775fd32013-05-22 12:50:42 -04002110 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, label, cred,
Andy Adamson82be4172012-05-23 05:02:35 -04002111 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (status == 0)
2113 break;
2114 /* NOTE: BAD_SEQID means the server and client disagree about the
2115 * book-keeping w.r.t. state-changing operations
2116 * (OPEN/CLOSE/LOCK/LOCKU...)
2117 * It is actually a sign of a bug on the client or on the server.
2118 *
2119 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002120 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * have unhashed the old state_owner for us, and that we can
2122 * therefore safely retry using a new one. We should still warn
2123 * the user though...
2124 */
2125 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002126 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002127 " returned a bad sequence-id error!\n",
2128 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 exception.retry = 1;
2130 continue;
2131 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002132 /*
2133 * BAD_STATEID on OPEN means that the server cancelled our
2134 * state before it received the OPEN_CONFIRM.
2135 * Recover by retrying the request as per the discussion
2136 * on Page 181 of RFC3530.
2137 */
2138 if (status == -NFS4ERR_BAD_STATEID) {
2139 exception.retry = 1;
2140 continue;
2141 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002142 if (status == -EAGAIN) {
2143 /* We must have found a delegation */
2144 exception.retry = 1;
2145 continue;
2146 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002147 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2148 continue;
2149 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 status, &exception));
2151 } while (exception.retry);
2152 return res;
2153}
2154
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002155static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2156 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002157 struct nfs4_state *state, struct nfs4_label *ilabel,
2158 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002160 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002162 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 .iap = sattr,
2164 .server = server,
2165 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002166 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 };
2168 struct nfs_setattrres res = {
2169 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002170 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .server = server,
2172 };
2173 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2175 .rpc_argp = &arg,
2176 .rpc_resp = &res,
2177 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002179 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002180 fmode_t fmode;
2181 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002182 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Trond Myklebust0e574af2005-10-27 22:12:38 -04002184 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Trond Myklebustee3ae842013-04-29 10:35:36 -04002186 /* Servers should only apply open mode checks for file size changes */
2187 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2188 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2189
2190 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2191 /* Use that stateid */
2192 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002193 struct nfs_lockowner lockowner = {
2194 .l_owner = current->files,
2195 .l_pid = current->tgid,
2196 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002197 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002198 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002199 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002200 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Bryan Schumaker7c513052011-03-24 17:12:24 +00002202 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002203 if (status == 0 && state != NULL)
2204 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002205 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002208static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2209 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002210 struct nfs4_state *state, struct nfs4_label *ilabel,
2211 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002213 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002214 struct nfs4_exception exception = {
2215 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002216 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002217 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 int err;
2219 do {
David Quigley1775fd32013-05-22 12:50:42 -04002220 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebust451146b2012-04-18 16:29:11 -04002221 switch (err) {
2222 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002223 if (!(sattr->ia_valid & ATTR_SIZE)) {
2224 pr_warn_once("NFSv4: server %s is incorrectly "
2225 "applying open mode checks to "
2226 "a SETATTR that is not "
2227 "changing file size.\n",
2228 server->nfs_client->cl_hostname);
2229 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002230 if (state && !(state->state & FMODE_WRITE)) {
2231 err = -EBADF;
2232 if (sattr->ia_valid & ATTR_OPEN)
2233 err = -EACCES;
2234 goto out;
2235 }
2236 }
2237 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002239out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return err;
2241}
2242
2243struct nfs4_closedata {
2244 struct inode *inode;
2245 struct nfs4_state *state;
2246 struct nfs_closeargs arg;
2247 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002248 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002249 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002250 bool roc;
2251 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252};
2253
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002254static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002255{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002256 struct nfs4_closedata *calldata = data;
2257 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002258 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002259
Fred Isamanf7e89172011-01-06 11:36:32 +00002260 if (calldata->roc)
2261 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002262 nfs4_put_open_state(calldata->state);
2263 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002264 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002265 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002266 kfree(calldata);
2267}
2268
Trond Myklebust88069f72009-12-08 08:33:16 -05002269static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2270 fmode_t fmode)
2271{
2272 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002273 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002274 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2275 case FMODE_WRITE:
2276 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2277 break;
2278 case FMODE_READ:
2279 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2280 break;
2281 case 0:
2282 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2283 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2284 clear_bit(NFS_OPEN_STATE, &state->flags);
2285 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002286 spin_unlock(&state->owner->so_lock);
2287}
2288
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002289static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002291 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 struct nfs_server *server = NFS_SERVER(calldata->inode);
2294
Chuck Levera3ca5652012-03-01 17:00:40 -05002295 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002296 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2297 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /* hmm. we are done with the inode, and in the process of freeing
2299 * the state_owner. we keep this around to process errors
2300 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 switch (task->tk_status) {
2302 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002303 if (calldata->roc)
2304 pnfs_roc_set_barrier(state->inode,
2305 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002306 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002307 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002308 nfs4_close_clear_stateid_flags(state,
2309 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 break;
2311 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002312 case -NFS4ERR_OLD_STATEID:
2313 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002315 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002316 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002318 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2319 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002321 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002322 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002323 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002326static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002328 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002329 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002330 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002331 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002332
Chuck Levera3ca5652012-03-01 17:00:40 -05002333 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002334 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002335 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002336
Trond Myklebust88069f72009-12-08 08:33:16 -05002337 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2338 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002339 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002340 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002341 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002342 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002343 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2344 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2345 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002346 }
2347 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002348 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2349 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2350 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002351 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002352 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002353 if (!nfs4_valid_open_stateid(state))
2354 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002355 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002356
2357 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002358 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002359 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002360 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002361
Fred Isamanf7e89172011-01-06 11:36:32 +00002362 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002363 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002364 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002365 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2366 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002367 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002368 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002369 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002370
Trond Myklebust516a6af2005-10-27 22:12:41 -04002371 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002372 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002373 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002374 &calldata->arg.seq_args,
2375 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002376 task) != 0)
2377 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002378 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002379 return;
2380out_no_action:
2381 task->tk_action = NULL;
2382out_wait:
2383 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002386static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002387 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002388 .rpc_call_done = nfs4_close_done,
2389 .rpc_release = nfs4_free_closedata,
2390};
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/*
2393 * It is possible for data to be read/written from a mem-mapped file
2394 * after the sys_close call (which hits the vfs layer as a flush).
2395 * This means that we can't safely call nfsv4 close on a file until
2396 * the inode is cleared. This in turn means that we are not good
2397 * NFSv4 citizens - we do not indicate to the server to update the file's
2398 * share state even when we are done with one of the three share
2399 * stateid's in the inode.
2400 *
2401 * NOTE: Caller must be holding the sp->so_owner semaphore!
2402 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002403int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002405 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002407 struct nfs4_state_owner *sp = state->owner;
2408 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002409 struct rpc_message msg = {
2410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2411 .rpc_cred = state->owner->so_cred,
2412 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002413 struct rpc_task_setup task_setup_data = {
2414 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002415 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002416 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002417 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002418 .flags = RPC_TASK_ASYNC,
2419 };
Trond Myklebust95121352005-10-18 14:20:12 -07002420 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002422 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002424 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002425 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002426 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002428 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002429 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002431 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002432 if (calldata->arg.seqid == NULL)
2433 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002434 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002435 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002436 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002437 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002438 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002439 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002440 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002441
Trond Myklebust1174dd12010-12-21 10:52:24 -05002442 msg.rpc_argp = &calldata->arg;
2443 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002444 task_setup_data.callback_data = calldata;
2445 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002446 if (IS_ERR(task))
2447 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002448 status = 0;
2449 if (wait)
2450 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002451 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002452 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002453out_free_calldata:
2454 kfree(calldata);
2455out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002456 nfs4_put_open_state(state);
2457 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002458 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Trond Myklebust2b484292010-09-17 10:56:51 -04002461static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002462nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct nfs4_state *state;
David Quigley1775fd32013-05-22 12:50:42 -04002465 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Trond Myklebust565277f2007-10-15 18:17:53 -04002467 /* Protect against concurrent sillydeletes */
David Quigley1775fd32013-05-22 12:50:42 -04002468 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, label,
Andy Adamson82be4172012-05-23 05:02:35 -04002469 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002470 if (IS_ERR(state))
2471 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002472 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002473 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Trond Myklebust1185a552009-12-03 15:54:02 -05002476static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002477{
2478 if (ctx->state == NULL)
2479 return;
2480 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002481 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002482 else
Al Viro643168c2011-06-22 18:20:23 -04002483 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2487{
Benny Halevy43652ad2009-04-01 09:21:54 -04002488 struct nfs4_server_caps_arg args = {
2489 .fhandle = fhandle,
2490 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 struct nfs4_server_caps_res res = {};
2492 struct rpc_message msg = {
2493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002494 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 .rpc_resp = &res,
2496 };
2497 int status;
2498
Bryan Schumaker7c513052011-03-24 17:12:24 +00002499 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (status == 0) {
2501 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002502 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2503 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2504 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2505 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2506 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2508 server->caps |= NFS_CAP_ACLS;
2509 if (res.has_links != 0)
2510 server->caps |= NFS_CAP_HARDLINKS;
2511 if (res.has_symlinks != 0)
2512 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002513 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2514 server->caps |= NFS_CAP_FILEID;
2515 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2516 server->caps |= NFS_CAP_MODE;
2517 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2518 server->caps |= NFS_CAP_NLINK;
2519 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2520 server->caps |= NFS_CAP_OWNER;
2521 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2522 server->caps |= NFS_CAP_OWNER_GROUP;
2523 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2524 server->caps |= NFS_CAP_ATIME;
2525 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2526 server->caps |= NFS_CAP_CTIME;
2527 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2528 server->caps |= NFS_CAP_MTIME;
2529
Trond Myklebusta65318b2009-03-11 14:10:28 -04002530 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2531 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2532 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002534 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return status;
2538}
2539
Trond Myklebust55a97592006-06-09 09:34:19 -04002540int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
2545 err = nfs4_handle_exception(server,
2546 _nfs4_server_capabilities(server, fhandle),
2547 &exception);
2548 } while (exception.retry);
2549 return err;
2550}
2551
2552static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2553 struct nfs_fsinfo *info)
2554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 struct nfs4_lookup_root_arg args = {
2556 .bitmask = nfs4_fattr_bitmap,
2557 };
2558 struct nfs4_lookup_res res = {
2559 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002560 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 .fh = fhandle,
2562 };
2563 struct rpc_message msg = {
2564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2565 .rpc_argp = &args,
2566 .rpc_resp = &res,
2567 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002568
Trond Myklebust0e574af2005-10-27 22:12:38 -04002569 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002570 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2574 struct nfs_fsinfo *info)
2575{
2576 struct nfs4_exception exception = { };
2577 int err;
2578 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002579 err = _nfs4_lookup_root(server, fhandle, info);
2580 switch (err) {
2581 case 0:
2582 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002583 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002584 default:
2585 err = nfs4_handle_exception(server, err, &exception);
2586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002588out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return err;
2590}
2591
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002592static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2593 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2594{
2595 struct rpc_auth *auth;
2596 int ret;
2597
2598 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002599 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002600 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002601 goto out;
2602 }
2603 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002604out:
2605 return ret;
2606}
2607
Chuck Lever9a744ba2013-03-16 15:56:02 -04002608/*
2609 * Retry pseudoroot lookup with various security flavors. We do this when:
2610 *
2611 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2612 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2613 *
2614 * Returns zero on success, or a negative NFS4ERR value, or a
2615 * negative errno value.
2616 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002617static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002618 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002620 /* Per 3530bis 15.33.5 */
2621 static const rpc_authflavor_t flav_array[] = {
2622 RPC_AUTH_GSS_KRB5P,
2623 RPC_AUTH_GSS_KRB5I,
2624 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002625 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002626 RPC_AUTH_NULL,
2627 };
2628 int status = -EPERM;
2629 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Chuck Lever9a744ba2013-03-16 15:56:02 -04002631 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002632 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002633 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002634 continue;
2635 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002636 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002637
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002638 /*
2639 * -EACCESS could mean that the user doesn't have correct permissions
2640 * to access the mount. It could also mean that we tried to mount
2641 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2642 * existing mount programs don't handle -EACCES very well so it should
2643 * be mapped to -EPERM instead.
2644 */
2645 if (status == -EACCES)
2646 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002647 return status;
2648}
2649
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002650static int nfs4_do_find_root_sec(struct nfs_server *server,
2651 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2652{
2653 int mv = server->nfs_client->cl_minorversion;
2654 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2655}
2656
2657/**
2658 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2659 * @server: initialized nfs_server handle
2660 * @fhandle: we fill in the pseudo-fs root file handle
2661 * @info: we fill in an FSINFO struct
2662 *
2663 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002664 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002665int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2666 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002667{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002668 int status;
2669
2670 status = nfs4_lookup_root(server, fhandle, info);
2671 if ((status == -NFS4ERR_WRONGSEC) &&
2672 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2673 status = nfs4_do_find_root_sec(server, fhandle, info);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (status == 0)
2676 status = nfs4_server_capabilities(server, fhandle);
2677 if (status == 0)
2678 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002679
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002680 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Bryan Schumakerbae36242012-05-10 15:07:31 -04002683static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2684 struct nfs_fsinfo *info)
2685{
2686 int error;
2687 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002688 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002689
2690 error = nfs4_server_capabilities(server, mntfh);
2691 if (error < 0) {
2692 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2693 return error;
2694 }
2695
David Quigley14c43f72013-05-22 12:50:43 -04002696 label = nfs4_label_alloc(server, GFP_KERNEL);
2697 if (IS_ERR(label))
2698 return PTR_ERR(label);
2699
David Quigley1775fd32013-05-22 12:50:42 -04002700 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002701 if (error < 0) {
2702 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002703 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002704 }
2705
2706 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2707 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2708 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2709
David Quigley14c43f72013-05-22 12:50:43 -04002710err_free_label:
2711 nfs4_label_free(label);
2712
Bryan Schumakerbae36242012-05-10 15:07:31 -04002713 return error;
2714}
2715
Manoj Naik6b97fd32006-06-09 09:34:29 -04002716/*
2717 * Get locations and (maybe) other attributes of a referral.
2718 * Note that we'll actually follow the referral later when
2719 * we detect fsid mismatch in inode revalidation
2720 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002721static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2722 const struct qstr *name, struct nfs_fattr *fattr,
2723 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002724{
2725 int status = -ENOMEM;
2726 struct page *page = NULL;
2727 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002728
2729 page = alloc_page(GFP_KERNEL);
2730 if (page == NULL)
2731 goto out;
2732 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2733 if (locations == NULL)
2734 goto out;
2735
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002736 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002737 if (status != 0)
2738 goto out;
2739 /* Make sure server returned a different fsid for the referral */
2740 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002741 dprintk("%s: server did not return a different fsid for"
2742 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002743 status = -EIO;
2744 goto out;
2745 }
Andy Adamson533eb462011-06-13 18:25:56 -04002746 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2747 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002748
Andy Adamson533eb462011-06-13 18:25:56 -04002749 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002750 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002751 memset(fhandle, 0, sizeof(struct nfs_fh));
2752out:
2753 if (page)
2754 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002755 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002756 return status;
2757}
2758
David Quigley1775fd32013-05-22 12:50:42 -04002759static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2760 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 struct nfs4_getattr_arg args = {
2763 .fh = fhandle,
2764 .bitmask = server->attr_bitmask,
2765 };
2766 struct nfs4_getattr_res res = {
2767 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002768 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 .server = server,
2770 };
2771 struct rpc_message msg = {
2772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2773 .rpc_argp = &args,
2774 .rpc_resp = &res,
2775 };
2776
Trond Myklebust0e574af2005-10-27 22:12:38 -04002777 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002778 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
David Quigley1775fd32013-05-22 12:50:42 -04002781static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2782 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 struct nfs4_exception exception = { };
2785 int err;
2786 do {
2787 err = nfs4_handle_exception(server,
David Quigley1775fd32013-05-22 12:50:42 -04002788 _nfs4_proc_getattr(server, fhandle, fattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 &exception);
2790 } while (exception.retry);
2791 return err;
2792}
2793
2794/*
2795 * The file is not closed if it is opened due to the a request to change
2796 * the size of the file. The open call will not be needed once the
2797 * VFS layer lookup-intents are implemented.
2798 *
2799 * Close is called when the inode is destroyed.
2800 * If we haven't opened the file for O_WRONLY, we
2801 * need to in the size_change case to obtain a stateid.
2802 *
2803 * Got race?
2804 * Because OPEN is always done by name in nfsv4, it is
2805 * possible that we opened a different file by the same
2806 * name. We can recognize this race condition, but we
2807 * can't do anything about it besides returning an error.
2808 *
2809 * This will be fixed with VFS changes (lookup-intent).
2810 */
2811static int
2812nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2813 struct iattr *sattr)
2814{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002815 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002816 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002817 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002818 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 int status;
2820
Benny Halevy8a1636c2010-07-14 15:43:57 -04002821 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002822 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002823
Trond Myklebust0e574af2005-10-27 22:12:38 -04002824 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Andy Adamson26699402012-05-30 16:12:24 -04002826 /* Deal with open(O_TRUNC) */
2827 if (sattr->ia_valid & ATTR_OPEN)
2828 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2829
2830 /* Optimization: if the end result is no change, don't RPC */
2831 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2832 return 0;
2833
Trond Myklebustd5308382005-11-04 15:33:38 -05002834 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002835 if (sattr->ia_valid & ATTR_FILE) {
2836 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002837
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002838 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002839 if (ctx) {
2840 cred = ctx->cred;
2841 state = ctx->state;
2842 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002843 }
2844
David Quigley14c43f72013-05-22 12:50:43 -04002845 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2846 if (IS_ERR(label))
2847 return PTR_ERR(label);
2848
2849 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
Trond Myklebust65e43082005-08-16 11:49:44 -04002850 if (status == 0)
2851 nfs_setattr_update_inode(inode, sattr);
David Quigley14c43f72013-05-22 12:50:43 -04002852
2853 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return status;
2855}
2856
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002857static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2858 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002859 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04002860{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002861 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002862 int status;
2863 struct nfs4_lookup_arg args = {
2864 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002865 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002866 .name = name,
2867 };
2868 struct nfs4_lookup_res res = {
2869 .server = server,
2870 .fattr = fattr,
2871 .fh = fhandle,
2872 };
2873 struct rpc_message msg = {
2874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2875 .rpc_argp = &args,
2876 .rpc_resp = &res,
2877 };
2878
2879 nfs_fattr_init(fattr);
2880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002882 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 dprintk("NFS reply lookup: %d\n", status);
2884 return status;
2885}
2886
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002887static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002888{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002889 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002890 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002891 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2892 fattr->nlink = 2;
2893}
2894
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002895static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2896 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002897 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002898{
2899 struct nfs4_exception exception = { };
2900 struct rpc_clnt *client = *clnt;
2901 int err;
2902 do {
David Quigley1775fd32013-05-22 12:50:42 -04002903 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002904 switch (err) {
2905 case -NFS4ERR_BADNAME:
2906 err = -ENOENT;
2907 goto out;
2908 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002909 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002910 goto out;
2911 case -NFS4ERR_WRONGSEC:
2912 err = -EPERM;
2913 if (client != *clnt)
2914 goto out;
2915
2916 client = nfs4_create_sec_client(client, dir, name);
2917 if (IS_ERR(client))
2918 return PTR_ERR(client);
2919
2920 exception.retry = 1;
2921 break;
2922 default:
2923 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2924 }
2925 } while (exception.retry);
2926
2927out:
2928 if (err == 0)
2929 *clnt = client;
2930 else if (client != *clnt)
2931 rpc_shutdown_client(client);
2932
2933 return err;
2934}
2935
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002936static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04002937 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
2938 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002940 int status;
2941 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002942
David Quigley1775fd32013-05-22 12:50:42 -04002943 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002944 if (client != NFS_CLIENT(dir)) {
2945 rpc_shutdown_client(client);
2946 nfs_fixup_secinfo_attributes(fattr);
2947 }
2948 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002951struct rpc_clnt *
2952nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2953 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2954{
2955 int status;
2956 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2957
David Quigley1775fd32013-05-22 12:50:42 -04002958 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002959 if (status < 0) {
2960 rpc_shutdown_client(client);
2961 return ERR_PTR(status);
2962 }
2963 return client;
2964}
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2967{
Trond Myklebust76b32992007-08-10 17:45:11 -04002968 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct nfs4_accessargs args = {
2970 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002971 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002973 struct nfs4_accessres res = {
2974 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002975 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 struct rpc_message msg = {
2977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2978 .rpc_argp = &args,
2979 .rpc_resp = &res,
2980 .rpc_cred = entry->cred,
2981 };
2982 int mode = entry->mask;
2983 int status;
2984
2985 /*
2986 * Determine which access bits we want to ask for...
2987 */
2988 if (mode & MAY_READ)
2989 args.access |= NFS4_ACCESS_READ;
2990 if (S_ISDIR(inode->i_mode)) {
2991 if (mode & MAY_WRITE)
2992 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2993 if (mode & MAY_EXEC)
2994 args.access |= NFS4_ACCESS_LOOKUP;
2995 } else {
2996 if (mode & MAY_WRITE)
2997 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2998 if (mode & MAY_EXEC)
2999 args.access |= NFS4_ACCESS_EXECUTE;
3000 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003001
3002 res.fattr = nfs_alloc_fattr();
3003 if (res.fattr == NULL)
3004 return -ENOMEM;
3005
Bryan Schumaker7c513052011-03-24 17:12:24 +00003006 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003008 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003009 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003011 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return status;
3013}
3014
3015static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3016{
3017 struct nfs4_exception exception = { };
3018 int err;
3019 do {
3020 err = nfs4_handle_exception(NFS_SERVER(inode),
3021 _nfs4_proc_access(inode, entry),
3022 &exception);
3023 } while (exception.retry);
3024 return err;
3025}
3026
3027/*
3028 * TODO: For the time being, we don't try to get any attributes
3029 * along with any of the zero-copy operations READ, READDIR,
3030 * READLINK, WRITE.
3031 *
3032 * In the case of the first three, we want to put the GETATTR
3033 * after the read-type operation -- this is because it is hard
3034 * to predict the length of a GETATTR response in v4, and thus
3035 * align the READ data correctly. This means that the GETATTR
3036 * may end up partially falling into the page cache, and we should
3037 * shift it into the 'tail' of the xdr_buf before processing.
3038 * To do this efficiently, we need to know the total length
3039 * of data received, which doesn't seem to be available outside
3040 * of the RPC layer.
3041 *
3042 * In the case of WRITE, we also want to put the GETATTR after
3043 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003044 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 *
3046 * Both of these changes to the XDR layer would in fact be quite
3047 * minor, but I decided to leave them for a subsequent patch.
3048 */
3049static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3050 unsigned int pgbase, unsigned int pglen)
3051{
3052 struct nfs4_readlink args = {
3053 .fh = NFS_FH(inode),
3054 .pgbase = pgbase,
3055 .pglen = pglen,
3056 .pages = &page,
3057 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003058 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 struct rpc_message msg = {
3060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3061 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003062 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 };
3064
Bryan Schumaker7c513052011-03-24 17:12:24 +00003065 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 -07003066}
3067
3068static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3069 unsigned int pgbase, unsigned int pglen)
3070{
3071 struct nfs4_exception exception = { };
3072 int err;
3073 do {
3074 err = nfs4_handle_exception(NFS_SERVER(inode),
3075 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3076 &exception);
3077 } while (exception.retry);
3078 return err;
3079}
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003082 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084static int
3085nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003086 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
David Quigley1775fd32013-05-22 12:50:42 -04003088 struct nfs4_label *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003089 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 int status = 0;
3092
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003093 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3094 if (IS_ERR(ctx))
3095 return PTR_ERR(ctx);
3096
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003097 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003098 state = nfs4_do_open(dir, dentry, ctx->mode,
David Quigley1775fd32013-05-22 12:50:42 -04003099 flags, sattr, ilabel, ctx->cred,
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003100 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003101 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (IS_ERR(state)) {
3103 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003104 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003106 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003107 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003108 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003110 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return status;
3112}
3113
3114static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3115{
Trond Myklebust16e42952005-10-27 22:12:44 -04003116 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003117 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003119 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003121 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003122 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003123 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3126 .rpc_argp = &args,
3127 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003129 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003130
Bryan Schumaker7c513052011-03-24 17:12:24 +00003131 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003132 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003133 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return status;
3135}
3136
3137static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3138{
3139 struct nfs4_exception exception = { };
3140 int err;
3141 do {
3142 err = nfs4_handle_exception(NFS_SERVER(dir),
3143 _nfs4_proc_remove(dir, name),
3144 &exception);
3145 } while (exception.retry);
3146 return err;
3147}
3148
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003149static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003151 struct nfs_server *server = NFS_SERVER(dir);
3152 struct nfs_removeargs *args = msg->rpc_argp;
3153 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003155 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003157 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003160static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3161{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003162 nfs4_setup_sequence(NFS_SERVER(data->dir),
3163 &data->args.seq_args,
3164 &data->res.seq_res,
3165 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003168static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003170 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3171
Trond Myklebust14516c32010-07-31 14:29:06 -04003172 if (!nfs4_sequence_done(task, &res->seq_res))
3173 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003174 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003175 return 0;
3176 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003177 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Jeff Laytond3d41522010-09-17 17:31:57 -04003180static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3181{
3182 struct nfs_server *server = NFS_SERVER(dir);
3183 struct nfs_renameargs *arg = msg->rpc_argp;
3184 struct nfs_renameres *res = msg->rpc_resp;
3185
3186 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003187 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003188 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003189}
3190
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003191static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3192{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003193 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3194 &data->args.seq_args,
3195 &data->res.seq_res,
3196 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003197}
3198
3199static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3200 struct inode *new_dir)
3201{
3202 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3203
3204 if (!nfs4_sequence_done(task, &res->seq_res))
3205 return 0;
3206 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3207 return 0;
3208
3209 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003210 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003211 return 1;
3212}
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3215 struct inode *new_dir, struct qstr *new_name)
3216{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003217 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003218 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 .old_dir = NFS_FH(old_dir),
3220 .new_dir = NFS_FH(new_dir),
3221 .old_name = old_name,
3222 .new_name = new_name,
3223 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003224 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003225 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003226 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 struct rpc_message msg = {
3228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3229 .rpc_argp = &arg,
3230 .rpc_resp = &res,
3231 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003232 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003233
Bryan Schumaker7c513052011-03-24 17:12:24 +00003234 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (!status) {
3236 update_changeattr(old_dir, &res.old_cinfo);
3237 update_changeattr(new_dir, &res.new_cinfo);
3238 }
3239 return status;
3240}
3241
3242static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3243 struct inode *new_dir, struct qstr *new_name)
3244{
3245 struct nfs4_exception exception = { };
3246 int err;
3247 do {
3248 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3249 _nfs4_proc_rename(old_dir, old_name,
3250 new_dir, new_name),
3251 &exception);
3252 } while (exception.retry);
3253 return err;
3254}
3255
3256static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3257{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003258 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 struct nfs4_link_arg arg = {
3260 .fh = NFS_FH(inode),
3261 .dir_fh = NFS_FH(dir),
3262 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003263 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003265 struct nfs4_link_res res = {
3266 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003267 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003268 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 struct rpc_message msg = {
3270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3271 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003272 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003274 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Trond Myklebust136f2622010-04-16 16:22:49 -04003276 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003277 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003278 goto out;
3279
David Quigley14c43f72013-05-22 12:50:43 -04003280 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3281 if (IS_ERR(res.label)) {
3282 status = PTR_ERR(res.label);
3283 goto out;
3284 }
3285
Bryan Schumaker7c513052011-03-24 17:12:24 +00003286 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003287 if (!status) {
3288 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003289 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003290 }
David Quigley14c43f72013-05-22 12:50:43 -04003291
3292
3293 nfs4_label_free(res.label);
3294
Trond Myklebust136f2622010-04-16 16:22:49 -04003295out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003296 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return status;
3298}
3299
3300static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3301{
3302 struct nfs4_exception exception = { };
3303 int err;
3304 do {
3305 err = nfs4_handle_exception(NFS_SERVER(inode),
3306 _nfs4_proc_link(inode, dir, name),
3307 &exception);
3308 } while (exception.retry);
3309 return err;
3310}
3311
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003312struct nfs4_createdata {
3313 struct rpc_message msg;
3314 struct nfs4_create_arg arg;
3315 struct nfs4_create_res res;
3316 struct nfs_fh fh;
3317 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003318 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003319};
3320
3321static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3322 struct qstr *name, struct iattr *sattr, u32 ftype)
3323{
3324 struct nfs4_createdata *data;
3325
3326 data = kzalloc(sizeof(*data), GFP_KERNEL);
3327 if (data != NULL) {
3328 struct nfs_server *server = NFS_SERVER(dir);
3329
David Quigley14c43f72013-05-22 12:50:43 -04003330 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3331 if (IS_ERR(data->label))
3332 goto out_free;
3333
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003334 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3335 data->msg.rpc_argp = &data->arg;
3336 data->msg.rpc_resp = &data->res;
3337 data->arg.dir_fh = NFS_FH(dir);
3338 data->arg.server = server;
3339 data->arg.name = name;
3340 data->arg.attrs = sattr;
3341 data->arg.ftype = ftype;
3342 data->arg.bitmask = server->attr_bitmask;
3343 data->res.server = server;
3344 data->res.fh = &data->fh;
3345 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003346 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003347 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003348 }
3349 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003350out_free:
3351 kfree(data);
3352 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003353}
3354
3355static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3356{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003357 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003358 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003359 if (status == 0) {
3360 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003361 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003362 }
3363 return status;
3364}
3365
3366static void nfs4_free_createdata(struct nfs4_createdata *data)
3367{
David Quigley14c43f72013-05-22 12:50:43 -04003368 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003369 kfree(data);
3370}
3371
Chuck Lever4f390c12006-08-22 20:06:22 -04003372static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003373 struct page *page, unsigned int len, struct iattr *sattr,
3374 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003376 struct nfs4_createdata *data;
3377 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Chuck Lever94a6d752006-08-22 20:06:23 -04003379 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003380 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003381
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003382 status = -ENOMEM;
3383 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3384 if (data == NULL)
3385 goto out;
3386
3387 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3388 data->arg.u.symlink.pages = &page;
3389 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003390 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003392 status = nfs4_do_create(dir, dentry, data);
3393
3394 nfs4_free_createdata(data);
3395out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return status;
3397}
3398
Chuck Lever4f390c12006-08-22 20:06:22 -04003399static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003400 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
3402 struct nfs4_exception exception = { };
David Quigley1775fd32013-05-22 12:50:42 -04003403 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 int err;
3405 do {
3406 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003407 _nfs4_proc_symlink(dir, dentry, page,
David Quigley1775fd32013-05-22 12:50:42 -04003408 len, sattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 &exception);
3410 } while (exception.retry);
3411 return err;
3412}
3413
3414static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003415 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003417 struct nfs4_createdata *data;
3418 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003420 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3421 if (data == NULL)
3422 goto out;
3423
David Quigley1775fd32013-05-22 12:50:42 -04003424 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003425 status = nfs4_do_create(dir, dentry, data);
3426
3427 nfs4_free_createdata(data);
3428out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return status;
3430}
3431
3432static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3433 struct iattr *sattr)
3434{
3435 struct nfs4_exception exception = { };
David Quigley1775fd32013-05-22 12:50:42 -04003436 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003438
3439 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 do {
3441 err = nfs4_handle_exception(NFS_SERVER(dir),
David Quigley1775fd32013-05-22 12:50:42 -04003442 _nfs4_proc_mkdir(dir, dentry, sattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 &exception);
3444 } while (exception.retry);
3445 return err;
3446}
3447
3448static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003449 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
3451 struct inode *dir = dentry->d_inode;
3452 struct nfs4_readdir_arg args = {
3453 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003454 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 .pgbase = 0,
3456 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003457 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003458 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 };
3460 struct nfs4_readdir_res res;
3461 struct rpc_message msg = {
3462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3463 .rpc_argp = &args,
3464 .rpc_resp = &res,
3465 .rpc_cred = cred,
3466 };
3467 int status;
3468
Harvey Harrison3110ff82008-05-02 13:42:44 -07003469 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003470 dentry->d_parent->d_name.name,
3471 dentry->d_name.name,
3472 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003473 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003475 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 -05003476 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003477 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003478 status += args.pgbase;
3479 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003480
3481 nfs_invalidate_atime(dir);
3482
Harvey Harrison3110ff82008-05-02 13:42:44 -07003483 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return status;
3485}
3486
3487static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003488 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
3490 struct nfs4_exception exception = { };
3491 int err;
3492 do {
3493 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3494 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003495 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 &exception);
3497 } while (exception.retry);
3498 return err;
3499}
3500
3501static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3502 struct iattr *sattr, dev_t rdev)
3503{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003504 struct nfs4_createdata *data;
3505 int mode = sattr->ia_mode;
3506 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003508 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3509 if (data == NULL)
3510 goto out;
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003513 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003515 data->arg.ftype = NF4BLK;
3516 data->arg.u.device.specdata1 = MAJOR(rdev);
3517 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 }
3519 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003520 data->arg.ftype = NF4CHR;
3521 data->arg.u.device.specdata1 = MAJOR(rdev);
3522 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003523 } else if (!S_ISSOCK(mode)) {
3524 status = -EINVAL;
3525 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
David Quigley1775fd32013-05-22 12:50:42 -04003527
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003528 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003529out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003530 nfs4_free_createdata(data);
3531out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return status;
3533}
3534
3535static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3536 struct iattr *sattr, dev_t rdev)
3537{
3538 struct nfs4_exception exception = { };
3539 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003540
3541 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 do {
3543 err = nfs4_handle_exception(NFS_SERVER(dir),
3544 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3545 &exception);
3546 } while (exception.retry);
3547 return err;
3548}
3549
3550static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3551 struct nfs_fsstat *fsstat)
3552{
3553 struct nfs4_statfs_arg args = {
3554 .fh = fhandle,
3555 .bitmask = server->attr_bitmask,
3556 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003557 struct nfs4_statfs_res res = {
3558 .fsstat = fsstat,
3559 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 struct rpc_message msg = {
3561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3562 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003563 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 };
3565
Trond Myklebust0e574af2005-10-27 22:12:38 -04003566 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003567 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
3570static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3571{
3572 struct nfs4_exception exception = { };
3573 int err;
3574 do {
3575 err = nfs4_handle_exception(server,
3576 _nfs4_proc_statfs(server, fhandle, fsstat),
3577 &exception);
3578 } while (exception.retry);
3579 return err;
3580}
3581
3582static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3583 struct nfs_fsinfo *fsinfo)
3584{
3585 struct nfs4_fsinfo_arg args = {
3586 .fh = fhandle,
3587 .bitmask = server->attr_bitmask,
3588 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003589 struct nfs4_fsinfo_res res = {
3590 .fsinfo = fsinfo,
3591 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 struct rpc_message msg = {
3593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3594 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003595 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 };
3597
Bryan Schumaker7c513052011-03-24 17:12:24 +00003598 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600
3601static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3602{
3603 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003604 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 int err;
3606
3607 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003608 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3609 if (err == 0) {
3610 struct nfs_client *clp = server->nfs_client;
3611
3612 spin_lock(&clp->cl_lock);
3613 clp->cl_lease_time = fsinfo->lease_time * HZ;
3614 clp->cl_last_renewal = now;
3615 spin_unlock(&clp->cl_lock);
3616 break;
3617 }
3618 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 } while (exception.retry);
3620 return err;
3621}
3622
3623static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3624{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003625 int error;
3626
Trond Myklebust0e574af2005-10-27 22:12:38 -04003627 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003628 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003629 if (error == 0) {
3630 /* block layout checks this! */
3631 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003632 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003633 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003634
3635 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636}
3637
3638static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3639 struct nfs_pathconf *pathconf)
3640{
3641 struct nfs4_pathconf_arg args = {
3642 .fh = fhandle,
3643 .bitmask = server->attr_bitmask,
3644 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003645 struct nfs4_pathconf_res res = {
3646 .pathconf = pathconf,
3647 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 struct rpc_message msg = {
3649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3650 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003651 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 };
3653
3654 /* None of the pathconf attributes are mandatory to implement */
3655 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3656 memset(pathconf, 0, sizeof(*pathconf));
3657 return 0;
3658 }
3659
Trond Myklebust0e574af2005-10-27 22:12:38 -04003660 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003661 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
3664static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3665 struct nfs_pathconf *pathconf)
3666{
3667 struct nfs4_exception exception = { };
3668 int err;
3669
3670 do {
3671 err = nfs4_handle_exception(server,
3672 _nfs4_proc_pathconf(server, fhandle, pathconf),
3673 &exception);
3674 } while (exception.retry);
3675 return err;
3676}
3677
Trond Myklebust5521abf2013-03-16 20:54:34 -04003678int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003679 const struct nfs_open_context *ctx,
3680 const struct nfs_lock_context *l_ctx,
3681 fmode_t fmode)
3682{
3683 const struct nfs_lockowner *lockowner = NULL;
3684
3685 if (l_ctx != NULL)
3686 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003687 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003688}
3689EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3690
Trond Myklebust5521abf2013-03-16 20:54:34 -04003691static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3692 const struct nfs_open_context *ctx,
3693 const struct nfs_lock_context *l_ctx,
3694 fmode_t fmode)
3695{
3696 nfs4_stateid current_stateid;
3697
3698 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3699 return false;
3700 return nfs4_stateid_match(stateid, &current_stateid);
3701}
3702
3703static bool nfs4_error_stateid_expired(int err)
3704{
3705 switch (err) {
3706 case -NFS4ERR_DELEG_REVOKED:
3707 case -NFS4ERR_ADMIN_REVOKED:
3708 case -NFS4ERR_BAD_STATEID:
3709 case -NFS4ERR_STALE_STATEID:
3710 case -NFS4ERR_OLD_STATEID:
3711 case -NFS4ERR_OPENMODE:
3712 case -NFS4ERR_EXPIRED:
3713 return true;
3714 }
3715 return false;
3716}
3717
Benny Halevyd20581a2011-05-22 19:52:03 +03003718void __nfs4_read_done_cb(struct nfs_read_data *data)
3719{
Fred Isamancd841602012-04-20 14:47:44 -04003720 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003721}
3722
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003723static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
Fred Isamancd841602012-04-20 14:47:44 -04003725 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003727 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003728 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003729 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003731
Benny Halevyd20581a2011-05-22 19:52:03 +03003732 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003734 renew_lease(server, data->timestamp);
3735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Trond Myklebust5521abf2013-03-16 20:54:34 -04003738static bool nfs4_read_stateid_changed(struct rpc_task *task,
3739 struct nfs_readargs *args)
3740{
3741
3742 if (!nfs4_error_stateid_expired(task->tk_status) ||
3743 nfs4_stateid_is_current(&args->stateid,
3744 args->context,
3745 args->lock_context,
3746 FMODE_READ))
3747 return false;
3748 rpc_restart_call_prepare(task);
3749 return true;
3750}
3751
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003752static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3753{
3754
3755 dprintk("--> %s\n", __func__);
3756
3757 if (!nfs4_sequence_done(task, &data->res.seq_res))
3758 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003759 if (nfs4_read_stateid_changed(task, &data->args))
3760 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003761 return data->read_done_cb ? data->read_done_cb(task, data) :
3762 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003763}
3764
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003765static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003768 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003769 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003770 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771}
3772
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003773static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3774{
Trond Myklebust9b206142013-03-17 15:52:00 -04003775 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003776 &data->args.seq_args,
3777 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003778 task))
3779 return;
3780 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3781 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
Fred Isamanb029bc92011-03-03 15:13:42 +00003784static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
Fred Isamancd841602012-04-20 14:47:44 -04003786 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003788 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003789 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003790 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003792 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003794 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003795 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Trond Myklebust5521abf2013-03-16 20:54:34 -04003799static bool nfs4_write_stateid_changed(struct rpc_task *task,
3800 struct nfs_writeargs *args)
3801{
3802
3803 if (!nfs4_error_stateid_expired(task->tk_status) ||
3804 nfs4_stateid_is_current(&args->stateid,
3805 args->context,
3806 args->lock_context,
3807 FMODE_WRITE))
3808 return false;
3809 rpc_restart_call_prepare(task);
3810 return true;
3811}
3812
Fred Isamanb029bc92011-03-03 15:13:42 +00003813static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3814{
3815 if (!nfs4_sequence_done(task, &data->res.seq_res))
3816 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003817 if (nfs4_write_stateid_changed(task, &data->args))
3818 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003819 return data->write_done_cb ? data->write_done_cb(task, data) :
3820 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003821}
3822
Trond Myklebust5a37f852012-04-28 14:55:16 -04003823static
3824bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003825{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003826 const struct nfs_pgio_header *hdr = data->header;
3827
3828 /* Don't request attributes for pNFS or O_DIRECT writes */
3829 if (data->ds_clp != NULL || hdr->dreq != NULL)
3830 return false;
3831 /* Otherwise, request attributes if and only if we don't hold
3832 * a delegation
3833 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003834 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003835}
Fred Isamana69aef12011-03-03 15:13:47 +00003836
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003837static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
Fred Isamancd841602012-04-20 14:47:44 -04003839 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003840
Trond Myklebust5a37f852012-04-28 14:55:16 -04003841 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003842 data->args.bitmask = NULL;
3843 data->res.fattr = NULL;
3844 } else
3845 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003846
Fred Isamanb029bc92011-03-03 15:13:42 +00003847 if (!data->write_done_cb)
3848 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003849 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 data->timestamp = jiffies;
3851
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003852 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003853 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854}
3855
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003856static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3857{
Trond Myklebust9b206142013-03-17 15:52:00 -04003858 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003859 &data->args.seq_args,
3860 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003861 task))
3862 return;
3863 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3864 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865}
3866
Fred Isaman0b7c0152012-04-20 14:47:39 -04003867static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3868{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003869 nfs4_setup_sequence(NFS_SERVER(data->inode),
3870 &data->args.seq_args,
3871 &data->res.seq_res,
3872 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
Fred Isaman0b7c0152012-04-20 14:47:39 -04003875static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003878
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003879 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003880 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003881 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
Fred Isaman0b7c0152012-04-20 14:47:39 -04003886static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003887{
3888 if (!nfs4_sequence_done(task, &data->res.seq_res))
3889 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003890 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003891}
3892
Fred Isaman0b7c0152012-04-20 14:47:39 -04003893static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003895 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003896
Fred Isaman0b7c0152012-04-20 14:47:39 -04003897 if (data->commit_done_cb == NULL)
3898 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003899 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003900 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003901 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
3903
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003904struct nfs4_renewdata {
3905 struct nfs_client *client;
3906 unsigned long timestamp;
3907};
3908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909/*
3910 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3911 * standalone procedure for queueing an asynchronous RENEW.
3912 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003913static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003914{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003915 struct nfs4_renewdata *data = calldata;
3916 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003917
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003918 if (atomic_read(&clp->cl_count) > 1)
3919 nfs4_schedule_state_renewal(clp);
3920 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003921 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003922}
3923
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003924static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003926 struct nfs4_renewdata *data = calldata;
3927 struct nfs_client *clp = data->client;
3928 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003931 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003932 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3933 return;
3934 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003935 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003936 return;
3937 }
3938 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003940 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941}
3942
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003943static const struct rpc_call_ops nfs4_renew_ops = {
3944 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003945 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003946};
3947
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003948static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
3950 struct rpc_message msg = {
3951 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3952 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003953 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003955 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003957 if (renew_flags == 0)
3958 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003959 if (!atomic_inc_not_zero(&clp->cl_count))
3960 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003961 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003962 if (data == NULL)
3963 return -ENOMEM;
3964 data->client = clp;
3965 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003966 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003967 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968}
3969
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003970static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
3972 struct rpc_message msg = {
3973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3974 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003975 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 };
3977 unsigned long now = jiffies;
3978 int status;
3979
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003980 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 if (status < 0)
3982 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003983 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return 0;
3985}
3986
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003987static inline int nfs4_server_supports_acls(struct nfs_server *server)
3988{
3989 return (server->caps & NFS_CAP_ACLS)
3990 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3991 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3992}
3993
Trond Myklebust21f498c2012-08-24 10:59:25 -04003994/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3995 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003996 * the stack.
3997 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003998#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003999
Neil Hormane9e3d722011-03-04 19:26:03 -05004000static int buf_to_pages_noslab(const void *buf, size_t buflen,
4001 struct page **pages, unsigned int *pgbase)
4002{
4003 struct page *newpage, **spages;
4004 int rc = 0;
4005 size_t len;
4006 spages = pages;
4007
4008 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004009 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004010 newpage = alloc_page(GFP_KERNEL);
4011
4012 if (newpage == NULL)
4013 goto unwind;
4014 memcpy(page_address(newpage), buf, len);
4015 buf += len;
4016 buflen -= len;
4017 *pages++ = newpage;
4018 rc++;
4019 } while (buflen != 0);
4020
4021 return rc;
4022
4023unwind:
4024 for(; rc > 0; rc--)
4025 __free_page(spages[rc-1]);
4026 return -ENOMEM;
4027}
4028
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004029struct nfs4_cached_acl {
4030 int cached;
4031 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004032 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004033};
4034
4035static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004036{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004037 struct nfs_inode *nfsi = NFS_I(inode);
4038
4039 spin_lock(&inode->i_lock);
4040 kfree(nfsi->nfs4_acl);
4041 nfsi->nfs4_acl = acl;
4042 spin_unlock(&inode->i_lock);
4043}
4044
4045static void nfs4_zap_acl_attr(struct inode *inode)
4046{
4047 nfs4_set_cached_acl(inode, NULL);
4048}
4049
4050static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4051{
4052 struct nfs_inode *nfsi = NFS_I(inode);
4053 struct nfs4_cached_acl *acl;
4054 int ret = -ENOENT;
4055
4056 spin_lock(&inode->i_lock);
4057 acl = nfsi->nfs4_acl;
4058 if (acl == NULL)
4059 goto out;
4060 if (buf == NULL) /* user is just asking for length */
4061 goto out_len;
4062 if (acl->cached == 0)
4063 goto out;
4064 ret = -ERANGE; /* see getxattr(2) man page */
4065 if (acl->len > buflen)
4066 goto out;
4067 memcpy(buf, acl->data, acl->len);
4068out_len:
4069 ret = acl->len;
4070out:
4071 spin_unlock(&inode->i_lock);
4072 return ret;
4073}
4074
Sachin Prabhu5794d212012-04-17 14:36:40 +01004075static 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 +00004076{
4077 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004078 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004079
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004080 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004081 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004082 if (acl == NULL)
4083 goto out;
4084 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004085 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004086 } else {
4087 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4088 if (acl == NULL)
4089 goto out;
4090 acl->cached = 0;
4091 }
4092 acl->len = acl_len;
4093out:
4094 nfs4_set_cached_acl(inode, acl);
4095}
4096
Andy Adamsonbf118a32011-12-07 11:55:27 -05004097/*
4098 * The getxattr API returns the required buffer length when called with a
4099 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4100 * the required buf. On a NULL buf, we send a page of data to the server
4101 * guessing that the ACL request can be serviced by a page. If so, we cache
4102 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4103 * the cache. If not so, we throw away the page, and cache the required
4104 * length. The next getxattr call will then produce another round trip to
4105 * the server, this time with the input buf of the required size.
4106 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004107static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004108{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004109 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004110 struct nfs_getaclargs args = {
4111 .fh = NFS_FH(inode),
4112 .acl_pages = pages,
4113 .acl_len = buflen,
4114 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004115 struct nfs_getaclres res = {
4116 .acl_len = buflen,
4117 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004118 struct rpc_message msg = {
4119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4120 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004121 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004122 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004123 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4124 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004125
Andy Adamsonbf118a32011-12-07 11:55:27 -05004126 /* As long as we're doing a round trip to the server anyway,
4127 * let's be prepared for a page of acl data. */
4128 if (npages == 0)
4129 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004130 if (npages > ARRAY_SIZE(pages))
4131 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004132
Andy Adamsonbf118a32011-12-07 11:55:27 -05004133 for (i = 0; i < npages; i++) {
4134 pages[i] = alloc_page(GFP_KERNEL);
4135 if (!pages[i])
4136 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004137 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004138
4139 /* for decoding across pages */
4140 res.acl_scratch = alloc_page(GFP_KERNEL);
4141 if (!res.acl_scratch)
4142 goto out_free;
4143
Andy Adamsonbf118a32011-12-07 11:55:27 -05004144 args.acl_len = npages * PAGE_SIZE;
4145 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004146
Peng Taode040be2012-01-10 22:42:47 +08004147 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004148 __func__, buf, buflen, npages, args.acl_len);
4149 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4150 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004151 if (ret)
4152 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004153
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004154 /* Handle the case where the passed-in buffer is too short */
4155 if (res.acl_flags & NFS4_ACL_TRUNC) {
4156 /* Did the user only issue a request for the acl length? */
4157 if (buf == NULL)
4158 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004159 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004160 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004161 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004162 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004163 if (buf) {
4164 if (res.acl_len > buflen) {
4165 ret = -ERANGE;
4166 goto out_free;
4167 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004168 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004169 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004170out_ok:
4171 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004172out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004173 for (i = 0; i < npages; i++)
4174 if (pages[i])
4175 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004176 if (res.acl_scratch)
4177 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004178 return ret;
4179}
4180
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004181static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4182{
4183 struct nfs4_exception exception = { };
4184 ssize_t ret;
4185 do {
4186 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4187 if (ret >= 0)
4188 break;
4189 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4190 } while (exception.retry);
4191 return ret;
4192}
4193
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004194static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4195{
4196 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004197 int ret;
4198
4199 if (!nfs4_server_supports_acls(server))
4200 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004201 ret = nfs_revalidate_inode(server, inode);
4202 if (ret < 0)
4203 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004204 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4205 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004206 ret = nfs4_read_cached_acl(inode, buf, buflen);
4207 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004208 /* -ENOENT is returned if there is no ACL or if there is an ACL
4209 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004210 return ret;
4211 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004212}
4213
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004214static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004215{
4216 struct nfs_server *server = NFS_SERVER(inode);
4217 struct page *pages[NFS4ACL_MAXPAGES];
4218 struct nfs_setaclargs arg = {
4219 .fh = NFS_FH(inode),
4220 .acl_pages = pages,
4221 .acl_len = buflen,
4222 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004223 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004224 struct rpc_message msg = {
4225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4226 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004227 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004228 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004229 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004230 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004231
4232 if (!nfs4_server_supports_acls(server))
4233 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004234 if (npages > ARRAY_SIZE(pages))
4235 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004236 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4237 if (i < 0)
4238 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004239 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004240 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004241
4242 /*
4243 * Free each page after tx, so the only ref left is
4244 * held by the network stack
4245 */
4246 for (; i > 0; i--)
4247 put_page(pages[i-1]);
4248
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004249 /*
4250 * Acl update can result in inode attribute update.
4251 * so mark the attribute cache invalid.
4252 */
4253 spin_lock(&inode->i_lock);
4254 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4255 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004256 nfs_access_zap_cache(inode);
4257 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004258 return ret;
4259}
4260
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004261static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4262{
4263 struct nfs4_exception exception = { };
4264 int err;
4265 do {
4266 err = nfs4_handle_exception(NFS_SERVER(inode),
4267 __nfs4_proc_set_acl(inode, buf, buflen),
4268 &exception);
4269 } while (exception.retry);
4270 return err;
4271}
4272
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004274nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004276 struct nfs_client *clp = server->nfs_client;
4277
4278 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return 0;
4280 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004281 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004282 case -NFS4ERR_ADMIN_REVOKED:
4283 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004284 if (state == NULL)
4285 break;
4286 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004287 case -NFS4ERR_OPENMODE:
4288 if (state == NULL)
4289 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004290 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4291 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004292 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004293 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004294 if (state != NULL) {
4295 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4296 goto stateid_invalid;
4297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004299 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004300 nfs4_schedule_lease_recovery(clp);
4301 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004302#if defined(CONFIG_NFS_V4_1)
4303 case -NFS4ERR_BADSESSION:
4304 case -NFS4ERR_BADSLOT:
4305 case -NFS4ERR_BAD_HIGH_SLOT:
4306 case -NFS4ERR_DEADSESSION:
4307 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4308 case -NFS4ERR_SEQ_FALSE_RETRY:
4309 case -NFS4ERR_SEQ_MISORDERED:
4310 dprintk("%s ERROR %d, Reset session\n", __func__,
4311 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004312 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004313 task->tk_status = 0;
4314 return -EAGAIN;
4315#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004317 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004318 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4320 task->tk_status = 0;
4321 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004322 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 case -NFS4ERR_OLD_STATEID:
4324 task->tk_status = 0;
4325 return -EAGAIN;
4326 }
4327 task->tk_status = nfs4_map_errors(task->tk_status);
4328 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004329stateid_invalid:
4330 task->tk_status = -EIO;
4331 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004332wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004333 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004334 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4335 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4336 task->tk_status = 0;
4337 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
4339
Chuck Leverf0920752012-05-21 22:45:41 -04004340static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4341 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004342{
4343 __be32 verf[2];
4344
Chuck Lever2c820d92012-05-21 22:45:33 -04004345 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4346 /* An impossible timestamp guarantees this value
4347 * will never match a generated boot time. */
4348 verf[0] = 0;
4349 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4350 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004351 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4352 verf[0] = (__be32)nn->boot_time.tv_sec;
4353 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004354 }
Chuck Levercd937102012-03-02 17:14:31 -05004355 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4356}
4357
Chuck Levere984a552012-09-14 17:24:21 -04004358static unsigned int
4359nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4360 char *buf, size_t len)
4361{
4362 unsigned int result;
4363
4364 rcu_read_lock();
4365 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4366 clp->cl_ipaddr,
4367 rpc_peeraddr2str(clp->cl_rpcclient,
4368 RPC_DISPLAY_ADDR),
4369 rpc_peeraddr2str(clp->cl_rpcclient,
4370 RPC_DISPLAY_PROTO));
4371 rcu_read_unlock();
4372 return result;
4373}
4374
4375static unsigned int
4376nfs4_init_uniform_client_string(const struct nfs_client *clp,
4377 char *buf, size_t len)
4378{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004379 char *nodename = clp->cl_rpcclient->cl_nodename;
4380
4381 if (nfs4_client_id_uniquifier[0] != '\0')
4382 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004383 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4384 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004385 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004386}
4387
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004388/**
4389 * nfs4_proc_setclientid - Negotiate client ID
4390 * @clp: state data structure
4391 * @program: RPC program for NFSv4 callback service
4392 * @port: IP port number for NFS4 callback service
4393 * @cred: RPC credential to use for this call
4394 * @res: where to place the result
4395 *
4396 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4397 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004398int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4399 unsigned short port, struct rpc_cred *cred,
4400 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
4402 nfs4_verifier sc_verifier;
4403 struct nfs4_setclientid setclientid = {
4404 .sc_verifier = &sc_verifier,
4405 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004406 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 };
4408 struct rpc_message msg = {
4409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4410 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004411 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004412 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004414 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Chuck Leverde734832012-07-11 16:30:50 -04004416 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004417 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004418 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4419 setclientid.sc_name_len =
4420 nfs4_init_uniform_client_string(clp,
4421 setclientid.sc_name,
4422 sizeof(setclientid.sc_name));
4423 else
4424 setclientid.sc_name_len =
4425 nfs4_init_nonuniform_client_string(clp,
4426 setclientid.sc_name,
4427 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004428 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004429 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004430 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004431 sizeof(setclientid.sc_netid),
4432 rpc_peeraddr2str(clp->cl_rpcclient,
4433 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004434 rcu_read_unlock();
4435 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004436 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 clp->cl_ipaddr, port >> 8, port & 255);
4438
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004439 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4440 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4441 setclientid.sc_name_len, setclientid.sc_name);
4442 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4443 dprintk("NFS reply setclientid: %d\n", status);
4444 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445}
4446
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004447/**
4448 * nfs4_proc_setclientid_confirm - Confirm client ID
4449 * @clp: state data structure
4450 * @res: result of a previous SETCLIENTID
4451 * @cred: RPC credential to use for this call
4452 *
4453 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4454 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004455int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004456 struct nfs4_setclientid_res *arg,
4457 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 struct rpc_message msg = {
4460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004461 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004462 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 int status;
4465
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004466 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4467 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4468 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004469 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004470 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 return status;
4472}
4473
Trond Myklebustfe650402006-01-03 09:55:18 +01004474struct nfs4_delegreturndata {
4475 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004476 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004477 struct nfs_fh fh;
4478 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004479 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004480 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004481 int rpc_status;
4482};
4483
Trond Myklebustfe650402006-01-03 09:55:18 +01004484static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4485{
4486 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004487
Trond Myklebust14516c32010-07-31 14:29:06 -04004488 if (!nfs4_sequence_done(task, &data->res.seq_res))
4489 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004490
Ricardo Labiaga79708862009-12-07 09:23:21 -05004491 switch (task->tk_status) {
4492 case -NFS4ERR_STALE_STATEID:
4493 case -NFS4ERR_EXPIRED:
4494 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004495 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004496 break;
4497 default:
4498 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4499 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004500 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004501 return;
4502 }
4503 }
4504 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004505}
4506
4507static void nfs4_delegreturn_release(void *calldata)
4508{
Trond Myklebustfe650402006-01-03 09:55:18 +01004509 kfree(calldata);
4510}
4511
Andy Adamson938e1012009-04-01 09:22:28 -04004512#if defined(CONFIG_NFS_V4_1)
4513static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4514{
4515 struct nfs4_delegreturndata *d_data;
4516
4517 d_data = (struct nfs4_delegreturndata *)data;
4518
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004519 nfs4_setup_sequence(d_data->res.server,
4520 &d_data->args.seq_args,
4521 &d_data->res.seq_res,
4522 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004523}
4524#endif /* CONFIG_NFS_V4_1 */
4525
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004526static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004527#if defined(CONFIG_NFS_V4_1)
4528 .rpc_call_prepare = nfs4_delegreturn_prepare,
4529#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004530 .rpc_call_done = nfs4_delegreturn_done,
4531 .rpc_release = nfs4_delegreturn_release,
4532};
4533
Trond Myklebuste6f81072008-01-24 18:14:34 -05004534static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004535{
4536 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004537 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004538 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004539 struct rpc_message msg = {
4540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4541 .rpc_cred = cred,
4542 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004543 struct rpc_task_setup task_setup_data = {
4544 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004545 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004546 .callback_ops = &nfs4_delegreturn_ops,
4547 .flags = RPC_TASK_ASYNC,
4548 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004549 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004550
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004551 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004552 if (data == NULL)
4553 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004554 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004555 data->args.fhandle = &data->fh;
4556 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004557 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004558 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004559 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004560 data->res.fattr = &data->fattr;
4561 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004562 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004563 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004564 data->rpc_status = 0;
4565
Trond Myklebustc970aa82007-07-14 15:39:59 -04004566 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004567 msg.rpc_argp = &data->args;
4568 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004569 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004570 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004571 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004572 if (!issync)
4573 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004574 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004575 if (status != 0)
4576 goto out;
4577 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004578 if (status == 0)
4579 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4580 else
4581 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004582out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004583 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004584 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585}
4586
Trond Myklebuste6f81072008-01-24 18:14:34 -05004587int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588{
4589 struct nfs_server *server = NFS_SERVER(inode);
4590 struct nfs4_exception exception = { };
4591 int err;
4592 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004593 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 switch (err) {
4595 case -NFS4ERR_STALE_STATEID:
4596 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 case 0:
4598 return 0;
4599 }
4600 err = nfs4_handle_exception(server, err, &exception);
4601 } while (exception.retry);
4602 return err;
4603}
4604
4605#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4606#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4607
4608/*
4609 * sleep, with exponential backoff, and retry the LOCK operation.
4610 */
4611static unsigned long
4612nfs4_set_lock_task_retry(unsigned long timeout)
4613{
Jeff Laytond3103102011-12-01 22:44:39 +01004614 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 timeout <<= 1;
4616 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4617 return NFS4_LOCK_MAXTIMEOUT;
4618 return timeout;
4619}
4620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4622{
4623 struct inode *inode = state->inode;
4624 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004625 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004626 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004628 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004630 struct nfs_lockt_res res = {
4631 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 };
4633 struct rpc_message msg = {
4634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4635 .rpc_argp = &arg,
4636 .rpc_resp = &res,
4637 .rpc_cred = state->owner->so_cred,
4638 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 struct nfs4_lock_state *lsp;
4640 int status;
4641
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004642 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004643 status = nfs4_set_lock_state(state, request);
4644 if (status != 0)
4645 goto out;
4646 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004647 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004648 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004649 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004650 switch (status) {
4651 case 0:
4652 request->fl_type = F_UNLCK;
4653 break;
4654 case -NFS4ERR_DENIED:
4655 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004657 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004658out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 return status;
4660}
4661
4662static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4663{
4664 struct nfs4_exception exception = { };
4665 int err;
4666
4667 do {
4668 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4669 _nfs4_proc_getlk(state, cmd, request),
4670 &exception);
4671 } while (exception.retry);
4672 return err;
4673}
4674
4675static int do_vfs_lock(struct file *file, struct file_lock *fl)
4676{
4677 int res = 0;
4678 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4679 case FL_POSIX:
4680 res = posix_lock_file_wait(file, fl);
4681 break;
4682 case FL_FLOCK:
4683 res = flock_lock_file_wait(file, fl);
4684 break;
4685 default:
4686 BUG();
4687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 return res;
4689}
4690
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004691struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004692 struct nfs_locku_args arg;
4693 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004694 struct nfs4_lock_state *lsp;
4695 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004696 struct file_lock fl;
4697 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004698 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004699};
4700
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004701static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4702 struct nfs_open_context *ctx,
4703 struct nfs4_lock_state *lsp,
4704 struct nfs_seqid *seqid)
4705{
4706 struct nfs4_unlockdata *p;
4707 struct inode *inode = lsp->ls_state->inode;
4708
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004709 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004710 if (p == NULL)
4711 return NULL;
4712 p->arg.fh = NFS_FH(inode);
4713 p->arg.fl = &p->fl;
4714 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004715 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004716 p->arg.stateid = &lsp->ls_stateid;
4717 p->lsp = lsp;
4718 atomic_inc(&lsp->ls_count);
4719 /* Ensure we don't close file until we're done freeing locks! */
4720 p->ctx = get_nfs_open_context(ctx);
4721 memcpy(&p->fl, fl, sizeof(p->fl));
4722 p->server = NFS_SERVER(inode);
4723 return p;
4724}
4725
Trond Myklebust06f814a2006-01-03 09:55:07 +01004726static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004727{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004728 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004729 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004730 nfs4_put_lock_state(calldata->lsp);
4731 put_nfs_open_context(calldata->ctx);
4732 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004733}
4734
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004735static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004736{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004737 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004738
Trond Myklebust14516c32010-07-31 14:29:06 -04004739 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4740 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004741 switch (task->tk_status) {
4742 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004743 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4744 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004745 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004746 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004747 case -NFS4ERR_BAD_STATEID:
4748 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004749 case -NFS4ERR_STALE_STATEID:
4750 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004751 break;
4752 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004753 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004754 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004755 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004756 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004757}
4758
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004759static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004760{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004761 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004763 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004764 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004765 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004766 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004767 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004768 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004769 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004770 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004771 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004772 &calldata->res.seq_res,
4773 task) != 0)
4774 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004775 return;
4776out_no_action:
4777 task->tk_action = NULL;
4778out_wait:
4779 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004782static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004783 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004784 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004785 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004786};
4787
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004788static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4789 struct nfs_open_context *ctx,
4790 struct nfs4_lock_state *lsp,
4791 struct nfs_seqid *seqid)
4792{
4793 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004794 struct rpc_message msg = {
4795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4796 .rpc_cred = ctx->cred,
4797 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004798 struct rpc_task_setup task_setup_data = {
4799 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004800 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004801 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004802 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004803 .flags = RPC_TASK_ASYNC,
4804 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004805
Frank Filz137d6ac2007-07-09 15:32:29 -07004806 /* Ensure this is an unlock - when canceling a lock, the
4807 * canceled lock is passed in, and it won't be an unlock.
4808 */
4809 fl->fl_type = F_UNLCK;
4810
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004811 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4812 if (data == NULL) {
4813 nfs_free_seqid(seqid);
4814 return ERR_PTR(-ENOMEM);
4815 }
4816
Trond Myklebust9d12b212012-01-17 22:04:25 -05004817 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004818 msg.rpc_argp = &data->arg;
4819 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004820 task_setup_data.callback_data = data;
4821 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004822}
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4825{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004826 struct inode *inode = state->inode;
4827 struct nfs4_state_owner *sp = state->owner;
4828 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004829 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004830 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004831 struct rpc_task *task;
4832 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004833 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Trond Myklebust9b073572006-06-29 16:38:34 -04004835 status = nfs4_set_lock_state(state, request);
4836 /* Unlock _before_ we do the RPC call */
4837 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004838 /* Exclude nfs_delegation_claim_locks() */
4839 mutex_lock(&sp->so_delegreturn_mutex);
4840 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004841 down_read(&nfsi->rwsem);
4842 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4843 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004844 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004845 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004846 }
4847 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004848 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004849 if (status != 0)
4850 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004851 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004852 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04004853 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
4854 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004855 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004856 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004857 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004858 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004859 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004860 status = PTR_ERR(task);
4861 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004862 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004863 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004864 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004865out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004866 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004867 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004870struct nfs4_lockdata {
4871 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004872 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004873 struct nfs4_lock_state *lsp;
4874 struct nfs_open_context *ctx;
4875 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004876 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004877 int rpc_status;
4878 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004879 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004880};
4881
4882static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004883 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4884 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004885{
4886 struct nfs4_lockdata *p;
4887 struct inode *inode = lsp->ls_state->inode;
4888 struct nfs_server *server = NFS_SERVER(inode);
4889
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004890 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004891 if (p == NULL)
4892 return NULL;
4893
4894 p->arg.fh = NFS_FH(inode);
4895 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004896 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004897 if (p->arg.open_seqid == NULL)
4898 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004899 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004900 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004901 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004902 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004903 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004904 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004905 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004906 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004907 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004908 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004909 atomic_inc(&lsp->ls_count);
4910 p->ctx = get_nfs_open_context(ctx);
4911 memcpy(&p->fl, fl, sizeof(p->fl));
4912 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004913out_free_seqid:
4914 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004915out_free:
4916 kfree(p);
4917 return NULL;
4918}
4919
4920static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4921{
4922 struct nfs4_lockdata *data = calldata;
4923 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Harvey Harrison3110ff82008-05-02 13:42:44 -07004925 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004926 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004927 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004928 /* Do we need to do an open_to_lock_owner? */
4929 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004930 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004931 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004932 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004933 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004934 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004935 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004936 } else
4937 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004938 if (!nfs4_valid_open_stateid(state)) {
4939 data->rpc_status = -EBADF;
4940 task->tk_action = NULL;
4941 goto out_release_open_seqid;
4942 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004943 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004944 if (nfs4_setup_sequence(data->server,
4945 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004946 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004947 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004948 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004949out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004950 nfs_release_seqid(data->arg.open_seqid);
4951out_release_lock_seqid:
4952 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004953out_wait:
4954 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004955 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004956}
4957
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004958static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4959{
4960 struct nfs4_lockdata *data = calldata;
4961
Harvey Harrison3110ff82008-05-02 13:42:44 -07004962 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004963
Trond Myklebust14516c32010-07-31 14:29:06 -04004964 if (!nfs4_sequence_done(task, &data->res.seq_res))
4965 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004966
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004967 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004968 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004969 if (data->rpc_status == 0)
4970 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4971 else
4972 goto out;
4973 }
4974 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004975 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004976 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004977 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004978 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004979out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004980 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004981}
4982
4983static void nfs4_lock_release(void *calldata)
4984{
4985 struct nfs4_lockdata *data = calldata;
4986
Harvey Harrison3110ff82008-05-02 13:42:44 -07004987 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004988 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004989 if (data->cancelled != 0) {
4990 struct rpc_task *task;
4991 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4992 data->arg.lock_seqid);
4993 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004994 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004995 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004996 } else
4997 nfs_free_seqid(data->arg.lock_seqid);
4998 nfs4_put_lock_state(data->lsp);
4999 put_nfs_open_context(data->ctx);
5000 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005001 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005002}
5003
5004static const struct rpc_call_ops nfs4_lock_ops = {
5005 .rpc_call_prepare = nfs4_lock_prepare,
5006 .rpc_call_done = nfs4_lock_done,
5007 .rpc_release = nfs4_lock_release,
5008};
5009
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005010static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5011{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005012 switch (error) {
5013 case -NFS4ERR_ADMIN_REVOKED:
5014 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005015 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005016 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005017 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005018 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005019 break;
5020 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005021 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005022 case -NFS4ERR_EXPIRED:
5023 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005024 };
5025}
5026
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005027static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005028{
5029 struct nfs4_lockdata *data;
5030 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005031 struct rpc_message msg = {
5032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5033 .rpc_cred = state->owner->so_cred,
5034 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005035 struct rpc_task_setup task_setup_data = {
5036 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005037 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005038 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005039 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005040 .flags = RPC_TASK_ASYNC,
5041 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005042 int ret;
5043
Harvey Harrison3110ff82008-05-02 13:42:44 -07005044 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005045 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005046 fl->fl_u.nfs4_fl.owner,
5047 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005048 if (data == NULL)
5049 return -ENOMEM;
5050 if (IS_SETLKW(cmd))
5051 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005052 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005053 msg.rpc_argp = &data->arg;
5054 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005055 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005056 if (recovery_type > NFS_LOCK_NEW) {
5057 if (recovery_type == NFS_LOCK_RECLAIM)
5058 data->arg.reclaim = NFS_LOCK_RECLAIM;
5059 nfs4_set_sequence_privileged(&data->arg.seq_args);
5060 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005061 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005062 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005063 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005064 ret = nfs4_wait_for_completion_rpc_task(task);
5065 if (ret == 0) {
5066 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005067 if (ret)
5068 nfs4_handle_setlk_error(data->server, data->lsp,
5069 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005070 } else
5071 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005072 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005073 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075}
5076
5077static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5078{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005079 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005080 struct nfs4_exception exception = {
5081 .inode = state->inode,
5082 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005083 int err;
5084
5085 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005086 /* Cache the lock if possible... */
5087 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5088 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005089 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005090 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005091 break;
5092 nfs4_handle_exception(server, err, &exception);
5093 } while (exception.retry);
5094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095}
5096
5097static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5098{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005099 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005100 struct nfs4_exception exception = {
5101 .inode = state->inode,
5102 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005103 int err;
5104
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005105 err = nfs4_set_lock_state(state, request);
5106 if (err != 0)
5107 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005108 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005109 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5110 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005111 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005112 switch (err) {
5113 default:
5114 goto out;
5115 case -NFS4ERR_GRACE:
5116 case -NFS4ERR_DELAY:
5117 nfs4_handle_exception(server, err, &exception);
5118 err = 0;
5119 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005120 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005121out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005122 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123}
5124
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005125#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005126/**
5127 * nfs41_check_expired_locks - possibly free a lock stateid
5128 *
5129 * @state: NFSv4 state for an inode
5130 *
5131 * Returns NFS_OK if recovery for this stateid is now finished.
5132 * Otherwise a negative NFS4ERR value is returned.
5133 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005134static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005135{
Chuck Levereb64cf92012-07-11 16:30:05 -04005136 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005137 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005138 struct nfs_server *server = NFS_SERVER(state->inode);
5139
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005140 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005141 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005142 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5143 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005144 /* Free the stateid unless the server
5145 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005146 if (status != -NFS4ERR_BAD_STATEID)
5147 nfs41_free_stateid(server,
5148 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005149 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005150 ret = status;
5151 }
5152 }
5153 };
5154
5155 return ret;
5156}
5157
5158static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5159{
5160 int status = NFS_OK;
5161
5162 if (test_bit(LK_STATE_IN_USE, &state->flags))
5163 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005164 if (status != NFS_OK)
5165 status = nfs4_lock_expired(state, request);
5166 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005167}
5168#endif
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5171{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005172 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005173 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005174 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005175 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005176 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005178 if ((fl_flags & FL_POSIX) &&
5179 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5180 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005181 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005182 status = nfs4_set_lock_state(state, request);
5183 if (status != 0)
5184 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005185 request->fl_flags |= FL_ACCESS;
5186 status = do_vfs_lock(request->fl_file, request);
5187 if (status < 0)
5188 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005189 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005190 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005191 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005192 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005193 request->fl_flags = fl_flags & ~FL_SLEEP;
5194 status = do_vfs_lock(request->fl_file, request);
5195 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005196 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005197 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5198 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005199 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005200 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005201 goto out;
5202 down_read(&nfsi->rwsem);
5203 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5204 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005205 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005206 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005207 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005208 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005209 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005210 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5211 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005212out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005213 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005214out:
5215 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return status;
5217}
5218
5219static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5220{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005221 struct nfs4_exception exception = {
5222 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005223 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005224 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 int err;
5226
5227 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005228 err = _nfs4_proc_setlk(state, cmd, request);
5229 if (err == -NFS4ERR_DENIED)
5230 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005232 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 } while (exception.retry);
5234 return err;
5235}
5236
5237static int
5238nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5239{
5240 struct nfs_open_context *ctx;
5241 struct nfs4_state *state;
5242 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5243 int status;
5244
5245 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005246 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 state = ctx->state;
5248
5249 if (request->fl_start < 0 || request->fl_end < 0)
5250 return -EINVAL;
5251
Trond Myklebustd9531262009-07-21 19:22:38 -04005252 if (IS_GETLK(cmd)) {
5253 if (state != NULL)
5254 return nfs4_proc_getlk(state, F_GETLK, request);
5255 return 0;
5256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
5258 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5259 return -EINVAL;
5260
Trond Myklebustd9531262009-07-21 19:22:38 -04005261 if (request->fl_type == F_UNLCK) {
5262 if (state != NULL)
5263 return nfs4_proc_unlck(state, cmd, request);
5264 return 0;
5265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Trond Myklebustd9531262009-07-21 19:22:38 -04005267 if (state == NULL)
5268 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005269 /*
5270 * Don't rely on the VFS having checked the file open mode,
5271 * since it won't do this for flock() locks.
5272 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005273 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005274 case F_RDLCK:
5275 if (!(filp->f_mode & FMODE_READ))
5276 return -EBADF;
5277 break;
5278 case F_WRLCK:
5279 if (!(filp->f_mode & FMODE_WRITE))
5280 return -EBADF;
5281 }
5282
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 do {
5284 status = nfs4_proc_setlk(state, cmd, request);
5285 if ((status != -EAGAIN) || IS_SETLK(cmd))
5286 break;
5287 timeout = nfs4_set_lock_task_retry(timeout);
5288 status = -ERESTARTSYS;
5289 if (signalled())
5290 break;
5291 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 return status;
5293}
5294
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005295int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005296{
5297 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005298 int err;
5299
5300 err = nfs4_set_lock_state(state, fl);
5301 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005302 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005303 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005304 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005305}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005306
Trond Myklebustcf470c32012-03-07 13:49:12 -05005307struct nfs_release_lockowner_data {
5308 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005309 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005310 struct nfs_release_lockowner_args args;
5311};
5312
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005313static void nfs4_release_lockowner_release(void *calldata)
5314{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005315 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005316 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005317 kfree(calldata);
5318}
5319
Trond Myklebust17280172012-03-11 13:11:00 -04005320static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005321 .rpc_release = nfs4_release_lockowner_release,
5322};
5323
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005324static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005325{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005326 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005327 struct rpc_message msg = {
5328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5329 };
5330
5331 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005332 return -EINVAL;
5333 data = kmalloc(sizeof(*data), GFP_NOFS);
5334 if (!data)
5335 return -ENOMEM;
5336 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005337 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005338 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5339 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5340 data->args.lock_owner.s_dev = server->s_dev;
5341 msg.rpc_argp = &data->args;
5342 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5343 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005344}
5345
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005346#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5347
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005348static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5349 const void *buf, size_t buflen,
5350 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005351{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005352 if (strcmp(key, "") != 0)
5353 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005354
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005355 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005356}
5357
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005358static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5359 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005360{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005361 if (strcmp(key, "") != 0)
5362 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005363
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005364 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005365}
5366
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005367static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5368 size_t list_len, const char *name,
5369 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005370{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005371 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005372
J. Bruce Fields096455a2006-03-20 23:23:42 -05005373 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5374 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005375
5376 if (list && len <= list_len)
5377 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005378 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005379}
5380
Andy Adamson533eb462011-06-13 18:25:56 -04005381/*
5382 * nfs_fhget will use either the mounted_on_fileid or the fileid
5383 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005384static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5385{
Andy Adamson533eb462011-06-13 18:25:56 -04005386 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5387 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5388 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005389 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005390 return;
5391
5392 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005393 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005394 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5395 fattr->nlink = 2;
5396}
5397
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005398static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5399 const struct qstr *name,
5400 struct nfs4_fs_locations *fs_locations,
5401 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005402{
5403 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005404 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005405 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005406 };
5407 struct nfs4_fs_locations_arg args = {
5408 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005409 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005410 .page = page,
5411 .bitmask = bitmask,
5412 };
Benny Halevy22958462009-04-01 09:22:02 -04005413 struct nfs4_fs_locations_res res = {
5414 .fs_locations = fs_locations,
5415 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005416 struct rpc_message msg = {
5417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5418 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005419 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005420 };
5421 int status;
5422
Harvey Harrison3110ff82008-05-02 13:42:44 -07005423 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005424
5425 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5426 * is not supported */
5427 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5428 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5429 else
5430 bitmask[0] |= FATTR4_WORD0_FILEID;
5431
Trond Myklebustc228fd32007-01-13 02:28:11 -05005432 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005433 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005434 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005435 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005436 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005437 return status;
5438}
5439
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005440int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5441 const struct qstr *name,
5442 struct nfs4_fs_locations *fs_locations,
5443 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005444{
5445 struct nfs4_exception exception = { };
5446 int err;
5447 do {
5448 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005449 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005450 &exception);
5451 } while (exception.retry);
5452 return err;
5453}
5454
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005455static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5456{
5457 int status;
5458 struct nfs4_secinfo_arg args = {
5459 .dir_fh = NFS_FH(dir),
5460 .name = name,
5461 };
5462 struct nfs4_secinfo_res res = {
5463 .flavors = flavors,
5464 };
5465 struct rpc_message msg = {
5466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5467 .rpc_argp = &args,
5468 .rpc_resp = &res,
5469 };
5470
5471 dprintk("NFS call secinfo %s\n", name->name);
5472 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5473 dprintk("NFS reply secinfo: %d\n", status);
5474 return status;
5475}
5476
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005477int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5478 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005479{
5480 struct nfs4_exception exception = { };
5481 int err;
5482 do {
5483 err = nfs4_handle_exception(NFS_SERVER(dir),
5484 _nfs4_proc_secinfo(dir, name, flavors),
5485 &exception);
5486 } while (exception.retry);
5487 return err;
5488}
5489
Andy Adamson557134a2009-04-01 09:21:53 -04005490#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005491/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005492 * Check the exchange flags returned by the server for invalid flags, having
5493 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5494 * DS flags set.
5495 */
5496static int nfs4_check_cl_exchange_flags(u32 flags)
5497{
5498 if (flags & ~EXCHGID4_FLAG_MASK_R)
5499 goto out_inval;
5500 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5501 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5502 goto out_inval;
5503 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5504 goto out_inval;
5505 return NFS_OK;
5506out_inval:
5507 return -NFS4ERR_INVAL;
5508}
5509
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005510static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005511nfs41_same_server_scope(struct nfs41_server_scope *a,
5512 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005513{
5514 if (a->server_scope_sz == b->server_scope_sz &&
5515 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5516 return true;
5517
5518 return false;
5519}
5520
Andy Adamson357f54d2010-12-14 10:11:57 -05005521/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005522 * nfs4_proc_bind_conn_to_session()
5523 *
5524 * The 4.1 client currently uses the same TCP connection for the
5525 * fore and backchannel.
5526 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005527int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005528{
5529 int status;
5530 struct nfs41_bind_conn_to_session_res res;
5531 struct rpc_message msg = {
5532 .rpc_proc =
5533 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5534 .rpc_argp = clp,
5535 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005536 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005537 };
5538
5539 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005540
5541 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5542 if (unlikely(res.session == NULL)) {
5543 status = -ENOMEM;
5544 goto out;
5545 }
5546
5547 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5548 if (status == 0) {
5549 if (memcmp(res.session->sess_id.data,
5550 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5551 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5552 status = -EIO;
5553 goto out_session;
5554 }
5555 if (res.dir != NFS4_CDFS4_BOTH) {
5556 dprintk("NFS: %s: Unexpected direction from server\n",
5557 __func__);
5558 status = -EIO;
5559 goto out_session;
5560 }
5561 if (res.use_conn_in_rdma_mode) {
5562 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5563 __func__);
5564 status = -EIO;
5565 goto out_session;
5566 }
5567 }
5568out_session:
5569 kfree(res.session);
5570out:
5571 dprintk("<-- %s status= %d\n", __func__, status);
5572 return status;
5573}
5574
5575/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005576 * nfs4_proc_exchange_id()
5577 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005578 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5579 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005580 * Since the clientid has expired, all compounds using sessions
5581 * associated with the stale clientid will be returning
5582 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5583 * be in some phase of session reset.
5584 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005585int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005586{
5587 nfs4_verifier verifier;
5588 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005589 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005590 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005591 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005592 };
5593 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005594 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005595 };
5596 int status;
5597 struct rpc_message msg = {
5598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5599 .rpc_argp = &args,
5600 .rpc_resp = &res,
5601 .rpc_cred = cred,
5602 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005603
Chuck Leverf0920752012-05-21 22:45:41 -04005604 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005605 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5606 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005607 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5608 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5609 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005610
Chuck Leveracdeb692012-05-21 22:46:16 -04005611 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005612 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005613 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005614 status = -ENOMEM;
5615 goto out;
5616 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005617
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005618 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005619 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005620 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005621 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005622 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005623 }
5624
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005625 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005626 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005627 status = -ENOMEM;
5628 goto out_server_scope;
5629 }
5630
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005631 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005632 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005633 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005634
Chuck Lever177313f2012-05-21 22:44:58 -04005635 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005636 clp->cl_clientid = res.clientid;
5637 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5638 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5639 clp->cl_seqid = res.seqid;
5640
Chuck Leveracdeb692012-05-21 22:46:16 -04005641 kfree(clp->cl_serverowner);
5642 clp->cl_serverowner = res.server_owner;
5643 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005644
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005645 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005646 kfree(clp->cl_implid);
5647 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005648
Chuck Lever177313f2012-05-21 22:44:58 -04005649 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005650 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005651 res.server_scope)) {
5652 dprintk("%s: server_scope mismatch detected\n",
5653 __func__);
5654 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005655 kfree(clp->cl_serverscope);
5656 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005657 }
5658
Chuck Lever177313f2012-05-21 22:44:58 -04005659 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005660 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005661 goto out;
5662 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005663 } else
5664 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005665
Chuck Leveracdeb692012-05-21 22:46:16 -04005666out_server_owner:
5667 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005668out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005669 kfree(res.server_scope);
5670out:
Chuck Lever177313f2012-05-21 22:44:58 -04005671 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005672 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005673 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005674 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005675 clp->cl_implid->date.seconds,
5676 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005677 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005678 return status;
5679}
5680
Trond Myklebust66245532012-05-25 17:18:09 -04005681static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5682 struct rpc_cred *cred)
5683{
5684 struct rpc_message msg = {
5685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5686 .rpc_argp = clp,
5687 .rpc_cred = cred,
5688 };
5689 int status;
5690
5691 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5692 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005693 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005694 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5695 return status;
5696}
5697
5698static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5699 struct rpc_cred *cred)
5700{
5701 unsigned int loop;
5702 int ret;
5703
5704 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5705 ret = _nfs4_proc_destroy_clientid(clp, cred);
5706 switch (ret) {
5707 case -NFS4ERR_DELAY:
5708 case -NFS4ERR_CLIENTID_BUSY:
5709 ssleep(1);
5710 break;
5711 default:
5712 return ret;
5713 }
5714 }
5715 return 0;
5716}
5717
5718int nfs4_destroy_clientid(struct nfs_client *clp)
5719{
5720 struct rpc_cred *cred;
5721 int ret = 0;
5722
5723 if (clp->cl_mvops->minor_version < 1)
5724 goto out;
5725 if (clp->cl_exchange_flags == 0)
5726 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005727 if (clp->cl_preserve_clid)
5728 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005729 cred = nfs4_get_exchange_id_cred(clp);
5730 ret = nfs4_proc_destroy_clientid(clp, cred);
5731 if (cred)
5732 put_rpccred(cred);
5733 switch (ret) {
5734 case 0:
5735 case -NFS4ERR_STALE_CLIENTID:
5736 clp->cl_exchange_flags = 0;
5737 }
5738out:
5739 return ret;
5740}
5741
Andy Adamson2050f0c2009-04-01 09:22:30 -04005742struct nfs4_get_lease_time_data {
5743 struct nfs4_get_lease_time_args *args;
5744 struct nfs4_get_lease_time_res *res;
5745 struct nfs_client *clp;
5746};
5747
5748static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5749 void *calldata)
5750{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005751 struct nfs4_get_lease_time_data *data =
5752 (struct nfs4_get_lease_time_data *)calldata;
5753
5754 dprintk("--> %s\n", __func__);
5755 /* just setup sequence, do not trigger session recovery
5756 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005757 nfs41_setup_sequence(data->clp->cl_session,
5758 &data->args->la_seq_args,
5759 &data->res->lr_seq_res,
5760 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005761 dprintk("<-- %s\n", __func__);
5762}
5763
5764/*
5765 * Called from nfs4_state_manager thread for session setup, so don't recover
5766 * from sequence operation or clientid errors.
5767 */
5768static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5769{
5770 struct nfs4_get_lease_time_data *data =
5771 (struct nfs4_get_lease_time_data *)calldata;
5772
5773 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005774 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5775 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005776 switch (task->tk_status) {
5777 case -NFS4ERR_DELAY:
5778 case -NFS4ERR_GRACE:
5779 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5780 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5781 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005782 /* fall through */
5783 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005784 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005785 return;
5786 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005787 dprintk("<-- %s\n", __func__);
5788}
5789
Trond Myklebust17280172012-03-11 13:11:00 -04005790static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005791 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5792 .rpc_call_done = nfs4_get_lease_time_done,
5793};
5794
5795int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5796{
5797 struct rpc_task *task;
5798 struct nfs4_get_lease_time_args args;
5799 struct nfs4_get_lease_time_res res = {
5800 .lr_fsinfo = fsinfo,
5801 };
5802 struct nfs4_get_lease_time_data data = {
5803 .args = &args,
5804 .res = &res,
5805 .clp = clp,
5806 };
5807 struct rpc_message msg = {
5808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5809 .rpc_argp = &args,
5810 .rpc_resp = &res,
5811 };
5812 struct rpc_task_setup task_setup = {
5813 .rpc_client = clp->cl_rpcclient,
5814 .rpc_message = &msg,
5815 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005816 .callback_data = &data,
5817 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005818 };
5819 int status;
5820
Trond Myklebust9d12b212012-01-17 22:04:25 -05005821 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005822 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005823 dprintk("--> %s\n", __func__);
5824 task = rpc_run_task(&task_setup);
5825
5826 if (IS_ERR(task))
5827 status = PTR_ERR(task);
5828 else {
5829 status = task->tk_status;
5830 rpc_put_task(task);
5831 }
5832 dprintk("<-- %s return %d\n", __func__, status);
5833
5834 return status;
5835}
5836
Andy Adamsonfc931582009-04-01 09:22:31 -04005837/*
5838 * Initialize the values to be used by the client in CREATE_SESSION
5839 * If nfs4_init_session set the fore channel request and response sizes,
5840 * use them.
5841 *
5842 * Set the back channel max_resp_sz_cached to zero to force the client to
5843 * always set csa_cachethis to FALSE because the current implementation
5844 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5845 */
5846static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5847{
5848 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005849 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5850 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005851
5852 if (mxrqst_sz == 0)
5853 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5854 if (mxresp_sz == 0)
5855 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5856 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005857 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5858 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005859 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005860 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005861
5862 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005863 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005864 __func__,
5865 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005866 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005867
5868 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005869 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5870 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5871 args->bc_attrs.max_resp_sz_cached = 0;
5872 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5873 args->bc_attrs.max_reqs = 1;
5874
5875 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5876 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5877 __func__,
5878 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5879 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5880 args->bc_attrs.max_reqs);
5881}
5882
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005883static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005884{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005885 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5886 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5887
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005888 if (rcvd->max_resp_sz > sent->max_resp_sz)
5889 return -EINVAL;
5890 /*
5891 * Our requested max_ops is the minimum we need; we're not
5892 * prepared to break up compounds into smaller pieces than that.
5893 * So, no point even trying to continue if the server won't
5894 * cooperate:
5895 */
5896 if (rcvd->max_ops < sent->max_ops)
5897 return -EINVAL;
5898 if (rcvd->max_reqs == 0)
5899 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005900 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5901 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005902 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005903}
5904
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005905static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5906{
5907 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5908 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005909
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005910 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5911 return -EINVAL;
5912 if (rcvd->max_resp_sz < sent->max_resp_sz)
5913 return -EINVAL;
5914 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5915 return -EINVAL;
5916 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005917 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005918 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005919 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005920 return -EINVAL;
5921 return 0;
5922}
Andy Adamson8d353012009-04-01 09:22:32 -04005923
Andy Adamson8d353012009-04-01 09:22:32 -04005924static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5925 struct nfs4_session *session)
5926{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005927 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005928
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005929 ret = nfs4_verify_fore_channel_attrs(args, session);
5930 if (ret)
5931 return ret;
5932 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005933}
5934
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005935static int _nfs4_proc_create_session(struct nfs_client *clp,
5936 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005937{
5938 struct nfs4_session *session = clp->cl_session;
5939 struct nfs41_create_session_args args = {
5940 .client = clp,
5941 .cb_program = NFS4_CALLBACK,
5942 };
5943 struct nfs41_create_session_res res = {
5944 .client = clp,
5945 };
5946 struct rpc_message msg = {
5947 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5948 .rpc_argp = &args,
5949 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005950 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005951 };
5952 int status;
5953
5954 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005955 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005956
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005957 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005958
Trond Myklebust43095d32012-11-20 11:13:12 -05005959 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005960 /* Verify the session's negotiated channel_attrs values */
5961 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005962 /* Increment the clientid slot sequence id */
5963 clp->cl_seqid++;
5964 }
5965
5966 return status;
5967}
5968
5969/*
5970 * Issues a CREATE_SESSION operation to the server.
5971 * It is the responsibility of the caller to verify the session is
5972 * expired before calling this routine.
5973 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005974int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005975{
5976 int status;
5977 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005978 struct nfs4_session *session = clp->cl_session;
5979
5980 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5981
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005982 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005983 if (status)
5984 goto out;
5985
Andy Adamsonaacd5532011-11-09 13:58:21 -05005986 /* Init or reset the session slot tables */
5987 status = nfs4_setup_session_slot_tables(session);
5988 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005989 if (status)
5990 goto out;
5991
5992 ptr = (unsigned *)&session->sess_id.data[0];
5993 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5994 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005995out:
5996 dprintk("<-- %s\n", __func__);
5997 return status;
5998}
5999
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006000/*
6001 * Issue the over-the-wire RPC DESTROY_SESSION.
6002 * The caller must serialize access to this routine.
6003 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006004int nfs4_proc_destroy_session(struct nfs4_session *session,
6005 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006006{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006007 struct rpc_message msg = {
6008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6009 .rpc_argp = session,
6010 .rpc_cred = cred,
6011 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006012 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006013
6014 dprintk("--> nfs4_proc_destroy_session\n");
6015
6016 /* session is still being setup */
6017 if (session->clp->cl_cons_state != NFS_CS_READY)
6018 return status;
6019
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006020 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006021
6022 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006023 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006024 "Session has been destroyed regardless...\n", status);
6025
6026 dprintk("<-- nfs4_proc_destroy_session\n");
6027 return status;
6028}
6029
Trond Myklebust7b38c362012-05-23 13:23:31 -04006030/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006031 * Renew the cl_session lease.
6032 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006033struct nfs4_sequence_data {
6034 struct nfs_client *clp;
6035 struct nfs4_sequence_args args;
6036 struct nfs4_sequence_res res;
6037};
6038
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006039static void nfs41_sequence_release(void *data)
6040{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006041 struct nfs4_sequence_data *calldata = data;
6042 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006043
Alexandros Batsakis71358402010-02-05 03:45:05 -08006044 if (atomic_read(&clp->cl_count) > 1)
6045 nfs4_schedule_state_renewal(clp);
6046 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006047 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006048}
6049
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006050static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6051{
6052 switch(task->tk_status) {
6053 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006054 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6055 return -EAGAIN;
6056 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006057 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006058 }
6059 return 0;
6060}
6061
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006062static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006063{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006064 struct nfs4_sequence_data *calldata = data;
6065 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006066
Trond Myklebust14516c32010-07-31 14:29:06 -04006067 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6068 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006069
6070 if (task->tk_status < 0) {
6071 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006072 if (atomic_read(&clp->cl_count) == 1)
6073 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006074
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006075 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6076 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006077 return;
6078 }
6079 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006080 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006081out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006082 dprintk("<-- %s\n", __func__);
6083}
6084
6085static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6086{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006087 struct nfs4_sequence_data *calldata = data;
6088 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006089 struct nfs4_sequence_args *args;
6090 struct nfs4_sequence_res *res;
6091
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006092 args = task->tk_msg.rpc_argp;
6093 res = task->tk_msg.rpc_resp;
6094
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006095 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006096}
6097
6098static const struct rpc_call_ops nfs41_sequence_ops = {
6099 .rpc_call_done = nfs41_sequence_call_done,
6100 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006101 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006102};
6103
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006104static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6105 struct rpc_cred *cred,
6106 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006107{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006108 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006109 struct rpc_message msg = {
6110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6111 .rpc_cred = cred,
6112 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006113 struct rpc_task_setup task_setup_data = {
6114 .rpc_client = clp->cl_rpcclient,
6115 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006116 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006117 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006118 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006119
Alexandros Batsakis71358402010-02-05 03:45:05 -08006120 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006121 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006122 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006123 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006124 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006125 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006126 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006127 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006128 if (is_privileged)
6129 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006130 msg.rpc_argp = &calldata->args;
6131 msg.rpc_resp = &calldata->res;
6132 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006133 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006134
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006135 return rpc_run_task(&task_setup_data);
6136}
6137
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006138static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006139{
6140 struct rpc_task *task;
6141 int ret = 0;
6142
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006143 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6144 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006145 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006146 if (IS_ERR(task))
6147 ret = PTR_ERR(task);
6148 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006149 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006150 dprintk("<-- %s status=%d\n", __func__, ret);
6151 return ret;
6152}
6153
6154static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6155{
6156 struct rpc_task *task;
6157 int ret;
6158
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006159 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006160 if (IS_ERR(task)) {
6161 ret = PTR_ERR(task);
6162 goto out;
6163 }
6164 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006165 if (!ret) {
6166 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6167
6168 if (task->tk_status == 0)
6169 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006170 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006171 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006172 rpc_put_task(task);
6173out:
6174 dprintk("<-- %s status=%d\n", __func__, ret);
6175 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006176}
6177
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006178struct nfs4_reclaim_complete_data {
6179 struct nfs_client *clp;
6180 struct nfs41_reclaim_complete_args arg;
6181 struct nfs41_reclaim_complete_res res;
6182};
6183
6184static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6185{
6186 struct nfs4_reclaim_complete_data *calldata = data;
6187
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006188 nfs41_setup_sequence(calldata->clp->cl_session,
6189 &calldata->arg.seq_args,
6190 &calldata->res.seq_res,
6191 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006192}
6193
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006194static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6195{
6196 switch(task->tk_status) {
6197 case 0:
6198 case -NFS4ERR_COMPLETE_ALREADY:
6199 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6200 break;
6201 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006202 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006203 /* fall through */
6204 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006205 return -EAGAIN;
6206 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006207 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006208 }
6209 return 0;
6210}
6211
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006212static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6213{
6214 struct nfs4_reclaim_complete_data *calldata = data;
6215 struct nfs_client *clp = calldata->clp;
6216 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6217
6218 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006219 if (!nfs41_sequence_done(task, res))
6220 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006221
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006222 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6223 rpc_restart_call_prepare(task);
6224 return;
6225 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006226 dprintk("<-- %s\n", __func__);
6227}
6228
6229static void nfs4_free_reclaim_complete_data(void *data)
6230{
6231 struct nfs4_reclaim_complete_data *calldata = data;
6232
6233 kfree(calldata);
6234}
6235
6236static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6237 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6238 .rpc_call_done = nfs4_reclaim_complete_done,
6239 .rpc_release = nfs4_free_reclaim_complete_data,
6240};
6241
6242/*
6243 * Issue a global reclaim complete.
6244 */
6245static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6246{
6247 struct nfs4_reclaim_complete_data *calldata;
6248 struct rpc_task *task;
6249 struct rpc_message msg = {
6250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6251 };
6252 struct rpc_task_setup task_setup_data = {
6253 .rpc_client = clp->cl_rpcclient,
6254 .rpc_message = &msg,
6255 .callback_ops = &nfs4_reclaim_complete_call_ops,
6256 .flags = RPC_TASK_ASYNC,
6257 };
6258 int status = -ENOMEM;
6259
6260 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006261 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006262 if (calldata == NULL)
6263 goto out;
6264 calldata->clp = clp;
6265 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006266
Trond Myklebust9d12b212012-01-17 22:04:25 -05006267 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006268 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006269 msg.rpc_argp = &calldata->arg;
6270 msg.rpc_resp = &calldata->res;
6271 task_setup_data.callback_data = calldata;
6272 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006273 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006274 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006275 goto out;
6276 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006277 status = nfs4_wait_for_completion_rpc_task(task);
6278 if (status == 0)
6279 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006280 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006281 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006282out:
6283 dprintk("<-- %s status=%d\n", __func__, status);
6284 return status;
6285}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006286
6287static void
6288nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6289{
6290 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006291 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006292 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006293
6294 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006295 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6296 * right now covering the LAYOUTGET we are about to send.
6297 * However, that is not so catastrophic, and there seems
6298 * to be no way to prevent it completely.
6299 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006300 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006301 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006302 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006303 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6304 NFS_I(lgp->args.inode)->layout,
6305 lgp->args.ctx->state)) {
6306 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006307 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006308}
6309
6310static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6311{
6312 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006313 struct inode *inode = lgp->args.inode;
6314 struct nfs_server *server = NFS_SERVER(inode);
6315 struct pnfs_layout_hdr *lo;
6316 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006317 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006318
6319 dprintk("--> %s\n", __func__);
6320
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006321 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006322 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006323
6324 switch (task->tk_status) {
6325 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006326 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006327 case -NFS4ERR_LAYOUTTRYLATER:
6328 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006329 timeo = rpc_get_timeout(task->tk_client);
6330 giveup = lgp->args.timestamp + timeo;
6331 if (time_after(giveup, jiffies))
6332 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006333 break;
6334 case -NFS4ERR_EXPIRED:
6335 case -NFS4ERR_BAD_STATEID:
6336 spin_lock(&inode->i_lock);
6337 lo = NFS_I(inode)->layout;
6338 if (!lo || list_empty(&lo->plh_segs)) {
6339 spin_unlock(&inode->i_lock);
6340 /* If the open stateid was bad, then recover it. */
6341 state = lgp->args.ctx->state;
6342 } else {
6343 LIST_HEAD(head);
6344
6345 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6346 spin_unlock(&inode->i_lock);
6347 /* Mark the bad layout state as invalid, then
6348 * retry using the open stateid. */
6349 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006350 }
6351 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006352 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6353 rpc_restart_call_prepare(task);
6354out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006355 dprintk("<-- %s\n", __func__);
6356}
6357
Idan Kedar85541162012-08-02 11:47:10 +03006358static size_t max_response_pages(struct nfs_server *server)
6359{
6360 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6361 return nfs_page_array_len(0, max_resp_sz);
6362}
6363
6364static void nfs4_free_pages(struct page **pages, size_t size)
6365{
6366 int i;
6367
6368 if (!pages)
6369 return;
6370
6371 for (i = 0; i < size; i++) {
6372 if (!pages[i])
6373 break;
6374 __free_page(pages[i]);
6375 }
6376 kfree(pages);
6377}
6378
6379static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6380{
6381 struct page **pages;
6382 int i;
6383
6384 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6385 if (!pages) {
6386 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6387 return NULL;
6388 }
6389
6390 for (i = 0; i < size; i++) {
6391 pages[i] = alloc_page(gfp_flags);
6392 if (!pages[i]) {
6393 dprintk("%s: failed to allocate page\n", __func__);
6394 nfs4_free_pages(pages, size);
6395 return NULL;
6396 }
6397 }
6398
6399 return pages;
6400}
6401
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006402static void nfs4_layoutget_release(void *calldata)
6403{
6404 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006405 struct inode *inode = lgp->args.inode;
6406 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006407 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006408
6409 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006410 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006411 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006412 put_nfs_open_context(lgp->args.ctx);
6413 kfree(calldata);
6414 dprintk("<-- %s\n", __func__);
6415}
6416
6417static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6418 .rpc_call_prepare = nfs4_layoutget_prepare,
6419 .rpc_call_done = nfs4_layoutget_done,
6420 .rpc_release = nfs4_layoutget_release,
6421};
6422
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006423struct pnfs_layout_segment *
6424nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006425{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006426 struct inode *inode = lgp->args.inode;
6427 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006428 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006429 struct rpc_task *task;
6430 struct rpc_message msg = {
6431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6432 .rpc_argp = &lgp->args,
6433 .rpc_resp = &lgp->res,
6434 };
6435 struct rpc_task_setup task_setup_data = {
6436 .rpc_client = server->client,
6437 .rpc_message = &msg,
6438 .callback_ops = &nfs4_layoutget_call_ops,
6439 .callback_data = lgp,
6440 .flags = RPC_TASK_ASYNC,
6441 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006442 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006443 int status = 0;
6444
6445 dprintk("--> %s\n", __func__);
6446
Idan Kedar85541162012-08-02 11:47:10 +03006447 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6448 if (!lgp->args.layout.pages) {
6449 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006450 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006451 }
6452 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006453 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006454
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006455 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006456 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006457 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006458
6459 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6460 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6461
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006462 task = rpc_run_task(&task_setup_data);
6463 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006464 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006465 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006466 if (status == 0)
6467 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006468 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6469 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006470 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006471 rpc_put_task(task);
6472 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006473 if (status)
6474 return ERR_PTR(status);
6475 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006476}
6477
Benny Halevycbe82602011-05-22 19:52:37 +03006478static void
6479nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6480{
6481 struct nfs4_layoutreturn *lrp = calldata;
6482
6483 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006484 nfs41_setup_sequence(lrp->clp->cl_session,
6485 &lrp->args.seq_args,
6486 &lrp->res.seq_res,
6487 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006488}
6489
6490static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6491{
6492 struct nfs4_layoutreturn *lrp = calldata;
6493 struct nfs_server *server;
6494
6495 dprintk("--> %s\n", __func__);
6496
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006497 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006498 return;
6499
6500 server = NFS_SERVER(lrp->args.inode);
6501 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006502 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006503 return;
6504 }
Benny Halevycbe82602011-05-22 19:52:37 +03006505 dprintk("<-- %s\n", __func__);
6506}
6507
6508static void nfs4_layoutreturn_release(void *calldata)
6509{
6510 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006511 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006512
6513 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006514 spin_lock(&lo->plh_inode->i_lock);
6515 if (lrp->res.lrs_present)
6516 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6517 lo->plh_block_lgets--;
6518 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006519 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006520 kfree(calldata);
6521 dprintk("<-- %s\n", __func__);
6522}
6523
6524static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6525 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6526 .rpc_call_done = nfs4_layoutreturn_done,
6527 .rpc_release = nfs4_layoutreturn_release,
6528};
6529
6530int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6531{
6532 struct rpc_task *task;
6533 struct rpc_message msg = {
6534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6535 .rpc_argp = &lrp->args,
6536 .rpc_resp = &lrp->res,
6537 };
6538 struct rpc_task_setup task_setup_data = {
6539 .rpc_client = lrp->clp->cl_rpcclient,
6540 .rpc_message = &msg,
6541 .callback_ops = &nfs4_layoutreturn_call_ops,
6542 .callback_data = lrp,
6543 };
6544 int status;
6545
6546 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006547 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006548 task = rpc_run_task(&task_setup_data);
6549 if (IS_ERR(task))
6550 return PTR_ERR(task);
6551 status = task->tk_status;
6552 dprintk("<-- %s status=%d\n", __func__, status);
6553 rpc_put_task(task);
6554 return status;
6555}
6556
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006557/*
6558 * Retrieve the list of Data Server devices from the MDS.
6559 */
6560static int _nfs4_getdevicelist(struct nfs_server *server,
6561 const struct nfs_fh *fh,
6562 struct pnfs_devicelist *devlist)
6563{
6564 struct nfs4_getdevicelist_args args = {
6565 .fh = fh,
6566 .layoutclass = server->pnfs_curr_ld->id,
6567 };
6568 struct nfs4_getdevicelist_res res = {
6569 .devlist = devlist,
6570 };
6571 struct rpc_message msg = {
6572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6573 .rpc_argp = &args,
6574 .rpc_resp = &res,
6575 };
6576 int status;
6577
6578 dprintk("--> %s\n", __func__);
6579 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6580 &res.seq_res, 0);
6581 dprintk("<-- %s status=%d\n", __func__, status);
6582 return status;
6583}
6584
6585int nfs4_proc_getdevicelist(struct nfs_server *server,
6586 const struct nfs_fh *fh,
6587 struct pnfs_devicelist *devlist)
6588{
6589 struct nfs4_exception exception = { };
6590 int err;
6591
6592 do {
6593 err = nfs4_handle_exception(server,
6594 _nfs4_getdevicelist(server, fh, devlist),
6595 &exception);
6596 } while (exception.retry);
6597
6598 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6599 err, devlist->num_devs);
6600
6601 return err;
6602}
6603EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6604
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006605static int
6606_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6607{
6608 struct nfs4_getdeviceinfo_args args = {
6609 .pdev = pdev,
6610 };
6611 struct nfs4_getdeviceinfo_res res = {
6612 .pdev = pdev,
6613 };
6614 struct rpc_message msg = {
6615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6616 .rpc_argp = &args,
6617 .rpc_resp = &res,
6618 };
6619 int status;
6620
6621 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006622 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006623 dprintk("<-- %s status=%d\n", __func__, status);
6624
6625 return status;
6626}
6627
6628int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6629{
6630 struct nfs4_exception exception = { };
6631 int err;
6632
6633 do {
6634 err = nfs4_handle_exception(server,
6635 _nfs4_proc_getdeviceinfo(server, pdev),
6636 &exception);
6637 } while (exception.retry);
6638 return err;
6639}
6640EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6641
Andy Adamson863a3c62011-03-23 13:27:54 +00006642static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6643{
6644 struct nfs4_layoutcommit_data *data = calldata;
6645 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006646 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006647
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006648 nfs41_setup_sequence(session,
6649 &data->args.seq_args,
6650 &data->res.seq_res,
6651 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006652}
6653
6654static void
6655nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6656{
6657 struct nfs4_layoutcommit_data *data = calldata;
6658 struct nfs_server *server = NFS_SERVER(data->args.inode);
6659
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006660 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006661 return;
6662
6663 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006664 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6665 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6666 case -NFS4ERR_BADLAYOUT: /* no layout */
6667 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006668 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006669 break;
6670 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006671 nfs_post_op_update_inode_force_wcc(data->args.inode,
6672 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006673 break;
6674 default:
6675 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6676 rpc_restart_call_prepare(task);
6677 return;
6678 }
6679 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006680}
6681
6682static void nfs4_layoutcommit_release(void *calldata)
6683{
6684 struct nfs4_layoutcommit_data *data = calldata;
6685
Andy Adamsondb29c082011-07-30 20:52:38 -04006686 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006687 put_rpccred(data->cred);
6688 kfree(data);
6689}
6690
6691static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6692 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6693 .rpc_call_done = nfs4_layoutcommit_done,
6694 .rpc_release = nfs4_layoutcommit_release,
6695};
6696
6697int
Andy Adamsonef311532011-03-12 02:58:10 -05006698nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006699{
6700 struct rpc_message msg = {
6701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6702 .rpc_argp = &data->args,
6703 .rpc_resp = &data->res,
6704 .rpc_cred = data->cred,
6705 };
6706 struct rpc_task_setup task_setup_data = {
6707 .task = &data->task,
6708 .rpc_client = NFS_CLIENT(data->args.inode),
6709 .rpc_message = &msg,
6710 .callback_ops = &nfs4_layoutcommit_ops,
6711 .callback_data = data,
6712 .flags = RPC_TASK_ASYNC,
6713 };
6714 struct rpc_task *task;
6715 int status = 0;
6716
6717 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6718 "lbw: %llu inode %lu\n",
6719 data->task.tk_pid, sync,
6720 data->args.lastbytewritten,
6721 data->args.inode->i_ino);
6722
Trond Myklebust9d12b212012-01-17 22:04:25 -05006723 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006724 task = rpc_run_task(&task_setup_data);
6725 if (IS_ERR(task))
6726 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006727 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006728 goto out;
6729 status = nfs4_wait_for_completion_rpc_task(task);
6730 if (status != 0)
6731 goto out;
6732 status = task->tk_status;
6733out:
6734 dprintk("%s: status %d\n", __func__, status);
6735 rpc_put_task(task);
6736 return status;
6737}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006738
6739static int
6740_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6741 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6742{
6743 struct nfs41_secinfo_no_name_args args = {
6744 .style = SECINFO_STYLE_CURRENT_FH,
6745 };
6746 struct nfs4_secinfo_res res = {
6747 .flavors = flavors,
6748 };
6749 struct rpc_message msg = {
6750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6751 .rpc_argp = &args,
6752 .rpc_resp = &res,
6753 };
6754 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6755}
6756
6757static int
6758nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6759 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6760{
6761 struct nfs4_exception exception = { };
6762 int err;
6763 do {
6764 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6765 switch (err) {
6766 case 0:
6767 case -NFS4ERR_WRONGSEC:
6768 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006769 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006770 default:
6771 err = nfs4_handle_exception(server, err, &exception);
6772 }
6773 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006774out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006775 return err;
6776}
6777
6778static int
6779nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6780 struct nfs_fsinfo *info)
6781{
6782 int err;
6783 struct page *page;
6784 rpc_authflavor_t flavor;
6785 struct nfs4_secinfo_flavors *flavors;
6786
6787 page = alloc_page(GFP_KERNEL);
6788 if (!page) {
6789 err = -ENOMEM;
6790 goto out;
6791 }
6792
6793 flavors = page_address(page);
6794 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6795
6796 /*
6797 * Fall back on "guess and check" method if
6798 * the server doesn't support SECINFO_NO_NAME
6799 */
6800 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6801 err = nfs4_find_root_sec(server, fhandle, info);
6802 goto out_freepage;
6803 }
6804 if (err)
6805 goto out_freepage;
6806
6807 flavor = nfs_find_best_sec(flavors);
6808 if (err == 0)
6809 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6810
6811out_freepage:
6812 put_page(page);
6813 if (err == -EACCES)
6814 return -EPERM;
6815out:
6816 return err;
6817}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006818
6819static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006820{
6821 int status;
6822 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006823 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006824 };
6825 struct nfs41_test_stateid_res res;
6826 struct rpc_message msg = {
6827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6828 .rpc_argp = &args,
6829 .rpc_resp = &res,
6830 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006831
Chuck Lever38527b12012-07-11 16:30:23 -04006832 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006833 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006834 nfs4_set_sequence_privileged(&args.seq_args);
6835 status = nfs4_call_sync_sequence(server->client, server, &msg,
6836 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006837 if (status != NFS_OK) {
6838 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006839 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006840 }
6841 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006842 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006843}
6844
Chuck Lever38527b12012-07-11 16:30:23 -04006845/**
6846 * nfs41_test_stateid - perform a TEST_STATEID operation
6847 *
6848 * @server: server / transport on which to perform the operation
6849 * @stateid: state ID to test
6850 *
6851 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6852 * Otherwise a negative NFS4ERR value is returned if the operation
6853 * failed or the state ID is not currently valid.
6854 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006855static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006856{
6857 struct nfs4_exception exception = { };
6858 int err;
6859 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006860 err = _nfs41_test_stateid(server, stateid);
6861 if (err != -NFS4ERR_DELAY)
6862 break;
6863 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006864 } while (exception.retry);
6865 return err;
6866}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006867
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006868struct nfs_free_stateid_data {
6869 struct nfs_server *server;
6870 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006871 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006872};
6873
6874static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
6875{
6876 struct nfs_free_stateid_data *data = calldata;
6877 nfs41_setup_sequence(nfs4_get_session(data->server),
6878 &data->args.seq_args,
6879 &data->res.seq_res,
6880 task);
6881}
6882
6883static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
6884{
6885 struct nfs_free_stateid_data *data = calldata;
6886
6887 nfs41_sequence_done(task, &data->res.seq_res);
6888
6889 switch (task->tk_status) {
6890 case -NFS4ERR_DELAY:
6891 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
6892 rpc_restart_call_prepare(task);
6893 }
6894}
6895
6896static void nfs41_free_stateid_release(void *calldata)
6897{
6898 kfree(calldata);
6899}
6900
6901const struct rpc_call_ops nfs41_free_stateid_ops = {
6902 .rpc_call_prepare = nfs41_free_stateid_prepare,
6903 .rpc_call_done = nfs41_free_stateid_done,
6904 .rpc_release = nfs41_free_stateid_release,
6905};
6906
6907static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
6908 nfs4_stateid *stateid,
6909 bool privileged)
6910{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006911 struct rpc_message msg = {
6912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006913 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006914 struct rpc_task_setup task_setup = {
6915 .rpc_client = server->client,
6916 .rpc_message = &msg,
6917 .callback_ops = &nfs41_free_stateid_ops,
6918 .flags = RPC_TASK_ASYNC,
6919 };
6920 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006921
Chuck Lever38527b12012-07-11 16:30:23 -04006922 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006923 data = kmalloc(sizeof(*data), GFP_NOFS);
6924 if (!data)
6925 return ERR_PTR(-ENOMEM);
6926 data->server = server;
6927 nfs4_stateid_copy(&data->args.stateid, stateid);
6928
6929 task_setup.callback_data = data;
6930
6931 msg.rpc_argp = &data->args;
6932 msg.rpc_resp = &data->res;
6933 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6934 if (privileged)
6935 nfs4_set_sequence_privileged(&data->args.seq_args);
6936
6937 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006938}
6939
Chuck Lever38527b12012-07-11 16:30:23 -04006940/**
6941 * nfs41_free_stateid - perform a FREE_STATEID operation
6942 *
6943 * @server: server / transport on which to perform the operation
6944 * @stateid: state ID to release
6945 *
6946 * Returns NFS_OK if the server freed "stateid". Otherwise a
6947 * negative NFS4ERR value is returned.
6948 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006949static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006950{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006951 struct rpc_task *task;
6952 int ret;
6953
6954 task = _nfs41_free_stateid(server, stateid, true);
6955 if (IS_ERR(task))
6956 return PTR_ERR(task);
6957 ret = rpc_wait_for_completion_task(task);
6958 if (!ret)
6959 ret = task->tk_status;
6960 rpc_put_task(task);
6961 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006962}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006963
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006964static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
6965{
6966 struct rpc_task *task;
6967
6968 task = _nfs41_free_stateid(server, &lsp->ls_stateid, false);
6969 nfs4_free_lock_state(server, lsp);
6970 if (IS_ERR(task))
6971 return PTR_ERR(task);
6972 rpc_put_task(task);
6973 return 0;
6974}
6975
Trond Myklebust36281ca2012-03-04 18:13:56 -05006976static bool nfs41_match_stateid(const nfs4_stateid *s1,
6977 const nfs4_stateid *s2)
6978{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006979 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006980 return false;
6981
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006982 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006983 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006984 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006985 return true;
6986
6987 return false;
6988}
6989
Andy Adamson557134a2009-04-01 09:21:53 -04006990#endif /* CONFIG_NFS_V4_1 */
6991
Trond Myklebust36281ca2012-03-04 18:13:56 -05006992static bool nfs4_match_stateid(const nfs4_stateid *s1,
6993 const nfs4_stateid *s2)
6994{
Trond Myklebustf597c532012-03-04 18:13:56 -05006995 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006996}
6997
6998
Trond Myklebust17280172012-03-11 13:11:00 -04006999static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007000 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007001 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 .recover_open = nfs4_open_reclaim,
7003 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007004 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007005 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007006 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007};
7008
Andy Adamson591d71c2009-04-01 09:22:47 -04007009#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007010static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007011 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7012 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7013 .recover_open = nfs4_open_reclaim,
7014 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007015 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007016 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007017 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007018 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007019};
7020#endif /* CONFIG_NFS_V4_1 */
7021
Trond Myklebust17280172012-03-11 13:11:00 -04007022static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007023 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007024 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 .recover_open = nfs4_open_expired,
7026 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007027 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007028 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029};
7030
Andy Adamson591d71c2009-04-01 09:22:47 -04007031#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007032static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007033 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7034 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007035 .recover_open = nfs41_open_expired,
7036 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007037 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007038 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007039};
7040#endif /* CONFIG_NFS_V4_1 */
7041
Trond Myklebust17280172012-03-11 13:11:00 -04007042static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007043 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007044 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007045 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007046};
7047
7048#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007049static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007050 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007051 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007052 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007053};
7054#endif
7055
Trond Myklebust97dc1352010-06-16 09:52:26 -04007056static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7057 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007058 .init_caps = NFS_CAP_READDIRPLUS
7059 | NFS_CAP_ATOMIC_OPEN
7060 | NFS_CAP_CHANGE_ATTR
7061 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007062 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007063 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007064 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007065 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007066 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7067 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7068 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007069};
7070
7071#if defined(CONFIG_NFS_V4_1)
7072static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7073 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007074 .init_caps = NFS_CAP_READDIRPLUS
7075 | NFS_CAP_ATOMIC_OPEN
7076 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007077 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007078 | NFS_CAP_STATEID_NFSV41
7079 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007080 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007081 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007082 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007083 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007084 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7085 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7086 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007087};
7088#endif
7089
Steve Dickson42c2c422013-05-22 12:50:38 -04007090#if defined(CONFIG_NFS_V4_2)
7091static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7092 .minor_version = 2,
7093 .call_sync = nfs4_call_sync_sequence,
7094 .match_stateid = nfs41_match_stateid,
7095 .find_root_sec = nfs41_find_root_sec,
7096 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7097 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7098 .state_renewal_ops = &nfs41_state_renewal_ops,
7099};
7100#endif
7101
Trond Myklebust97dc1352010-06-16 09:52:26 -04007102const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7103 [0] = &nfs_v4_0_minor_ops,
7104#if defined(CONFIG_NFS_V4_1)
7105 [1] = &nfs_v4_1_minor_ops,
7106#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007107#if defined(CONFIG_NFS_V4_2)
7108 [2] = &nfs_v4_2_minor_ops,
7109#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007110};
7111
Bryan Schumaker73a79702012-07-16 16:39:12 -04007112const struct inode_operations nfs4_dir_inode_operations = {
7113 .create = nfs_create,
7114 .lookup = nfs_lookup,
7115 .atomic_open = nfs_atomic_open,
7116 .link = nfs_link,
7117 .unlink = nfs_unlink,
7118 .symlink = nfs_symlink,
7119 .mkdir = nfs_mkdir,
7120 .rmdir = nfs_rmdir,
7121 .mknod = nfs_mknod,
7122 .rename = nfs_rename,
7123 .permission = nfs_permission,
7124 .getattr = nfs_getattr,
7125 .setattr = nfs_setattr,
7126 .getxattr = generic_getxattr,
7127 .setxattr = generic_setxattr,
7128 .listxattr = generic_listxattr,
7129 .removexattr = generic_removexattr,
7130};
7131
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007132static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007133 .permission = nfs_permission,
7134 .getattr = nfs_getattr,
7135 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007136 .getxattr = generic_getxattr,
7137 .setxattr = generic_setxattr,
7138 .listxattr = generic_listxattr,
7139 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007140};
7141
David Howells509de812006-08-22 20:06:11 -04007142const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 .version = 4, /* protocol version */
7144 .dentry_ops = &nfs4_dentry_operations,
7145 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007146 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007147 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007149 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007150 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 .getattr = nfs4_proc_getattr,
7152 .setattr = nfs4_proc_setattr,
7153 .lookup = nfs4_proc_lookup,
7154 .access = nfs4_proc_access,
7155 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 .create = nfs4_proc_create,
7157 .remove = nfs4_proc_remove,
7158 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007159 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 .unlink_done = nfs4_proc_unlink_done,
7161 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007162 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007163 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007164 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 .link = nfs4_proc_link,
7166 .symlink = nfs4_proc_symlink,
7167 .mkdir = nfs4_proc_mkdir,
7168 .rmdir = nfs4_proc_remove,
7169 .readdir = nfs4_proc_readdir,
7170 .mknod = nfs4_proc_mknod,
7171 .statfs = nfs4_proc_statfs,
7172 .fsinfo = nfs4_proc_fsinfo,
7173 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007174 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 .decode_dirent = nfs4_decode_dirent,
7176 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007177 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007178 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007179 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007181 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007182 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007183 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007185 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007186 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007188 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007189 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007190 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007191 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007192 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007193 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007194 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007195 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007196 .create_server = nfs4_create_server,
7197 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198};
7199
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007200static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7201 .prefix = XATTR_NAME_NFSV4_ACL,
7202 .list = nfs4_xattr_list_nfs4_acl,
7203 .get = nfs4_xattr_get_nfs4_acl,
7204 .set = nfs4_xattr_set_nfs4_acl,
7205};
7206
7207const struct xattr_handler *nfs4_xattr_handlers[] = {
7208 &nfs4_xattr_nfs4_acl_handler,
7209 NULL
7210};
7211
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212/*
7213 * Local variables:
7214 * c-basic-offset: 8
7215 * End:
7216 */