blob: 5881f2cc5d81fcb169723937a6986327b0f5ad8e [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>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050055#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040056#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000057#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050058#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010059#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebust4ce79712005-06-22 17:16:21 +000061#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050063#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050064#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040065#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040066#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040067#include "netns.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 Myklebusta78cb572009-08-09 15:06:19 -040074#define NFS4_MAX_LOOP_ON_RECOVER (10)
75
Trond Myklebustef159e92012-02-06 19:50:40 -050076static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
77
Trond Myklebustcdd4e682006-01-03 09:55:12 +010078struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010079static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080080static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050082static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050083static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040084static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040085static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040086static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
87 struct nfs_fattr *fattr, struct iattr *sattr,
88 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050094static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Trond Myklebust52567b02009-10-23 14:46:42 -040096 if (err >= -1000)
97 return err;
98 switch (err) {
99 case -NFS4ERR_RESOURCE:
100 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000101 case -NFS4ERR_WRONGSEC:
102 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800103 case -NFS4ERR_BADOWNER:
104 case -NFS4ERR_BADNAME:
105 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400106 case -NFS4ERR_SHARE_DENIED:
107 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400108 case -NFS4ERR_MINOR_VERS_MISMATCH:
109 return -EPROTONOSUPPORT;
Trond Myklebust52567b02009-10-23 14:46:42 -0400110 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700112 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400115 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/*
119 * This is our standard bitmap for GETATTR requests.
120 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400121const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 FATTR4_WORD0_TYPE
123 | FATTR4_WORD0_CHANGE
124 | FATTR4_WORD0_SIZE
125 | FATTR4_WORD0_FSID
126 | FATTR4_WORD0_FILEID,
127 FATTR4_WORD1_MODE
128 | FATTR4_WORD1_NUMLINKS
129 | FATTR4_WORD1_OWNER
130 | FATTR4_WORD1_OWNER_GROUP
131 | FATTR4_WORD1_RAWDEV
132 | FATTR4_WORD1_SPACE_USED
133 | FATTR4_WORD1_TIME_ACCESS
134 | FATTR4_WORD1_TIME_METADATA
135 | FATTR4_WORD1_TIME_MODIFY
136};
137
Trond Myklebust1549210f2012-06-05 09:16:47 -0400138static const u32 nfs4_pnfs_open_bitmap[3] = {
139 FATTR4_WORD0_TYPE
140 | FATTR4_WORD0_CHANGE
141 | FATTR4_WORD0_SIZE
142 | FATTR4_WORD0_FSID
143 | FATTR4_WORD0_FILEID,
144 FATTR4_WORD1_MODE
145 | FATTR4_WORD1_NUMLINKS
146 | FATTR4_WORD1_OWNER
147 | FATTR4_WORD1_OWNER_GROUP
148 | FATTR4_WORD1_RAWDEV
149 | FATTR4_WORD1_SPACE_USED
150 | FATTR4_WORD1_TIME_ACCESS
151 | FATTR4_WORD1_TIME_METADATA
152 | FATTR4_WORD1_TIME_MODIFY,
153 FATTR4_WORD2_MDSTHRESHOLD
154};
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156const u32 nfs4_statfs_bitmap[2] = {
157 FATTR4_WORD0_FILES_AVAIL
158 | FATTR4_WORD0_FILES_FREE
159 | FATTR4_WORD0_FILES_TOTAL,
160 FATTR4_WORD1_SPACE_AVAIL
161 | FATTR4_WORD1_SPACE_FREE
162 | FATTR4_WORD1_SPACE_TOTAL
163};
164
Trond Myklebust4ce79712005-06-22 17:16:21 +0000165const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 FATTR4_WORD0_MAXLINK
167 | FATTR4_WORD0_MAXNAME,
168 0
169};
170
Fred Isamandae100c2011-07-30 20:52:37 -0400171const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 | FATTR4_WORD0_MAXREAD
173 | FATTR4_WORD0_MAXWRITE
174 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700175 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400176 | FATTR4_WORD1_FS_LAYOUT_TYPES,
177 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
Manoj Naik830b8e32006-06-09 09:34:25 -0400180const u32 nfs4_fs_locations_bitmap[2] = {
181 FATTR4_WORD0_TYPE
182 | FATTR4_WORD0_CHANGE
183 | FATTR4_WORD0_SIZE
184 | FATTR4_WORD0_FSID
185 | FATTR4_WORD0_FILEID
186 | FATTR4_WORD0_FS_LOCATIONS,
187 FATTR4_WORD1_MODE
188 | FATTR4_WORD1_NUMLINKS
189 | FATTR4_WORD1_OWNER
190 | FATTR4_WORD1_OWNER_GROUP
191 | FATTR4_WORD1_RAWDEV
192 | FATTR4_WORD1_SPACE_USED
193 | FATTR4_WORD1_TIME_ACCESS
194 | FATTR4_WORD1_TIME_METADATA
195 | FATTR4_WORD1_TIME_MODIFY
196 | FATTR4_WORD1_MOUNTED_ON_FILEID
197};
198
Al Virobc4785c2006-10-19 23:28:51 -0700199static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct nfs4_readdir_arg *readdir)
201{
Al Viro0dbb4c62006-10-19 23:28:49 -0700202 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 BUG_ON(readdir->count < 80);
205 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000206 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
208 return;
209 }
210
211 readdir->cookie = 0;
212 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
213 if (cookie == 2)
214 return;
215
216 /*
217 * NFSv4 servers do not return entries for '.' and '..'
218 * Therefore, we fake these entries here. We let '.'
219 * have cookie 0 and '..' have cookie 1. Note that
220 * when talking to the server, we always send cookie 0
221 * instead of 1 or 2.
222 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800223 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (cookie == 0) {
226 *p++ = xdr_one; /* next */
227 *p++ = xdr_zero; /* cookie, first word */
228 *p++ = xdr_one; /* cookie, second word */
229 *p++ = xdr_one; /* entry len */
230 memcpy(p, ".\0\0\0", 4); /* entry */
231 p++;
232 *p++ = xdr_one; /* bitmap length */
233 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
234 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400235 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
238 *p++ = xdr_one; /* next */
239 *p++ = xdr_zero; /* cookie, first word */
240 *p++ = xdr_two; /* cookie, second word */
241 *p++ = xdr_two; /* entry len */
242 memcpy(p, "..\0\0", 4); /* entry */
243 p++;
244 *p++ = xdr_one; /* bitmap length */
245 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
246 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400247 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 readdir->pgbase = (char *)p - (char *)start;
250 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800251 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Trond Myklebust65de8722008-12-23 15:21:44 -0500254static int nfs4_wait_clnt_recover(struct nfs_client *clp)
255{
256 int res;
257
258 might_sleep();
259
Trond Myklebuste005e802008-12-23 15:21:48 -0500260 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400261 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 return res;
263}
264
265static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
266{
267 int res = 0;
268
269 might_sleep();
270
271 if (*timeout <= 0)
272 *timeout = NFS4_POLL_RETRY_MIN;
273 if (*timeout > NFS4_POLL_RETRY_MAX)
274 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100275 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500276 if (fatal_signal_pending(current))
277 res = -ERESTARTSYS;
278 *timeout <<= 1;
279 return res;
280}
281
282/* This is the error handling routine for processes that are allowed
283 * to sleep.
284 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800285static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500286{
287 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500288 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500289 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500290 int ret = errorcode;
291
292 exception->retry = 0;
293 switch(errorcode) {
294 case 0:
295 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500296 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400297 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500298 nfs_inode_return_delegation(inode);
299 exception->retry = 1;
300 return 0;
301 }
302 if (state == NULL)
303 break;
304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500306 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500307 case -NFS4ERR_ADMIN_REVOKED:
308 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500309 if (state == NULL)
310 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400311 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500312 nfs4_schedule_stateid_recovery(server, state);
313 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400314 case -NFS4ERR_EXPIRED:
315 if (state != NULL)
316 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500317 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500318 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500319 nfs4_schedule_lease_recovery(clp);
320 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500321#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400322 case -NFS4ERR_BADSESSION:
323 case -NFS4ERR_BADSLOT:
324 case -NFS4ERR_BAD_HIGH_SLOT:
325 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
326 case -NFS4ERR_DEADSESSION:
327 case -NFS4ERR_SEQ_FALSE_RETRY:
328 case -NFS4ERR_SEQ_MISORDERED:
329 dprintk("%s ERROR: %d Reset session\n", __func__,
330 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400331 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400332 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500333 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500334#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500335 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500336 if (exception->timeout > HZ) {
337 /* We have retried a decent amount, time to
338 * fail
339 */
340 ret = -EBUSY;
341 break;
342 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500343 case -NFS4ERR_GRACE:
344 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500345 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500346 ret = nfs4_delay(server->client, &exception->timeout);
347 if (ret != 0)
348 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400349 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500350 case -NFS4ERR_OLD_STATEID:
351 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800352 break;
353 case -NFS4ERR_BADOWNER:
354 /* The following works around a Linux server bug! */
355 case -NFS4ERR_BADNAME:
356 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
357 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
358 exception->retry = 1;
359 printk(KERN_WARNING "NFS: v4 server %s "
360 "does not accept raw "
361 "uid/gids. "
362 "Reenabling the idmapper.\n",
363 server->nfs_client->cl_hostname);
364 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500365 }
366 /* We failed to handle the error */
367 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500368wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500369 ret = nfs4_wait_clnt_recover(clp);
370 if (ret == 0)
371 exception->retry = 1;
372 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500373}
374
375
Trond Myklebust452e9352010-07-31 14:29:06 -0400376static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 spin_lock(&clp->cl_lock);
379 if (time_before(clp->cl_last_renewal,timestamp))
380 clp->cl_last_renewal = timestamp;
381 spin_unlock(&clp->cl_lock);
382}
383
Trond Myklebust452e9352010-07-31 14:29:06 -0400384static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
385{
386 do_renew_lease(server->nfs_client, timestamp);
387}
388
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400389#if defined(CONFIG_NFS_V4_1)
390
Benny Halevy510b8172009-04-01 09:22:14 -0400391/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400392 * nfs4_free_slot - free a slot and efficiently update slot table.
393 *
394 * freeing a slot is trivially done by clearing its respective bit
395 * in the bitmap.
396 * If the freed slotid equals highest_used_slotid we want to update it
397 * so that the server would be able to size down the slot table if needed,
398 * otherwise we know that the highest_used_slotid is still in use.
399 * When updating highest_used_slotid there may be "holes" in the bitmap
400 * so we need to scan down from highest_used_slotid to 0 looking for the now
401 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500402 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500403 *
404 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400405 */
406static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500407nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400408{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500409 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400410 /* clear used bit in bitmap */
411 __clear_bit(slotid, tbl->used_slots);
412
413 /* update highest_used_slotid when it is freed */
414 if (slotid == tbl->highest_used_slotid) {
415 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500416 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400417 tbl->highest_used_slotid = slotid;
418 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500419 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400420 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500421 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
422 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400423}
424
Trond Myklebust961a8282012-01-17 22:57:37 -0500425bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
426{
427 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
428 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400429}
430
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800431/*
Andy Adamson42acd022011-01-06 02:04:34 +0000432 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800433 */
Andy Adamson42acd022011-01-06 02:04:34 +0000434static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400436 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500437 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
438 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800439 return;
440 }
441
Trond Myklebust45d43c22012-02-06 19:38:51 -0500442 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800443 return;
444
Andy Adamson42acd022011-01-06 02:04:34 +0000445 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
446 complete(&ses->fc_slot_table.complete);
447}
448
449/*
450 * Signal state manager thread if session back channel is drained
451 */
452void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
453{
454 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500455 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000456 return;
457 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
458 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800459}
460
Trond Myklebustd185a332010-06-16 09:52:25 -0400461static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400462{
463 struct nfs4_slot_table *tbl;
464
Trond Myklebustd185a332010-06-16 09:52:25 -0400465 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400466 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400467 /* just wake up the next guy waiting since
468 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500469 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500470 return;
Andy Adamson13615872009-04-01 09:22:17 -0400471 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500472
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500473 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500474 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000475 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500476 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400477 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400478}
479
Trond Myklebust14516c32010-07-31 14:29:06 -0400480static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400481{
482 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400483 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400484
485 /*
486 * sr_status remains 1 if an RPC level error occurred. The server
487 * may or may not have processed the sequence operation..
488 * Proceed as if the server received and processed the sequence
489 * operation.
490 */
491 if (res->sr_status == 1)
492 res->sr_status = NFS_OK;
493
Bryan Schumaker468f8612011-04-18 15:57:32 -0400494 /* don't increment the sequence number if the task wasn't sent */
495 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400496 goto out;
497
Andy Adamson691daf32009-12-04 15:55:39 -0500498 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400499 switch (res->sr_status) {
500 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400502 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400503 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400504 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400505 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500506 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500507 if (res->sr_status_flags != 0)
508 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400509 break;
510 case -NFS4ERR_DELAY:
511 /* The server detected a resend of the RPC call and
512 * returned NFS4ERR_DELAY as per Section 2.10.6.2
513 * of RFC5661.
514 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200515 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400516 __func__,
517 res->sr_slot - res->sr_session->fc_slot_table.slots,
518 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400519 goto out_retry;
520 default:
521 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400522 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400523 }
524out:
525 /* The session may be reset by one of the error handlers. */
526 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400527 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400528 return 1;
529out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400530 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400531 goto out;
532 rpc_delay(task, NFS4_POLL_RETRY_MAX);
533 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400534}
535
Trond Myklebust14516c32010-07-31 14:29:06 -0400536static int nfs4_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400538{
Trond Myklebust14516c32010-07-31 14:29:06 -0400539 if (res->sr_session == NULL)
540 return 1;
541 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400542}
543
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400544/*
Benny Halevy510b8172009-04-01 09:22:14 -0400545 * nfs4_find_slot - efficiently look for a free slot
546 *
547 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
548 * If found, we mark the slot as used, update the highest_used_slotid,
549 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500550 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400551 *
552 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400553 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500554static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800555nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400556{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500557 u32 slotid;
558 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400559
Trond Myklebust45d43c22012-02-06 19:38:51 -0500560 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400561 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
562 tbl->max_slots);
563 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
564 if (slotid >= tbl->max_slots)
565 goto out;
566 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500567 if (slotid > tbl->highest_used_slotid ||
568 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400569 tbl->highest_used_slotid = slotid;
570 ret_id = slotid;
571out:
572 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
573 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400574 return ret_id;
575}
576
Trond Myklebust9d12b212012-01-17 22:04:25 -0500577static void nfs41_init_sequence(struct nfs4_sequence_args *args,
578 struct nfs4_sequence_res *res, int cache_reply)
579{
580 args->sa_session = NULL;
581 args->sa_cache_this = 0;
582 if (cache_reply)
583 args->sa_cache_this = 1;
584 res->sr_session = NULL;
585 res->sr_slot = NULL;
586}
587
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000588int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400589 struct nfs4_sequence_args *args,
590 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 struct rpc_task *task)
592{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593 struct nfs4_slot *slot;
594 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500595 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596
597 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400599 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 return 0;
601
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400602 tbl = &session->fc_slot_table;
603
604 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400605 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800606 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500607 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500608 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400609 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500610 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500611 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400612 }
613
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800614 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
615 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
616 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
617 spin_unlock(&tbl->slot_tbl_lock);
618 dprintk("%s enforce FIFO order\n", __func__);
619 return -EAGAIN;
620 }
621
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800622 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500623 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400624 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
625 spin_unlock(&tbl->slot_tbl_lock);
626 dprintk("<-- %s: no free slots\n", __func__);
627 return -EAGAIN;
628 }
629 spin_unlock(&tbl->slot_tbl_lock);
630
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800631 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400632 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400633 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400634 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400635
636 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
637
Benny Halevy99fe60d2009-04-01 09:22:29 -0400638 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400639 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400640 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400641 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642 /*
643 * sr_status is only set in decode_sequence, and so will remain
644 * set to 1 if an rpc level failure occurs.
645 */
646 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647 return 0;
648}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000649EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650
Trond Myklebust035168a2010-06-16 09:52:26 -0400651int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 struct nfs4_sequence_args *args,
653 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 struct rpc_task *task)
655{
Trond Myklebust035168a2010-06-16 09:52:26 -0400656 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657 int ret = 0;
658
Trond Myklebust9d12b212012-01-17 22:04:25 -0500659 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400660 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400661
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200662 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400663 __func__, session->clp, session, res->sr_slot ?
664 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400665
Trond Myklebust9d12b212012-01-17 22:04:25 -0500666 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667out:
668 dprintk("<-- %s status=%d\n", __func__, ret);
669 return ret;
670}
671
672struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400673 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674 struct nfs4_sequence_args *seq_args;
675 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676};
677
678static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
679{
680 struct nfs41_call_sync_data *data = calldata;
681
Trond Myklebust035168a2010-06-16 09:52:26 -0400682 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
683
684 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500685 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 return;
687 rpc_call_start(task);
688}
689
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800690static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
691{
692 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
693 nfs41_call_sync_prepare(task, calldata);
694}
695
Andy Adamson69ab40c2009-04-01 09:22:19 -0400696static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
697{
698 struct nfs41_call_sync_data *data = calldata;
699
Trond Myklebust14516c32010-07-31 14:29:06 -0400700 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400701}
702
Trond Myklebust17280172012-03-11 13:11:00 -0400703static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400705 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706};
707
Trond Myklebust17280172012-03-11 13:11:00 -0400708static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800709 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
710 .rpc_call_done = nfs41_call_sync_done,
711};
712
Bryan Schumaker7c513052011-03-24 17:12:24 +0000713static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
714 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715 struct rpc_message *msg,
716 struct nfs4_sequence_args *args,
717 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800718 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719{
720 int ret;
721 struct rpc_task *task;
722 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400723 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400724 .seq_args = args,
725 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726 };
727 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000728 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729 .rpc_message = msg,
730 .callback_ops = &nfs41_call_sync_ops,
731 .callback_data = &data
732 };
733
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800734 if (privileged)
735 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736 task = rpc_run_task(&task_setup);
737 if (IS_ERR(task))
738 ret = PTR_ERR(task);
739 else {
740 ret = task->tk_status;
741 rpc_put_task(task);
742 }
743 return ret;
744}
745
Bryan Schumaker7c513052011-03-24 17:12:24 +0000746int _nfs4_call_sync_session(struct rpc_clnt *clnt,
747 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400748 struct rpc_message *msg,
749 struct nfs4_sequence_args *args,
750 struct nfs4_sequence_res *res,
751 int cache_reply)
752{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500753 nfs41_init_sequence(args, res, cache_reply);
754 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400755}
756
Trond Myklebustdf896452010-06-16 09:52:26 -0400757#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500758static inline
759void nfs41_init_sequence(struct nfs4_sequence_args *args,
760 struct nfs4_sequence_res *res, int cache_reply)
761{
762}
763
Trond Myklebust14516c32010-07-31 14:29:06 -0400764static int nfs4_sequence_done(struct rpc_task *task,
765 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400766{
Trond Myklebust14516c32010-07-31 14:29:06 -0400767 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400768}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400769#endif /* CONFIG_NFS_V4_1 */
770
Bryan Schumaker7c513052011-03-24 17:12:24 +0000771int _nfs4_call_sync(struct rpc_clnt *clnt,
772 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400773 struct rpc_message *msg,
774 struct nfs4_sequence_args *args,
775 struct nfs4_sequence_res *res,
776 int cache_reply)
777{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500778 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000779 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400780}
781
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000782static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000783int nfs4_call_sync(struct rpc_clnt *clnt,
784 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000785 struct rpc_message *msg,
786 struct nfs4_sequence_args *args,
787 struct nfs4_sequence_res *res,
788 int cache_reply)
789{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000790 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
791 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000792}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400793
Trond Myklebust38478b22006-05-25 01:40:57 -0400794static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Trond Myklebust38478b22006-05-25 01:40:57 -0400796 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Trond Myklebust38478b22006-05-25 01:40:57 -0400798 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400799 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700800 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400801 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700802 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400803 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400807 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808 struct nfs_openargs o_arg;
809 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100810 struct nfs_open_confirmargs c_arg;
811 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500812 struct nfs4_string owner_name;
813 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400816 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400818 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100820 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400821 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100822 int rpc_status;
823 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824};
825
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400826
827static void nfs4_init_opendata_res(struct nfs4_opendata *p)
828{
829 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400830 p->o_res.seqid = p->o_arg.seqid;
831 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400832 p->o_res.server = p->o_arg.server;
833 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500834 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400835}
836
Al Viro82a2c1b2011-06-22 18:30:55 -0400837static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400839 const struct iattr *attrs,
840 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841{
Al Viro82a2c1b2011-06-22 18:30:55 -0400842 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843 struct inode *dir = parent->d_inode;
844 struct nfs_server *server = NFS_SERVER(dir);
845 struct nfs4_opendata *p;
846
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400847 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 if (p == NULL)
849 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400850 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851 if (p->o_arg.seqid == NULL)
852 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400853 nfs_sb_active(dentry->d_sb);
854 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 p->dir = parent;
856 p->owner = sp;
857 atomic_inc(&sp->so_count);
858 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500859 p->o_arg.open_flags = flags;
860 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400861 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400862 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
863 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400864 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865 p->o_arg.server = server;
866 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400867 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100868 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500869 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500870 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400871
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100872 p->o_arg.u.attrs = &p->attrs;
873 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500874
875 verf[0] = jiffies;
876 verf[1] = current->pid;
877 memcpy(p->o_arg.u.verifier.data, verf,
878 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100880 p->c_arg.fh = &p->o_res.fh;
881 p->c_arg.stateid = &p->o_res.stateid;
882 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400883 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400884 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 return p;
886err_free:
887 kfree(p);
888err:
889 dput(parent);
890 return NULL;
891}
892
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400893static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100894{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400895 struct nfs4_opendata *p = container_of(kref,
896 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400897 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898
899 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 if (p->state != NULL)
901 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400902 nfs4_put_state_owner(p->owner);
903 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400904 dput(p->dentry);
905 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500906 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400907 kfree(p);
908}
909
910static void nfs4_opendata_put(struct nfs4_opendata *p)
911{
912 if (p != NULL)
913 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100914}
915
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
917{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100918 int ret;
919
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100921 return ret;
922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925{
926 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927
Trond Myklebust536e43d2012-01-17 22:04:26 -0500928 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 goto out;
930 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400931 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500932 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
933 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 break;
935 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
937 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 break;
939 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500940 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
941 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 return ret;
945}
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500949 if (delegation == NULL)
950 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500953 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500955 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400956 return 1;
957}
958
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100960{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100962 case FMODE_WRITE:
963 state->n_wronly++;
964 break;
965 case FMODE_READ:
966 state->n_rdonly++;
967 break;
968 case FMODE_READ|FMODE_WRITE:
969 state->n_rdwr++;
970 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100972}
973
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400975{
976 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500977 nfs4_stateid_copy(&state->stateid, stateid);
978 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400980 case FMODE_READ:
981 set_bit(NFS_O_RDONLY_STATE, &state->flags);
982 break;
983 case FMODE_WRITE:
984 set_bit(NFS_O_WRONLY_STATE, &state->flags);
985 break;
986 case FMODE_READ|FMODE_WRITE:
987 set_bit(NFS_O_RDWR_STATE, &state->flags);
988 }
989}
990
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400992{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400993 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400995 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400996}
997
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998static 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 -0700999{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001000 /*
1001 * Protect the call to nfs4_state_set_mode_locked and
1002 * serialise the stateid update
1003 */
1004 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001005 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001006 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001007 set_bit(NFS_DELEGATED_STATE, &state->flags);
1008 }
1009 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001011 write_sequnlock(&state->seqlock);
1012 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001014 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017static 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 -05001018{
1019 struct nfs_inode *nfsi = NFS_I(state->inode);
1020 struct nfs_delegation *deleg_cur;
1021 int ret = 0;
1022
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001024
1025 rcu_read_lock();
1026 deleg_cur = rcu_dereference(nfsi->delegation);
1027 if (deleg_cur == NULL)
1028 goto no_delegation;
1029
1030 spin_lock(&deleg_cur->lock);
1031 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001033 goto no_delegation_unlock;
1034
1035 if (delegation == NULL)
1036 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001037 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001038 goto no_delegation_unlock;
1039
Trond Myklebustb7391f42008-12-23 15:21:52 -05001040 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001042 ret = 1;
1043no_delegation_unlock:
1044 spin_unlock(&deleg_cur->lock);
1045no_delegation:
1046 rcu_read_unlock();
1047
1048 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001050 ret = 1;
1051 }
1052
1053 return ret;
1054}
1055
1056
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058{
1059 struct nfs_delegation *delegation;
1060
1061 rcu_read_lock();
1062 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 rcu_read_unlock();
1065 return;
1066 }
1067 rcu_read_unlock();
1068 nfs_inode_return_delegation(inode);
1069}
1070
Trond Myklebust6ee41262007-07-08 14:11:36 -04001071static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072{
1073 struct nfs4_state *state = opendata->state;
1074 struct nfs_inode *nfsi = NFS_I(state->inode);
1075 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001076 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001077 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 nfs4_stateid stateid;
1079 int ret = -EAGAIN;
1080
Trond Myklebustaac00a82007-07-05 19:02:21 -04001081 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001083 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 if (can_open_cached(state, fmode, open_mode)) {
1085 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001086 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001087 goto out_return_state;
1088 }
1089 spin_unlock(&state->owner->so_lock);
1090 }
Trond Myklebust34310432008-12-23 15:21:38 -05001091 rcu_read_lock();
1092 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001093 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001094 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001095 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001096 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001097 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001098 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001100 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 if (ret != 0)
1102 goto out;
1103 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001104
1105 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001107 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001109out:
1110 return ERR_PTR(ret);
1111out_return_state:
1112 atomic_inc(&state->count);
1113 return state;
1114}
1115
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1117{
1118 struct inode *inode;
1119 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001120 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001121 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001122
Trond Myklebustaac00a82007-07-05 19:02:21 -04001123 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001124 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001125 goto out;
1126 }
1127
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001133 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 goto err;
1135 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001136 state = nfs4_get_open_state(inode, data->owner);
1137 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001138 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001139 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001140 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001141 int delegation_flags = 0;
1142
Trond Myklebust003707c2007-07-05 18:07:55 -04001143 rcu_read_lock();
1144 delegation = rcu_dereference(NFS_I(inode)->delegation);
1145 if (delegation)
1146 delegation_flags = delegation->flags;
1147 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001148 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1149 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1150 "returning a delegation for "
1151 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001152 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001153 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001154 nfs_inode_set_delegation(state->inode,
1155 data->owner->so_cred,
1156 &data->o_res);
1157 else
1158 nfs_inode_reclaim_delegation(state->inode,
1159 data->owner->so_cred,
1160 &data->o_res);
1161 }
Trond Myklebust34310432008-12-23 15:21:38 -05001162
1163 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001164 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001165 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001166out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001167 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001168err_put_inode:
1169 iput(inode);
1170err:
1171 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001172}
1173
Trond Myklebust864472e2006-01-03 09:55:15 +01001174static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1175{
1176 struct nfs_inode *nfsi = NFS_I(state->inode);
1177 struct nfs_open_context *ctx;
1178
1179 spin_lock(&state->inode->i_lock);
1180 list_for_each_entry(ctx, &nfsi->open_files, list) {
1181 if (ctx->state != state)
1182 continue;
1183 get_nfs_open_context(ctx);
1184 spin_unlock(&state->inode->i_lock);
1185 return ctx;
1186 }
1187 spin_unlock(&state->inode->i_lock);
1188 return ERR_PTR(-ENOENT);
1189}
1190
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001191static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1192{
1193 struct nfs4_opendata *opendata;
1194
Al Viro3d4ff432011-06-22 18:40:12 -04001195 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001196 if (opendata == NULL)
1197 return ERR_PTR(-ENOMEM);
1198 opendata->state = state;
1199 atomic_inc(&state->count);
1200 return opendata;
1201}
1202
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001203static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001204{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 int ret;
1207
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001208 opendata->o_arg.open_flags = 0;
1209 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1211 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1212 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001213 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 if (ret != 0)
1215 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001217 if (IS_ERR(newstate))
1218 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001219 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001220 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001221 return 0;
1222}
1223
1224static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1225{
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 int ret;
1228
1229 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001230 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 smp_rmb();
1232 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001233 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001234 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 if (ret != 0)
1236 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001237 if (newstate != state)
1238 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 }
1240 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001241 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001242 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001243 if (ret != 0)
1244 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001245 if (newstate != state)
1246 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001247 }
1248 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001249 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001250 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 if (ret != 0)
1252 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001253 if (newstate != state)
1254 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001255 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001256 /*
1257 * We may have performed cached opens for all three recoveries.
1258 * Check if we need to update the current stateid.
1259 */
1260 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001261 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001262 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001263 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001264 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001265 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001266 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 return 0;
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
1271 * OPEN_RECLAIM:
1272 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001274static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001276 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001277 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int status;
1280
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001281 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1282 if (IS_ERR(opendata))
1283 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1285 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001286 rcu_read_lock();
1287 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001288 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001289 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001290 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001291 opendata->o_arg.u.delegation_type = delegation_type;
1292 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return status;
1295}
1296
Trond Myklebust539cd032007-06-05 11:46:42 -04001297static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct nfs_server *server = NFS_SERVER(state->inode);
1300 struct nfs4_exception exception = { };
1301 int err;
1302 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001303 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001304 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001305 break;
1306 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 } while (exception.retry);
1308 return err;
1309}
1310
Trond Myklebust864472e2006-01-03 09:55:15 +01001311static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1312{
1313 struct nfs_open_context *ctx;
1314 int ret;
1315
1316 ctx = nfs4_state_find_open_context(state);
1317 if (IS_ERR(ctx))
1318 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001319 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001320 put_nfs_open_context(ctx);
1321 return ret;
1322}
1323
Trond Myklebust13437e12007-07-06 15:10:43 -04001324static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001326 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001327 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001329 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1330 if (IS_ERR(opendata))
1331 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001332 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001333 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001335 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Trond Myklebust13437e12007-07-06 15:10:43 -04001339int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001342 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int err;
1344 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001345 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 switch (err) {
1347 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001348 case -ENOENT:
1349 case -ESTALE:
1350 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001351 case -NFS4ERR_BADSESSION:
1352 case -NFS4ERR_BADSLOT:
1353 case -NFS4ERR_BAD_HIGH_SLOT:
1354 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1355 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001356 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 case -NFS4ERR_STALE_CLIENTID:
1359 case -NFS4ERR_STALE_STATEID:
1360 case -NFS4ERR_EXPIRED:
1361 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001362 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001363 goto out;
1364 case -ERESTARTSYS:
1365 /*
1366 * The show must go on: exit, but mark the
1367 * stateid as needing recovery.
1368 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001369 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001370 case -NFS4ERR_ADMIN_REVOKED:
1371 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001372 nfs_inode_find_state_and_recover(state->inode,
1373 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001374 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001375 case -EKEYEXPIRED:
1376 /*
1377 * User RPCSEC_GSS context has expired.
1378 * We cannot recover this stateid now, so
1379 * skip it and allow recovery thread to
1380 * proceed.
1381 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001382 case -ENOMEM:
1383 err = 0;
1384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386 err = nfs4_handle_exception(server, err, &exception);
1387 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001388out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return err;
1390}
1391
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001392static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1393{
1394 struct nfs4_opendata *data = calldata;
1395
1396 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001397 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001398 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001399 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001400 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001401 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001402 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403}
1404
1405static void nfs4_open_confirm_release(void *calldata)
1406{
1407 struct nfs4_opendata *data = calldata;
1408 struct nfs4_state *state = NULL;
1409
1410 /* If this request hasn't been cancelled, do nothing */
1411 if (data->cancelled == 0)
1412 goto out_free;
1413 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001414 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001415 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001417 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001418 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001420 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001421}
1422
1423static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001424 .rpc_call_done = nfs4_open_confirm_done,
1425 .rpc_release = nfs4_open_confirm_release,
1426};
1427
1428/*
1429 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1430 */
1431static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1432{
1433 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1434 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001435 struct rpc_message msg = {
1436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1437 .rpc_argp = &data->c_arg,
1438 .rpc_resp = &data->c_res,
1439 .rpc_cred = data->owner->so_cred,
1440 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001441 struct rpc_task_setup task_setup_data = {
1442 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001443 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001444 .callback_ops = &nfs4_open_confirm_ops,
1445 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001446 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001447 .flags = RPC_TASK_ASYNC,
1448 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 int status;
1450
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001451 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001452 data->rpc_done = 0;
1453 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001454 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001455 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001456 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001457 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001458 status = nfs4_wait_for_completion_rpc_task(task);
1459 if (status != 0) {
1460 data->cancelled = 1;
1461 smp_wmb();
1462 } else
1463 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001464 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return status;
1466}
1467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470 struct nfs4_opendata *data = calldata;
1471 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001472
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001473 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1474 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001475 /*
1476 * Check if we still need to send an OPEN call, or if we can use
1477 * a delegation instead.
1478 */
1479 if (data->state != NULL) {
1480 struct nfs_delegation *delegation;
1481
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001482 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001483 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001484 rcu_read_lock();
1485 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001486 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1487 can_open_delegated(delegation, data->o_arg.fmode))
1488 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001489 rcu_read_unlock();
1490 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001491 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001492 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001493 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001494 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001495 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1496 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001497 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001498 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001499 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001500 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001501 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001502 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001503 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001504unlock_no_action:
1505 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001506out_no_action:
1507 task->tk_action = NULL;
1508
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001511static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1512{
1513 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1514 nfs4_open_prepare(task, calldata);
1515}
1516
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517static void nfs4_open_done(struct rpc_task *task, void *calldata)
1518{
1519 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001522
Trond Myklebust14516c32010-07-31 14:29:06 -04001523 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1524 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001525
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526 if (task->tk_status == 0) {
1527 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001528 case S_IFREG:
1529 break;
1530 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001532 break;
1533 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001535 break;
1536 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001538 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001539 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001540 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1541 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001542 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001543 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001545
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546static void nfs4_open_release(void *calldata)
1547{
1548 struct nfs4_opendata *data = calldata;
1549 struct nfs4_state *state = NULL;
1550
1551 /* If this request hasn't been cancelled, do nothing */
1552 if (data->cancelled == 0)
1553 goto out_free;
1554 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001555 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001556 goto out_free;
1557 /* In case we need an open_confirm, no cleanup! */
1558 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1559 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001561 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001562 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001564 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001565}
1566
1567static const struct rpc_call_ops nfs4_open_ops = {
1568 .rpc_call_prepare = nfs4_open_prepare,
1569 .rpc_call_done = nfs4_open_done,
1570 .rpc_release = nfs4_open_release,
1571};
1572
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001573static const struct rpc_call_ops nfs4_recover_open_ops = {
1574 .rpc_call_prepare = nfs4_recover_open_prepare,
1575 .rpc_call_done = nfs4_open_done,
1576 .rpc_release = nfs4_open_release,
1577};
1578
1579static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001580{
1581 struct inode *dir = data->dir->d_inode;
1582 struct nfs_server *server = NFS_SERVER(dir);
1583 struct nfs_openargs *o_arg = &data->o_arg;
1584 struct nfs_openres *o_res = &data->o_res;
1585 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001586 struct rpc_message msg = {
1587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1588 .rpc_argp = o_arg,
1589 .rpc_resp = o_res,
1590 .rpc_cred = data->owner->so_cred,
1591 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001592 struct rpc_task_setup task_setup_data = {
1593 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001594 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 .callback_ops = &nfs4_open_ops,
1596 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001597 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001598 .flags = RPC_TASK_ASYNC,
1599 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600 int status;
1601
Trond Myklebust9d12b212012-01-17 22:04:25 -05001602 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001603 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001604 data->rpc_done = 0;
1605 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001606 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001607 if (isrecover)
1608 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001609 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001610 if (IS_ERR(task))
1611 return PTR_ERR(task);
1612 status = nfs4_wait_for_completion_rpc_task(task);
1613 if (status != 0) {
1614 data->cancelled = 1;
1615 smp_wmb();
1616 } else
1617 status = data->rpc_status;
1618 rpc_put_task(task);
1619
1620 return status;
1621}
1622
1623static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1624{
1625 struct inode *dir = data->dir->d_inode;
1626 struct nfs_openres *o_res = &data->o_res;
1627 int status;
1628
1629 status = nfs4_run_open_task(data, 1);
1630 if (status != 0 || !data->rpc_done)
1631 return status;
1632
Trond Myklebust6926afd2012-01-07 13:22:46 -05001633 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1634
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001635 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1636 status = _nfs4_proc_open_confirm(data);
1637 if (status != 0)
1638 return status;
1639 }
1640
1641 return status;
1642}
1643
1644/*
1645 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1646 */
1647static int _nfs4_proc_open(struct nfs4_opendata *data)
1648{
1649 struct inode *dir = data->dir->d_inode;
1650 struct nfs_server *server = NFS_SERVER(dir);
1651 struct nfs_openargs *o_arg = &data->o_arg;
1652 struct nfs_openres *o_res = &data->o_res;
1653 int status;
1654
1655 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001656 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001657 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001658 if (status != 0) {
1659 if (status == -NFS4ERR_BADNAME &&
1660 !(o_arg->open_flags & O_CREAT))
1661 return -ENOENT;
1662 return status;
1663 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664
Trond Myklebust6926afd2012-01-07 13:22:46 -05001665 nfs_fattr_map_and_free_names(server, &data->f_attr);
1666
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001667 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001668 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001669 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1670 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001672 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001674 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001677 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Andy Adamsond83217c2011-03-01 01:34:17 +00001681static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001682{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001683 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001684 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001685
Trond Myklebusta78cb572009-08-09 15:06:19 -04001686 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001687 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001688 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001689 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001690 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1691 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001692 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001693 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001694 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001695 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001696 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001697}
1698
Andy Adamsond83217c2011-03-01 01:34:17 +00001699static int nfs4_recover_expired_lease(struct nfs_server *server)
1700{
1701 return nfs4_client_recover_expired_lease(server->nfs_client);
1702}
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704/*
1705 * OPEN_EXPIRED:
1706 * reclaim state on the server after a network partition.
1707 * Assumes caller holds the appropriate lock
1708 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001709static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001711 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001712 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001714 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1715 if (IS_ERR(opendata))
1716 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001717 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001718 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001719 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001720 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001724static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001725{
Trond Myklebust539cd032007-06-05 11:46:42 -04001726 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727 struct nfs4_exception exception = { };
1728 int err;
1729
1730 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001731 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001732 switch (err) {
1733 default:
1734 goto out;
1735 case -NFS4ERR_GRACE:
1736 case -NFS4ERR_DELAY:
1737 nfs4_handle_exception(server, err, &exception);
1738 err = 0;
1739 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001740 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001741out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001742 return err;
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Trond Myklebust864472e2006-01-03 09:55:15 +01001750 ctx = nfs4_state_find_open_context(state);
1751 if (IS_ERR(ctx))
1752 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001753 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001754 put_nfs_open_context(ctx);
1755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001758#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001759static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001761 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762 struct nfs_server *server = NFS_SERVER(state->inode);
1763
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001764 if (state->flags & flags) {
1765 status = nfs41_test_stateid(server, stateid);
1766 if (status != NFS_OK) {
1767 nfs41_free_stateid(server, stateid);
1768 state->flags &= ~flags;
1769 }
1770 }
1771 return status;
1772}
1773
1774static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1775{
1776 int deleg_status, open_status;
1777 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1778 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1779
1780 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1781 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1782
1783 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1784 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001785 return nfs4_open_expired(sp, state);
1786}
1787#endif
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001790 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1791 * fields corresponding to attributes that were used to store the verifier.
1792 * Make sure we clobber those fields in the later setattr call
1793 */
1794static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1795{
1796 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1797 !(sattr->ia_valid & ATTR_ATIME_SET))
1798 sattr->ia_valid |= ATTR_ATIME;
1799
1800 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1801 !(sattr->ia_valid & ATTR_MTIME_SET))
1802 sattr->ia_valid |= ATTR_MTIME;
1803}
1804
1805/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001806 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Andy Adamson82be4172012-05-23 05:02:35 -04001808static int _nfs4_do_open(struct inode *dir,
1809 struct dentry *dentry,
1810 fmode_t fmode,
1811 int flags,
1812 struct iattr *sattr,
1813 struct rpc_cred *cred,
1814 struct nfs4_state **res,
1815 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 struct nfs4_state_owner *sp;
1818 struct nfs4_state *state = NULL;
1819 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001820 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001821 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001825 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1826 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1828 goto out_err;
1829 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001830 status = nfs4_recover_expired_lease(server);
1831 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001832 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001833 if (dentry->d_inode != NULL)
1834 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001835 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001836 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001837 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001838 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Andy Adamson82be4172012-05-23 05:02:35 -04001840 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1841 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1842 if (!opendata->f_attr.mdsthreshold)
1843 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001844 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001845 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001846 if (dentry->d_inode != NULL)
1847 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001848
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001849 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001851 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001853 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001854 status = PTR_ERR(state);
1855 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001856 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001857 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001858 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001859
1860 if (opendata->o_arg.open_flags & O_EXCL) {
1861 nfs4_exclusive_attrset(opendata, sattr);
1862
1863 nfs_fattr_init(opendata->o_res.f_attr);
1864 status = nfs4_do_setattr(state->inode, cred,
1865 opendata->o_res.f_attr, sattr,
1866 state);
1867 if (status == 0)
1868 nfs_setattr_update_inode(state->inode, sattr);
1869 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1870 }
Andy Adamson82be4172012-05-23 05:02:35 -04001871
1872 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1873 *ctx_th = opendata->f_attr.mdsthreshold;
1874 else
1875 kfree(opendata->f_attr.mdsthreshold);
1876 opendata->f_attr.mdsthreshold = NULL;
1877
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001878 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 *res = state;
1881 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001882err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001883 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001884 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001885err_put_state_owner:
1886 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 *res = NULL;
1889 return status;
1890}
1891
1892
Andy Adamson82be4172012-05-23 05:02:35 -04001893static struct nfs4_state *nfs4_do_open(struct inode *dir,
1894 struct dentry *dentry,
1895 fmode_t fmode,
1896 int flags,
1897 struct iattr *sattr,
1898 struct rpc_cred *cred,
1899 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 struct nfs4_exception exception = { };
1902 struct nfs4_state *res;
1903 int status;
1904
1905 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001906 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1907 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (status == 0)
1909 break;
1910 /* NOTE: BAD_SEQID means the server and client disagree about the
1911 * book-keeping w.r.t. state-changing operations
1912 * (OPEN/CLOSE/LOCK/LOCKU...)
1913 * It is actually a sign of a bug on the client or on the server.
1914 *
1915 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001916 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 * have unhashed the old state_owner for us, and that we can
1918 * therefore safely retry using a new one. We should still warn
1919 * the user though...
1920 */
1921 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001922 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001923 " returned a bad sequence-id error!\n",
1924 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 exception.retry = 1;
1926 continue;
1927 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001928 /*
1929 * BAD_STATEID on OPEN means that the server cancelled our
1930 * state before it received the OPEN_CONFIRM.
1931 * Recover by retrying the request as per the discussion
1932 * on Page 181 of RFC3530.
1933 */
1934 if (status == -NFS4ERR_BAD_STATEID) {
1935 exception.retry = 1;
1936 continue;
1937 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001938 if (status == -EAGAIN) {
1939 /* We must have found a delegation */
1940 exception.retry = 1;
1941 continue;
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1944 status, &exception));
1945 } while (exception.retry);
1946 return res;
1947}
1948
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001949static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1950 struct nfs_fattr *fattr, struct iattr *sattr,
1951 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001953 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001955 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 .iap = sattr,
1957 .server = server,
1958 .bitmask = server->attr_bitmask,
1959 };
1960 struct nfs_setattrres res = {
1961 .fattr = fattr,
1962 .server = server,
1963 };
1964 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1966 .rpc_argp = &arg,
1967 .rpc_resp = &res,
1968 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001970 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001971 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Trond Myklebust0e574af2005-10-27 22:12:38 -04001973 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Trond Myklebust4fc87962012-03-08 17:42:01 -05001975 if (state != NULL) {
1976 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1977 current->files, current->tgid);
1978 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1979 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001980 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001981 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001982 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Bryan Schumaker7c513052011-03-24 17:12:24 +00001984 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001985 if (status == 0 && state != NULL)
1986 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001987 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001990static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1991 struct nfs_fattr *fattr, struct iattr *sattr,
1992 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001994 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001995 struct nfs4_exception exception = {
1996 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001997 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int err;
2000 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002001 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2002 switch (err) {
2003 case -NFS4ERR_OPENMODE:
2004 if (state && !(state->state & FMODE_WRITE)) {
2005 err = -EBADF;
2006 if (sattr->ia_valid & ATTR_OPEN)
2007 err = -EACCES;
2008 goto out;
2009 }
2010 }
2011 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002013out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return err;
2015}
2016
2017struct nfs4_closedata {
2018 struct inode *inode;
2019 struct nfs4_state *state;
2020 struct nfs_closeargs arg;
2021 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002022 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002023 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002024 bool roc;
2025 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026};
2027
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002028static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002029{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002030 struct nfs4_closedata *calldata = data;
2031 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002032 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002033
Fred Isamanf7e89172011-01-06 11:36:32 +00002034 if (calldata->roc)
2035 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002036 nfs4_put_open_state(calldata->state);
2037 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002038 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002039 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002040 kfree(calldata);
2041}
2042
Trond Myklebust88069f72009-12-08 08:33:16 -05002043static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2044 fmode_t fmode)
2045{
2046 spin_lock(&state->owner->so_lock);
2047 if (!(fmode & FMODE_READ))
2048 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2049 if (!(fmode & FMODE_WRITE))
2050 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2051 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2052 spin_unlock(&state->owner->so_lock);
2053}
2054
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002055static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002057 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct nfs_server *server = NFS_SERVER(calldata->inode);
2060
Chuck Levera3ca5652012-03-01 17:00:40 -05002061 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002062 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2063 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 /* hmm. we are done with the inode, and in the process of freeing
2065 * the state_owner. we keep this around to process errors
2066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 switch (task->tk_status) {
2068 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002069 if (calldata->roc)
2070 pnfs_roc_set_barrier(state->inode,
2071 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002072 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002073 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002074 nfs4_close_clear_stateid_flags(state,
2075 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 break;
2077 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002078 case -NFS4ERR_OLD_STATEID:
2079 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002081 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002082 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002084 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2085 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002087 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002088 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002089 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002092static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002094 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002095 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002096 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002097
Chuck Levera3ca5652012-03-01 17:00:40 -05002098 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002099 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002100 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002101
Trond Myklebust88069f72009-12-08 08:33:16 -05002102 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2103 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002104 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002105 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002106 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002107 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002108 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2109 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2110 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002111 }
2112 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002113 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2114 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2115 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002116 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002117 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002118 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002119
2120 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002121 /* Note: exit _without_ calling nfs4_close_done */
2122 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002123 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002124 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002125
Fred Isamanf7e89172011-01-06 11:36:32 +00002126 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002127 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002128 if (calldata->roc &&
2129 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2130 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2131 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002132 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002133 }
2134 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002135
Trond Myklebust516a6af2005-10-27 22:12:41 -04002136 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002137 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002138 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002139 &calldata->arg.seq_args,
2140 &calldata->res.seq_res,
2141 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002142 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002143 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002144out:
2145 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002148static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002149 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002150 .rpc_call_done = nfs4_close_done,
2151 .rpc_release = nfs4_free_closedata,
2152};
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154/*
2155 * It is possible for data to be read/written from a mem-mapped file
2156 * after the sys_close call (which hits the vfs layer as a flush).
2157 * This means that we can't safely call nfsv4 close on a file until
2158 * the inode is cleared. This in turn means that we are not good
2159 * NFSv4 citizens - we do not indicate to the server to update the file's
2160 * share state even when we are done with one of the three share
2161 * stateid's in the inode.
2162 *
2163 * NOTE: Caller must be holding the sp->so_owner semaphore!
2164 */
Al Viro643168c2011-06-22 18:20:23 -04002165int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002167 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002169 struct nfs4_state_owner *sp = state->owner;
2170 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002171 struct rpc_message msg = {
2172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2173 .rpc_cred = state->owner->so_cred,
2174 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002175 struct rpc_task_setup task_setup_data = {
2176 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002177 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002178 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002179 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002180 .flags = RPC_TASK_ASYNC,
2181 };
Trond Myklebust95121352005-10-18 14:20:12 -07002182 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002184 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002186 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002187 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002188 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002190 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002191 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002193 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002194 if (calldata->arg.seqid == NULL)
2195 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002196 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002197 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002198 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002199 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002200 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002201 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002202 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002203
Trond Myklebust1174dd12010-12-21 10:52:24 -05002204 msg.rpc_argp = &calldata->arg;
2205 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002206 task_setup_data.callback_data = calldata;
2207 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002208 if (IS_ERR(task))
2209 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002210 status = 0;
2211 if (wait)
2212 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002213 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002214 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002215out_free_calldata:
2216 kfree(calldata);
2217out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002218 if (roc)
2219 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002220 nfs4_put_open_state(state);
2221 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002222 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Trond Myklebust2b484292010-09-17 10:56:51 -04002225static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002226nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 struct nfs4_state *state;
2229
Trond Myklebust565277f2007-10-15 18:17:53 -04002230 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002231 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2232 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002233 if (IS_ERR(state))
2234 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002235 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002236 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Trond Myklebust1185a552009-12-03 15:54:02 -05002239static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002240{
2241 if (ctx->state == NULL)
2242 return;
2243 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002244 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002245 else
Al Viro643168c2011-06-22 18:20:23 -04002246 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2250{
Benny Halevy43652ad2009-04-01 09:21:54 -04002251 struct nfs4_server_caps_arg args = {
2252 .fhandle = fhandle,
2253 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 struct nfs4_server_caps_res res = {};
2255 struct rpc_message msg = {
2256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002257 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 .rpc_resp = &res,
2259 };
2260 int status;
2261
Bryan Schumaker7c513052011-03-24 17:12:24 +00002262 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (status == 0) {
2264 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002265 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2266 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2267 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2268 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2269 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2271 server->caps |= NFS_CAP_ACLS;
2272 if (res.has_links != 0)
2273 server->caps |= NFS_CAP_HARDLINKS;
2274 if (res.has_symlinks != 0)
2275 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002276 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2277 server->caps |= NFS_CAP_FILEID;
2278 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2279 server->caps |= NFS_CAP_MODE;
2280 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2281 server->caps |= NFS_CAP_NLINK;
2282 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2283 server->caps |= NFS_CAP_OWNER;
2284 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2285 server->caps |= NFS_CAP_OWNER_GROUP;
2286 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2287 server->caps |= NFS_CAP_ATIME;
2288 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2289 server->caps |= NFS_CAP_CTIME;
2290 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2291 server->caps |= NFS_CAP_MTIME;
2292
Trond Myklebusta65318b2009-03-11 14:10:28 -04002293 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2294 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2295 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002297 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return status;
2301}
2302
Trond Myklebust55a97592006-06-09 09:34:19 -04002303int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 struct nfs4_exception exception = { };
2306 int err;
2307 do {
2308 err = nfs4_handle_exception(server,
2309 _nfs4_server_capabilities(server, fhandle),
2310 &exception);
2311 } while (exception.retry);
2312 return err;
2313}
2314
2315static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2316 struct nfs_fsinfo *info)
2317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 struct nfs4_lookup_root_arg args = {
2319 .bitmask = nfs4_fattr_bitmap,
2320 };
2321 struct nfs4_lookup_res res = {
2322 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002323 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 .fh = fhandle,
2325 };
2326 struct rpc_message msg = {
2327 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2328 .rpc_argp = &args,
2329 .rpc_resp = &res,
2330 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002331
Trond Myklebust0e574af2005-10-27 22:12:38 -04002332 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002333 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
2336static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2337 struct nfs_fsinfo *info)
2338{
2339 struct nfs4_exception exception = { };
2340 int err;
2341 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002342 err = _nfs4_lookup_root(server, fhandle, info);
2343 switch (err) {
2344 case 0:
2345 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002346 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002347 default:
2348 err = nfs4_handle_exception(server, err, &exception);
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return err;
2353}
2354
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002355static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2356 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2357{
2358 struct rpc_auth *auth;
2359 int ret;
2360
2361 auth = rpcauth_create(flavor, server->client);
2362 if (!auth) {
2363 ret = -EIO;
2364 goto out;
2365 }
2366 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002367out:
2368 return ret;
2369}
2370
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002371static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002372 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002374 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002375 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002377 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2378 flav_array[len] = RPC_AUTH_NULL;
2379 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002380
2381 for (i = 0; i < len; i++) {
2382 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002383 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002384 continue;
2385 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002386 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002387 /*
2388 * -EACCESS could mean that the user doesn't have correct permissions
2389 * to access the mount. It could also mean that we tried to mount
2390 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2391 * existing mount programs don't handle -EACCES very well so it should
2392 * be mapped to -EPERM instead.
2393 */
2394 if (status == -EACCES)
2395 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002396 return status;
2397}
2398
2399/*
2400 * get the file handle for the "/" directory on the server
2401 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002402int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2403 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002404{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002405 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002406 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002407 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2408 /*
2409 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2410 * by nfs4_map_errors() as this function exits.
2411 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002412 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (status == 0)
2414 status = nfs4_server_capabilities(server, fhandle);
2415 if (status == 0)
2416 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002417 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Bryan Schumakerbae36242012-05-10 15:07:31 -04002420static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2421 struct nfs_fsinfo *info)
2422{
2423 int error;
2424 struct nfs_fattr *fattr = info->fattr;
2425
2426 error = nfs4_server_capabilities(server, mntfh);
2427 if (error < 0) {
2428 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2429 return error;
2430 }
2431
2432 error = nfs4_proc_getattr(server, mntfh, fattr);
2433 if (error < 0) {
2434 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2435 return error;
2436 }
2437
2438 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2439 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2440 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2441
2442 return error;
2443}
2444
Manoj Naik6b97fd32006-06-09 09:34:29 -04002445/*
2446 * Get locations and (maybe) other attributes of a referral.
2447 * Note that we'll actually follow the referral later when
2448 * we detect fsid mismatch in inode revalidation
2449 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002450static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2451 const struct qstr *name, struct nfs_fattr *fattr,
2452 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002453{
2454 int status = -ENOMEM;
2455 struct page *page = NULL;
2456 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002457
2458 page = alloc_page(GFP_KERNEL);
2459 if (page == NULL)
2460 goto out;
2461 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2462 if (locations == NULL)
2463 goto out;
2464
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002465 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002466 if (status != 0)
2467 goto out;
2468 /* Make sure server returned a different fsid for the referral */
2469 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002470 dprintk("%s: server did not return a different fsid for"
2471 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002472 status = -EIO;
2473 goto out;
2474 }
Andy Adamson533eb462011-06-13 18:25:56 -04002475 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2476 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002477
Andy Adamson533eb462011-06-13 18:25:56 -04002478 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002479 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002480 memset(fhandle, 0, sizeof(struct nfs_fh));
2481out:
2482 if (page)
2483 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002484 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002485 return status;
2486}
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2489{
2490 struct nfs4_getattr_arg args = {
2491 .fh = fhandle,
2492 .bitmask = server->attr_bitmask,
2493 };
2494 struct nfs4_getattr_res res = {
2495 .fattr = fattr,
2496 .server = server,
2497 };
2498 struct rpc_message msg = {
2499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2500 .rpc_argp = &args,
2501 .rpc_resp = &res,
2502 };
2503
Trond Myklebust0e574af2005-10-27 22:12:38 -04002504 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002505 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2509{
2510 struct nfs4_exception exception = { };
2511 int err;
2512 do {
2513 err = nfs4_handle_exception(server,
2514 _nfs4_proc_getattr(server, fhandle, fattr),
2515 &exception);
2516 } while (exception.retry);
2517 return err;
2518}
2519
2520/*
2521 * The file is not closed if it is opened due to the a request to change
2522 * the size of the file. The open call will not be needed once the
2523 * VFS layer lookup-intents are implemented.
2524 *
2525 * Close is called when the inode is destroyed.
2526 * If we haven't opened the file for O_WRONLY, we
2527 * need to in the size_change case to obtain a stateid.
2528 *
2529 * Got race?
2530 * Because OPEN is always done by name in nfsv4, it is
2531 * possible that we opened a different file by the same
2532 * name. We can recognize this race condition, but we
2533 * can't do anything about it besides returning an error.
2534 *
2535 * This will be fixed with VFS changes (lookup-intent).
2536 */
2537static int
2538nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2539 struct iattr *sattr)
2540{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002541 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002542 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002543 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 int status;
2545
Benny Halevy8a1636c2010-07-14 15:43:57 -04002546 if (pnfs_ld_layoutret_on_setattr(inode))
2547 pnfs_return_layout(inode);
2548
Trond Myklebust0e574af2005-10-27 22:12:38 -04002549 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Trond Myklebustd5308382005-11-04 15:33:38 -05002551 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002552 if (sattr->ia_valid & ATTR_FILE) {
2553 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002554
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002555 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002556 if (ctx) {
2557 cred = ctx->cred;
2558 state = ctx->state;
2559 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002560 }
2561
Trond Myklebust536e43d2012-01-17 22:04:26 -05002562 /* Deal with open(O_TRUNC) */
2563 if (sattr->ia_valid & ATTR_OPEN)
2564 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2565
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002566 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002567 if (status == 0)
2568 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return status;
2570}
2571
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002572static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2573 const struct qstr *name, struct nfs_fh *fhandle,
2574 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002575{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002576 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002577 int status;
2578 struct nfs4_lookup_arg args = {
2579 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002580 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002581 .name = name,
2582 };
2583 struct nfs4_lookup_res res = {
2584 .server = server,
2585 .fattr = fattr,
2586 .fh = fhandle,
2587 };
2588 struct rpc_message msg = {
2589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2590 .rpc_argp = &args,
2591 .rpc_resp = &res,
2592 };
2593
2594 nfs_fattr_init(fattr);
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002597 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 dprintk("NFS reply lookup: %d\n", status);
2599 return status;
2600}
2601
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002602static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002603{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002604 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002605 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002606 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2607 fattr->nlink = 2;
2608}
2609
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002610static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2611 struct qstr *name, struct nfs_fh *fhandle,
2612 struct nfs_fattr *fattr)
2613{
2614 struct nfs4_exception exception = { };
2615 struct rpc_clnt *client = *clnt;
2616 int err;
2617 do {
2618 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2619 switch (err) {
2620 case -NFS4ERR_BADNAME:
2621 err = -ENOENT;
2622 goto out;
2623 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002624 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002625 goto out;
2626 case -NFS4ERR_WRONGSEC:
2627 err = -EPERM;
2628 if (client != *clnt)
2629 goto out;
2630
2631 client = nfs4_create_sec_client(client, dir, name);
2632 if (IS_ERR(client))
2633 return PTR_ERR(client);
2634
2635 exception.retry = 1;
2636 break;
2637 default:
2638 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2639 }
2640 } while (exception.retry);
2641
2642out:
2643 if (err == 0)
2644 *clnt = client;
2645 else if (client != *clnt)
2646 rpc_shutdown_client(client);
2647
2648 return err;
2649}
2650
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002651static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002652 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002654 int status;
2655 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002656
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002657 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2658 if (client != NFS_CLIENT(dir)) {
2659 rpc_shutdown_client(client);
2660 nfs_fixup_secinfo_attributes(fattr);
2661 }
2662 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002665struct rpc_clnt *
2666nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2667 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2668{
2669 int status;
2670 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2671
2672 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2673 if (status < 0) {
2674 rpc_shutdown_client(client);
2675 return ERR_PTR(status);
2676 }
2677 return client;
2678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2681{
Trond Myklebust76b32992007-08-10 17:45:11 -04002682 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct nfs4_accessargs args = {
2684 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002685 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002687 struct nfs4_accessres res = {
2688 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002689 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct rpc_message msg = {
2691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2692 .rpc_argp = &args,
2693 .rpc_resp = &res,
2694 .rpc_cred = entry->cred,
2695 };
2696 int mode = entry->mask;
2697 int status;
2698
2699 /*
2700 * Determine which access bits we want to ask for...
2701 */
2702 if (mode & MAY_READ)
2703 args.access |= NFS4_ACCESS_READ;
2704 if (S_ISDIR(inode->i_mode)) {
2705 if (mode & MAY_WRITE)
2706 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2707 if (mode & MAY_EXEC)
2708 args.access |= NFS4_ACCESS_LOOKUP;
2709 } else {
2710 if (mode & MAY_WRITE)
2711 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2712 if (mode & MAY_EXEC)
2713 args.access |= NFS4_ACCESS_EXECUTE;
2714 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002715
2716 res.fattr = nfs_alloc_fattr();
2717 if (res.fattr == NULL)
2718 return -ENOMEM;
2719
Bryan Schumaker7c513052011-03-24 17:12:24 +00002720 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (!status) {
2722 entry->mask = 0;
2723 if (res.access & NFS4_ACCESS_READ)
2724 entry->mask |= MAY_READ;
2725 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2726 entry->mask |= MAY_WRITE;
2727 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2728 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002729 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002731 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return status;
2733}
2734
2735static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2736{
2737 struct nfs4_exception exception = { };
2738 int err;
2739 do {
2740 err = nfs4_handle_exception(NFS_SERVER(inode),
2741 _nfs4_proc_access(inode, entry),
2742 &exception);
2743 } while (exception.retry);
2744 return err;
2745}
2746
2747/*
2748 * TODO: For the time being, we don't try to get any attributes
2749 * along with any of the zero-copy operations READ, READDIR,
2750 * READLINK, WRITE.
2751 *
2752 * In the case of the first three, we want to put the GETATTR
2753 * after the read-type operation -- this is because it is hard
2754 * to predict the length of a GETATTR response in v4, and thus
2755 * align the READ data correctly. This means that the GETATTR
2756 * may end up partially falling into the page cache, and we should
2757 * shift it into the 'tail' of the xdr_buf before processing.
2758 * To do this efficiently, we need to know the total length
2759 * of data received, which doesn't seem to be available outside
2760 * of the RPC layer.
2761 *
2762 * In the case of WRITE, we also want to put the GETATTR after
2763 * the operation -- in this case because we want to make sure
2764 * we get the post-operation mtime and size. This means that
2765 * we can't use xdr_encode_pages() as written: we need a variant
2766 * of it which would leave room in the 'tail' iovec.
2767 *
2768 * Both of these changes to the XDR layer would in fact be quite
2769 * minor, but I decided to leave them for a subsequent patch.
2770 */
2771static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2772 unsigned int pgbase, unsigned int pglen)
2773{
2774 struct nfs4_readlink args = {
2775 .fh = NFS_FH(inode),
2776 .pgbase = pgbase,
2777 .pglen = pglen,
2778 .pages = &page,
2779 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002780 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 struct rpc_message msg = {
2782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2783 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002784 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 };
2786
Bryan Schumaker7c513052011-03-24 17:12:24 +00002787 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 -07002788}
2789
2790static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2791 unsigned int pgbase, unsigned int pglen)
2792{
2793 struct nfs4_exception exception = { };
2794 int err;
2795 do {
2796 err = nfs4_handle_exception(NFS_SERVER(inode),
2797 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2798 &exception);
2799 } while (exception.retry);
2800 return err;
2801}
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803/*
2804 * Got race?
2805 * We will need to arrange for the VFS layer to provide an atomic open.
2806 * Until then, this create/open method is prone to inefficiency and race
2807 * conditions due to the lookup, create, and open VFS calls from sys_open()
2808 * placed on the wire.
2809 *
2810 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2811 * The file will be opened again in the subsequent VFS open call
2812 * (nfs4_proc_file_open).
2813 *
2814 * The open for read will just hang around to be used by any process that
2815 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2816 */
2817
2818static int
2819nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002820 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
Al Viro82a2c1b2011-06-22 18:30:55 -04002822 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002824 struct rpc_cred *cred = NULL;
2825 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 int status = 0;
2827
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002828 if (ctx != NULL) {
2829 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002830 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002831 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002833 sattr->ia_mode &= ~current_umask();
Andy Adamson82be4172012-05-23 05:02:35 -04002834 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred, NULL);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002835 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (IS_ERR(state)) {
2837 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002840 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002841 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002842 if (ctx != NULL)
2843 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002844 else
Al Viro643168c2011-06-22 18:20:23 -04002845 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846out:
2847 return status;
2848}
2849
2850static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2851{
Trond Myklebust16e42952005-10-27 22:12:44 -04002852 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002853 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002855 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002857 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002858 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002859 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2862 .rpc_argp = &args,
2863 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002865 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002866
Bryan Schumaker7c513052011-03-24 17:12:24 +00002867 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002868 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002869 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return status;
2871}
2872
2873static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2874{
2875 struct nfs4_exception exception = { };
2876 int err;
2877 do {
2878 err = nfs4_handle_exception(NFS_SERVER(dir),
2879 _nfs4_proc_remove(dir, name),
2880 &exception);
2881 } while (exception.retry);
2882 return err;
2883}
2884
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002885static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002887 struct nfs_server *server = NFS_SERVER(dir);
2888 struct nfs_removeargs *args = msg->rpc_argp;
2889 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002891 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002893 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002896static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2897{
2898 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2899 &data->args.seq_args,
2900 &data->res.seq_res,
2901 task))
2902 return;
2903 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002906static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002908 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2909
Trond Myklebust14516c32010-07-31 14:29:06 -04002910 if (!nfs4_sequence_done(task, &res->seq_res))
2911 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002912 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002913 return 0;
2914 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002915 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
Jeff Laytond3d41522010-09-17 17:31:57 -04002918static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2919{
2920 struct nfs_server *server = NFS_SERVER(dir);
2921 struct nfs_renameargs *arg = msg->rpc_argp;
2922 struct nfs_renameres *res = msg->rpc_resp;
2923
2924 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002925 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002926 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002927}
2928
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002929static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2930{
2931 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2932 &data->args.seq_args,
2933 &data->res.seq_res,
2934 task))
2935 return;
2936 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002937}
2938
2939static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2940 struct inode *new_dir)
2941{
2942 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2943
2944 if (!nfs4_sequence_done(task, &res->seq_res))
2945 return 0;
2946 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2947 return 0;
2948
2949 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002950 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002951 return 1;
2952}
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2955 struct inode *new_dir, struct qstr *new_name)
2956{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002957 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002958 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 .old_dir = NFS_FH(old_dir),
2960 .new_dir = NFS_FH(new_dir),
2961 .old_name = old_name,
2962 .new_name = new_name,
2963 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002964 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002965 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002966 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 struct rpc_message msg = {
2968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2969 .rpc_argp = &arg,
2970 .rpc_resp = &res,
2971 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002972 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Bryan Schumaker7c513052011-03-24 17:12:24 +00002974 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (!status) {
2976 update_changeattr(old_dir, &res.old_cinfo);
2977 update_changeattr(new_dir, &res.new_cinfo);
2978 }
2979 return status;
2980}
2981
2982static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2983 struct inode *new_dir, struct qstr *new_name)
2984{
2985 struct nfs4_exception exception = { };
2986 int err;
2987 do {
2988 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2989 _nfs4_proc_rename(old_dir, old_name,
2990 new_dir, new_name),
2991 &exception);
2992 } while (exception.retry);
2993 return err;
2994}
2995
2996static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2997{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002998 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 struct nfs4_link_arg arg = {
3000 .fh = NFS_FH(inode),
3001 .dir_fh = NFS_FH(dir),
3002 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003003 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003005 struct nfs4_link_res res = {
3006 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003007 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 struct rpc_message msg = {
3009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3010 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003011 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003013 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Trond Myklebust136f2622010-04-16 16:22:49 -04003015 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003016 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003017 goto out;
3018
Bryan Schumaker7c513052011-03-24 17:12:24 +00003019 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003020 if (!status) {
3021 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003022 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003023 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003024out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003025 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return status;
3027}
3028
3029static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3030{
3031 struct nfs4_exception exception = { };
3032 int err;
3033 do {
3034 err = nfs4_handle_exception(NFS_SERVER(inode),
3035 _nfs4_proc_link(inode, dir, name),
3036 &exception);
3037 } while (exception.retry);
3038 return err;
3039}
3040
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003041struct nfs4_createdata {
3042 struct rpc_message msg;
3043 struct nfs4_create_arg arg;
3044 struct nfs4_create_res res;
3045 struct nfs_fh fh;
3046 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047};
3048
3049static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3050 struct qstr *name, struct iattr *sattr, u32 ftype)
3051{
3052 struct nfs4_createdata *data;
3053
3054 data = kzalloc(sizeof(*data), GFP_KERNEL);
3055 if (data != NULL) {
3056 struct nfs_server *server = NFS_SERVER(dir);
3057
3058 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3059 data->msg.rpc_argp = &data->arg;
3060 data->msg.rpc_resp = &data->res;
3061 data->arg.dir_fh = NFS_FH(dir);
3062 data->arg.server = server;
3063 data->arg.name = name;
3064 data->arg.attrs = sattr;
3065 data->arg.ftype = ftype;
3066 data->arg.bitmask = server->attr_bitmask;
3067 data->res.server = server;
3068 data->res.fh = &data->fh;
3069 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003070 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071 }
3072 return data;
3073}
3074
3075static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3076{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003077 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003078 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003079 if (status == 0) {
3080 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003081 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3082 }
3083 return status;
3084}
3085
3086static void nfs4_free_createdata(struct nfs4_createdata *data)
3087{
3088 kfree(data);
3089}
3090
Chuck Lever4f390c12006-08-22 20:06:22 -04003091static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003092 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003094 struct nfs4_createdata *data;
3095 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Chuck Lever94a6d752006-08-22 20:06:23 -04003097 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003098 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003099
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003100 status = -ENOMEM;
3101 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3102 if (data == NULL)
3103 goto out;
3104
3105 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3106 data->arg.u.symlink.pages = &page;
3107 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003109 status = nfs4_do_create(dir, dentry, data);
3110
3111 nfs4_free_createdata(data);
3112out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return status;
3114}
3115
Chuck Lever4f390c12006-08-22 20:06:22 -04003116static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003117 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
3119 struct nfs4_exception exception = { };
3120 int err;
3121 do {
3122 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003123 _nfs4_proc_symlink(dir, dentry, page,
3124 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 &exception);
3126 } while (exception.retry);
3127 return err;
3128}
3129
3130static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3131 struct iattr *sattr)
3132{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003133 struct nfs4_createdata *data;
3134 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003136 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3137 if (data == NULL)
3138 goto out;
3139
3140 status = nfs4_do_create(dir, dentry, data);
3141
3142 nfs4_free_createdata(data);
3143out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return status;
3145}
3146
3147static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3148 struct iattr *sattr)
3149{
3150 struct nfs4_exception exception = { };
3151 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003152
3153 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 do {
3155 err = nfs4_handle_exception(NFS_SERVER(dir),
3156 _nfs4_proc_mkdir(dir, dentry, sattr),
3157 &exception);
3158 } while (exception.retry);
3159 return err;
3160}
3161
3162static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003163 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
3165 struct inode *dir = dentry->d_inode;
3166 struct nfs4_readdir_arg args = {
3167 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003168 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 .pgbase = 0,
3170 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003171 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003172 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 };
3174 struct nfs4_readdir_res res;
3175 struct rpc_message msg = {
3176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3177 .rpc_argp = &args,
3178 .rpc_resp = &res,
3179 .rpc_cred = cred,
3180 };
3181 int status;
3182
Harvey Harrison3110ff82008-05-02 13:42:44 -07003183 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003184 dentry->d_parent->d_name.name,
3185 dentry->d_name.name,
3186 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3188 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003189 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 -05003190 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003192 status += args.pgbase;
3193 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003194
3195 nfs_invalidate_atime(dir);
3196
Harvey Harrison3110ff82008-05-02 13:42:44 -07003197 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 return status;
3199}
3200
3201static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003202 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
3204 struct nfs4_exception exception = { };
3205 int err;
3206 do {
3207 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3208 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003209 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 &exception);
3211 } while (exception.retry);
3212 return err;
3213}
3214
3215static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3216 struct iattr *sattr, dev_t rdev)
3217{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003218 struct nfs4_createdata *data;
3219 int mode = sattr->ia_mode;
3220 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3223 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003224
3225 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3226 if (data == NULL)
3227 goto out;
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003230 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003232 data->arg.ftype = NF4BLK;
3233 data->arg.u.device.specdata1 = MAJOR(rdev);
3234 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 }
3236 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003237 data->arg.ftype = NF4CHR;
3238 data->arg.u.device.specdata1 = MAJOR(rdev);
3239 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003242 status = nfs4_do_create(dir, dentry, data);
3243
3244 nfs4_free_createdata(data);
3245out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return status;
3247}
3248
3249static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3250 struct iattr *sattr, dev_t rdev)
3251{
3252 struct nfs4_exception exception = { };
3253 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003254
3255 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 do {
3257 err = nfs4_handle_exception(NFS_SERVER(dir),
3258 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3259 &exception);
3260 } while (exception.retry);
3261 return err;
3262}
3263
3264static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3265 struct nfs_fsstat *fsstat)
3266{
3267 struct nfs4_statfs_arg args = {
3268 .fh = fhandle,
3269 .bitmask = server->attr_bitmask,
3270 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003271 struct nfs4_statfs_res res = {
3272 .fsstat = fsstat,
3273 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 struct rpc_message msg = {
3275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3276 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003277 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 };
3279
Trond Myklebust0e574af2005-10-27 22:12:38 -04003280 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003281 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
3284static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3285{
3286 struct nfs4_exception exception = { };
3287 int err;
3288 do {
3289 err = nfs4_handle_exception(server,
3290 _nfs4_proc_statfs(server, fhandle, fsstat),
3291 &exception);
3292 } while (exception.retry);
3293 return err;
3294}
3295
3296static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3297 struct nfs_fsinfo *fsinfo)
3298{
3299 struct nfs4_fsinfo_arg args = {
3300 .fh = fhandle,
3301 .bitmask = server->attr_bitmask,
3302 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003303 struct nfs4_fsinfo_res res = {
3304 .fsinfo = fsinfo,
3305 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 struct rpc_message msg = {
3307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3308 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003309 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 };
3311
Bryan Schumaker7c513052011-03-24 17:12:24 +00003312 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
3315static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3316{
3317 struct nfs4_exception exception = { };
3318 int err;
3319
3320 do {
3321 err = nfs4_handle_exception(server,
3322 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3323 &exception);
3324 } while (exception.retry);
3325 return err;
3326}
3327
3328static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3329{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003330 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3332}
3333
3334static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3335 struct nfs_pathconf *pathconf)
3336{
3337 struct nfs4_pathconf_arg args = {
3338 .fh = fhandle,
3339 .bitmask = server->attr_bitmask,
3340 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003341 struct nfs4_pathconf_res res = {
3342 .pathconf = pathconf,
3343 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct rpc_message msg = {
3345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3346 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003347 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 };
3349
3350 /* None of the pathconf attributes are mandatory to implement */
3351 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3352 memset(pathconf, 0, sizeof(*pathconf));
3353 return 0;
3354 }
3355
Trond Myklebust0e574af2005-10-27 22:12:38 -04003356 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003357 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
3360static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3361 struct nfs_pathconf *pathconf)
3362{
3363 struct nfs4_exception exception = { };
3364 int err;
3365
3366 do {
3367 err = nfs4_handle_exception(server,
3368 _nfs4_proc_pathconf(server, fhandle, pathconf),
3369 &exception);
3370 } while (exception.retry);
3371 return err;
3372}
3373
Benny Halevyd20581a2011-05-22 19:52:03 +03003374void __nfs4_read_done_cb(struct nfs_read_data *data)
3375{
Fred Isamancd841602012-04-20 14:47:44 -04003376 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003377}
3378
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003379static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Fred Isamancd841602012-04-20 14:47:44 -04003381 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003383 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003384 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003385 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003387
Benny Halevyd20581a2011-05-22 19:52:03 +03003388 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003390 renew_lease(server, data->timestamp);
3391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003394static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3395{
3396
3397 dprintk("--> %s\n", __func__);
3398
3399 if (!nfs4_sequence_done(task, &data->res.seq_res))
3400 return -EAGAIN;
3401
Benny Halevyd20581a2011-05-22 19:52:03 +03003402 return data->read_done_cb ? data->read_done_cb(task, data) :
3403 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003404}
3405
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003406static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003409 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003410 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003411 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003414static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3415{
Fred Isamancd841602012-04-20 14:47:44 -04003416 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003417 &data->args.seq_args,
3418 &data->res.seq_res,
3419 task))
3420 return;
3421 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
Fred Isamanb029bc92011-03-03 15:13:42 +00003424static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
Fred Isamancd841602012-04-20 14:47:44 -04003426 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003428 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003429 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003430 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003432 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003434 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003435 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437}
3438
Fred Isamanb029bc92011-03-03 15:13:42 +00003439static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3440{
3441 if (!nfs4_sequence_done(task, &data->res.seq_res))
3442 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003443 return data->write_done_cb ? data->write_done_cb(task, data) :
3444 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003445}
3446
Trond Myklebust5a37f852012-04-28 14:55:16 -04003447static
3448bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003449{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003450 const struct nfs_pgio_header *hdr = data->header;
3451
3452 /* Don't request attributes for pNFS or O_DIRECT writes */
3453 if (data->ds_clp != NULL || hdr->dreq != NULL)
3454 return false;
3455 /* Otherwise, request attributes if and only if we don't hold
3456 * a delegation
3457 */
3458 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003459}
Fred Isamana69aef12011-03-03 15:13:47 +00003460
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003461static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Fred Isamancd841602012-04-20 14:47:44 -04003463 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003464
Trond Myklebust5a37f852012-04-28 14:55:16 -04003465 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003466 data->args.bitmask = NULL;
3467 data->res.fattr = NULL;
3468 } else
3469 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003470
Fred Isamanb029bc92011-03-03 15:13:42 +00003471 if (!data->write_done_cb)
3472 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003473 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 data->timestamp = jiffies;
3475
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003476 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003477 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003480static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3481{
Fred Isamancd841602012-04-20 14:47:44 -04003482 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003483 &data->args.seq_args,
3484 &data->res.seq_res,
3485 task))
3486 return;
3487 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Fred Isaman0b7c0152012-04-20 14:47:39 -04003490static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3493 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003494 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 return;
3497 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003498}
3499
Fred Isaman0b7c0152012-04-20 14:47:39 -04003500static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
3502 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003503
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003504 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003505 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003506 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
Fred Isaman0b7c0152012-04-20 14:47:39 -04003511static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003512{
3513 if (!nfs4_sequence_done(task, &data->res.seq_res))
3514 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003515 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003516}
3517
Fred Isaman0b7c0152012-04-20 14:47:39 -04003518static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003520 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003521
Fred Isaman0b7c0152012-04-20 14:47:39 -04003522 if (data->commit_done_cb == NULL)
3523 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003524 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003525 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003526 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003529struct nfs4_renewdata {
3530 struct nfs_client *client;
3531 unsigned long timestamp;
3532};
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534/*
3535 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3536 * standalone procedure for queueing an asynchronous RENEW.
3537 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003538static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003539{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003540 struct nfs4_renewdata *data = calldata;
3541 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003542
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003543 if (atomic_read(&clp->cl_count) > 1)
3544 nfs4_schedule_state_renewal(clp);
3545 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003546 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003547}
3548
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003549static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003551 struct nfs4_renewdata *data = calldata;
3552 struct nfs_client *clp = data->client;
3553 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
3555 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003556 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003557 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3558 return;
3559 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003560 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003561 return;
3562 }
3563 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003565 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003568static const struct rpc_call_ops nfs4_renew_ops = {
3569 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003570 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003571};
3572
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003573static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
3575 struct rpc_message msg = {
3576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3577 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003578 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003580 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003582 if (renew_flags == 0)
3583 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003584 if (!atomic_inc_not_zero(&clp->cl_count))
3585 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003586 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003587 if (data == NULL)
3588 return -ENOMEM;
3589 data->client = clp;
3590 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003592 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003595static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
3597 struct rpc_message msg = {
3598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3599 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003600 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 };
3602 unsigned long now = jiffies;
3603 int status;
3604
3605 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3606 if (status < 0)
3607 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003608 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 return 0;
3610}
3611
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003612static inline int nfs4_server_supports_acls(struct nfs_server *server)
3613{
3614 return (server->caps & NFS_CAP_ACLS)
3615 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3616 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3617}
3618
3619/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3620 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3621 * the stack.
3622 */
3623#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3624
Neil Hormane9e3d722011-03-04 19:26:03 -05003625static int buf_to_pages_noslab(const void *buf, size_t buflen,
3626 struct page **pages, unsigned int *pgbase)
3627{
3628 struct page *newpage, **spages;
3629 int rc = 0;
3630 size_t len;
3631 spages = pages;
3632
3633 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003634 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003635 newpage = alloc_page(GFP_KERNEL);
3636
3637 if (newpage == NULL)
3638 goto unwind;
3639 memcpy(page_address(newpage), buf, len);
3640 buf += len;
3641 buflen -= len;
3642 *pages++ = newpage;
3643 rc++;
3644 } while (buflen != 0);
3645
3646 return rc;
3647
3648unwind:
3649 for(; rc > 0; rc--)
3650 __free_page(spages[rc-1]);
3651 return -ENOMEM;
3652}
3653
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003654struct nfs4_cached_acl {
3655 int cached;
3656 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003657 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003658};
3659
3660static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003661{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003662 struct nfs_inode *nfsi = NFS_I(inode);
3663
3664 spin_lock(&inode->i_lock);
3665 kfree(nfsi->nfs4_acl);
3666 nfsi->nfs4_acl = acl;
3667 spin_unlock(&inode->i_lock);
3668}
3669
3670static void nfs4_zap_acl_attr(struct inode *inode)
3671{
3672 nfs4_set_cached_acl(inode, NULL);
3673}
3674
3675static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3676{
3677 struct nfs_inode *nfsi = NFS_I(inode);
3678 struct nfs4_cached_acl *acl;
3679 int ret = -ENOENT;
3680
3681 spin_lock(&inode->i_lock);
3682 acl = nfsi->nfs4_acl;
3683 if (acl == NULL)
3684 goto out;
3685 if (buf == NULL) /* user is just asking for length */
3686 goto out_len;
3687 if (acl->cached == 0)
3688 goto out;
3689 ret = -ERANGE; /* see getxattr(2) man page */
3690 if (acl->len > buflen)
3691 goto out;
3692 memcpy(buf, acl->data, acl->len);
3693out_len:
3694 ret = acl->len;
3695out:
3696 spin_unlock(&inode->i_lock);
3697 return ret;
3698}
3699
Sachin Prabhu5794d212012-04-17 14:36:40 +01003700static 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 +00003701{
3702 struct nfs4_cached_acl *acl;
3703
Sachin Prabhu5794d212012-04-17 14:36:40 +01003704 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003705 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3706 if (acl == NULL)
3707 goto out;
3708 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003709 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003710 } else {
3711 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3712 if (acl == NULL)
3713 goto out;
3714 acl->cached = 0;
3715 }
3716 acl->len = acl_len;
3717out:
3718 nfs4_set_cached_acl(inode, acl);
3719}
3720
Andy Adamsonbf118a32011-12-07 11:55:27 -05003721/*
3722 * The getxattr API returns the required buffer length when called with a
3723 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3724 * the required buf. On a NULL buf, we send a page of data to the server
3725 * guessing that the ACL request can be serviced by a page. If so, we cache
3726 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3727 * the cache. If not so, we throw away the page, and cache the required
3728 * length. The next getxattr call will then produce another round trip to
3729 * the server, this time with the input buf of the required size.
3730 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003731static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003732{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003733 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003734 struct nfs_getaclargs args = {
3735 .fh = NFS_FH(inode),
3736 .acl_pages = pages,
3737 .acl_len = buflen,
3738 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003739 struct nfs_getaclres res = {
3740 .acl_len = buflen,
3741 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003742 struct rpc_message msg = {
3743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3744 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003745 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003746 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003747 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003748
Andy Adamsonbf118a32011-12-07 11:55:27 -05003749 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3750 /* As long as we're doing a round trip to the server anyway,
3751 * let's be prepared for a page of acl data. */
3752 if (npages == 0)
3753 npages = 1;
3754
Sachin Prabhu5a006892012-04-17 14:35:39 +01003755 /* Add an extra page to handle the bitmap returned */
3756 npages++;
3757
Andy Adamsonbf118a32011-12-07 11:55:27 -05003758 for (i = 0; i < npages; i++) {
3759 pages[i] = alloc_page(GFP_KERNEL);
3760 if (!pages[i])
3761 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003762 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003763
3764 /* for decoding across pages */
3765 res.acl_scratch = alloc_page(GFP_KERNEL);
3766 if (!res.acl_scratch)
3767 goto out_free;
3768
Andy Adamsonbf118a32011-12-07 11:55:27 -05003769 args.acl_len = npages * PAGE_SIZE;
3770 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003771
Andy Adamsonbf118a32011-12-07 11:55:27 -05003772 /* Let decode_getfacl know not to fail if the ACL data is larger than
3773 * the page we send as a guess */
3774 if (buf == NULL)
3775 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003776
Peng Taode040be2012-01-10 22:42:47 +08003777 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003778 __func__, buf, buflen, npages, args.acl_len);
3779 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3780 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781 if (ret)
3782 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003783
3784 acl_len = res.acl_len - res.acl_data_offset;
3785 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003786 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003787 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003788 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003789 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003790 if (buf) {
3791 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003792 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003793 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003794 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003795 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003796 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003797 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003798out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003799 for (i = 0; i < npages; i++)
3800 if (pages[i])
3801 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003802 if (res.acl_scratch)
3803 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003804 return ret;
3805}
3806
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003807static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3808{
3809 struct nfs4_exception exception = { };
3810 ssize_t ret;
3811 do {
3812 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3813 if (ret >= 0)
3814 break;
3815 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3816 } while (exception.retry);
3817 return ret;
3818}
3819
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003820static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3821{
3822 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003823 int ret;
3824
3825 if (!nfs4_server_supports_acls(server))
3826 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003827 ret = nfs_revalidate_inode(server, inode);
3828 if (ret < 0)
3829 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003830 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3831 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003832 ret = nfs4_read_cached_acl(inode, buf, buflen);
3833 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003834 /* -ENOENT is returned if there is no ACL or if there is an ACL
3835 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003836 return ret;
3837 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003838}
3839
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003840static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003841{
3842 struct nfs_server *server = NFS_SERVER(inode);
3843 struct page *pages[NFS4ACL_MAXPAGES];
3844 struct nfs_setaclargs arg = {
3845 .fh = NFS_FH(inode),
3846 .acl_pages = pages,
3847 .acl_len = buflen,
3848 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003849 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003850 struct rpc_message msg = {
3851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3852 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003853 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003854 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003855 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003856
3857 if (!nfs4_server_supports_acls(server))
3858 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003859 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3860 if (i < 0)
3861 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003862 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003863 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003864
3865 /*
3866 * Free each page after tx, so the only ref left is
3867 * held by the network stack
3868 */
3869 for (; i > 0; i--)
3870 put_page(pages[i-1]);
3871
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003872 /*
3873 * Acl update can result in inode attribute update.
3874 * so mark the attribute cache invalid.
3875 */
3876 spin_lock(&inode->i_lock);
3877 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3878 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003879 nfs_access_zap_cache(inode);
3880 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003881 return ret;
3882}
3883
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003884static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3885{
3886 struct nfs4_exception exception = { };
3887 int err;
3888 do {
3889 err = nfs4_handle_exception(NFS_SERVER(inode),
3890 __nfs4_proc_set_acl(inode, buf, buflen),
3891 &exception);
3892 } while (exception.retry);
3893 return err;
3894}
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003897nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003899 struct nfs_client *clp = server->nfs_client;
3900
3901 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return 0;
3903 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003904 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003905 case -NFS4ERR_ADMIN_REVOKED:
3906 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003907 if (state == NULL)
3908 break;
3909 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003910 case -NFS4ERR_OPENMODE:
3911 if (state == NULL)
3912 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003913 nfs4_schedule_stateid_recovery(server, state);
3914 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003915 case -NFS4ERR_EXPIRED:
3916 if (state != NULL)
3917 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003919 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003920 nfs4_schedule_lease_recovery(clp);
3921 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003922#if defined(CONFIG_NFS_V4_1)
3923 case -NFS4ERR_BADSESSION:
3924 case -NFS4ERR_BADSLOT:
3925 case -NFS4ERR_BAD_HIGH_SLOT:
3926 case -NFS4ERR_DEADSESSION:
3927 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3928 case -NFS4ERR_SEQ_FALSE_RETRY:
3929 case -NFS4ERR_SEQ_MISORDERED:
3930 dprintk("%s ERROR %d, Reset session\n", __func__,
3931 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003932 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003933 task->tk_status = 0;
3934 return -EAGAIN;
3935#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003937 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003938 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003939 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3941 task->tk_status = 0;
3942 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003943 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 case -NFS4ERR_OLD_STATEID:
3945 task->tk_status = 0;
3946 return -EAGAIN;
3947 }
3948 task->tk_status = nfs4_map_errors(task->tk_status);
3949 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003950wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003951 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003952 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3953 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3954 task->tk_status = 0;
3955 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
Chuck Leverf0920752012-05-21 22:45:41 -04003958static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3959 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003960{
3961 __be32 verf[2];
3962
Chuck Lever2c820d92012-05-21 22:45:33 -04003963 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3964 /* An impossible timestamp guarantees this value
3965 * will never match a generated boot time. */
3966 verf[0] = 0;
3967 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3968 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003969 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3970 verf[0] = (__be32)nn->boot_time.tv_sec;
3971 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003972 }
Chuck Levercd937102012-03-02 17:14:31 -05003973 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3974}
3975
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003976int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3977 unsigned short port, struct rpc_cred *cred,
3978 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
3980 nfs4_verifier sc_verifier;
3981 struct nfs4_setclientid setclientid = {
3982 .sc_verifier = &sc_verifier,
3983 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003984 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 };
3986 struct rpc_message msg = {
3987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3988 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003989 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003990 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 int loop = 0;
3993 int status;
3994
Chuck Leverf0920752012-05-21 22:45:41 -04003995 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003998 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05004000 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004001 clp->cl_ipaddr,
4002 rpc_peeraddr2str(clp->cl_rpcclient,
4003 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05004004 rpc_peeraddr2str(clp->cl_rpcclient,
4005 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04004006 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 clp->cl_id_uniquifier);
4008 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004009 sizeof(setclientid.sc_netid),
4010 rpc_peeraddr2str(clp->cl_rpcclient,
4011 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004013 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05004015 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004017 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 if (status != -NFS4ERR_CLID_INUSE)
4019 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004020 if (loop != 0) {
4021 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004023 }
4024 ++loop;
4025 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 }
4027 return status;
4028}
4029
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004030int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004031 struct nfs4_setclientid_res *arg,
4032 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
4034 struct nfs_fsinfo fsinfo;
4035 struct rpc_message msg = {
4036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004037 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004039 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 };
4041 unsigned long now;
4042 int status;
4043
4044 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004045 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if (status == 0) {
4047 spin_lock(&clp->cl_lock);
4048 clp->cl_lease_time = fsinfo.lease_time * HZ;
4049 clp->cl_last_renewal = now;
4050 spin_unlock(&clp->cl_lock);
4051 }
4052 return status;
4053}
4054
Trond Myklebustfe650402006-01-03 09:55:18 +01004055struct nfs4_delegreturndata {
4056 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004057 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004058 struct nfs_fh fh;
4059 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004060 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004061 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004062 int rpc_status;
4063};
4064
Trond Myklebustfe650402006-01-03 09:55:18 +01004065static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4066{
4067 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004068
Trond Myklebust14516c32010-07-31 14:29:06 -04004069 if (!nfs4_sequence_done(task, &data->res.seq_res))
4070 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004071
Ricardo Labiaga79708862009-12-07 09:23:21 -05004072 switch (task->tk_status) {
4073 case -NFS4ERR_STALE_STATEID:
4074 case -NFS4ERR_EXPIRED:
4075 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004076 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004077 break;
4078 default:
4079 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4080 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004081 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004082 return;
4083 }
4084 }
4085 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004086}
4087
4088static void nfs4_delegreturn_release(void *calldata)
4089{
Trond Myklebustfe650402006-01-03 09:55:18 +01004090 kfree(calldata);
4091}
4092
Andy Adamson938e1012009-04-01 09:22:28 -04004093#if defined(CONFIG_NFS_V4_1)
4094static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4095{
4096 struct nfs4_delegreturndata *d_data;
4097
4098 d_data = (struct nfs4_delegreturndata *)data;
4099
Trond Myklebust035168a2010-06-16 09:52:26 -04004100 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004101 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004102 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004103 return;
4104 rpc_call_start(task);
4105}
4106#endif /* CONFIG_NFS_V4_1 */
4107
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004108static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004109#if defined(CONFIG_NFS_V4_1)
4110 .rpc_call_prepare = nfs4_delegreturn_prepare,
4111#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004112 .rpc_call_done = nfs4_delegreturn_done,
4113 .rpc_release = nfs4_delegreturn_release,
4114};
4115
Trond Myklebuste6f81072008-01-24 18:14:34 -05004116static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004117{
4118 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004119 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004120 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004121 struct rpc_message msg = {
4122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4123 .rpc_cred = cred,
4124 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004125 struct rpc_task_setup task_setup_data = {
4126 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004127 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004128 .callback_ops = &nfs4_delegreturn_ops,
4129 .flags = RPC_TASK_ASYNC,
4130 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004131 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004132
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004133 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004134 if (data == NULL)
4135 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004136 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004137 data->args.fhandle = &data->fh;
4138 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004139 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004140 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004141 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004142 data->res.fattr = &data->fattr;
4143 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004144 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004145 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004146 data->rpc_status = 0;
4147
Trond Myklebustc970aa82007-07-14 15:39:59 -04004148 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004149 msg.rpc_argp = &data->args;
4150 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004151 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004152 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004153 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004154 if (!issync)
4155 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004156 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004157 if (status != 0)
4158 goto out;
4159 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004160 if (status == 0)
4161 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4162 else
4163 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004164out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004165 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004166 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
Trond Myklebuste6f81072008-01-24 18:14:34 -05004169int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170{
4171 struct nfs_server *server = NFS_SERVER(inode);
4172 struct nfs4_exception exception = { };
4173 int err;
4174 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004175 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 switch (err) {
4177 case -NFS4ERR_STALE_STATEID:
4178 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 case 0:
4180 return 0;
4181 }
4182 err = nfs4_handle_exception(server, err, &exception);
4183 } while (exception.retry);
4184 return err;
4185}
4186
4187#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4188#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4189
4190/*
4191 * sleep, with exponential backoff, and retry the LOCK operation.
4192 */
4193static unsigned long
4194nfs4_set_lock_task_retry(unsigned long timeout)
4195{
Jeff Laytond3103102011-12-01 22:44:39 +01004196 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 timeout <<= 1;
4198 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4199 return NFS4_LOCK_MAXTIMEOUT;
4200 return timeout;
4201}
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4204{
4205 struct inode *inode = state->inode;
4206 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004207 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004208 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004210 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004212 struct nfs_lockt_res res = {
4213 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 };
4215 struct rpc_message msg = {
4216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4217 .rpc_argp = &arg,
4218 .rpc_resp = &res,
4219 .rpc_cred = state->owner->so_cred,
4220 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 struct nfs4_lock_state *lsp;
4222 int status;
4223
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004224 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004225 status = nfs4_set_lock_state(state, request);
4226 if (status != 0)
4227 goto out;
4228 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004229 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004230 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004231 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004232 switch (status) {
4233 case 0:
4234 request->fl_type = F_UNLCK;
4235 break;
4236 case -NFS4ERR_DENIED:
4237 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004239 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004240out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return status;
4242}
4243
4244static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4245{
4246 struct nfs4_exception exception = { };
4247 int err;
4248
4249 do {
4250 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4251 _nfs4_proc_getlk(state, cmd, request),
4252 &exception);
4253 } while (exception.retry);
4254 return err;
4255}
4256
4257static int do_vfs_lock(struct file *file, struct file_lock *fl)
4258{
4259 int res = 0;
4260 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4261 case FL_POSIX:
4262 res = posix_lock_file_wait(file, fl);
4263 break;
4264 case FL_FLOCK:
4265 res = flock_lock_file_wait(file, fl);
4266 break;
4267 default:
4268 BUG();
4269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return res;
4271}
4272
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004273struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004274 struct nfs_locku_args arg;
4275 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004276 struct nfs4_lock_state *lsp;
4277 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004278 struct file_lock fl;
4279 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004280 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004281};
4282
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004283static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4284 struct nfs_open_context *ctx,
4285 struct nfs4_lock_state *lsp,
4286 struct nfs_seqid *seqid)
4287{
4288 struct nfs4_unlockdata *p;
4289 struct inode *inode = lsp->ls_state->inode;
4290
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004291 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004292 if (p == NULL)
4293 return NULL;
4294 p->arg.fh = NFS_FH(inode);
4295 p->arg.fl = &p->fl;
4296 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004297 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004298 p->arg.stateid = &lsp->ls_stateid;
4299 p->lsp = lsp;
4300 atomic_inc(&lsp->ls_count);
4301 /* Ensure we don't close file until we're done freeing locks! */
4302 p->ctx = get_nfs_open_context(ctx);
4303 memcpy(&p->fl, fl, sizeof(p->fl));
4304 p->server = NFS_SERVER(inode);
4305 return p;
4306}
4307
Trond Myklebust06f814a2006-01-03 09:55:07 +01004308static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004309{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004310 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004311 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004312 nfs4_put_lock_state(calldata->lsp);
4313 put_nfs_open_context(calldata->ctx);
4314 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004315}
4316
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004317static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004318{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004319 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004320
Trond Myklebust14516c32010-07-31 14:29:06 -04004321 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4322 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004323 switch (task->tk_status) {
4324 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004325 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4326 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004327 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004328 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004329 case -NFS4ERR_BAD_STATEID:
4330 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004331 case -NFS4ERR_STALE_STATEID:
4332 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004333 break;
4334 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004335 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004336 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004337 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004338}
4339
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004340static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004341{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004342 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004344 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004345 return;
4346 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004347 /* Note: exit _without_ running nfs4_locku_done */
4348 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004349 return;
4350 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004351 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004352 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004353 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004354 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004355 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004356 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
4358
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004359static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004360 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004361 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004362 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004363};
4364
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004365static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4366 struct nfs_open_context *ctx,
4367 struct nfs4_lock_state *lsp,
4368 struct nfs_seqid *seqid)
4369{
4370 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004371 struct rpc_message msg = {
4372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4373 .rpc_cred = ctx->cred,
4374 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004375 struct rpc_task_setup task_setup_data = {
4376 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004377 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004378 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004379 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004380 .flags = RPC_TASK_ASYNC,
4381 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004382
Frank Filz137d6ac2007-07-09 15:32:29 -07004383 /* Ensure this is an unlock - when canceling a lock, the
4384 * canceled lock is passed in, and it won't be an unlock.
4385 */
4386 fl->fl_type = F_UNLCK;
4387
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004388 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4389 if (data == NULL) {
4390 nfs_free_seqid(seqid);
4391 return ERR_PTR(-ENOMEM);
4392 }
4393
Trond Myklebust9d12b212012-01-17 22:04:25 -05004394 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004395 msg.rpc_argp = &data->arg;
4396 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004397 task_setup_data.callback_data = data;
4398 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004399}
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4402{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004403 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004404 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004405 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004406 struct rpc_task *task;
4407 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004408 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Trond Myklebust9b073572006-06-29 16:38:34 -04004410 status = nfs4_set_lock_state(state, request);
4411 /* Unlock _before_ we do the RPC call */
4412 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004413 down_read(&nfsi->rwsem);
4414 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4415 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004416 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004417 }
4418 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004419 if (status != 0)
4420 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004421 /* Is this a delegated lock? */
4422 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004423 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004424 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004425 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004426 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004427 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004428 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004429 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004430 status = PTR_ERR(task);
4431 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004432 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004433 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004434 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004435out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004436 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004437 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438}
4439
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004440struct nfs4_lockdata {
4441 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004442 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443 struct nfs4_lock_state *lsp;
4444 struct nfs_open_context *ctx;
4445 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004446 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447 int rpc_status;
4448 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004449 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004450};
4451
4452static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004453 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4454 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004455{
4456 struct nfs4_lockdata *p;
4457 struct inode *inode = lsp->ls_state->inode;
4458 struct nfs_server *server = NFS_SERVER(inode);
4459
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004460 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004461 if (p == NULL)
4462 return NULL;
4463
4464 p->arg.fh = NFS_FH(inode);
4465 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004466 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004467 if (p->arg.open_seqid == NULL)
4468 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004469 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004470 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004471 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004472 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004473 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004474 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004475 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004476 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004478 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004479 atomic_inc(&lsp->ls_count);
4480 p->ctx = get_nfs_open_context(ctx);
4481 memcpy(&p->fl, fl, sizeof(p->fl));
4482 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004483out_free_seqid:
4484 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485out_free:
4486 kfree(p);
4487 return NULL;
4488}
4489
4490static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4491{
4492 struct nfs4_lockdata *data = calldata;
4493 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Harvey Harrison3110ff82008-05-02 13:42:44 -07004495 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004496 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4497 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004498 /* Do we need to do an open_to_lock_owner? */
4499 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004500 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4501 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004502 data->arg.open_stateid = &state->stateid;
4503 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004504 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004505 } else
4506 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004507 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004508 if (nfs4_setup_sequence(data->server,
4509 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004510 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004511 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004512 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004513 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514}
4515
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004516static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4517{
4518 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4519 nfs4_lock_prepare(task, calldata);
4520}
4521
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4523{
4524 struct nfs4_lockdata *data = calldata;
4525
Harvey Harrison3110ff82008-05-02 13:42:44 -07004526 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527
Trond Myklebust14516c32010-07-31 14:29:06 -04004528 if (!nfs4_sequence_done(task, &data->res.seq_res))
4529 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004530
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004531 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533 if (data->rpc_status == 0)
4534 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4535 else
4536 goto out;
4537 }
4538 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004539 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004541 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004542 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004543out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004544 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004545}
4546
4547static void nfs4_lock_release(void *calldata)
4548{
4549 struct nfs4_lockdata *data = calldata;
4550
Harvey Harrison3110ff82008-05-02 13:42:44 -07004551 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004552 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004553 if (data->cancelled != 0) {
4554 struct rpc_task *task;
4555 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4556 data->arg.lock_seqid);
4557 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004558 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004559 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004560 } else
4561 nfs_free_seqid(data->arg.lock_seqid);
4562 nfs4_put_lock_state(data->lsp);
4563 put_nfs_open_context(data->ctx);
4564 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004565 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004566}
4567
4568static const struct rpc_call_ops nfs4_lock_ops = {
4569 .rpc_call_prepare = nfs4_lock_prepare,
4570 .rpc_call_done = nfs4_lock_done,
4571 .rpc_release = nfs4_lock_release,
4572};
4573
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004574static const struct rpc_call_ops nfs4_recover_lock_ops = {
4575 .rpc_call_prepare = nfs4_recover_lock_prepare,
4576 .rpc_call_done = nfs4_lock_done,
4577 .rpc_release = nfs4_lock_release,
4578};
4579
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004580static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4581{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004582 switch (error) {
4583 case -NFS4ERR_ADMIN_REVOKED:
4584 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004585 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004586 if (new_lock_owner != 0 ||
4587 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004588 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004589 break;
4590 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004591 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004592 case -NFS4ERR_EXPIRED:
4593 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004594 };
4595}
4596
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004597static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598{
4599 struct nfs4_lockdata *data;
4600 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004601 struct rpc_message msg = {
4602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4603 .rpc_cred = state->owner->so_cred,
4604 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004605 struct rpc_task_setup task_setup_data = {
4606 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004607 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004608 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004609 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004610 .flags = RPC_TASK_ASYNC,
4611 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612 int ret;
4613
Harvey Harrison3110ff82008-05-02 13:42:44 -07004614 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004615 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004616 fl->fl_u.nfs4_fl.owner,
4617 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004618 if (data == NULL)
4619 return -ENOMEM;
4620 if (IS_SETLKW(cmd))
4621 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004622 if (recovery_type > NFS_LOCK_NEW) {
4623 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004624 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004625 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4626 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004627 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004628 msg.rpc_argp = &data->arg;
4629 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004630 task_setup_data.callback_data = data;
4631 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004632 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004633 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004634 ret = nfs4_wait_for_completion_rpc_task(task);
4635 if (ret == 0) {
4636 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004637 if (ret)
4638 nfs4_handle_setlk_error(data->server, data->lsp,
4639 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640 } else
4641 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004642 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004643 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004644 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
4647static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4648{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004649 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004650 struct nfs4_exception exception = {
4651 .inode = state->inode,
4652 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004653 int err;
4654
4655 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004656 /* Cache the lock if possible... */
4657 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4658 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004659 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004660 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004661 break;
4662 nfs4_handle_exception(server, err, &exception);
4663 } while (exception.retry);
4664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
4667static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4668{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004669 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004670 struct nfs4_exception exception = {
4671 .inode = state->inode,
4672 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004673 int err;
4674
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004675 err = nfs4_set_lock_state(state, request);
4676 if (err != 0)
4677 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004678 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004679 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4680 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004681 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004682 switch (err) {
4683 default:
4684 goto out;
4685 case -NFS4ERR_GRACE:
4686 case -NFS4ERR_DELAY:
4687 nfs4_handle_exception(server, err, &exception);
4688 err = 0;
4689 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004690 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004691out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004692 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
4694
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004695#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004696static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004697{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004698 int status, ret = NFS_OK;
4699 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004700 struct nfs_server *server = NFS_SERVER(state->inode);
4701
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004702 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4703 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4704 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4705 if (status != NFS_OK) {
4706 nfs41_free_stateid(server, &lsp->ls_stateid);
4707 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4708 ret = status;
4709 }
4710 }
4711 };
4712
4713 return ret;
4714}
4715
4716static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4717{
4718 int status = NFS_OK;
4719
4720 if (test_bit(LK_STATE_IN_USE, &state->flags))
4721 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004722 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004723 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004724 return nfs4_lock_expired(state, request);
4725}
4726#endif
4727
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4729{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004730 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004731 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004732 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004734 if ((fl_flags & FL_POSIX) &&
4735 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4736 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004737 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004738 status = nfs4_set_lock_state(state, request);
4739 if (status != 0)
4740 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004741 request->fl_flags |= FL_ACCESS;
4742 status = do_vfs_lock(request->fl_file, request);
4743 if (status < 0)
4744 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004745 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004746 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004747 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004748 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004749 request->fl_flags = fl_flags & ~FL_SLEEP;
4750 status = do_vfs_lock(request->fl_file, request);
4751 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004752 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004753 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004754 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004755 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004756 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004757 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004758 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004759 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4760 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004761out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004762 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004763out:
4764 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 return status;
4766}
4767
4768static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4769{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004770 struct nfs4_exception exception = {
4771 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004772 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004773 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 int err;
4775
4776 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004777 err = _nfs4_proc_setlk(state, cmd, request);
4778 if (err == -NFS4ERR_DENIED)
4779 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004781 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 } while (exception.retry);
4783 return err;
4784}
4785
4786static int
4787nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4788{
4789 struct nfs_open_context *ctx;
4790 struct nfs4_state *state;
4791 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4792 int status;
4793
4794 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004795 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 state = ctx->state;
4797
4798 if (request->fl_start < 0 || request->fl_end < 0)
4799 return -EINVAL;
4800
Trond Myklebustd9531262009-07-21 19:22:38 -04004801 if (IS_GETLK(cmd)) {
4802 if (state != NULL)
4803 return nfs4_proc_getlk(state, F_GETLK, request);
4804 return 0;
4805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
4807 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4808 return -EINVAL;
4809
Trond Myklebustd9531262009-07-21 19:22:38 -04004810 if (request->fl_type == F_UNLCK) {
4811 if (state != NULL)
4812 return nfs4_proc_unlck(state, cmd, request);
4813 return 0;
4814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Trond Myklebustd9531262009-07-21 19:22:38 -04004816 if (state == NULL)
4817 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004818 /*
4819 * Don't rely on the VFS having checked the file open mode,
4820 * since it won't do this for flock() locks.
4821 */
4822 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4823 case F_RDLCK:
4824 if (!(filp->f_mode & FMODE_READ))
4825 return -EBADF;
4826 break;
4827 case F_WRLCK:
4828 if (!(filp->f_mode & FMODE_WRITE))
4829 return -EBADF;
4830 }
4831
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 do {
4833 status = nfs4_proc_setlk(state, cmd, request);
4834 if ((status != -EAGAIN) || IS_SETLK(cmd))
4835 break;
4836 timeout = nfs4_set_lock_task_retry(timeout);
4837 status = -ERESTARTSYS;
4838 if (signalled())
4839 break;
4840 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 return status;
4842}
4843
Trond Myklebust888e6942005-11-04 15:38:11 -05004844int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4845{
4846 struct nfs_server *server = NFS_SERVER(state->inode);
4847 struct nfs4_exception exception = { };
4848 int err;
4849
4850 err = nfs4_set_lock_state(state, fl);
4851 if (err != 0)
4852 goto out;
4853 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004854 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004855 switch (err) {
4856 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004857 printk(KERN_ERR "NFS: %s: unhandled error "
4858 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004859 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004860 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004861 goto out;
4862 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004863 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004864 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004865 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004866 nfs4_schedule_lease_recovery(server->nfs_client);
4867 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004868 case -NFS4ERR_BADSESSION:
4869 case -NFS4ERR_BADSLOT:
4870 case -NFS4ERR_BAD_HIGH_SLOT:
4871 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4872 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004873 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004874 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004875 case -ERESTARTSYS:
4876 /*
4877 * The show must go on: exit, but mark the
4878 * stateid as needing recovery.
4879 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004880 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004881 case -NFS4ERR_ADMIN_REVOKED:
4882 case -NFS4ERR_BAD_STATEID:
4883 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004884 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004885 err = 0;
4886 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004887 case -EKEYEXPIRED:
4888 /*
4889 * User RPCSEC_GSS context has expired.
4890 * We cannot recover this stateid now, so
4891 * skip it and allow recovery thread to
4892 * proceed.
4893 */
4894 err = 0;
4895 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004896 case -ENOMEM:
4897 case -NFS4ERR_DENIED:
4898 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4899 err = 0;
4900 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004901 case -NFS4ERR_DELAY:
4902 break;
4903 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004904 err = nfs4_handle_exception(server, err, &exception);
4905 } while (exception.retry);
4906out:
4907 return err;
4908}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004909
Trond Myklebustcf470c32012-03-07 13:49:12 -05004910struct nfs_release_lockowner_data {
4911 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004912 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004913 struct nfs_release_lockowner_args args;
4914};
4915
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004916static void nfs4_release_lockowner_release(void *calldata)
4917{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004918 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004919 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004920 kfree(calldata);
4921}
4922
Trond Myklebust17280172012-03-11 13:11:00 -04004923static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004924 .rpc_release = nfs4_release_lockowner_release,
4925};
4926
Trond Myklebustcf470c32012-03-07 13:49:12 -05004927int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004928{
4929 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004930 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004931 struct rpc_message msg = {
4932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4933 };
4934
4935 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004936 return -EINVAL;
4937 data = kmalloc(sizeof(*data), GFP_NOFS);
4938 if (!data)
4939 return -ENOMEM;
4940 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004941 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004942 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4943 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4944 data->args.lock_owner.s_dev = server->s_dev;
4945 msg.rpc_argp = &data->args;
4946 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4947 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004948}
4949
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004950#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4951
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004952static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4953 const void *buf, size_t buflen,
4954 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004955{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004956 if (strcmp(key, "") != 0)
4957 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004958
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004959 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004960}
4961
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004962static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4963 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004964{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004965 if (strcmp(key, "") != 0)
4966 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004967
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004968 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004969}
4970
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004971static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4972 size_t list_len, const char *name,
4973 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004974{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004975 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004976
J. Bruce Fields096455a2006-03-20 23:23:42 -05004977 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4978 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004979
4980 if (list && len <= list_len)
4981 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004982 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004983}
4984
Andy Adamson533eb462011-06-13 18:25:56 -04004985/*
4986 * nfs_fhget will use either the mounted_on_fileid or the fileid
4987 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004988static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4989{
Andy Adamson533eb462011-06-13 18:25:56 -04004990 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4991 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4992 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004993 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004994 return;
4995
4996 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004997 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004998 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4999 fattr->nlink = 2;
5000}
5001
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005002static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5003 const struct qstr *name,
5004 struct nfs4_fs_locations *fs_locations,
5005 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005006{
5007 struct nfs_server *server = NFS_SERVER(dir);
5008 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005009 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005010 };
5011 struct nfs4_fs_locations_arg args = {
5012 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005013 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005014 .page = page,
5015 .bitmask = bitmask,
5016 };
Benny Halevy22958462009-04-01 09:22:02 -04005017 struct nfs4_fs_locations_res res = {
5018 .fs_locations = fs_locations,
5019 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005020 struct rpc_message msg = {
5021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5022 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005023 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005024 };
5025 int status;
5026
Harvey Harrison3110ff82008-05-02 13:42:44 -07005027 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005028
5029 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5030 * is not supported */
5031 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5032 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5033 else
5034 bitmask[0] |= FATTR4_WORD0_FILEID;
5035
Trond Myklebustc228fd32007-01-13 02:28:11 -05005036 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005037 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005038 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005039 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005040 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005041 return status;
5042}
5043
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005044int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5045 const struct qstr *name,
5046 struct nfs4_fs_locations *fs_locations,
5047 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005048{
5049 struct nfs4_exception exception = { };
5050 int err;
5051 do {
5052 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005053 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005054 &exception);
5055 } while (exception.retry);
5056 return err;
5057}
5058
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005059static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5060{
5061 int status;
5062 struct nfs4_secinfo_arg args = {
5063 .dir_fh = NFS_FH(dir),
5064 .name = name,
5065 };
5066 struct nfs4_secinfo_res res = {
5067 .flavors = flavors,
5068 };
5069 struct rpc_message msg = {
5070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5071 .rpc_argp = &args,
5072 .rpc_resp = &res,
5073 };
5074
5075 dprintk("NFS call secinfo %s\n", name->name);
5076 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5077 dprintk("NFS reply secinfo: %d\n", status);
5078 return status;
5079}
5080
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005081int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5082 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005083{
5084 struct nfs4_exception exception = { };
5085 int err;
5086 do {
5087 err = nfs4_handle_exception(NFS_SERVER(dir),
5088 _nfs4_proc_secinfo(dir, name, flavors),
5089 &exception);
5090 } while (exception.retry);
5091 return err;
5092}
5093
Andy Adamson557134a2009-04-01 09:21:53 -04005094#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005095/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005096 * Check the exchange flags returned by the server for invalid flags, having
5097 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5098 * DS flags set.
5099 */
5100static int nfs4_check_cl_exchange_flags(u32 flags)
5101{
5102 if (flags & ~EXCHGID4_FLAG_MASK_R)
5103 goto out_inval;
5104 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5105 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5106 goto out_inval;
5107 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5108 goto out_inval;
5109 return NFS_OK;
5110out_inval:
5111 return -NFS4ERR_INVAL;
5112}
5113
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005114static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005115nfs41_same_server_scope(struct nfs41_server_scope *a,
5116 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005117{
5118 if (a->server_scope_sz == b->server_scope_sz &&
5119 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5120 return true;
5121
5122 return false;
5123}
5124
Andy Adamson357f54d2010-12-14 10:11:57 -05005125/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005126 * nfs4_proc_bind_conn_to_session()
5127 *
5128 * The 4.1 client currently uses the same TCP connection for the
5129 * fore and backchannel.
5130 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005131int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005132{
5133 int status;
5134 struct nfs41_bind_conn_to_session_res res;
5135 struct rpc_message msg = {
5136 .rpc_proc =
5137 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5138 .rpc_argp = clp,
5139 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005140 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005141 };
5142
5143 dprintk("--> %s\n", __func__);
5144 BUG_ON(clp == NULL);
5145
5146 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5147 if (unlikely(res.session == NULL)) {
5148 status = -ENOMEM;
5149 goto out;
5150 }
5151
5152 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5153 if (status == 0) {
5154 if (memcmp(res.session->sess_id.data,
5155 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5156 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5157 status = -EIO;
5158 goto out_session;
5159 }
5160 if (res.dir != NFS4_CDFS4_BOTH) {
5161 dprintk("NFS: %s: Unexpected direction from server\n",
5162 __func__);
5163 status = -EIO;
5164 goto out_session;
5165 }
5166 if (res.use_conn_in_rdma_mode) {
5167 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5168 __func__);
5169 status = -EIO;
5170 goto out_session;
5171 }
5172 }
5173out_session:
5174 kfree(res.session);
5175out:
5176 dprintk("<-- %s status= %d\n", __func__, status);
5177 return status;
5178}
5179
5180/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005181 * nfs4_proc_exchange_id()
5182 *
5183 * Since the clientid has expired, all compounds using sessions
5184 * associated with the stale clientid will be returning
5185 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5186 * be in some phase of session reset.
5187 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005188int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005189{
5190 nfs4_verifier verifier;
5191 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005192 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005193 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005194 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005195 };
5196 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005197 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005198 };
5199 int status;
5200 struct rpc_message msg = {
5201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5202 .rpc_argp = &args,
5203 .rpc_resp = &res,
5204 .rpc_cred = cred,
5205 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005206
5207 dprintk("--> %s\n", __func__);
5208 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005209
Chuck Leverf0920752012-05-21 22:45:41 -04005210 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005211
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005212 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005213 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005214 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005215 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005216 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005217
Chuck Leveracdeb692012-05-21 22:46:16 -04005218 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005219 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005220 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005221 status = -ENOMEM;
5222 goto out;
5223 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005224
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005225 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005226 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005227 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005228 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005229 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005230 }
5231
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005232 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005233 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005234 status = -ENOMEM;
5235 goto out_server_scope;
5236 }
5237
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005238 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005239 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005240 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005241
Chuck Lever177313f2012-05-21 22:44:58 -04005242 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005243 clp->cl_clientid = res.clientid;
5244 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5245 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5246 clp->cl_seqid = res.seqid;
5247
Chuck Leveracdeb692012-05-21 22:46:16 -04005248 kfree(clp->cl_serverowner);
5249 clp->cl_serverowner = res.server_owner;
5250 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005251
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005252 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005253 kfree(clp->cl_implid);
5254 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005255
Chuck Lever177313f2012-05-21 22:44:58 -04005256 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005257 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005258 res.server_scope)) {
5259 dprintk("%s: server_scope mismatch detected\n",
5260 __func__);
5261 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005262 kfree(clp->cl_serverscope);
5263 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005264 }
5265
Chuck Lever177313f2012-05-21 22:44:58 -04005266 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005267 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005268 goto out;
5269 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005270 } else
5271 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005272
Chuck Leveracdeb692012-05-21 22:46:16 -04005273out_server_owner:
5274 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005275out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005276 kfree(res.server_scope);
5277out:
Chuck Lever177313f2012-05-21 22:44:58 -04005278 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005279 dprintk("%s: Server Implementation ID: "
5280 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005281 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5282 clp->cl_implid->date.seconds,
5283 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005284 dprintk("<-- %s status= %d\n", __func__, status);
5285 return status;
5286}
5287
Trond Myklebust66245532012-05-25 17:18:09 -04005288static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5289 struct rpc_cred *cred)
5290{
5291 struct rpc_message msg = {
5292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5293 .rpc_argp = clp,
5294 .rpc_cred = cred,
5295 };
5296 int status;
5297
5298 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5299 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005300 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005301 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5302 return status;
5303}
5304
5305static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5306 struct rpc_cred *cred)
5307{
5308 unsigned int loop;
5309 int ret;
5310
5311 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5312 ret = _nfs4_proc_destroy_clientid(clp, cred);
5313 switch (ret) {
5314 case -NFS4ERR_DELAY:
5315 case -NFS4ERR_CLIENTID_BUSY:
5316 ssleep(1);
5317 break;
5318 default:
5319 return ret;
5320 }
5321 }
5322 return 0;
5323}
5324
5325int nfs4_destroy_clientid(struct nfs_client *clp)
5326{
5327 struct rpc_cred *cred;
5328 int ret = 0;
5329
5330 if (clp->cl_mvops->minor_version < 1)
5331 goto out;
5332 if (clp->cl_exchange_flags == 0)
5333 goto out;
5334 cred = nfs4_get_exchange_id_cred(clp);
5335 ret = nfs4_proc_destroy_clientid(clp, cred);
5336 if (cred)
5337 put_rpccred(cred);
5338 switch (ret) {
5339 case 0:
5340 case -NFS4ERR_STALE_CLIENTID:
5341 clp->cl_exchange_flags = 0;
5342 }
5343out:
5344 return ret;
5345}
5346
Andy Adamson2050f0c2009-04-01 09:22:30 -04005347struct nfs4_get_lease_time_data {
5348 struct nfs4_get_lease_time_args *args;
5349 struct nfs4_get_lease_time_res *res;
5350 struct nfs_client *clp;
5351};
5352
5353static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5354 void *calldata)
5355{
5356 int ret;
5357 struct nfs4_get_lease_time_data *data =
5358 (struct nfs4_get_lease_time_data *)calldata;
5359
5360 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005361 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005362 /* just setup sequence, do not trigger session recovery
5363 since we're invoked within one */
5364 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005365 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005366 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005367
5368 BUG_ON(ret == -EAGAIN);
5369 rpc_call_start(task);
5370 dprintk("<-- %s\n", __func__);
5371}
5372
5373/*
5374 * Called from nfs4_state_manager thread for session setup, so don't recover
5375 * from sequence operation or clientid errors.
5376 */
5377static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5378{
5379 struct nfs4_get_lease_time_data *data =
5380 (struct nfs4_get_lease_time_data *)calldata;
5381
5382 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005383 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5384 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005385 switch (task->tk_status) {
5386 case -NFS4ERR_DELAY:
5387 case -NFS4ERR_GRACE:
5388 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5389 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5390 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005391 /* fall through */
5392 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005393 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005394 return;
5395 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005396 dprintk("<-- %s\n", __func__);
5397}
5398
Trond Myklebust17280172012-03-11 13:11:00 -04005399static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005400 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5401 .rpc_call_done = nfs4_get_lease_time_done,
5402};
5403
5404int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5405{
5406 struct rpc_task *task;
5407 struct nfs4_get_lease_time_args args;
5408 struct nfs4_get_lease_time_res res = {
5409 .lr_fsinfo = fsinfo,
5410 };
5411 struct nfs4_get_lease_time_data data = {
5412 .args = &args,
5413 .res = &res,
5414 .clp = clp,
5415 };
5416 struct rpc_message msg = {
5417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5418 .rpc_argp = &args,
5419 .rpc_resp = &res,
5420 };
5421 struct rpc_task_setup task_setup = {
5422 .rpc_client = clp->cl_rpcclient,
5423 .rpc_message = &msg,
5424 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005425 .callback_data = &data,
5426 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005427 };
5428 int status;
5429
Trond Myklebust9d12b212012-01-17 22:04:25 -05005430 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005431 dprintk("--> %s\n", __func__);
5432 task = rpc_run_task(&task_setup);
5433
5434 if (IS_ERR(task))
5435 status = PTR_ERR(task);
5436 else {
5437 status = task->tk_status;
5438 rpc_put_task(task);
5439 }
5440 dprintk("<-- %s return %d\n", __func__, status);
5441
5442 return status;
5443}
5444
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005445static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5446{
5447 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5448}
5449
5450static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5451 struct nfs4_slot *new,
5452 u32 max_slots,
5453 u32 ivalue)
5454{
5455 struct nfs4_slot *old = NULL;
5456 u32 i;
5457
5458 spin_lock(&tbl->slot_tbl_lock);
5459 if (new) {
5460 old = tbl->slots;
5461 tbl->slots = new;
5462 tbl->max_slots = max_slots;
5463 }
5464 tbl->highest_used_slotid = -1; /* no slot is currently used */
5465 for (i = 0; i < tbl->max_slots; i++)
5466 tbl->slots[i].seq_nr = ivalue;
5467 spin_unlock(&tbl->slot_tbl_lock);
5468 kfree(old);
5469}
5470
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005471/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005472 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005473 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005474static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5475 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005476{
Andy Adamson104aeba2010-01-14 17:45:10 -05005477 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005478 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005479
Andy Adamson104aeba2010-01-14 17:45:10 -05005480 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5481 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005482
Andy Adamson104aeba2010-01-14 17:45:10 -05005483 /* Does the newly negotiated max_reqs match the existing slot table? */
5484 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005485 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005486 if (!new)
5487 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005488 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005489 ret = 0;
5490
5491 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005492 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5493 tbl, tbl->slots, tbl->max_slots);
5494out:
5495 dprintk("<-- %s: return %d\n", __func__, ret);
5496 return ret;
5497}
5498
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005499/* Destroy the slot table */
5500static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5501{
5502 if (session->fc_slot_table.slots != NULL) {
5503 kfree(session->fc_slot_table.slots);
5504 session->fc_slot_table.slots = NULL;
5505 }
5506 if (session->bc_slot_table.slots != NULL) {
5507 kfree(session->bc_slot_table.slots);
5508 session->bc_slot_table.slots = NULL;
5509 }
5510 return;
5511}
5512
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005513/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005514 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005515 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005516static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005517{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005518 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005519 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005520
Andy Adamsonaacd5532011-11-09 13:58:21 -05005521 dprintk("--> %s\n", __func__);
5522 /* Fore channel */
5523 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005524 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5525 if (status) /* -ENOMEM */
5526 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005527 /* Back channel */
5528 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005529 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5530 if (status && tbl->slots == NULL)
5531 /* Fore and back channel share a connection so get
5532 * both slot tables or neither */
5533 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005534 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005535}
5536
5537struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5538{
5539 struct nfs4_session *session;
5540 struct nfs4_slot_table *tbl;
5541
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005542 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005543 if (!session)
5544 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005545
Andy Adamson557134a2009-04-01 09:21:53 -04005546 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005547 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005548 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005549 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005550 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005551
5552 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005553 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005554 spin_lock_init(&tbl->slot_tbl_lock);
5555 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005556 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005557
Trond Myklebust1055d762010-06-16 09:52:27 -04005558 session->session_state = 1<<NFS4_SESSION_INITING;
5559
Andy Adamson557134a2009-04-01 09:21:53 -04005560 session->clp = clp;
5561 return session;
5562}
5563
5564void nfs4_destroy_session(struct nfs4_session *session)
5565{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005566 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005567 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005568
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005569 cred = nfs4_get_exchange_id_cred(session->clp);
5570 nfs4_proc_destroy_session(session, cred);
5571 if (cred)
5572 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005573
5574 rcu_read_lock();
5575 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5576 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005577 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005578 __func__, xprt);
5579 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005580 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005581 kfree(session);
5582}
5583
Andy Adamsonfc931582009-04-01 09:22:31 -04005584/*
5585 * Initialize the values to be used by the client in CREATE_SESSION
5586 * If nfs4_init_session set the fore channel request and response sizes,
5587 * use them.
5588 *
5589 * Set the back channel max_resp_sz_cached to zero to force the client to
5590 * always set csa_cachethis to FALSE because the current implementation
5591 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5592 */
5593static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5594{
5595 struct nfs4_session *session = args->client->cl_session;
5596 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5597 mxresp_sz = session->fc_attrs.max_resp_sz;
5598
5599 if (mxrqst_sz == 0)
5600 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5601 if (mxresp_sz == 0)
5602 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5603 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005604 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5605 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005606 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005607 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005608
5609 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005610 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005611 __func__,
5612 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005613 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005614
5615 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005616 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5617 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5618 args->bc_attrs.max_resp_sz_cached = 0;
5619 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5620 args->bc_attrs.max_reqs = 1;
5621
5622 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5623 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5624 __func__,
5625 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5626 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5627 args->bc_attrs.max_reqs);
5628}
5629
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005630static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005631{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005632 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5633 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5634
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005635 if (rcvd->max_resp_sz > sent->max_resp_sz)
5636 return -EINVAL;
5637 /*
5638 * Our requested max_ops is the minimum we need; we're not
5639 * prepared to break up compounds into smaller pieces than that.
5640 * So, no point even trying to continue if the server won't
5641 * cooperate:
5642 */
5643 if (rcvd->max_ops < sent->max_ops)
5644 return -EINVAL;
5645 if (rcvd->max_reqs == 0)
5646 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005647 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5648 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005649 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005650}
5651
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005652static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5653{
5654 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5655 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005656
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005657 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5658 return -EINVAL;
5659 if (rcvd->max_resp_sz < sent->max_resp_sz)
5660 return -EINVAL;
5661 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5662 return -EINVAL;
5663 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005664 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005665 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005666 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005667 return -EINVAL;
5668 return 0;
5669}
Andy Adamson8d353012009-04-01 09:22:32 -04005670
Andy Adamson8d353012009-04-01 09:22:32 -04005671static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5672 struct nfs4_session *session)
5673{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005674 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005675
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005676 ret = nfs4_verify_fore_channel_attrs(args, session);
5677 if (ret)
5678 return ret;
5679 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005680}
5681
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005682static int _nfs4_proc_create_session(struct nfs_client *clp,
5683 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005684{
5685 struct nfs4_session *session = clp->cl_session;
5686 struct nfs41_create_session_args args = {
5687 .client = clp,
5688 .cb_program = NFS4_CALLBACK,
5689 };
5690 struct nfs41_create_session_res res = {
5691 .client = clp,
5692 };
5693 struct rpc_message msg = {
5694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5695 .rpc_argp = &args,
5696 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005697 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005698 };
5699 int status;
5700
5701 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005702 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005703
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005704 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005705
Andy Adamson8d353012009-04-01 09:22:32 -04005706 if (!status)
5707 /* Verify the session's negotiated channel_attrs values */
5708 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005709 if (!status) {
5710 /* Increment the clientid slot sequence id */
5711 clp->cl_seqid++;
5712 }
5713
5714 return status;
5715}
5716
5717/*
5718 * Issues a CREATE_SESSION operation to the server.
5719 * It is the responsibility of the caller to verify the session is
5720 * expired before calling this routine.
5721 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005722int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005723{
5724 int status;
5725 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005726 struct nfs4_session *session = clp->cl_session;
5727
5728 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5729
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005730 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005731 if (status)
5732 goto out;
5733
Andy Adamsonaacd5532011-11-09 13:58:21 -05005734 /* Init or reset the session slot tables */
5735 status = nfs4_setup_session_slot_tables(session);
5736 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005737 if (status)
5738 goto out;
5739
5740 ptr = (unsigned *)&session->sess_id.data[0];
5741 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5742 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005743out:
5744 dprintk("<-- %s\n", __func__);
5745 return status;
5746}
5747
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005748/*
5749 * Issue the over-the-wire RPC DESTROY_SESSION.
5750 * The caller must serialize access to this routine.
5751 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005752int nfs4_proc_destroy_session(struct nfs4_session *session,
5753 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005754{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005755 struct rpc_message msg = {
5756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5757 .rpc_argp = session,
5758 .rpc_cred = cred,
5759 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005760 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005761
5762 dprintk("--> nfs4_proc_destroy_session\n");
5763
5764 /* session is still being setup */
5765 if (session->clp->cl_cons_state != NFS_CS_READY)
5766 return status;
5767
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005768 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005769
5770 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005771 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005772 "Session has been destroyed regardless...\n", status);
5773
5774 dprintk("<-- nfs4_proc_destroy_session\n");
5775 return status;
5776}
5777
Trond Myklebust7b38c362012-05-23 13:23:31 -04005778/*
5779 * With sessions, the client is not marked ready until after a
5780 * successful EXCHANGE_ID and CREATE_SESSION.
5781 *
5782 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5783 * other versions of NFS can be tried.
5784 */
5785static int nfs41_check_session_ready(struct nfs_client *clp)
5786{
5787 int ret;
5788
5789 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5790 ret = nfs4_client_recover_expired_lease(clp);
5791 if (ret)
5792 return ret;
5793 }
5794 if (clp->cl_cons_state < NFS_CS_READY)
5795 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005796 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005797 return 0;
5798}
5799
Trond Myklebustfccba802009-07-21 16:48:07 -04005800int nfs4_init_session(struct nfs_server *server)
5801{
5802 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005803 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005804 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005805
5806 if (!nfs4_has_session(clp))
5807 return 0;
5808
Trond Myklebust1055d762010-06-16 09:52:27 -04005809 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005810 spin_lock(&clp->cl_lock);
5811 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005812
Trond Myklebust7b38c362012-05-23 13:23:31 -04005813 rsize = server->rsize;
5814 if (rsize == 0)
5815 rsize = NFS_MAX_FILE_IO_SIZE;
5816 wsize = server->wsize;
5817 if (wsize == 0)
5818 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005819
Trond Myklebust7b38c362012-05-23 13:23:31 -04005820 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5821 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5822 }
5823 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005824
Trond Myklebust7b38c362012-05-23 13:23:31 -04005825 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005826}
5827
Trond Myklebust7b38c362012-05-23 13:23:31 -04005828int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005829{
5830 struct nfs4_session *session = clp->cl_session;
5831 int ret;
5832
Trond Myklebust7b38c362012-05-23 13:23:31 -04005833 spin_lock(&clp->cl_lock);
5834 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5835 /*
5836 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5837 * DS lease to be equal to the MDS lease.
5838 */
5839 clp->cl_lease_time = lease_time;
5840 clp->cl_last_renewal = jiffies;
5841 }
5842 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005843
Trond Myklebust7b38c362012-05-23 13:23:31 -04005844 ret = nfs41_check_session_ready(clp);
5845 if (ret)
5846 return ret;
5847 /* Test for the DS role */
5848 if (!is_ds_client(clp))
5849 return -ENODEV;
5850 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005851}
5852EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5853
5854
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005855/*
5856 * Renew the cl_session lease.
5857 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005858struct nfs4_sequence_data {
5859 struct nfs_client *clp;
5860 struct nfs4_sequence_args args;
5861 struct nfs4_sequence_res res;
5862};
5863
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005864static void nfs41_sequence_release(void *data)
5865{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005866 struct nfs4_sequence_data *calldata = data;
5867 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005868
Alexandros Batsakis71358402010-02-05 03:45:05 -08005869 if (atomic_read(&clp->cl_count) > 1)
5870 nfs4_schedule_state_renewal(clp);
5871 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005872 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005873}
5874
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005875static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5876{
5877 switch(task->tk_status) {
5878 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005879 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5880 return -EAGAIN;
5881 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005882 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005883 }
5884 return 0;
5885}
5886
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005887static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005888{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005889 struct nfs4_sequence_data *calldata = data;
5890 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005891
Trond Myklebust14516c32010-07-31 14:29:06 -04005892 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5893 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005894
5895 if (task->tk_status < 0) {
5896 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005897 if (atomic_read(&clp->cl_count) == 1)
5898 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005899
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005900 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5901 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005902 return;
5903 }
5904 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005905 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005906out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005907 dprintk("<-- %s\n", __func__);
5908}
5909
5910static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5911{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005912 struct nfs4_sequence_data *calldata = data;
5913 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005914 struct nfs4_sequence_args *args;
5915 struct nfs4_sequence_res *res;
5916
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005917 args = task->tk_msg.rpc_argp;
5918 res = task->tk_msg.rpc_resp;
5919
Trond Myklebust9d12b212012-01-17 22:04:25 -05005920 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005921 return;
5922 rpc_call_start(task);
5923}
5924
5925static const struct rpc_call_ops nfs41_sequence_ops = {
5926 .rpc_call_done = nfs41_sequence_call_done,
5927 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005928 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005929};
5930
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005931static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005932{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005933 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005934 struct rpc_message msg = {
5935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5936 .rpc_cred = cred,
5937 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005938 struct rpc_task_setup task_setup_data = {
5939 .rpc_client = clp->cl_rpcclient,
5940 .rpc_message = &msg,
5941 .callback_ops = &nfs41_sequence_ops,
5942 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5943 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005944
Alexandros Batsakis71358402010-02-05 03:45:05 -08005945 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005946 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005947 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005948 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005949 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005950 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005951 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005952 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005953 msg.rpc_argp = &calldata->args;
5954 msg.rpc_resp = &calldata->res;
5955 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005956 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005957
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005958 return rpc_run_task(&task_setup_data);
5959}
5960
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005961static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005962{
5963 struct rpc_task *task;
5964 int ret = 0;
5965
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005966 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5967 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005968 task = _nfs41_proc_sequence(clp, cred);
5969 if (IS_ERR(task))
5970 ret = PTR_ERR(task);
5971 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005972 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005973 dprintk("<-- %s status=%d\n", __func__, ret);
5974 return ret;
5975}
5976
5977static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5978{
5979 struct rpc_task *task;
5980 int ret;
5981
5982 task = _nfs41_proc_sequence(clp, cred);
5983 if (IS_ERR(task)) {
5984 ret = PTR_ERR(task);
5985 goto out;
5986 }
5987 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005988 if (!ret) {
5989 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5990
5991 if (task->tk_status == 0)
5992 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005993 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005994 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005995 rpc_put_task(task);
5996out:
5997 dprintk("<-- %s status=%d\n", __func__, ret);
5998 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005999}
6000
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006001struct nfs4_reclaim_complete_data {
6002 struct nfs_client *clp;
6003 struct nfs41_reclaim_complete_args arg;
6004 struct nfs41_reclaim_complete_res res;
6005};
6006
6007static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6008{
6009 struct nfs4_reclaim_complete_data *calldata = data;
6010
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006011 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006012 if (nfs41_setup_sequence(calldata->clp->cl_session,
6013 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006014 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006015 return;
6016
6017 rpc_call_start(task);
6018}
6019
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006020static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6021{
6022 switch(task->tk_status) {
6023 case 0:
6024 case -NFS4ERR_COMPLETE_ALREADY:
6025 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6026 break;
6027 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006028 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006029 /* fall through */
6030 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006031 return -EAGAIN;
6032 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006033 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006034 }
6035 return 0;
6036}
6037
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006038static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6039{
6040 struct nfs4_reclaim_complete_data *calldata = data;
6041 struct nfs_client *clp = calldata->clp;
6042 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6043
6044 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006045 if (!nfs41_sequence_done(task, res))
6046 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006047
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006048 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6049 rpc_restart_call_prepare(task);
6050 return;
6051 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006052 dprintk("<-- %s\n", __func__);
6053}
6054
6055static void nfs4_free_reclaim_complete_data(void *data)
6056{
6057 struct nfs4_reclaim_complete_data *calldata = data;
6058
6059 kfree(calldata);
6060}
6061
6062static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6063 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6064 .rpc_call_done = nfs4_reclaim_complete_done,
6065 .rpc_release = nfs4_free_reclaim_complete_data,
6066};
6067
6068/*
6069 * Issue a global reclaim complete.
6070 */
6071static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6072{
6073 struct nfs4_reclaim_complete_data *calldata;
6074 struct rpc_task *task;
6075 struct rpc_message msg = {
6076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6077 };
6078 struct rpc_task_setup task_setup_data = {
6079 .rpc_client = clp->cl_rpcclient,
6080 .rpc_message = &msg,
6081 .callback_ops = &nfs4_reclaim_complete_call_ops,
6082 .flags = RPC_TASK_ASYNC,
6083 };
6084 int status = -ENOMEM;
6085
6086 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006087 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006088 if (calldata == NULL)
6089 goto out;
6090 calldata->clp = clp;
6091 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006092
Trond Myklebust9d12b212012-01-17 22:04:25 -05006093 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006094 msg.rpc_argp = &calldata->arg;
6095 msg.rpc_resp = &calldata->res;
6096 task_setup_data.callback_data = calldata;
6097 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006098 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006099 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006100 goto out;
6101 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006102 status = nfs4_wait_for_completion_rpc_task(task);
6103 if (status == 0)
6104 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006105 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006106 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006107out:
6108 dprintk("<-- %s status=%d\n", __func__, status);
6109 return status;
6110}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006111
6112static void
6113nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6114{
6115 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006116 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006117
6118 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006119 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6120 * right now covering the LAYOUTGET we are about to send.
6121 * However, that is not so catastrophic, and there seems
6122 * to be no way to prevent it completely.
6123 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006124 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006125 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006126 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006127 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6128 NFS_I(lgp->args.inode)->layout,
6129 lgp->args.ctx->state)) {
6130 rpc_exit(task, NFS4_OK);
6131 return;
6132 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006133 rpc_call_start(task);
6134}
6135
6136static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6137{
6138 struct nfs4_layoutget *lgp = calldata;
6139 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6140
6141 dprintk("--> %s\n", __func__);
6142
6143 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6144 return;
6145
6146 switch (task->tk_status) {
6147 case 0:
6148 break;
6149 case -NFS4ERR_LAYOUTTRYLATER:
6150 case -NFS4ERR_RECALLCONFLICT:
6151 task->tk_status = -NFS4ERR_DELAY;
6152 /* Fall through */
6153 default:
6154 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6155 rpc_restart_call_prepare(task);
6156 return;
6157 }
6158 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006159 dprintk("<-- %s\n", __func__);
6160}
6161
6162static void nfs4_layoutget_release(void *calldata)
6163{
6164 struct nfs4_layoutget *lgp = calldata;
6165
6166 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006167 put_nfs_open_context(lgp->args.ctx);
6168 kfree(calldata);
6169 dprintk("<-- %s\n", __func__);
6170}
6171
6172static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6173 .rpc_call_prepare = nfs4_layoutget_prepare,
6174 .rpc_call_done = nfs4_layoutget_done,
6175 .rpc_release = nfs4_layoutget_release,
6176};
6177
6178int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6179{
6180 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6181 struct rpc_task *task;
6182 struct rpc_message msg = {
6183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6184 .rpc_argp = &lgp->args,
6185 .rpc_resp = &lgp->res,
6186 };
6187 struct rpc_task_setup task_setup_data = {
6188 .rpc_client = server->client,
6189 .rpc_message = &msg,
6190 .callback_ops = &nfs4_layoutget_call_ops,
6191 .callback_data = lgp,
6192 .flags = RPC_TASK_ASYNC,
6193 };
6194 int status = 0;
6195
6196 dprintk("--> %s\n", __func__);
6197
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006198 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006199 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006200 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006201 task = rpc_run_task(&task_setup_data);
6202 if (IS_ERR(task))
6203 return PTR_ERR(task);
6204 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006205 if (status == 0)
6206 status = task->tk_status;
6207 if (status == 0)
6208 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006209 rpc_put_task(task);
6210 dprintk("<-- %s status=%d\n", __func__, status);
6211 return status;
6212}
6213
Benny Halevycbe82602011-05-22 19:52:37 +03006214static void
6215nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6216{
6217 struct nfs4_layoutreturn *lrp = calldata;
6218
6219 dprintk("--> %s\n", __func__);
6220 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006221 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006222 return;
6223 rpc_call_start(task);
6224}
6225
6226static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6227{
6228 struct nfs4_layoutreturn *lrp = calldata;
6229 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006230 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006231
6232 dprintk("--> %s\n", __func__);
6233
6234 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6235 return;
6236
6237 server = NFS_SERVER(lrp->args.inode);
6238 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006239 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006240 return;
6241 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006242 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006243 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006244 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006245 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006246 } else
6247 BUG_ON(!list_empty(&lo->plh_segs));
6248 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006249 lo->plh_block_lgets--;
6250 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006251 dprintk("<-- %s\n", __func__);
6252}
6253
6254static void nfs4_layoutreturn_release(void *calldata)
6255{
6256 struct nfs4_layoutreturn *lrp = calldata;
6257
6258 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006259 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006260 kfree(calldata);
6261 dprintk("<-- %s\n", __func__);
6262}
6263
6264static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6265 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6266 .rpc_call_done = nfs4_layoutreturn_done,
6267 .rpc_release = nfs4_layoutreturn_release,
6268};
6269
6270int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6271{
6272 struct rpc_task *task;
6273 struct rpc_message msg = {
6274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6275 .rpc_argp = &lrp->args,
6276 .rpc_resp = &lrp->res,
6277 };
6278 struct rpc_task_setup task_setup_data = {
6279 .rpc_client = lrp->clp->cl_rpcclient,
6280 .rpc_message = &msg,
6281 .callback_ops = &nfs4_layoutreturn_call_ops,
6282 .callback_data = lrp,
6283 };
6284 int status;
6285
6286 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006287 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006288 task = rpc_run_task(&task_setup_data);
6289 if (IS_ERR(task))
6290 return PTR_ERR(task);
6291 status = task->tk_status;
6292 dprintk("<-- %s status=%d\n", __func__, status);
6293 rpc_put_task(task);
6294 return status;
6295}
6296
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006297/*
6298 * Retrieve the list of Data Server devices from the MDS.
6299 */
6300static int _nfs4_getdevicelist(struct nfs_server *server,
6301 const struct nfs_fh *fh,
6302 struct pnfs_devicelist *devlist)
6303{
6304 struct nfs4_getdevicelist_args args = {
6305 .fh = fh,
6306 .layoutclass = server->pnfs_curr_ld->id,
6307 };
6308 struct nfs4_getdevicelist_res res = {
6309 .devlist = devlist,
6310 };
6311 struct rpc_message msg = {
6312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6313 .rpc_argp = &args,
6314 .rpc_resp = &res,
6315 };
6316 int status;
6317
6318 dprintk("--> %s\n", __func__);
6319 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6320 &res.seq_res, 0);
6321 dprintk("<-- %s status=%d\n", __func__, status);
6322 return status;
6323}
6324
6325int nfs4_proc_getdevicelist(struct nfs_server *server,
6326 const struct nfs_fh *fh,
6327 struct pnfs_devicelist *devlist)
6328{
6329 struct nfs4_exception exception = { };
6330 int err;
6331
6332 do {
6333 err = nfs4_handle_exception(server,
6334 _nfs4_getdevicelist(server, fh, devlist),
6335 &exception);
6336 } while (exception.retry);
6337
6338 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6339 err, devlist->num_devs);
6340
6341 return err;
6342}
6343EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6344
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006345static int
6346_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6347{
6348 struct nfs4_getdeviceinfo_args args = {
6349 .pdev = pdev,
6350 };
6351 struct nfs4_getdeviceinfo_res res = {
6352 .pdev = pdev,
6353 };
6354 struct rpc_message msg = {
6355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6356 .rpc_argp = &args,
6357 .rpc_resp = &res,
6358 };
6359 int status;
6360
6361 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006362 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006363 dprintk("<-- %s status=%d\n", __func__, status);
6364
6365 return status;
6366}
6367
6368int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6369{
6370 struct nfs4_exception exception = { };
6371 int err;
6372
6373 do {
6374 err = nfs4_handle_exception(server,
6375 _nfs4_proc_getdeviceinfo(server, pdev),
6376 &exception);
6377 } while (exception.retry);
6378 return err;
6379}
6380EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6381
Andy Adamson863a3c62011-03-23 13:27:54 +00006382static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6383{
6384 struct nfs4_layoutcommit_data *data = calldata;
6385 struct nfs_server *server = NFS_SERVER(data->args.inode);
6386
6387 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006388 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006389 return;
6390 rpc_call_start(task);
6391}
6392
6393static void
6394nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6395{
6396 struct nfs4_layoutcommit_data *data = calldata;
6397 struct nfs_server *server = NFS_SERVER(data->args.inode);
6398
6399 if (!nfs4_sequence_done(task, &data->res.seq_res))
6400 return;
6401
6402 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006403 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6404 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6405 case -NFS4ERR_BADLAYOUT: /* no layout */
6406 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006407 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006408 break;
6409 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006410 nfs_post_op_update_inode_force_wcc(data->args.inode,
6411 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006412 break;
6413 default:
6414 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6415 rpc_restart_call_prepare(task);
6416 return;
6417 }
6418 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006419}
6420
6421static void nfs4_layoutcommit_release(void *calldata)
6422{
6423 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006424 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006425 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006426
Andy Adamsondb29c082011-07-30 20:52:38 -04006427 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006428 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006429 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6430 list_del_init(&lseg->pls_lc_list);
6431 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6432 &lseg->pls_flags))
6433 put_lseg(lseg);
6434 }
Peng Tao92407e72011-10-23 20:21:17 -07006435
6436 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6437 smp_mb__after_clear_bit();
6438 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6439
Andy Adamson863a3c62011-03-23 13:27:54 +00006440 put_rpccred(data->cred);
6441 kfree(data);
6442}
6443
6444static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6445 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6446 .rpc_call_done = nfs4_layoutcommit_done,
6447 .rpc_release = nfs4_layoutcommit_release,
6448};
6449
6450int
Andy Adamsonef311532011-03-12 02:58:10 -05006451nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006452{
6453 struct rpc_message msg = {
6454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6455 .rpc_argp = &data->args,
6456 .rpc_resp = &data->res,
6457 .rpc_cred = data->cred,
6458 };
6459 struct rpc_task_setup task_setup_data = {
6460 .task = &data->task,
6461 .rpc_client = NFS_CLIENT(data->args.inode),
6462 .rpc_message = &msg,
6463 .callback_ops = &nfs4_layoutcommit_ops,
6464 .callback_data = data,
6465 .flags = RPC_TASK_ASYNC,
6466 };
6467 struct rpc_task *task;
6468 int status = 0;
6469
6470 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6471 "lbw: %llu inode %lu\n",
6472 data->task.tk_pid, sync,
6473 data->args.lastbytewritten,
6474 data->args.inode->i_ino);
6475
Trond Myklebust9d12b212012-01-17 22:04:25 -05006476 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006477 task = rpc_run_task(&task_setup_data);
6478 if (IS_ERR(task))
6479 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006480 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006481 goto out;
6482 status = nfs4_wait_for_completion_rpc_task(task);
6483 if (status != 0)
6484 goto out;
6485 status = task->tk_status;
6486out:
6487 dprintk("%s: status %d\n", __func__, status);
6488 rpc_put_task(task);
6489 return status;
6490}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006491
6492static int
6493_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6494 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6495{
6496 struct nfs41_secinfo_no_name_args args = {
6497 .style = SECINFO_STYLE_CURRENT_FH,
6498 };
6499 struct nfs4_secinfo_res res = {
6500 .flavors = flavors,
6501 };
6502 struct rpc_message msg = {
6503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6504 .rpc_argp = &args,
6505 .rpc_resp = &res,
6506 };
6507 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6508}
6509
6510static int
6511nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6512 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6513{
6514 struct nfs4_exception exception = { };
6515 int err;
6516 do {
6517 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6518 switch (err) {
6519 case 0:
6520 case -NFS4ERR_WRONGSEC:
6521 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006522 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006523 default:
6524 err = nfs4_handle_exception(server, err, &exception);
6525 }
6526 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006527out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006528 return err;
6529}
6530
6531static int
6532nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6533 struct nfs_fsinfo *info)
6534{
6535 int err;
6536 struct page *page;
6537 rpc_authflavor_t flavor;
6538 struct nfs4_secinfo_flavors *flavors;
6539
6540 page = alloc_page(GFP_KERNEL);
6541 if (!page) {
6542 err = -ENOMEM;
6543 goto out;
6544 }
6545
6546 flavors = page_address(page);
6547 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6548
6549 /*
6550 * Fall back on "guess and check" method if
6551 * the server doesn't support SECINFO_NO_NAME
6552 */
6553 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6554 err = nfs4_find_root_sec(server, fhandle, info);
6555 goto out_freepage;
6556 }
6557 if (err)
6558 goto out_freepage;
6559
6560 flavor = nfs_find_best_sec(flavors);
6561 if (err == 0)
6562 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6563
6564out_freepage:
6565 put_page(page);
6566 if (err == -EACCES)
6567 return -EPERM;
6568out:
6569 return err;
6570}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006571
6572static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006573{
6574 int status;
6575 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006576 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006577 };
6578 struct nfs41_test_stateid_res res;
6579 struct rpc_message msg = {
6580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6581 .rpc_argp = &args,
6582 .rpc_resp = &res,
6583 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006584
Trond Myklebust9d12b212012-01-17 22:04:25 -05006585 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006586 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6587
6588 if (status == NFS_OK)
6589 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006590 return status;
6591}
6592
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006593static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006594{
6595 struct nfs4_exception exception = { };
6596 int err;
6597 do {
6598 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006599 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006600 &exception);
6601 } while (exception.retry);
6602 return err;
6603}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006604
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006605static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006606{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006607 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006608 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006609 };
6610 struct nfs41_free_stateid_res res;
6611 struct rpc_message msg = {
6612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6613 .rpc_argp = &args,
6614 .rpc_resp = &res,
6615 };
6616
Trond Myklebust9d12b212012-01-17 22:04:25 -05006617 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6618 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006619}
6620
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006621static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006622{
6623 struct nfs4_exception exception = { };
6624 int err;
6625 do {
6626 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006627 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006628 &exception);
6629 } while (exception.retry);
6630 return err;
6631}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006632
6633static bool nfs41_match_stateid(const nfs4_stateid *s1,
6634 const nfs4_stateid *s2)
6635{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006636 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006637 return false;
6638
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006639 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006640 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006641 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006642 return true;
6643
6644 return false;
6645}
6646
Andy Adamson557134a2009-04-01 09:21:53 -04006647#endif /* CONFIG_NFS_V4_1 */
6648
Trond Myklebust36281ca2012-03-04 18:13:56 -05006649static bool nfs4_match_stateid(const nfs4_stateid *s1,
6650 const nfs4_stateid *s2)
6651{
Trond Myklebustf597c532012-03-04 18:13:56 -05006652 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006653}
6654
6655
Trond Myklebust17280172012-03-11 13:11:00 -04006656static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006657 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006658 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 .recover_open = nfs4_open_reclaim,
6660 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006661 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006662 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663};
6664
Andy Adamson591d71c2009-04-01 09:22:47 -04006665#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006666static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006667 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6668 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6669 .recover_open = nfs4_open_reclaim,
6670 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006671 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006672 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006673 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006674};
6675#endif /* CONFIG_NFS_V4_1 */
6676
Trond Myklebust17280172012-03-11 13:11:00 -04006677static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006678 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006679 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 .recover_open = nfs4_open_expired,
6681 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006682 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006683 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684};
6685
Andy Adamson591d71c2009-04-01 09:22:47 -04006686#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006687static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006688 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6689 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006690 .recover_open = nfs41_open_expired,
6691 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006692 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006693 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006694};
6695#endif /* CONFIG_NFS_V4_1 */
6696
Trond Myklebust17280172012-03-11 13:11:00 -04006697static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006698 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006699 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006700 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006701};
6702
6703#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006704static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006705 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006706 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006707 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006708};
6709#endif
6710
Trond Myklebust97dc1352010-06-16 09:52:26 -04006711static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6712 .minor_version = 0,
6713 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006714 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006715 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006716 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6717 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6718 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006719};
6720
6721#if defined(CONFIG_NFS_V4_1)
6722static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6723 .minor_version = 1,
6724 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006725 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006726 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006727 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6728 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6729 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006730};
6731#endif
6732
Trond Myklebust97dc1352010-06-16 09:52:26 -04006733const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6734 [0] = &nfs_v4_0_minor_ops,
6735#if defined(CONFIG_NFS_V4_1)
6736 [1] = &nfs_v4_1_minor_ops,
6737#endif
6738};
6739
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006740static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006741 .permission = nfs_permission,
6742 .getattr = nfs_getattr,
6743 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006744 .getxattr = generic_getxattr,
6745 .setxattr = generic_setxattr,
6746 .listxattr = generic_listxattr,
6747 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006748};
6749
David Howells509de812006-08-22 20:06:11 -04006750const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 .version = 4, /* protocol version */
6752 .dentry_ops = &nfs4_dentry_operations,
6753 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006754 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006755 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006757 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 .getattr = nfs4_proc_getattr,
6759 .setattr = nfs4_proc_setattr,
6760 .lookup = nfs4_proc_lookup,
6761 .access = nfs4_proc_access,
6762 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 .create = nfs4_proc_create,
6764 .remove = nfs4_proc_remove,
6765 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006766 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 .unlink_done = nfs4_proc_unlink_done,
6768 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006769 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006770 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006771 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 .link = nfs4_proc_link,
6773 .symlink = nfs4_proc_symlink,
6774 .mkdir = nfs4_proc_mkdir,
6775 .rmdir = nfs4_proc_remove,
6776 .readdir = nfs4_proc_readdir,
6777 .mknod = nfs4_proc_mknod,
6778 .statfs = nfs4_proc_statfs,
6779 .fsinfo = nfs4_proc_fsinfo,
6780 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006781 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 .decode_dirent = nfs4_decode_dirent,
6783 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006784 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006785 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006787 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006788 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006790 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006791 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006793 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006794 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006795 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006796 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797};
6798
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006799static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6800 .prefix = XATTR_NAME_NFSV4_ACL,
6801 .list = nfs4_xattr_list_nfs4_acl,
6802 .get = nfs4_xattr_get_nfs4_acl,
6803 .set = nfs4_xattr_set_nfs4_acl,
6804};
6805
6806const struct xattr_handler *nfs4_xattr_handlers[] = {
6807 &nfs4_xattr_nfs4_acl_handler,
6808 NULL
6809};
6810
Trond Myklebustef159e92012-02-06 19:50:40 -05006811module_param(max_session_slots, ushort, 0644);
6812MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6813 "requests the client will negotiate");
6814
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815/*
6816 * Local variables:
6817 * c-basic-offset: 8
6818 * End:
6819 */