blob: f23977e4ac0f7b8e525f5687de78504b4fc31901 [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
Trond Myklebust2d0dbc62012-06-08 10:58:09 -04001905 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001907 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1908 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (status == 0)
1910 break;
1911 /* NOTE: BAD_SEQID means the server and client disagree about the
1912 * book-keeping w.r.t. state-changing operations
1913 * (OPEN/CLOSE/LOCK/LOCKU...)
1914 * It is actually a sign of a bug on the client or on the server.
1915 *
1916 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001917 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 * have unhashed the old state_owner for us, and that we can
1919 * therefore safely retry using a new one. We should still warn
1920 * the user though...
1921 */
1922 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001923 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001924 " returned a bad sequence-id error!\n",
1925 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 exception.retry = 1;
1927 continue;
1928 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001929 /*
1930 * BAD_STATEID on OPEN means that the server cancelled our
1931 * state before it received the OPEN_CONFIRM.
1932 * Recover by retrying the request as per the discussion
1933 * on Page 181 of RFC3530.
1934 */
1935 if (status == -NFS4ERR_BAD_STATEID) {
1936 exception.retry = 1;
1937 continue;
1938 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001939 if (status == -EAGAIN) {
1940 /* We must have found a delegation */
1941 exception.retry = 1;
1942 continue;
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1945 status, &exception));
1946 } while (exception.retry);
1947 return res;
1948}
1949
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001950static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1951 struct nfs_fattr *fattr, struct iattr *sattr,
1952 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001954 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001956 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 .iap = sattr,
1958 .server = server,
1959 .bitmask = server->attr_bitmask,
1960 };
1961 struct nfs_setattrres res = {
1962 .fattr = fattr,
1963 .server = server,
1964 };
1965 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1967 .rpc_argp = &arg,
1968 .rpc_resp = &res,
1969 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001971 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001972 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Trond Myklebust0e574af2005-10-27 22:12:38 -04001974 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Trond Myklebust4fc87962012-03-08 17:42:01 -05001976 if (state != NULL) {
1977 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1978 current->files, current->tgid);
1979 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1980 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001981 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001982 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001983 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Bryan Schumaker7c513052011-03-24 17:12:24 +00001985 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001986 if (status == 0 && state != NULL)
1987 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001988 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001991static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1992 struct nfs_fattr *fattr, struct iattr *sattr,
1993 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001995 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001996 struct nfs4_exception exception = {
1997 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001998 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 int err;
2001 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002002 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2003 switch (err) {
2004 case -NFS4ERR_OPENMODE:
2005 if (state && !(state->state & FMODE_WRITE)) {
2006 err = -EBADF;
2007 if (sattr->ia_valid & ATTR_OPEN)
2008 err = -EACCES;
2009 goto out;
2010 }
2011 }
2012 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002014out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return err;
2016}
2017
2018struct nfs4_closedata {
2019 struct inode *inode;
2020 struct nfs4_state *state;
2021 struct nfs_closeargs arg;
2022 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002023 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002024 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002025 bool roc;
2026 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027};
2028
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002029static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002030{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002031 struct nfs4_closedata *calldata = data;
2032 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002033 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002034
Fred Isamanf7e89172011-01-06 11:36:32 +00002035 if (calldata->roc)
2036 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002037 nfs4_put_open_state(calldata->state);
2038 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002039 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002040 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002041 kfree(calldata);
2042}
2043
Trond Myklebust88069f72009-12-08 08:33:16 -05002044static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2045 fmode_t fmode)
2046{
2047 spin_lock(&state->owner->so_lock);
2048 if (!(fmode & FMODE_READ))
2049 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2050 if (!(fmode & FMODE_WRITE))
2051 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2052 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2053 spin_unlock(&state->owner->so_lock);
2054}
2055
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002056static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002058 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct nfs_server *server = NFS_SERVER(calldata->inode);
2061
Chuck Levera3ca5652012-03-01 17:00:40 -05002062 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002063 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2064 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 /* hmm. we are done with the inode, and in the process of freeing
2066 * the state_owner. we keep this around to process errors
2067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 switch (task->tk_status) {
2069 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002070 if (calldata->roc)
2071 pnfs_roc_set_barrier(state->inode,
2072 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002073 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002074 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002075 nfs4_close_clear_stateid_flags(state,
2076 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 break;
2078 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002079 case -NFS4ERR_OLD_STATEID:
2080 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002082 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002085 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2086 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002088 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002089 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002090 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002093static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002095 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002096 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002097 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002098
Chuck Levera3ca5652012-03-01 17:00:40 -05002099 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002100 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002101 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002102
Trond Myklebust88069f72009-12-08 08:33:16 -05002103 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2104 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002105 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002106 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002107 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002108 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002109 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2110 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2111 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002112 }
2113 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002114 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2115 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2116 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002117 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002118 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002119 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002120
2121 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002122 /* Note: exit _without_ calling nfs4_close_done */
2123 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002124 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002125 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002126
Fred Isamanf7e89172011-01-06 11:36:32 +00002127 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002128 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002129 if (calldata->roc &&
2130 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2131 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2132 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002133 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002134 }
2135 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002136
Trond Myklebust516a6af2005-10-27 22:12:41 -04002137 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002138 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002139 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002140 &calldata->arg.seq_args,
2141 &calldata->res.seq_res,
2142 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002143 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002144 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002145out:
2146 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002149static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002150 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002151 .rpc_call_done = nfs4_close_done,
2152 .rpc_release = nfs4_free_closedata,
2153};
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155/*
2156 * It is possible for data to be read/written from a mem-mapped file
2157 * after the sys_close call (which hits the vfs layer as a flush).
2158 * This means that we can't safely call nfsv4 close on a file until
2159 * the inode is cleared. This in turn means that we are not good
2160 * NFSv4 citizens - we do not indicate to the server to update the file's
2161 * share state even when we are done with one of the three share
2162 * stateid's in the inode.
2163 *
2164 * NOTE: Caller must be holding the sp->so_owner semaphore!
2165 */
Al Viro643168c2011-06-22 18:20:23 -04002166int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002168 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002170 struct nfs4_state_owner *sp = state->owner;
2171 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2174 .rpc_cred = state->owner->so_cred,
2175 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 struct rpc_task_setup task_setup_data = {
2177 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002178 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002180 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002181 .flags = RPC_TASK_ASYNC,
2182 };
Trond Myklebust95121352005-10-18 14:20:12 -07002183 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002185 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002187 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002188 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002189 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002191 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002192 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002194 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002195 if (calldata->arg.seqid == NULL)
2196 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002197 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002198 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002199 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002200 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002201 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002202 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002203 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002204
Trond Myklebust1174dd12010-12-21 10:52:24 -05002205 msg.rpc_argp = &calldata->arg;
2206 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002207 task_setup_data.callback_data = calldata;
2208 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002209 if (IS_ERR(task))
2210 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002211 status = 0;
2212 if (wait)
2213 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002214 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002215 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002216out_free_calldata:
2217 kfree(calldata);
2218out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002219 if (roc)
2220 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002221 nfs4_put_open_state(state);
2222 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002223 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Trond Myklebust2b484292010-09-17 10:56:51 -04002226static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002227nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 struct nfs4_state *state;
2230
Trond Myklebust565277f2007-10-15 18:17:53 -04002231 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002232 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2233 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002234 if (IS_ERR(state))
2235 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002236 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002237 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Trond Myklebust1185a552009-12-03 15:54:02 -05002240static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002241{
2242 if (ctx->state == NULL)
2243 return;
2244 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002245 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002246 else
Al Viro643168c2011-06-22 18:20:23 -04002247 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2251{
Benny Halevy43652ad2009-04-01 09:21:54 -04002252 struct nfs4_server_caps_arg args = {
2253 .fhandle = fhandle,
2254 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 struct nfs4_server_caps_res res = {};
2256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002258 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .rpc_resp = &res,
2260 };
2261 int status;
2262
Bryan Schumaker7c513052011-03-24 17:12:24 +00002263 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (status == 0) {
2265 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002266 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2267 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2268 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2269 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2270 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2272 server->caps |= NFS_CAP_ACLS;
2273 if (res.has_links != 0)
2274 server->caps |= NFS_CAP_HARDLINKS;
2275 if (res.has_symlinks != 0)
2276 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002277 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2278 server->caps |= NFS_CAP_FILEID;
2279 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2280 server->caps |= NFS_CAP_MODE;
2281 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2282 server->caps |= NFS_CAP_NLINK;
2283 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2284 server->caps |= NFS_CAP_OWNER;
2285 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2286 server->caps |= NFS_CAP_OWNER_GROUP;
2287 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2288 server->caps |= NFS_CAP_ATIME;
2289 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2290 server->caps |= NFS_CAP_CTIME;
2291 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2292 server->caps |= NFS_CAP_MTIME;
2293
Trond Myklebusta65318b2009-03-11 14:10:28 -04002294 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2295 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2296 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002298 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return status;
2302}
2303
Trond Myklebust55a97592006-06-09 09:34:19 -04002304int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
2306 struct nfs4_exception exception = { };
2307 int err;
2308 do {
2309 err = nfs4_handle_exception(server,
2310 _nfs4_server_capabilities(server, fhandle),
2311 &exception);
2312 } while (exception.retry);
2313 return err;
2314}
2315
2316static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2317 struct nfs_fsinfo *info)
2318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 struct nfs4_lookup_root_arg args = {
2320 .bitmask = nfs4_fattr_bitmap,
2321 };
2322 struct nfs4_lookup_res res = {
2323 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002324 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 .fh = fhandle,
2326 };
2327 struct rpc_message msg = {
2328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2329 .rpc_argp = &args,
2330 .rpc_resp = &res,
2331 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002332
Trond Myklebust0e574af2005-10-27 22:12:38 -04002333 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
2337static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2338 struct nfs_fsinfo *info)
2339{
2340 struct nfs4_exception exception = { };
2341 int err;
2342 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002343 err = _nfs4_lookup_root(server, fhandle, info);
2344 switch (err) {
2345 case 0:
2346 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002347 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002348 default:
2349 err = nfs4_handle_exception(server, err, &exception);
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return err;
2354}
2355
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002356static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2357 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2358{
2359 struct rpc_auth *auth;
2360 int ret;
2361
2362 auth = rpcauth_create(flavor, server->client);
2363 if (!auth) {
2364 ret = -EIO;
2365 goto out;
2366 }
2367 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002368out:
2369 return ret;
2370}
2371
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002372static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002373 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002375 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002376 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002378 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2379 flav_array[len] = RPC_AUTH_NULL;
2380 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002381
2382 for (i = 0; i < len; i++) {
2383 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002384 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002385 continue;
2386 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002387 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002388 /*
2389 * -EACCESS could mean that the user doesn't have correct permissions
2390 * to access the mount. It could also mean that we tried to mount
2391 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2392 * existing mount programs don't handle -EACCES very well so it should
2393 * be mapped to -EPERM instead.
2394 */
2395 if (status == -EACCES)
2396 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002397 return status;
2398}
2399
2400/*
2401 * get the file handle for the "/" directory on the server
2402 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002403int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2404 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002405{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002406 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002407 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002408 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2409 /*
2410 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2411 * by nfs4_map_errors() as this function exits.
2412 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002413 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (status == 0)
2415 status = nfs4_server_capabilities(server, fhandle);
2416 if (status == 0)
2417 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002418 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Bryan Schumakerbae36242012-05-10 15:07:31 -04002421static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2422 struct nfs_fsinfo *info)
2423{
2424 int error;
2425 struct nfs_fattr *fattr = info->fattr;
2426
2427 error = nfs4_server_capabilities(server, mntfh);
2428 if (error < 0) {
2429 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2430 return error;
2431 }
2432
2433 error = nfs4_proc_getattr(server, mntfh, fattr);
2434 if (error < 0) {
2435 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2436 return error;
2437 }
2438
2439 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2440 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2441 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2442
2443 return error;
2444}
2445
Manoj Naik6b97fd32006-06-09 09:34:29 -04002446/*
2447 * Get locations and (maybe) other attributes of a referral.
2448 * Note that we'll actually follow the referral later when
2449 * we detect fsid mismatch in inode revalidation
2450 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002451static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2452 const struct qstr *name, struct nfs_fattr *fattr,
2453 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002454{
2455 int status = -ENOMEM;
2456 struct page *page = NULL;
2457 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002458
2459 page = alloc_page(GFP_KERNEL);
2460 if (page == NULL)
2461 goto out;
2462 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2463 if (locations == NULL)
2464 goto out;
2465
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002466 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002467 if (status != 0)
2468 goto out;
2469 /* Make sure server returned a different fsid for the referral */
2470 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002471 dprintk("%s: server did not return a different fsid for"
2472 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002473 status = -EIO;
2474 goto out;
2475 }
Andy Adamson533eb462011-06-13 18:25:56 -04002476 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2477 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002478
Andy Adamson533eb462011-06-13 18:25:56 -04002479 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002480 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002481 memset(fhandle, 0, sizeof(struct nfs_fh));
2482out:
2483 if (page)
2484 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002485 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002486 return status;
2487}
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2490{
2491 struct nfs4_getattr_arg args = {
2492 .fh = fhandle,
2493 .bitmask = server->attr_bitmask,
2494 };
2495 struct nfs4_getattr_res res = {
2496 .fattr = fattr,
2497 .server = server,
2498 };
2499 struct rpc_message msg = {
2500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2501 .rpc_argp = &args,
2502 .rpc_resp = &res,
2503 };
2504
Trond Myklebust0e574af2005-10-27 22:12:38 -04002505 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002506 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
2509static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2510{
2511 struct nfs4_exception exception = { };
2512 int err;
2513 do {
2514 err = nfs4_handle_exception(server,
2515 _nfs4_proc_getattr(server, fhandle, fattr),
2516 &exception);
2517 } while (exception.retry);
2518 return err;
2519}
2520
2521/*
2522 * The file is not closed if it is opened due to the a request to change
2523 * the size of the file. The open call will not be needed once the
2524 * VFS layer lookup-intents are implemented.
2525 *
2526 * Close is called when the inode is destroyed.
2527 * If we haven't opened the file for O_WRONLY, we
2528 * need to in the size_change case to obtain a stateid.
2529 *
2530 * Got race?
2531 * Because OPEN is always done by name in nfsv4, it is
2532 * possible that we opened a different file by the same
2533 * name. We can recognize this race condition, but we
2534 * can't do anything about it besides returning an error.
2535 *
2536 * This will be fixed with VFS changes (lookup-intent).
2537 */
2538static int
2539nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2540 struct iattr *sattr)
2541{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002542 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002543 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002544 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 int status;
2546
Benny Halevy8a1636c2010-07-14 15:43:57 -04002547 if (pnfs_ld_layoutret_on_setattr(inode))
2548 pnfs_return_layout(inode);
2549
Trond Myklebust0e574af2005-10-27 22:12:38 -04002550 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Trond Myklebustd5308382005-11-04 15:33:38 -05002552 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002553 if (sattr->ia_valid & ATTR_FILE) {
2554 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002555
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002556 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002557 if (ctx) {
2558 cred = ctx->cred;
2559 state = ctx->state;
2560 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002561 }
2562
Trond Myklebust536e43d2012-01-17 22:04:26 -05002563 /* Deal with open(O_TRUNC) */
2564 if (sattr->ia_valid & ATTR_OPEN)
2565 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2566
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002567 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002568 if (status == 0)
2569 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return status;
2571}
2572
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002573static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2574 const struct qstr *name, struct nfs_fh *fhandle,
2575 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002576{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002577 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002578 int status;
2579 struct nfs4_lookup_arg args = {
2580 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002581 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002582 .name = name,
2583 };
2584 struct nfs4_lookup_res res = {
2585 .server = server,
2586 .fattr = fattr,
2587 .fh = fhandle,
2588 };
2589 struct rpc_message msg = {
2590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2591 .rpc_argp = &args,
2592 .rpc_resp = &res,
2593 };
2594
2595 nfs_fattr_init(fattr);
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002598 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 dprintk("NFS reply lookup: %d\n", status);
2600 return status;
2601}
2602
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002603static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002604{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002605 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002606 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002607 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2608 fattr->nlink = 2;
2609}
2610
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002611static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2612 struct qstr *name, struct nfs_fh *fhandle,
2613 struct nfs_fattr *fattr)
2614{
2615 struct nfs4_exception exception = { };
2616 struct rpc_clnt *client = *clnt;
2617 int err;
2618 do {
2619 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2620 switch (err) {
2621 case -NFS4ERR_BADNAME:
2622 err = -ENOENT;
2623 goto out;
2624 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002625 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002626 goto out;
2627 case -NFS4ERR_WRONGSEC:
2628 err = -EPERM;
2629 if (client != *clnt)
2630 goto out;
2631
2632 client = nfs4_create_sec_client(client, dir, name);
2633 if (IS_ERR(client))
2634 return PTR_ERR(client);
2635
2636 exception.retry = 1;
2637 break;
2638 default:
2639 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2640 }
2641 } while (exception.retry);
2642
2643out:
2644 if (err == 0)
2645 *clnt = client;
2646 else if (client != *clnt)
2647 rpc_shutdown_client(client);
2648
2649 return err;
2650}
2651
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002652static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002653 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002655 int status;
2656 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002657
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002658 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2659 if (client != NFS_CLIENT(dir)) {
2660 rpc_shutdown_client(client);
2661 nfs_fixup_secinfo_attributes(fattr);
2662 }
2663 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002666struct rpc_clnt *
2667nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2668 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2669{
2670 int status;
2671 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2672
2673 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2674 if (status < 0) {
2675 rpc_shutdown_client(client);
2676 return ERR_PTR(status);
2677 }
2678 return client;
2679}
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2682{
Trond Myklebust76b32992007-08-10 17:45:11 -04002683 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 struct nfs4_accessargs args = {
2685 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002686 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002688 struct nfs4_accessres res = {
2689 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002690 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct rpc_message msg = {
2692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2693 .rpc_argp = &args,
2694 .rpc_resp = &res,
2695 .rpc_cred = entry->cred,
2696 };
2697 int mode = entry->mask;
2698 int status;
2699
2700 /*
2701 * Determine which access bits we want to ask for...
2702 */
2703 if (mode & MAY_READ)
2704 args.access |= NFS4_ACCESS_READ;
2705 if (S_ISDIR(inode->i_mode)) {
2706 if (mode & MAY_WRITE)
2707 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2708 if (mode & MAY_EXEC)
2709 args.access |= NFS4_ACCESS_LOOKUP;
2710 } else {
2711 if (mode & MAY_WRITE)
2712 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2713 if (mode & MAY_EXEC)
2714 args.access |= NFS4_ACCESS_EXECUTE;
2715 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002716
2717 res.fattr = nfs_alloc_fattr();
2718 if (res.fattr == NULL)
2719 return -ENOMEM;
2720
Bryan Schumaker7c513052011-03-24 17:12:24 +00002721 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (!status) {
2723 entry->mask = 0;
2724 if (res.access & NFS4_ACCESS_READ)
2725 entry->mask |= MAY_READ;
2726 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2727 entry->mask |= MAY_WRITE;
2728 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2729 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002730 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002732 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return status;
2734}
2735
2736static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2737{
2738 struct nfs4_exception exception = { };
2739 int err;
2740 do {
2741 err = nfs4_handle_exception(NFS_SERVER(inode),
2742 _nfs4_proc_access(inode, entry),
2743 &exception);
2744 } while (exception.retry);
2745 return err;
2746}
2747
2748/*
2749 * TODO: For the time being, we don't try to get any attributes
2750 * along with any of the zero-copy operations READ, READDIR,
2751 * READLINK, WRITE.
2752 *
2753 * In the case of the first three, we want to put the GETATTR
2754 * after the read-type operation -- this is because it is hard
2755 * to predict the length of a GETATTR response in v4, and thus
2756 * align the READ data correctly. This means that the GETATTR
2757 * may end up partially falling into the page cache, and we should
2758 * shift it into the 'tail' of the xdr_buf before processing.
2759 * To do this efficiently, we need to know the total length
2760 * of data received, which doesn't seem to be available outside
2761 * of the RPC layer.
2762 *
2763 * In the case of WRITE, we also want to put the GETATTR after
2764 * the operation -- in this case because we want to make sure
2765 * we get the post-operation mtime and size. This means that
2766 * we can't use xdr_encode_pages() as written: we need a variant
2767 * of it which would leave room in the 'tail' iovec.
2768 *
2769 * Both of these changes to the XDR layer would in fact be quite
2770 * minor, but I decided to leave them for a subsequent patch.
2771 */
2772static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2773 unsigned int pgbase, unsigned int pglen)
2774{
2775 struct nfs4_readlink args = {
2776 .fh = NFS_FH(inode),
2777 .pgbase = pgbase,
2778 .pglen = pglen,
2779 .pages = &page,
2780 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002781 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 struct rpc_message msg = {
2783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2784 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002785 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 };
2787
Bryan Schumaker7c513052011-03-24 17:12:24 +00002788 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 -07002789}
2790
2791static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2792 unsigned int pgbase, unsigned int pglen)
2793{
2794 struct nfs4_exception exception = { };
2795 int err;
2796 do {
2797 err = nfs4_handle_exception(NFS_SERVER(inode),
2798 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2799 &exception);
2800 } while (exception.retry);
2801 return err;
2802}
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804/*
2805 * Got race?
2806 * We will need to arrange for the VFS layer to provide an atomic open.
2807 * Until then, this create/open method is prone to inefficiency and race
2808 * conditions due to the lookup, create, and open VFS calls from sys_open()
2809 * placed on the wire.
2810 *
2811 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2812 * The file will be opened again in the subsequent VFS open call
2813 * (nfs4_proc_file_open).
2814 *
2815 * The open for read will just hang around to be used by any process that
2816 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2817 */
2818
2819static int
2820nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002821 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Al Viro82a2c1b2011-06-22 18:30:55 -04002823 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002825 struct rpc_cred *cred = NULL;
2826 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 int status = 0;
2828
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002829 if (ctx != NULL) {
2830 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002831 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002832 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002834 sattr->ia_mode &= ~current_umask();
Andy Adamson82be4172012-05-23 05:02:35 -04002835 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred, NULL);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002836 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (IS_ERR(state)) {
2838 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002839 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002841 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002842 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002843 if (ctx != NULL)
2844 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002845 else
Al Viro643168c2011-06-22 18:20:23 -04002846 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847out:
2848 return status;
2849}
2850
2851static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2852{
Trond Myklebust16e42952005-10-27 22:12:44 -04002853 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002854 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002856 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002858 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002859 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002860 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2863 .rpc_argp = &args,
2864 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002866 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002867
Bryan Schumaker7c513052011-03-24 17:12:24 +00002868 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002869 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002870 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return status;
2872}
2873
2874static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2875{
2876 struct nfs4_exception exception = { };
2877 int err;
2878 do {
2879 err = nfs4_handle_exception(NFS_SERVER(dir),
2880 _nfs4_proc_remove(dir, name),
2881 &exception);
2882 } while (exception.retry);
2883 return err;
2884}
2885
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002886static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002888 struct nfs_server *server = NFS_SERVER(dir);
2889 struct nfs_removeargs *args = msg->rpc_argp;
2890 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002892 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002894 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002897static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2898{
2899 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2900 &data->args.seq_args,
2901 &data->res.seq_res,
2902 task))
2903 return;
2904 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
2906
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002907static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002909 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2910
Trond Myklebust14516c32010-07-31 14:29:06 -04002911 if (!nfs4_sequence_done(task, &res->seq_res))
2912 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002913 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002914 return 0;
2915 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002916 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
Jeff Laytond3d41522010-09-17 17:31:57 -04002919static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2920{
2921 struct nfs_server *server = NFS_SERVER(dir);
2922 struct nfs_renameargs *arg = msg->rpc_argp;
2923 struct nfs_renameres *res = msg->rpc_resp;
2924
2925 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002926 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002927 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002928}
2929
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002930static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2931{
2932 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2933 &data->args.seq_args,
2934 &data->res.seq_res,
2935 task))
2936 return;
2937 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002938}
2939
2940static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2941 struct inode *new_dir)
2942{
2943 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2944
2945 if (!nfs4_sequence_done(task, &res->seq_res))
2946 return 0;
2947 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2948 return 0;
2949
2950 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002951 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002952 return 1;
2953}
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2956 struct inode *new_dir, struct qstr *new_name)
2957{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002958 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002959 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 .old_dir = NFS_FH(old_dir),
2961 .new_dir = NFS_FH(new_dir),
2962 .old_name = old_name,
2963 .new_name = new_name,
2964 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002965 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002966 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002967 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct rpc_message msg = {
2969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2970 .rpc_argp = &arg,
2971 .rpc_resp = &res,
2972 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002973 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Bryan Schumaker7c513052011-03-24 17:12:24 +00002975 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (!status) {
2977 update_changeattr(old_dir, &res.old_cinfo);
2978 update_changeattr(new_dir, &res.new_cinfo);
2979 }
2980 return status;
2981}
2982
2983static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2984 struct inode *new_dir, struct qstr *new_name)
2985{
2986 struct nfs4_exception exception = { };
2987 int err;
2988 do {
2989 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2990 _nfs4_proc_rename(old_dir, old_name,
2991 new_dir, new_name),
2992 &exception);
2993 } while (exception.retry);
2994 return err;
2995}
2996
2997static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2998{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002999 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct nfs4_link_arg arg = {
3001 .fh = NFS_FH(inode),
3002 .dir_fh = NFS_FH(dir),
3003 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003004 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003006 struct nfs4_link_res res = {
3007 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003008 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct rpc_message msg = {
3010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3011 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003012 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003014 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Trond Myklebust136f2622010-04-16 16:22:49 -04003016 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003017 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003018 goto out;
3019
Bryan Schumaker7c513052011-03-24 17:12:24 +00003020 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003021 if (!status) {
3022 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003023 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003024 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003025out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003026 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return status;
3028}
3029
3030static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3031{
3032 struct nfs4_exception exception = { };
3033 int err;
3034 do {
3035 err = nfs4_handle_exception(NFS_SERVER(inode),
3036 _nfs4_proc_link(inode, dir, name),
3037 &exception);
3038 } while (exception.retry);
3039 return err;
3040}
3041
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003042struct nfs4_createdata {
3043 struct rpc_message msg;
3044 struct nfs4_create_arg arg;
3045 struct nfs4_create_res res;
3046 struct nfs_fh fh;
3047 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048};
3049
3050static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3051 struct qstr *name, struct iattr *sattr, u32 ftype)
3052{
3053 struct nfs4_createdata *data;
3054
3055 data = kzalloc(sizeof(*data), GFP_KERNEL);
3056 if (data != NULL) {
3057 struct nfs_server *server = NFS_SERVER(dir);
3058
3059 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3060 data->msg.rpc_argp = &data->arg;
3061 data->msg.rpc_resp = &data->res;
3062 data->arg.dir_fh = NFS_FH(dir);
3063 data->arg.server = server;
3064 data->arg.name = name;
3065 data->arg.attrs = sattr;
3066 data->arg.ftype = ftype;
3067 data->arg.bitmask = server->attr_bitmask;
3068 data->res.server = server;
3069 data->res.fh = &data->fh;
3070 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072 }
3073 return data;
3074}
3075
3076static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3077{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003078 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003079 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003080 if (status == 0) {
3081 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003082 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3083 }
3084 return status;
3085}
3086
3087static void nfs4_free_createdata(struct nfs4_createdata *data)
3088{
3089 kfree(data);
3090}
3091
Chuck Lever4f390c12006-08-22 20:06:22 -04003092static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003093 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003095 struct nfs4_createdata *data;
3096 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Chuck Lever94a6d752006-08-22 20:06:23 -04003098 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003100
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 status = -ENOMEM;
3102 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3103 if (data == NULL)
3104 goto out;
3105
3106 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3107 data->arg.u.symlink.pages = &page;
3108 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110 status = nfs4_do_create(dir, dentry, data);
3111
3112 nfs4_free_createdata(data);
3113out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return status;
3115}
3116
Chuck Lever4f390c12006-08-22 20:06:22 -04003117static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003118 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 struct nfs4_exception exception = { };
3121 int err;
3122 do {
3123 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003124 _nfs4_proc_symlink(dir, dentry, page,
3125 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 &exception);
3127 } while (exception.retry);
3128 return err;
3129}
3130
3131static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3132 struct iattr *sattr)
3133{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003134 struct nfs4_createdata *data;
3135 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003137 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3138 if (data == NULL)
3139 goto out;
3140
3141 status = nfs4_do_create(dir, dentry, data);
3142
3143 nfs4_free_createdata(data);
3144out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return status;
3146}
3147
3148static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3149 struct iattr *sattr)
3150{
3151 struct nfs4_exception exception = { };
3152 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003153
3154 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 do {
3156 err = nfs4_handle_exception(NFS_SERVER(dir),
3157 _nfs4_proc_mkdir(dir, dentry, sattr),
3158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003164 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
3166 struct inode *dir = dentry->d_inode;
3167 struct nfs4_readdir_arg args = {
3168 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003169 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 .pgbase = 0,
3171 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003172 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003173 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 };
3175 struct nfs4_readdir_res res;
3176 struct rpc_message msg = {
3177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3178 .rpc_argp = &args,
3179 .rpc_resp = &res,
3180 .rpc_cred = cred,
3181 };
3182 int status;
3183
Harvey Harrison3110ff82008-05-02 13:42:44 -07003184 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003185 dentry->d_parent->d_name.name,
3186 dentry->d_name.name,
3187 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3189 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003190 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 -05003191 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003193 status += args.pgbase;
3194 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003195
3196 nfs_invalidate_atime(dir);
3197
Harvey Harrison3110ff82008-05-02 13:42:44 -07003198 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return status;
3200}
3201
3202static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003203 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 struct nfs4_exception exception = { };
3206 int err;
3207 do {
3208 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3209 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003210 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 &exception);
3212 } while (exception.retry);
3213 return err;
3214}
3215
3216static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3217 struct iattr *sattr, dev_t rdev)
3218{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003219 struct nfs4_createdata *data;
3220 int mode = sattr->ia_mode;
3221 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3224 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003225
3226 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3227 if (data == NULL)
3228 goto out;
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003231 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003233 data->arg.ftype = NF4BLK;
3234 data->arg.u.device.specdata1 = MAJOR(rdev);
3235 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
3237 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003238 data->arg.ftype = NF4CHR;
3239 data->arg.u.device.specdata1 = MAJOR(rdev);
3240 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243 status = nfs4_do_create(dir, dentry, data);
3244
3245 nfs4_free_createdata(data);
3246out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return status;
3248}
3249
3250static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3251 struct iattr *sattr, dev_t rdev)
3252{
3253 struct nfs4_exception exception = { };
3254 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003255
3256 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 do {
3258 err = nfs4_handle_exception(NFS_SERVER(dir),
3259 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3260 &exception);
3261 } while (exception.retry);
3262 return err;
3263}
3264
3265static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3266 struct nfs_fsstat *fsstat)
3267{
3268 struct nfs4_statfs_arg args = {
3269 .fh = fhandle,
3270 .bitmask = server->attr_bitmask,
3271 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003272 struct nfs4_statfs_res res = {
3273 .fsstat = fsstat,
3274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct rpc_message msg = {
3276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3277 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003278 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 };
3280
Trond Myklebust0e574af2005-10-27 22:12:38 -04003281 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003282 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
3285static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3286{
3287 struct nfs4_exception exception = { };
3288 int err;
3289 do {
3290 err = nfs4_handle_exception(server,
3291 _nfs4_proc_statfs(server, fhandle, fsstat),
3292 &exception);
3293 } while (exception.retry);
3294 return err;
3295}
3296
3297static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3298 struct nfs_fsinfo *fsinfo)
3299{
3300 struct nfs4_fsinfo_arg args = {
3301 .fh = fhandle,
3302 .bitmask = server->attr_bitmask,
3303 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003304 struct nfs4_fsinfo_res res = {
3305 .fsinfo = fsinfo,
3306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3309 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003310 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 };
3312
Bryan Schumaker7c513052011-03-24 17:12:24 +00003313 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
3316static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3317{
3318 struct nfs4_exception exception = { };
3319 int err;
3320
3321 do {
3322 err = nfs4_handle_exception(server,
3323 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
3329static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3330{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003331 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3333}
3334
3335static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3336 struct nfs_pathconf *pathconf)
3337{
3338 struct nfs4_pathconf_arg args = {
3339 .fh = fhandle,
3340 .bitmask = server->attr_bitmask,
3341 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003342 struct nfs4_pathconf_res res = {
3343 .pathconf = pathconf,
3344 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 struct rpc_message msg = {
3346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3347 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003348 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 };
3350
3351 /* None of the pathconf attributes are mandatory to implement */
3352 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3353 memset(pathconf, 0, sizeof(*pathconf));
3354 return 0;
3355 }
3356
Trond Myklebust0e574af2005-10-27 22:12:38 -04003357 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003358 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
3361static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3362 struct nfs_pathconf *pathconf)
3363{
3364 struct nfs4_exception exception = { };
3365 int err;
3366
3367 do {
3368 err = nfs4_handle_exception(server,
3369 _nfs4_proc_pathconf(server, fhandle, pathconf),
3370 &exception);
3371 } while (exception.retry);
3372 return err;
3373}
3374
Benny Halevyd20581a2011-05-22 19:52:03 +03003375void __nfs4_read_done_cb(struct nfs_read_data *data)
3376{
Fred Isamancd841602012-04-20 14:47:44 -04003377 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003378}
3379
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003380static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
Fred Isamancd841602012-04-20 14:47:44 -04003382 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003384 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003385 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003386 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003388
Benny Halevyd20581a2011-05-22 19:52:03 +03003389 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003391 renew_lease(server, data->timestamp);
3392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003395static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3396{
3397
3398 dprintk("--> %s\n", __func__);
3399
3400 if (!nfs4_sequence_done(task, &data->res.seq_res))
3401 return -EAGAIN;
3402
Benny Halevyd20581a2011-05-22 19:52:03 +03003403 return data->read_done_cb ? data->read_done_cb(task, data) :
3404 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003405}
3406
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003407static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003410 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003411 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003412 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003415static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3416{
Fred Isamancd841602012-04-20 14:47:44 -04003417 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003418 &data->args.seq_args,
3419 &data->res.seq_res,
3420 task))
3421 return;
3422 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Fred Isamanb029bc92011-03-03 15:13:42 +00003425static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Fred Isamancd841602012-04-20 14:47:44 -04003427 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003429 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003430 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003431 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003433 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003435 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003436 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
Fred Isamanb029bc92011-03-03 15:13:42 +00003440static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3441{
3442 if (!nfs4_sequence_done(task, &data->res.seq_res))
3443 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003444 return data->write_done_cb ? data->write_done_cb(task, data) :
3445 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003446}
3447
Trond Myklebust5a37f852012-04-28 14:55:16 -04003448static
3449bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003450{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003451 const struct nfs_pgio_header *hdr = data->header;
3452
3453 /* Don't request attributes for pNFS or O_DIRECT writes */
3454 if (data->ds_clp != NULL || hdr->dreq != NULL)
3455 return false;
3456 /* Otherwise, request attributes if and only if we don't hold
3457 * a delegation
3458 */
3459 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003460}
Fred Isamana69aef12011-03-03 15:13:47 +00003461
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003462static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
Fred Isamancd841602012-04-20 14:47:44 -04003464 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003465
Trond Myklebust5a37f852012-04-28 14:55:16 -04003466 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003467 data->args.bitmask = NULL;
3468 data->res.fattr = NULL;
3469 } else
3470 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003471
Fred Isamanb029bc92011-03-03 15:13:42 +00003472 if (!data->write_done_cb)
3473 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003474 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 data->timestamp = jiffies;
3476
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003477 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003478 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003481static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3482{
Fred Isamancd841602012-04-20 14:47:44 -04003483 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003484 &data->args.seq_args,
3485 &data->res.seq_res,
3486 task))
3487 return;
3488 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Fred Isaman0b7c0152012-04-20 14:47:39 -04003491static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3492{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3494 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003495 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return;
3498 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003499}
3500
Fred Isaman0b7c0152012-04-20 14:47:39 -04003501static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
3503 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003504
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003505 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003506 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003507 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Fred Isaman0b7c0152012-04-20 14:47:39 -04003512static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003513{
3514 if (!nfs4_sequence_done(task, &data->res.seq_res))
3515 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003516 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003517}
3518
Fred Isaman0b7c0152012-04-20 14:47:39 -04003519static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003521 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003522
Fred Isaman0b7c0152012-04-20 14:47:39 -04003523 if (data->commit_done_cb == NULL)
3524 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003525 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003526 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003527 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003530struct nfs4_renewdata {
3531 struct nfs_client *client;
3532 unsigned long timestamp;
3533};
3534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535/*
3536 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3537 * standalone procedure for queueing an asynchronous RENEW.
3538 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003539static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003540{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003541 struct nfs4_renewdata *data = calldata;
3542 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003543
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003544 if (atomic_read(&clp->cl_count) > 1)
3545 nfs4_schedule_state_renewal(clp);
3546 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003547 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003548}
3549
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003550static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003552 struct nfs4_renewdata *data = calldata;
3553 struct nfs_client *clp = data->client;
3554 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003557 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003558 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3559 return;
3560 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003561 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003562 return;
3563 }
3564 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003566 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003569static const struct rpc_call_ops nfs4_renew_ops = {
3570 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003571 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003572};
3573
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003574static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
3576 struct rpc_message msg = {
3577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3578 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003579 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003581 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003583 if (renew_flags == 0)
3584 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003585 if (!atomic_inc_not_zero(&clp->cl_count))
3586 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003587 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003588 if (data == NULL)
3589 return -ENOMEM;
3590 data->client = clp;
3591 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003593 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003596static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
3598 struct rpc_message msg = {
3599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3600 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003601 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 };
3603 unsigned long now = jiffies;
3604 int status;
3605
3606 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3607 if (status < 0)
3608 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003609 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 return 0;
3611}
3612
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003613static inline int nfs4_server_supports_acls(struct nfs_server *server)
3614{
3615 return (server->caps & NFS_CAP_ACLS)
3616 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3617 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3618}
3619
3620/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3621 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3622 * the stack.
3623 */
3624#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3625
Neil Hormane9e3d722011-03-04 19:26:03 -05003626static int buf_to_pages_noslab(const void *buf, size_t buflen,
3627 struct page **pages, unsigned int *pgbase)
3628{
3629 struct page *newpage, **spages;
3630 int rc = 0;
3631 size_t len;
3632 spages = pages;
3633
3634 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003635 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003636 newpage = alloc_page(GFP_KERNEL);
3637
3638 if (newpage == NULL)
3639 goto unwind;
3640 memcpy(page_address(newpage), buf, len);
3641 buf += len;
3642 buflen -= len;
3643 *pages++ = newpage;
3644 rc++;
3645 } while (buflen != 0);
3646
3647 return rc;
3648
3649unwind:
3650 for(; rc > 0; rc--)
3651 __free_page(spages[rc-1]);
3652 return -ENOMEM;
3653}
3654
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003655struct nfs4_cached_acl {
3656 int cached;
3657 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003658 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003659};
3660
3661static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003662{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003663 struct nfs_inode *nfsi = NFS_I(inode);
3664
3665 spin_lock(&inode->i_lock);
3666 kfree(nfsi->nfs4_acl);
3667 nfsi->nfs4_acl = acl;
3668 spin_unlock(&inode->i_lock);
3669}
3670
3671static void nfs4_zap_acl_attr(struct inode *inode)
3672{
3673 nfs4_set_cached_acl(inode, NULL);
3674}
3675
3676static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3677{
3678 struct nfs_inode *nfsi = NFS_I(inode);
3679 struct nfs4_cached_acl *acl;
3680 int ret = -ENOENT;
3681
3682 spin_lock(&inode->i_lock);
3683 acl = nfsi->nfs4_acl;
3684 if (acl == NULL)
3685 goto out;
3686 if (buf == NULL) /* user is just asking for length */
3687 goto out_len;
3688 if (acl->cached == 0)
3689 goto out;
3690 ret = -ERANGE; /* see getxattr(2) man page */
3691 if (acl->len > buflen)
3692 goto out;
3693 memcpy(buf, acl->data, acl->len);
3694out_len:
3695 ret = acl->len;
3696out:
3697 spin_unlock(&inode->i_lock);
3698 return ret;
3699}
3700
Sachin Prabhu5794d212012-04-17 14:36:40 +01003701static 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 +00003702{
3703 struct nfs4_cached_acl *acl;
3704
Sachin Prabhu5794d212012-04-17 14:36:40 +01003705 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003706 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3707 if (acl == NULL)
3708 goto out;
3709 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003710 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003711 } else {
3712 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3713 if (acl == NULL)
3714 goto out;
3715 acl->cached = 0;
3716 }
3717 acl->len = acl_len;
3718out:
3719 nfs4_set_cached_acl(inode, acl);
3720}
3721
Andy Adamsonbf118a32011-12-07 11:55:27 -05003722/*
3723 * The getxattr API returns the required buffer length when called with a
3724 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3725 * the required buf. On a NULL buf, we send a page of data to the server
3726 * guessing that the ACL request can be serviced by a page. If so, we cache
3727 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3728 * the cache. If not so, we throw away the page, and cache the required
3729 * length. The next getxattr call will then produce another round trip to
3730 * the server, this time with the input buf of the required size.
3731 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003732static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003733{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003734 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003735 struct nfs_getaclargs args = {
3736 .fh = NFS_FH(inode),
3737 .acl_pages = pages,
3738 .acl_len = buflen,
3739 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003740 struct nfs_getaclres res = {
3741 .acl_len = buflen,
3742 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003743 struct rpc_message msg = {
3744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3745 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003746 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003747 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003748 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003749
Andy Adamsonbf118a32011-12-07 11:55:27 -05003750 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3751 /* As long as we're doing a round trip to the server anyway,
3752 * let's be prepared for a page of acl data. */
3753 if (npages == 0)
3754 npages = 1;
3755
Sachin Prabhu5a006892012-04-17 14:35:39 +01003756 /* Add an extra page to handle the bitmap returned */
3757 npages++;
3758
Andy Adamsonbf118a32011-12-07 11:55:27 -05003759 for (i = 0; i < npages; i++) {
3760 pages[i] = alloc_page(GFP_KERNEL);
3761 if (!pages[i])
3762 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003763 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003764
3765 /* for decoding across pages */
3766 res.acl_scratch = alloc_page(GFP_KERNEL);
3767 if (!res.acl_scratch)
3768 goto out_free;
3769
Andy Adamsonbf118a32011-12-07 11:55:27 -05003770 args.acl_len = npages * PAGE_SIZE;
3771 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003772
Andy Adamsonbf118a32011-12-07 11:55:27 -05003773 /* Let decode_getfacl know not to fail if the ACL data is larger than
3774 * the page we send as a guess */
3775 if (buf == NULL)
3776 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003777
Peng Taode040be2012-01-10 22:42:47 +08003778 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003779 __func__, buf, buflen, npages, args.acl_len);
3780 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3781 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003782 if (ret)
3783 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003784
3785 acl_len = res.acl_len - res.acl_data_offset;
3786 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003787 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003788 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003789 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003790 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003791 if (buf) {
3792 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003793 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003794 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003795 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003796 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003797 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003798 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003799out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003800 for (i = 0; i < npages; i++)
3801 if (pages[i])
3802 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003803 if (res.acl_scratch)
3804 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003805 return ret;
3806}
3807
Trond Myklebust16b42892006-08-24 12:27:15 -04003808static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3809{
3810 struct nfs4_exception exception = { };
3811 ssize_t ret;
3812 do {
3813 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3814 if (ret >= 0)
3815 break;
3816 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3817 } while (exception.retry);
3818 return ret;
3819}
3820
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003821static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3822{
3823 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003824 int ret;
3825
3826 if (!nfs4_server_supports_acls(server))
3827 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828 ret = nfs_revalidate_inode(server, inode);
3829 if (ret < 0)
3830 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003831 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3832 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003833 ret = nfs4_read_cached_acl(inode, buf, buflen);
3834 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003835 /* -ENOENT is returned if there is no ACL or if there is an ACL
3836 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003837 return ret;
3838 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003839}
3840
Trond Myklebust16b42892006-08-24 12:27:15 -04003841static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003842{
3843 struct nfs_server *server = NFS_SERVER(inode);
3844 struct page *pages[NFS4ACL_MAXPAGES];
3845 struct nfs_setaclargs arg = {
3846 .fh = NFS_FH(inode),
3847 .acl_pages = pages,
3848 .acl_len = buflen,
3849 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003850 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003851 struct rpc_message msg = {
3852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3853 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003854 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003855 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003856 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003857
3858 if (!nfs4_server_supports_acls(server))
3859 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003860 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3861 if (i < 0)
3862 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003863 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003864 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003865
3866 /*
3867 * Free each page after tx, so the only ref left is
3868 * held by the network stack
3869 */
3870 for (; i > 0; i--)
3871 put_page(pages[i-1]);
3872
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003873 /*
3874 * Acl update can result in inode attribute update.
3875 * so mark the attribute cache invalid.
3876 */
3877 spin_lock(&inode->i_lock);
3878 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3879 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003880 nfs_access_zap_cache(inode);
3881 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003882 return ret;
3883}
3884
Trond Myklebust16b42892006-08-24 12:27:15 -04003885static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3886{
3887 struct nfs4_exception exception = { };
3888 int err;
3889 do {
3890 err = nfs4_handle_exception(NFS_SERVER(inode),
3891 __nfs4_proc_set_acl(inode, buf, buflen),
3892 &exception);
3893 } while (exception.retry);
3894 return err;
3895}
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003898nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003900 struct nfs_client *clp = server->nfs_client;
3901
3902 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return 0;
3904 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003905 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003906 case -NFS4ERR_ADMIN_REVOKED:
3907 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003908 if (state == NULL)
3909 break;
3910 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003911 case -NFS4ERR_OPENMODE:
3912 if (state == NULL)
3913 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003914 nfs4_schedule_stateid_recovery(server, state);
3915 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003916 case -NFS4ERR_EXPIRED:
3917 if (state != NULL)
3918 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003920 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003921 nfs4_schedule_lease_recovery(clp);
3922 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003923#if defined(CONFIG_NFS_V4_1)
3924 case -NFS4ERR_BADSESSION:
3925 case -NFS4ERR_BADSLOT:
3926 case -NFS4ERR_BAD_HIGH_SLOT:
3927 case -NFS4ERR_DEADSESSION:
3928 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3929 case -NFS4ERR_SEQ_FALSE_RETRY:
3930 case -NFS4ERR_SEQ_MISORDERED:
3931 dprintk("%s ERROR %d, Reset session\n", __func__,
3932 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003933 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003934 task->tk_status = 0;
3935 return -EAGAIN;
3936#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003938 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003939 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003940 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3942 task->tk_status = 0;
3943 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003944 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 case -NFS4ERR_OLD_STATEID:
3946 task->tk_status = 0;
3947 return -EAGAIN;
3948 }
3949 task->tk_status = nfs4_map_errors(task->tk_status);
3950 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003951wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003952 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003953 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3954 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3955 task->tk_status = 0;
3956 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957}
3958
Chuck Leverf0920752012-05-21 22:45:41 -04003959static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3960 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003961{
3962 __be32 verf[2];
3963
Chuck Lever2c820d92012-05-21 22:45:33 -04003964 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3965 /* An impossible timestamp guarantees this value
3966 * will never match a generated boot time. */
3967 verf[0] = 0;
3968 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3969 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003970 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3971 verf[0] = (__be32)nn->boot_time.tv_sec;
3972 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003973 }
Chuck Levercd937102012-03-02 17:14:31 -05003974 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3975}
3976
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003977int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3978 unsigned short port, struct rpc_cred *cred,
3979 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
3981 nfs4_verifier sc_verifier;
3982 struct nfs4_setclientid setclientid = {
3983 .sc_verifier = &sc_verifier,
3984 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003985 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 };
3987 struct rpc_message msg = {
3988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3989 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003990 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003991 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 int loop = 0;
3994 int status;
3995
Chuck Leverf0920752012-05-21 22:45:41 -04003996 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
3998 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003999 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05004001 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004002 clp->cl_ipaddr,
4003 rpc_peeraddr2str(clp->cl_rpcclient,
4004 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05004005 rpc_peeraddr2str(clp->cl_rpcclient,
4006 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04004007 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 clp->cl_id_uniquifier);
4009 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004010 sizeof(setclientid.sc_netid),
4011 rpc_peeraddr2str(clp->cl_rpcclient,
4012 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004014 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05004016 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004018 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (status != -NFS4ERR_CLID_INUSE)
4020 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004021 if (loop != 0) {
4022 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004024 }
4025 ++loop;
4026 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028 return status;
4029}
4030
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004031int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004032 struct nfs4_setclientid_res *arg,
4033 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
4035 struct nfs_fsinfo fsinfo;
4036 struct rpc_message msg = {
4037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004038 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004040 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 };
4042 unsigned long now;
4043 int status;
4044
4045 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004046 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 if (status == 0) {
4048 spin_lock(&clp->cl_lock);
4049 clp->cl_lease_time = fsinfo.lease_time * HZ;
4050 clp->cl_last_renewal = now;
4051 spin_unlock(&clp->cl_lock);
4052 }
4053 return status;
4054}
4055
Trond Myklebustfe650402006-01-03 09:55:18 +01004056struct nfs4_delegreturndata {
4057 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004058 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004059 struct nfs_fh fh;
4060 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004061 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004062 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004063 int rpc_status;
4064};
4065
Trond Myklebustfe650402006-01-03 09:55:18 +01004066static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4067{
4068 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004069
Trond Myklebust14516c32010-07-31 14:29:06 -04004070 if (!nfs4_sequence_done(task, &data->res.seq_res))
4071 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004072
Ricardo Labiaga79708862009-12-07 09:23:21 -05004073 switch (task->tk_status) {
4074 case -NFS4ERR_STALE_STATEID:
4075 case -NFS4ERR_EXPIRED:
4076 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004077 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004078 break;
4079 default:
4080 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4081 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004082 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004083 return;
4084 }
4085 }
4086 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004087}
4088
4089static void nfs4_delegreturn_release(void *calldata)
4090{
Trond Myklebustfe650402006-01-03 09:55:18 +01004091 kfree(calldata);
4092}
4093
Andy Adamson938e1012009-04-01 09:22:28 -04004094#if defined(CONFIG_NFS_V4_1)
4095static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4096{
4097 struct nfs4_delegreturndata *d_data;
4098
4099 d_data = (struct nfs4_delegreturndata *)data;
4100
Trond Myklebust035168a2010-06-16 09:52:26 -04004101 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004102 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004103 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004104 return;
4105 rpc_call_start(task);
4106}
4107#endif /* CONFIG_NFS_V4_1 */
4108
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004109static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004110#if defined(CONFIG_NFS_V4_1)
4111 .rpc_call_prepare = nfs4_delegreturn_prepare,
4112#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004113 .rpc_call_done = nfs4_delegreturn_done,
4114 .rpc_release = nfs4_delegreturn_release,
4115};
4116
Trond Myklebuste6f81072008-01-24 18:14:34 -05004117static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004118{
4119 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004120 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004121 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004122 struct rpc_message msg = {
4123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4124 .rpc_cred = cred,
4125 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004126 struct rpc_task_setup task_setup_data = {
4127 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004128 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004129 .callback_ops = &nfs4_delegreturn_ops,
4130 .flags = RPC_TASK_ASYNC,
4131 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004132 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004133
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004134 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004135 if (data == NULL)
4136 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004137 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004138 data->args.fhandle = &data->fh;
4139 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004140 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004141 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004142 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004143 data->res.fattr = &data->fattr;
4144 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004145 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004146 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004147 data->rpc_status = 0;
4148
Trond Myklebustc970aa82007-07-14 15:39:59 -04004149 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004150 msg.rpc_argp = &data->args;
4151 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004152 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004153 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004154 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004155 if (!issync)
4156 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004157 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004158 if (status != 0)
4159 goto out;
4160 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004161 if (status == 0)
4162 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4163 else
4164 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004165out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004166 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004167 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168}
4169
Trond Myklebuste6f81072008-01-24 18:14:34 -05004170int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
4172 struct nfs_server *server = NFS_SERVER(inode);
4173 struct nfs4_exception exception = { };
4174 int err;
4175 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004176 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 switch (err) {
4178 case -NFS4ERR_STALE_STATEID:
4179 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 case 0:
4181 return 0;
4182 }
4183 err = nfs4_handle_exception(server, err, &exception);
4184 } while (exception.retry);
4185 return err;
4186}
4187
4188#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4189#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4190
4191/*
4192 * sleep, with exponential backoff, and retry the LOCK operation.
4193 */
4194static unsigned long
4195nfs4_set_lock_task_retry(unsigned long timeout)
4196{
Jeff Laytond3103102011-12-01 22:44:39 +01004197 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 timeout <<= 1;
4199 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4200 return NFS4_LOCK_MAXTIMEOUT;
4201 return timeout;
4202}
4203
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4205{
4206 struct inode *inode = state->inode;
4207 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004208 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004209 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004211 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004213 struct nfs_lockt_res res = {
4214 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 };
4216 struct rpc_message msg = {
4217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4218 .rpc_argp = &arg,
4219 .rpc_resp = &res,
4220 .rpc_cred = state->owner->so_cred,
4221 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 struct nfs4_lock_state *lsp;
4223 int status;
4224
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004225 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004226 status = nfs4_set_lock_state(state, request);
4227 if (status != 0)
4228 goto out;
4229 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004230 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004231 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004232 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004233 switch (status) {
4234 case 0:
4235 request->fl_type = F_UNLCK;
4236 break;
4237 case -NFS4ERR_DENIED:
4238 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004240 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004241out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 return status;
4243}
4244
4245static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4246{
4247 struct nfs4_exception exception = { };
4248 int err;
4249
4250 do {
4251 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4252 _nfs4_proc_getlk(state, cmd, request),
4253 &exception);
4254 } while (exception.retry);
4255 return err;
4256}
4257
4258static int do_vfs_lock(struct file *file, struct file_lock *fl)
4259{
4260 int res = 0;
4261 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4262 case FL_POSIX:
4263 res = posix_lock_file_wait(file, fl);
4264 break;
4265 case FL_FLOCK:
4266 res = flock_lock_file_wait(file, fl);
4267 break;
4268 default:
4269 BUG();
4270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 return res;
4272}
4273
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004274struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004275 struct nfs_locku_args arg;
4276 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004277 struct nfs4_lock_state *lsp;
4278 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004279 struct file_lock fl;
4280 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004281 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004282};
4283
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004284static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4285 struct nfs_open_context *ctx,
4286 struct nfs4_lock_state *lsp,
4287 struct nfs_seqid *seqid)
4288{
4289 struct nfs4_unlockdata *p;
4290 struct inode *inode = lsp->ls_state->inode;
4291
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004292 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004293 if (p == NULL)
4294 return NULL;
4295 p->arg.fh = NFS_FH(inode);
4296 p->arg.fl = &p->fl;
4297 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004298 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004299 p->arg.stateid = &lsp->ls_stateid;
4300 p->lsp = lsp;
4301 atomic_inc(&lsp->ls_count);
4302 /* Ensure we don't close file until we're done freeing locks! */
4303 p->ctx = get_nfs_open_context(ctx);
4304 memcpy(&p->fl, fl, sizeof(p->fl));
4305 p->server = NFS_SERVER(inode);
4306 return p;
4307}
4308
Trond Myklebust06f814a2006-01-03 09:55:07 +01004309static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004310{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004311 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004312 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004313 nfs4_put_lock_state(calldata->lsp);
4314 put_nfs_open_context(calldata->ctx);
4315 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316}
4317
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004318static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004319{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004320 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321
Trond Myklebust14516c32010-07-31 14:29:06 -04004322 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4323 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004324 switch (task->tk_status) {
4325 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004326 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4327 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004328 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004329 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004330 case -NFS4ERR_BAD_STATEID:
4331 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004332 case -NFS4ERR_STALE_STATEID:
4333 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004334 break;
4335 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004336 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004337 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004338 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004339}
4340
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004341static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004342{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004343 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004345 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004346 return;
4347 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004348 /* Note: exit _without_ running nfs4_locku_done */
4349 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004350 return;
4351 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004352 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004353 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004354 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004355 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004356 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004357 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004360static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004361 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004362 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004363 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004364};
4365
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004366static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4367 struct nfs_open_context *ctx,
4368 struct nfs4_lock_state *lsp,
4369 struct nfs_seqid *seqid)
4370{
4371 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004372 struct rpc_message msg = {
4373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4374 .rpc_cred = ctx->cred,
4375 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004376 struct rpc_task_setup task_setup_data = {
4377 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004378 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004379 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004380 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004381 .flags = RPC_TASK_ASYNC,
4382 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383
Frank Filz137d6ac2007-07-09 15:32:29 -07004384 /* Ensure this is an unlock - when canceling a lock, the
4385 * canceled lock is passed in, and it won't be an unlock.
4386 */
4387 fl->fl_type = F_UNLCK;
4388
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004389 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4390 if (data == NULL) {
4391 nfs_free_seqid(seqid);
4392 return ERR_PTR(-ENOMEM);
4393 }
4394
Trond Myklebust9d12b212012-01-17 22:04:25 -05004395 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004396 msg.rpc_argp = &data->arg;
4397 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004398 task_setup_data.callback_data = data;
4399 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004400}
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4403{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004404 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004405 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004407 struct rpc_task *task;
4408 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004409 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Trond Myklebust9b073572006-06-29 16:38:34 -04004411 status = nfs4_set_lock_state(state, request);
4412 /* Unlock _before_ we do the RPC call */
4413 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004414 down_read(&nfsi->rwsem);
4415 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4416 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004417 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004418 }
4419 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004420 if (status != 0)
4421 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004422 /* Is this a delegated lock? */
4423 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004424 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004425 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004426 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004427 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004428 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004429 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004430 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004431 status = PTR_ERR(task);
4432 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004433 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004434 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004435 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004436out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004437 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439}
4440
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004441struct nfs4_lockdata {
4442 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004443 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004444 struct nfs4_lock_state *lsp;
4445 struct nfs_open_context *ctx;
4446 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004447 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004448 int rpc_status;
4449 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004450 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004451};
4452
4453static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004454 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4455 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004456{
4457 struct nfs4_lockdata *p;
4458 struct inode *inode = lsp->ls_state->inode;
4459 struct nfs_server *server = NFS_SERVER(inode);
4460
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004461 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004462 if (p == NULL)
4463 return NULL;
4464
4465 p->arg.fh = NFS_FH(inode);
4466 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004467 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004468 if (p->arg.open_seqid == NULL)
4469 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004470 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004471 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004472 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004473 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004474 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004475 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004476 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004477 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004478 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004479 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004480 atomic_inc(&lsp->ls_count);
4481 p->ctx = get_nfs_open_context(ctx);
4482 memcpy(&p->fl, fl, sizeof(p->fl));
4483 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004484out_free_seqid:
4485 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004486out_free:
4487 kfree(p);
4488 return NULL;
4489}
4490
4491static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4492{
4493 struct nfs4_lockdata *data = calldata;
4494 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Harvey Harrison3110ff82008-05-02 13:42:44 -07004496 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004497 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4498 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004499 /* Do we need to do an open_to_lock_owner? */
4500 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004501 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4502 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503 data->arg.open_stateid = &state->stateid;
4504 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004505 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004506 } else
4507 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004508 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004509 if (nfs4_setup_sequence(data->server,
4510 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004511 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004512 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004513 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004514 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515}
4516
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004517static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4518{
4519 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4520 nfs4_lock_prepare(task, calldata);
4521}
4522
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4524{
4525 struct nfs4_lockdata *data = calldata;
4526
Harvey Harrison3110ff82008-05-02 13:42:44 -07004527 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004528
Trond Myklebust14516c32010-07-31 14:29:06 -04004529 if (!nfs4_sequence_done(task, &data->res.seq_res))
4530 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004531
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004534 if (data->rpc_status == 0)
4535 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4536 else
4537 goto out;
4538 }
4539 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004540 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004541 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004542 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004543 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004544out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004545 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004546}
4547
4548static void nfs4_lock_release(void *calldata)
4549{
4550 struct nfs4_lockdata *data = calldata;
4551
Harvey Harrison3110ff82008-05-02 13:42:44 -07004552 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004553 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554 if (data->cancelled != 0) {
4555 struct rpc_task *task;
4556 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4557 data->arg.lock_seqid);
4558 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004559 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004560 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561 } else
4562 nfs_free_seqid(data->arg.lock_seqid);
4563 nfs4_put_lock_state(data->lsp);
4564 put_nfs_open_context(data->ctx);
4565 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004566 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567}
4568
4569static const struct rpc_call_ops nfs4_lock_ops = {
4570 .rpc_call_prepare = nfs4_lock_prepare,
4571 .rpc_call_done = nfs4_lock_done,
4572 .rpc_release = nfs4_lock_release,
4573};
4574
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004575static const struct rpc_call_ops nfs4_recover_lock_ops = {
4576 .rpc_call_prepare = nfs4_recover_lock_prepare,
4577 .rpc_call_done = nfs4_lock_done,
4578 .rpc_release = nfs4_lock_release,
4579};
4580
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004581static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4582{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004583 switch (error) {
4584 case -NFS4ERR_ADMIN_REVOKED:
4585 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004586 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004587 if (new_lock_owner != 0 ||
4588 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004589 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004590 break;
4591 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004592 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004593 case -NFS4ERR_EXPIRED:
4594 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004595 };
4596}
4597
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004598static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004599{
4600 struct nfs4_lockdata *data;
4601 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004602 struct rpc_message msg = {
4603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4604 .rpc_cred = state->owner->so_cred,
4605 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004606 struct rpc_task_setup task_setup_data = {
4607 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004608 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004609 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004610 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004611 .flags = RPC_TASK_ASYNC,
4612 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004613 int ret;
4614
Harvey Harrison3110ff82008-05-02 13:42:44 -07004615 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004616 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004617 fl->fl_u.nfs4_fl.owner,
4618 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619 if (data == NULL)
4620 return -ENOMEM;
4621 if (IS_SETLKW(cmd))
4622 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004623 if (recovery_type > NFS_LOCK_NEW) {
4624 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004625 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004626 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4627 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004628 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004629 msg.rpc_argp = &data->arg;
4630 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004631 task_setup_data.callback_data = data;
4632 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004633 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004634 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004635 ret = nfs4_wait_for_completion_rpc_task(task);
4636 if (ret == 0) {
4637 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004638 if (ret)
4639 nfs4_handle_setlk_error(data->server, data->lsp,
4640 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004641 } else
4642 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004643 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004644 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004645 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646}
4647
4648static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4649{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004650 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004651 struct nfs4_exception exception = {
4652 .inode = state->inode,
4653 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004654 int err;
4655
4656 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004657 /* Cache the lock if possible... */
4658 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4659 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004660 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004661 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004662 break;
4663 nfs4_handle_exception(server, err, &exception);
4664 } while (exception.retry);
4665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666}
4667
4668static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4669{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004670 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004671 struct nfs4_exception exception = {
4672 .inode = state->inode,
4673 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004674 int err;
4675
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004676 err = nfs4_set_lock_state(state, request);
4677 if (err != 0)
4678 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004679 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004680 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4681 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004682 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004683 switch (err) {
4684 default:
4685 goto out;
4686 case -NFS4ERR_GRACE:
4687 case -NFS4ERR_DELAY:
4688 nfs4_handle_exception(server, err, &exception);
4689 err = 0;
4690 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004691 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004692out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004696#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004697static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004698{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004699 int status, ret = NFS_OK;
4700 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004701 struct nfs_server *server = NFS_SERVER(state->inode);
4702
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004703 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4704 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4705 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4706 if (status != NFS_OK) {
4707 nfs41_free_stateid(server, &lsp->ls_stateid);
4708 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4709 ret = status;
4710 }
4711 }
4712 };
4713
4714 return ret;
4715}
4716
4717static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4718{
4719 int status = NFS_OK;
4720
4721 if (test_bit(LK_STATE_IN_USE, &state->flags))
4722 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004723 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004724 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004725 return nfs4_lock_expired(state, request);
4726}
4727#endif
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4730{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004731 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004732 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004733 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004735 if ((fl_flags & FL_POSIX) &&
4736 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4737 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004738 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004739 status = nfs4_set_lock_state(state, request);
4740 if (status != 0)
4741 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004742 request->fl_flags |= FL_ACCESS;
4743 status = do_vfs_lock(request->fl_file, request);
4744 if (status < 0)
4745 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004746 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004747 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004748 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004749 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004750 request->fl_flags = fl_flags & ~FL_SLEEP;
4751 status = do_vfs_lock(request->fl_file, request);
4752 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004753 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004754 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004755 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004756 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004757 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004758 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004759 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004760 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4761 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004762out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004763 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004764out:
4765 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 return status;
4767}
4768
4769static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4770{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004771 struct nfs4_exception exception = {
4772 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004773 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004774 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 int err;
4776
4777 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004778 err = _nfs4_proc_setlk(state, cmd, request);
4779 if (err == -NFS4ERR_DENIED)
4780 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004782 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 } while (exception.retry);
4784 return err;
4785}
4786
4787static int
4788nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4789{
4790 struct nfs_open_context *ctx;
4791 struct nfs4_state *state;
4792 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4793 int status;
4794
4795 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004796 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 state = ctx->state;
4798
4799 if (request->fl_start < 0 || request->fl_end < 0)
4800 return -EINVAL;
4801
Trond Myklebustd9531262009-07-21 19:22:38 -04004802 if (IS_GETLK(cmd)) {
4803 if (state != NULL)
4804 return nfs4_proc_getlk(state, F_GETLK, request);
4805 return 0;
4806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4809 return -EINVAL;
4810
Trond Myklebustd9531262009-07-21 19:22:38 -04004811 if (request->fl_type == F_UNLCK) {
4812 if (state != NULL)
4813 return nfs4_proc_unlck(state, cmd, request);
4814 return 0;
4815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Trond Myklebustd9531262009-07-21 19:22:38 -04004817 if (state == NULL)
4818 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004819 /*
4820 * Don't rely on the VFS having checked the file open mode,
4821 * since it won't do this for flock() locks.
4822 */
4823 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4824 case F_RDLCK:
4825 if (!(filp->f_mode & FMODE_READ))
4826 return -EBADF;
4827 break;
4828 case F_WRLCK:
4829 if (!(filp->f_mode & FMODE_WRITE))
4830 return -EBADF;
4831 }
4832
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 do {
4834 status = nfs4_proc_setlk(state, cmd, request);
4835 if ((status != -EAGAIN) || IS_SETLK(cmd))
4836 break;
4837 timeout = nfs4_set_lock_task_retry(timeout);
4838 status = -ERESTARTSYS;
4839 if (signalled())
4840 break;
4841 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 return status;
4843}
4844
Trond Myklebust888e6942005-11-04 15:38:11 -05004845int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4846{
4847 struct nfs_server *server = NFS_SERVER(state->inode);
4848 struct nfs4_exception exception = { };
4849 int err;
4850
4851 err = nfs4_set_lock_state(state, fl);
4852 if (err != 0)
4853 goto out;
4854 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004855 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004856 switch (err) {
4857 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004858 printk(KERN_ERR "NFS: %s: unhandled error "
4859 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004860 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004861 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004862 goto out;
4863 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004864 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004865 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004866 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004867 nfs4_schedule_lease_recovery(server->nfs_client);
4868 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004869 case -NFS4ERR_BADSESSION:
4870 case -NFS4ERR_BADSLOT:
4871 case -NFS4ERR_BAD_HIGH_SLOT:
4872 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4873 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004874 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004875 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004876 case -ERESTARTSYS:
4877 /*
4878 * The show must go on: exit, but mark the
4879 * stateid as needing recovery.
4880 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004881 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004882 case -NFS4ERR_ADMIN_REVOKED:
4883 case -NFS4ERR_BAD_STATEID:
4884 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004885 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004886 err = 0;
4887 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004888 case -EKEYEXPIRED:
4889 /*
4890 * User RPCSEC_GSS context has expired.
4891 * We cannot recover this stateid now, so
4892 * skip it and allow recovery thread to
4893 * proceed.
4894 */
4895 err = 0;
4896 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004897 case -ENOMEM:
4898 case -NFS4ERR_DENIED:
4899 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4900 err = 0;
4901 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004902 case -NFS4ERR_DELAY:
4903 break;
4904 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004905 err = nfs4_handle_exception(server, err, &exception);
4906 } while (exception.retry);
4907out:
4908 return err;
4909}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004910
Trond Myklebustcf470c32012-03-07 13:49:12 -05004911struct nfs_release_lockowner_data {
4912 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004913 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004914 struct nfs_release_lockowner_args args;
4915};
4916
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004917static void nfs4_release_lockowner_release(void *calldata)
4918{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004919 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004920 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004921 kfree(calldata);
4922}
4923
Trond Myklebust17280172012-03-11 13:11:00 -04004924static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004925 .rpc_release = nfs4_release_lockowner_release,
4926};
4927
Trond Myklebustcf470c32012-03-07 13:49:12 -05004928int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004929{
4930 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004931 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004932 struct rpc_message msg = {
4933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4934 };
4935
4936 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004937 return -EINVAL;
4938 data = kmalloc(sizeof(*data), GFP_NOFS);
4939 if (!data)
4940 return -ENOMEM;
4941 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004942 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004943 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4944 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4945 data->args.lock_owner.s_dev = server->s_dev;
4946 msg.rpc_argp = &data->args;
4947 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4948 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004949}
4950
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004951#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4952
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004953static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4954 const void *buf, size_t buflen,
4955 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004956{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004957 if (strcmp(key, "") != 0)
4958 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004959
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004960 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004961}
4962
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004963static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4964 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004965{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004966 if (strcmp(key, "") != 0)
4967 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004968
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004969 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004970}
4971
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004972static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4973 size_t list_len, const char *name,
4974 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004975{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004976 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004977
J. Bruce Fields096455a2006-03-20 23:23:42 -05004978 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4979 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004980
4981 if (list && len <= list_len)
4982 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004983 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004984}
4985
Andy Adamson533eb462011-06-13 18:25:56 -04004986/*
4987 * nfs_fhget will use either the mounted_on_fileid or the fileid
4988 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004989static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4990{
Andy Adamson533eb462011-06-13 18:25:56 -04004991 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4992 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4993 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004994 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004995 return;
4996
4997 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004998 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004999 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5000 fattr->nlink = 2;
5001}
5002
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005003static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5004 const struct qstr *name,
5005 struct nfs4_fs_locations *fs_locations,
5006 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005007{
5008 struct nfs_server *server = NFS_SERVER(dir);
5009 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005010 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005011 };
5012 struct nfs4_fs_locations_arg args = {
5013 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005014 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005015 .page = page,
5016 .bitmask = bitmask,
5017 };
Benny Halevy22958462009-04-01 09:22:02 -04005018 struct nfs4_fs_locations_res res = {
5019 .fs_locations = fs_locations,
5020 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005021 struct rpc_message msg = {
5022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5023 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005024 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005025 };
5026 int status;
5027
Harvey Harrison3110ff82008-05-02 13:42:44 -07005028 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005029
5030 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5031 * is not supported */
5032 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5033 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5034 else
5035 bitmask[0] |= FATTR4_WORD0_FILEID;
5036
Trond Myklebustc228fd32007-01-13 02:28:11 -05005037 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005038 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005039 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005040 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005041 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005042 return status;
5043}
5044
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005045int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5046 const struct qstr *name,
5047 struct nfs4_fs_locations *fs_locations,
5048 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005049{
5050 struct nfs4_exception exception = { };
5051 int err;
5052 do {
5053 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005054 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005055 &exception);
5056 } while (exception.retry);
5057 return err;
5058}
5059
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005060static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5061{
5062 int status;
5063 struct nfs4_secinfo_arg args = {
5064 .dir_fh = NFS_FH(dir),
5065 .name = name,
5066 };
5067 struct nfs4_secinfo_res res = {
5068 .flavors = flavors,
5069 };
5070 struct rpc_message msg = {
5071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5072 .rpc_argp = &args,
5073 .rpc_resp = &res,
5074 };
5075
5076 dprintk("NFS call secinfo %s\n", name->name);
5077 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5078 dprintk("NFS reply secinfo: %d\n", status);
5079 return status;
5080}
5081
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005082int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5083 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005084{
5085 struct nfs4_exception exception = { };
5086 int err;
5087 do {
5088 err = nfs4_handle_exception(NFS_SERVER(dir),
5089 _nfs4_proc_secinfo(dir, name, flavors),
5090 &exception);
5091 } while (exception.retry);
5092 return err;
5093}
5094
Andy Adamson557134a2009-04-01 09:21:53 -04005095#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005096/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005097 * Check the exchange flags returned by the server for invalid flags, having
5098 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5099 * DS flags set.
5100 */
5101static int nfs4_check_cl_exchange_flags(u32 flags)
5102{
5103 if (flags & ~EXCHGID4_FLAG_MASK_R)
5104 goto out_inval;
5105 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5106 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5107 goto out_inval;
5108 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5109 goto out_inval;
5110 return NFS_OK;
5111out_inval:
5112 return -NFS4ERR_INVAL;
5113}
5114
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005115static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005116nfs41_same_server_scope(struct nfs41_server_scope *a,
5117 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005118{
5119 if (a->server_scope_sz == b->server_scope_sz &&
5120 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5121 return true;
5122
5123 return false;
5124}
5125
Andy Adamson357f54d2010-12-14 10:11:57 -05005126/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005127 * nfs4_proc_bind_conn_to_session()
5128 *
5129 * The 4.1 client currently uses the same TCP connection for the
5130 * fore and backchannel.
5131 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005132int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005133{
5134 int status;
5135 struct nfs41_bind_conn_to_session_res res;
5136 struct rpc_message msg = {
5137 .rpc_proc =
5138 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5139 .rpc_argp = clp,
5140 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005141 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005142 };
5143
5144 dprintk("--> %s\n", __func__);
5145 BUG_ON(clp == NULL);
5146
5147 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5148 if (unlikely(res.session == NULL)) {
5149 status = -ENOMEM;
5150 goto out;
5151 }
5152
5153 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5154 if (status == 0) {
5155 if (memcmp(res.session->sess_id.data,
5156 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5157 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5158 status = -EIO;
5159 goto out_session;
5160 }
5161 if (res.dir != NFS4_CDFS4_BOTH) {
5162 dprintk("NFS: %s: Unexpected direction from server\n",
5163 __func__);
5164 status = -EIO;
5165 goto out_session;
5166 }
5167 if (res.use_conn_in_rdma_mode) {
5168 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5169 __func__);
5170 status = -EIO;
5171 goto out_session;
5172 }
5173 }
5174out_session:
5175 kfree(res.session);
5176out:
5177 dprintk("<-- %s status= %d\n", __func__, status);
5178 return status;
5179}
5180
5181/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005182 * nfs4_proc_exchange_id()
5183 *
5184 * Since the clientid has expired, all compounds using sessions
5185 * associated with the stale clientid will be returning
5186 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5187 * be in some phase of session reset.
5188 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005189int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005190{
5191 nfs4_verifier verifier;
5192 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005193 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005194 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005195 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005196 };
5197 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005198 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005199 };
5200 int status;
5201 struct rpc_message msg = {
5202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5203 .rpc_argp = &args,
5204 .rpc_resp = &res,
5205 .rpc_cred = cred,
5206 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005207
5208 dprintk("--> %s\n", __func__);
5209 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005210
Chuck Leverf0920752012-05-21 22:45:41 -04005211 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005212
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005213 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005214 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005215 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005216 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005217 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005218
Chuck Leveracdeb692012-05-21 22:46:16 -04005219 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005220 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005221 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005222 status = -ENOMEM;
5223 goto out;
5224 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005225
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005226 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005227 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005228 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005229 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005230 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005231 }
5232
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005233 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005234 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005235 status = -ENOMEM;
5236 goto out_server_scope;
5237 }
5238
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005239 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005240 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005241 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005242
Chuck Lever177313f2012-05-21 22:44:58 -04005243 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005244 clp->cl_clientid = res.clientid;
5245 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5246 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5247 clp->cl_seqid = res.seqid;
5248
Chuck Leveracdeb692012-05-21 22:46:16 -04005249 kfree(clp->cl_serverowner);
5250 clp->cl_serverowner = res.server_owner;
5251 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005252
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005253 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005254 kfree(clp->cl_implid);
5255 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005256
Chuck Lever177313f2012-05-21 22:44:58 -04005257 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005258 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005259 res.server_scope)) {
5260 dprintk("%s: server_scope mismatch detected\n",
5261 __func__);
5262 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005263 kfree(clp->cl_serverscope);
5264 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005265 }
5266
Chuck Lever177313f2012-05-21 22:44:58 -04005267 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005268 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005269 goto out;
5270 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005271 } else
5272 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005273
Chuck Leveracdeb692012-05-21 22:46:16 -04005274out_server_owner:
5275 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005276out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005277 kfree(res.server_scope);
5278out:
Chuck Lever177313f2012-05-21 22:44:58 -04005279 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005280 dprintk("%s: Server Implementation ID: "
5281 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005282 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5283 clp->cl_implid->date.seconds,
5284 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005285 dprintk("<-- %s status= %d\n", __func__, status);
5286 return status;
5287}
5288
Trond Myklebust66245532012-05-25 17:18:09 -04005289static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5290 struct rpc_cred *cred)
5291{
5292 struct rpc_message msg = {
5293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5294 .rpc_argp = clp,
5295 .rpc_cred = cred,
5296 };
5297 int status;
5298
5299 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5300 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005301 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005302 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5303 return status;
5304}
5305
5306static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5307 struct rpc_cred *cred)
5308{
5309 unsigned int loop;
5310 int ret;
5311
5312 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5313 ret = _nfs4_proc_destroy_clientid(clp, cred);
5314 switch (ret) {
5315 case -NFS4ERR_DELAY:
5316 case -NFS4ERR_CLIENTID_BUSY:
5317 ssleep(1);
5318 break;
5319 default:
5320 return ret;
5321 }
5322 }
5323 return 0;
5324}
5325
5326int nfs4_destroy_clientid(struct nfs_client *clp)
5327{
5328 struct rpc_cred *cred;
5329 int ret = 0;
5330
5331 if (clp->cl_mvops->minor_version < 1)
5332 goto out;
5333 if (clp->cl_exchange_flags == 0)
5334 goto out;
5335 cred = nfs4_get_exchange_id_cred(clp);
5336 ret = nfs4_proc_destroy_clientid(clp, cred);
5337 if (cred)
5338 put_rpccred(cred);
5339 switch (ret) {
5340 case 0:
5341 case -NFS4ERR_STALE_CLIENTID:
5342 clp->cl_exchange_flags = 0;
5343 }
5344out:
5345 return ret;
5346}
5347
Andy Adamson2050f0c2009-04-01 09:22:30 -04005348struct nfs4_get_lease_time_data {
5349 struct nfs4_get_lease_time_args *args;
5350 struct nfs4_get_lease_time_res *res;
5351 struct nfs_client *clp;
5352};
5353
5354static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5355 void *calldata)
5356{
5357 int ret;
5358 struct nfs4_get_lease_time_data *data =
5359 (struct nfs4_get_lease_time_data *)calldata;
5360
5361 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005362 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005363 /* just setup sequence, do not trigger session recovery
5364 since we're invoked within one */
5365 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005366 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005367 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005368
5369 BUG_ON(ret == -EAGAIN);
5370 rpc_call_start(task);
5371 dprintk("<-- %s\n", __func__);
5372}
5373
5374/*
5375 * Called from nfs4_state_manager thread for session setup, so don't recover
5376 * from sequence operation or clientid errors.
5377 */
5378static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5379{
5380 struct nfs4_get_lease_time_data *data =
5381 (struct nfs4_get_lease_time_data *)calldata;
5382
5383 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005384 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5385 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005386 switch (task->tk_status) {
5387 case -NFS4ERR_DELAY:
5388 case -NFS4ERR_GRACE:
5389 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5390 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5391 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005392 /* fall through */
5393 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005394 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005395 return;
5396 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005397 dprintk("<-- %s\n", __func__);
5398}
5399
Trond Myklebust17280172012-03-11 13:11:00 -04005400static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005401 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5402 .rpc_call_done = nfs4_get_lease_time_done,
5403};
5404
5405int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5406{
5407 struct rpc_task *task;
5408 struct nfs4_get_lease_time_args args;
5409 struct nfs4_get_lease_time_res res = {
5410 .lr_fsinfo = fsinfo,
5411 };
5412 struct nfs4_get_lease_time_data data = {
5413 .args = &args,
5414 .res = &res,
5415 .clp = clp,
5416 };
5417 struct rpc_message msg = {
5418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5419 .rpc_argp = &args,
5420 .rpc_resp = &res,
5421 };
5422 struct rpc_task_setup task_setup = {
5423 .rpc_client = clp->cl_rpcclient,
5424 .rpc_message = &msg,
5425 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005426 .callback_data = &data,
5427 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005428 };
5429 int status;
5430
Trond Myklebust9d12b212012-01-17 22:04:25 -05005431 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005432 dprintk("--> %s\n", __func__);
5433 task = rpc_run_task(&task_setup);
5434
5435 if (IS_ERR(task))
5436 status = PTR_ERR(task);
5437 else {
5438 status = task->tk_status;
5439 rpc_put_task(task);
5440 }
5441 dprintk("<-- %s return %d\n", __func__, status);
5442
5443 return status;
5444}
5445
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005446static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5447{
5448 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5449}
5450
5451static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5452 struct nfs4_slot *new,
5453 u32 max_slots,
5454 u32 ivalue)
5455{
5456 struct nfs4_slot *old = NULL;
5457 u32 i;
5458
5459 spin_lock(&tbl->slot_tbl_lock);
5460 if (new) {
5461 old = tbl->slots;
5462 tbl->slots = new;
5463 tbl->max_slots = max_slots;
5464 }
5465 tbl->highest_used_slotid = -1; /* no slot is currently used */
5466 for (i = 0; i < tbl->max_slots; i++)
5467 tbl->slots[i].seq_nr = ivalue;
5468 spin_unlock(&tbl->slot_tbl_lock);
5469 kfree(old);
5470}
5471
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005472/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005473 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005474 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005475static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5476 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005477{
Andy Adamson104aeba2010-01-14 17:45:10 -05005478 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005479 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005480
Andy Adamson104aeba2010-01-14 17:45:10 -05005481 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5482 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005483
Andy Adamson104aeba2010-01-14 17:45:10 -05005484 /* Does the newly negotiated max_reqs match the existing slot table? */
5485 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005486 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005487 if (!new)
5488 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005489 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005490 ret = 0;
5491
5492 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005493 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5494 tbl, tbl->slots, tbl->max_slots);
5495out:
5496 dprintk("<-- %s: return %d\n", __func__, ret);
5497 return ret;
5498}
5499
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005500/* Destroy the slot table */
5501static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5502{
5503 if (session->fc_slot_table.slots != NULL) {
5504 kfree(session->fc_slot_table.slots);
5505 session->fc_slot_table.slots = NULL;
5506 }
5507 if (session->bc_slot_table.slots != NULL) {
5508 kfree(session->bc_slot_table.slots);
5509 session->bc_slot_table.slots = NULL;
5510 }
5511 return;
5512}
5513
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005514/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005515 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005516 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005517static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005518{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005519 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005520 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005521
Andy Adamsonaacd5532011-11-09 13:58:21 -05005522 dprintk("--> %s\n", __func__);
5523 /* Fore channel */
5524 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005525 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5526 if (status) /* -ENOMEM */
5527 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005528 /* Back channel */
5529 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005530 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5531 if (status && tbl->slots == NULL)
5532 /* Fore and back channel share a connection so get
5533 * both slot tables or neither */
5534 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005535 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005536}
5537
5538struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5539{
5540 struct nfs4_session *session;
5541 struct nfs4_slot_table *tbl;
5542
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005543 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005544 if (!session)
5545 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005546
Andy Adamson557134a2009-04-01 09:21:53 -04005547 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005548 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005549 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005550 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005551 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005552
5553 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005554 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005555 spin_lock_init(&tbl->slot_tbl_lock);
5556 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005557 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005558
Trond Myklebust1055d762010-06-16 09:52:27 -04005559 session->session_state = 1<<NFS4_SESSION_INITING;
5560
Andy Adamson557134a2009-04-01 09:21:53 -04005561 session->clp = clp;
5562 return session;
5563}
5564
5565void nfs4_destroy_session(struct nfs4_session *session)
5566{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005567 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005568 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005569
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005570 cred = nfs4_get_exchange_id_cred(session->clp);
5571 nfs4_proc_destroy_session(session, cred);
5572 if (cred)
5573 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005574
5575 rcu_read_lock();
5576 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5577 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005578 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005579 __func__, xprt);
5580 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005581 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005582 kfree(session);
5583}
5584
Andy Adamsonfc931582009-04-01 09:22:31 -04005585/*
5586 * Initialize the values to be used by the client in CREATE_SESSION
5587 * If nfs4_init_session set the fore channel request and response sizes,
5588 * use them.
5589 *
5590 * Set the back channel max_resp_sz_cached to zero to force the client to
5591 * always set csa_cachethis to FALSE because the current implementation
5592 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5593 */
5594static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5595{
5596 struct nfs4_session *session = args->client->cl_session;
5597 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5598 mxresp_sz = session->fc_attrs.max_resp_sz;
5599
5600 if (mxrqst_sz == 0)
5601 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5602 if (mxresp_sz == 0)
5603 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5604 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005605 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5606 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005607 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005608 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005609
5610 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005611 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005612 __func__,
5613 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005614 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005615
5616 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005617 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5618 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5619 args->bc_attrs.max_resp_sz_cached = 0;
5620 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5621 args->bc_attrs.max_reqs = 1;
5622
5623 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5624 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5625 __func__,
5626 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5627 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5628 args->bc_attrs.max_reqs);
5629}
5630
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005631static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005632{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005633 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5634 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5635
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005636 if (rcvd->max_resp_sz > sent->max_resp_sz)
5637 return -EINVAL;
5638 /*
5639 * Our requested max_ops is the minimum we need; we're not
5640 * prepared to break up compounds into smaller pieces than that.
5641 * So, no point even trying to continue if the server won't
5642 * cooperate:
5643 */
5644 if (rcvd->max_ops < sent->max_ops)
5645 return -EINVAL;
5646 if (rcvd->max_reqs == 0)
5647 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005648 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5649 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005650 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005651}
5652
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005653static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5654{
5655 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5656 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005657
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005658 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5659 return -EINVAL;
5660 if (rcvd->max_resp_sz < sent->max_resp_sz)
5661 return -EINVAL;
5662 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5663 return -EINVAL;
5664 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005665 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005666 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005667 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005668 return -EINVAL;
5669 return 0;
5670}
Andy Adamson8d353012009-04-01 09:22:32 -04005671
Andy Adamson8d353012009-04-01 09:22:32 -04005672static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5673 struct nfs4_session *session)
5674{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005675 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005676
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005677 ret = nfs4_verify_fore_channel_attrs(args, session);
5678 if (ret)
5679 return ret;
5680 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005681}
5682
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005683static int _nfs4_proc_create_session(struct nfs_client *clp,
5684 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005685{
5686 struct nfs4_session *session = clp->cl_session;
5687 struct nfs41_create_session_args args = {
5688 .client = clp,
5689 .cb_program = NFS4_CALLBACK,
5690 };
5691 struct nfs41_create_session_res res = {
5692 .client = clp,
5693 };
5694 struct rpc_message msg = {
5695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5696 .rpc_argp = &args,
5697 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005698 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005699 };
5700 int status;
5701
5702 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005703 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005704
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005705 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005706
Andy Adamson8d353012009-04-01 09:22:32 -04005707 if (!status)
5708 /* Verify the session's negotiated channel_attrs values */
5709 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005710 if (!status) {
5711 /* Increment the clientid slot sequence id */
5712 clp->cl_seqid++;
5713 }
5714
5715 return status;
5716}
5717
5718/*
5719 * Issues a CREATE_SESSION operation to the server.
5720 * It is the responsibility of the caller to verify the session is
5721 * expired before calling this routine.
5722 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005723int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005724{
5725 int status;
5726 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005727 struct nfs4_session *session = clp->cl_session;
5728
5729 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5730
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005731 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005732 if (status)
5733 goto out;
5734
Andy Adamsonaacd5532011-11-09 13:58:21 -05005735 /* Init or reset the session slot tables */
5736 status = nfs4_setup_session_slot_tables(session);
5737 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005738 if (status)
5739 goto out;
5740
5741 ptr = (unsigned *)&session->sess_id.data[0];
5742 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5743 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005744out:
5745 dprintk("<-- %s\n", __func__);
5746 return status;
5747}
5748
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005749/*
5750 * Issue the over-the-wire RPC DESTROY_SESSION.
5751 * The caller must serialize access to this routine.
5752 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005753int nfs4_proc_destroy_session(struct nfs4_session *session,
5754 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005755{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005756 struct rpc_message msg = {
5757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5758 .rpc_argp = session,
5759 .rpc_cred = cred,
5760 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005761 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005762
5763 dprintk("--> nfs4_proc_destroy_session\n");
5764
5765 /* session is still being setup */
5766 if (session->clp->cl_cons_state != NFS_CS_READY)
5767 return status;
5768
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005769 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005770
5771 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005772 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005773 "Session has been destroyed regardless...\n", status);
5774
5775 dprintk("<-- nfs4_proc_destroy_session\n");
5776 return status;
5777}
5778
Trond Myklebust7b38c362012-05-23 13:23:31 -04005779/*
5780 * With sessions, the client is not marked ready until after a
5781 * successful EXCHANGE_ID and CREATE_SESSION.
5782 *
5783 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5784 * other versions of NFS can be tried.
5785 */
5786static int nfs41_check_session_ready(struct nfs_client *clp)
5787{
5788 int ret;
5789
5790 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5791 ret = nfs4_client_recover_expired_lease(clp);
5792 if (ret)
5793 return ret;
5794 }
5795 if (clp->cl_cons_state < NFS_CS_READY)
5796 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005797 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005798 return 0;
5799}
5800
Trond Myklebustfccba802009-07-21 16:48:07 -04005801int nfs4_init_session(struct nfs_server *server)
5802{
5803 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005804 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005805 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005806
5807 if (!nfs4_has_session(clp))
5808 return 0;
5809
Trond Myklebust1055d762010-06-16 09:52:27 -04005810 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005811 spin_lock(&clp->cl_lock);
5812 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005813
Trond Myklebust7b38c362012-05-23 13:23:31 -04005814 rsize = server->rsize;
5815 if (rsize == 0)
5816 rsize = NFS_MAX_FILE_IO_SIZE;
5817 wsize = server->wsize;
5818 if (wsize == 0)
5819 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005820
Trond Myklebust7b38c362012-05-23 13:23:31 -04005821 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5822 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5823 }
5824 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005825
Trond Myklebust7b38c362012-05-23 13:23:31 -04005826 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005827}
5828
Trond Myklebust7b38c362012-05-23 13:23:31 -04005829int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005830{
5831 struct nfs4_session *session = clp->cl_session;
5832 int ret;
5833
Trond Myklebust7b38c362012-05-23 13:23:31 -04005834 spin_lock(&clp->cl_lock);
5835 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5836 /*
5837 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5838 * DS lease to be equal to the MDS lease.
5839 */
5840 clp->cl_lease_time = lease_time;
5841 clp->cl_last_renewal = jiffies;
5842 }
5843 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005844
Trond Myklebust7b38c362012-05-23 13:23:31 -04005845 ret = nfs41_check_session_ready(clp);
5846 if (ret)
5847 return ret;
5848 /* Test for the DS role */
5849 if (!is_ds_client(clp))
5850 return -ENODEV;
5851 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005852}
5853EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5854
5855
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005856/*
5857 * Renew the cl_session lease.
5858 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005859struct nfs4_sequence_data {
5860 struct nfs_client *clp;
5861 struct nfs4_sequence_args args;
5862 struct nfs4_sequence_res res;
5863};
5864
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005865static void nfs41_sequence_release(void *data)
5866{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005867 struct nfs4_sequence_data *calldata = data;
5868 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005869
Alexandros Batsakis71358402010-02-05 03:45:05 -08005870 if (atomic_read(&clp->cl_count) > 1)
5871 nfs4_schedule_state_renewal(clp);
5872 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005873 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005874}
5875
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005876static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5877{
5878 switch(task->tk_status) {
5879 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005880 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5881 return -EAGAIN;
5882 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005883 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005884 }
5885 return 0;
5886}
5887
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005888static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005889{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005890 struct nfs4_sequence_data *calldata = data;
5891 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005892
Trond Myklebust14516c32010-07-31 14:29:06 -04005893 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5894 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005895
5896 if (task->tk_status < 0) {
5897 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005898 if (atomic_read(&clp->cl_count) == 1)
5899 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005900
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005901 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5902 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005903 return;
5904 }
5905 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005906 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005907out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005908 dprintk("<-- %s\n", __func__);
5909}
5910
5911static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5912{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005913 struct nfs4_sequence_data *calldata = data;
5914 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005915 struct nfs4_sequence_args *args;
5916 struct nfs4_sequence_res *res;
5917
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005918 args = task->tk_msg.rpc_argp;
5919 res = task->tk_msg.rpc_resp;
5920
Trond Myklebust9d12b212012-01-17 22:04:25 -05005921 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005922 return;
5923 rpc_call_start(task);
5924}
5925
5926static const struct rpc_call_ops nfs41_sequence_ops = {
5927 .rpc_call_done = nfs41_sequence_call_done,
5928 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005929 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005930};
5931
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005932static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005933{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005934 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005935 struct rpc_message msg = {
5936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5937 .rpc_cred = cred,
5938 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005939 struct rpc_task_setup task_setup_data = {
5940 .rpc_client = clp->cl_rpcclient,
5941 .rpc_message = &msg,
5942 .callback_ops = &nfs41_sequence_ops,
5943 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5944 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005945
Alexandros Batsakis71358402010-02-05 03:45:05 -08005946 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005947 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005948 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005949 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005950 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005951 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005952 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005953 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005954 msg.rpc_argp = &calldata->args;
5955 msg.rpc_resp = &calldata->res;
5956 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005957 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005958
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005959 return rpc_run_task(&task_setup_data);
5960}
5961
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005962static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005963{
5964 struct rpc_task *task;
5965 int ret = 0;
5966
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005967 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5968 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005969 task = _nfs41_proc_sequence(clp, cred);
5970 if (IS_ERR(task))
5971 ret = PTR_ERR(task);
5972 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005973 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005974 dprintk("<-- %s status=%d\n", __func__, ret);
5975 return ret;
5976}
5977
5978static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5979{
5980 struct rpc_task *task;
5981 int ret;
5982
5983 task = _nfs41_proc_sequence(clp, cred);
5984 if (IS_ERR(task)) {
5985 ret = PTR_ERR(task);
5986 goto out;
5987 }
5988 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005989 if (!ret) {
5990 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5991
5992 if (task->tk_status == 0)
5993 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005994 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005995 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005996 rpc_put_task(task);
5997out:
5998 dprintk("<-- %s status=%d\n", __func__, ret);
5999 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006000}
6001
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006002struct nfs4_reclaim_complete_data {
6003 struct nfs_client *clp;
6004 struct nfs41_reclaim_complete_args arg;
6005 struct nfs41_reclaim_complete_res res;
6006};
6007
6008static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6009{
6010 struct nfs4_reclaim_complete_data *calldata = data;
6011
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006012 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006013 if (nfs41_setup_sequence(calldata->clp->cl_session,
6014 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006015 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006016 return;
6017
6018 rpc_call_start(task);
6019}
6020
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006021static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6022{
6023 switch(task->tk_status) {
6024 case 0:
6025 case -NFS4ERR_COMPLETE_ALREADY:
6026 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6027 break;
6028 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006029 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006030 /* fall through */
6031 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006032 return -EAGAIN;
6033 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006034 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006035 }
6036 return 0;
6037}
6038
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006039static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6040{
6041 struct nfs4_reclaim_complete_data *calldata = data;
6042 struct nfs_client *clp = calldata->clp;
6043 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6044
6045 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006046 if (!nfs41_sequence_done(task, res))
6047 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006048
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006049 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6050 rpc_restart_call_prepare(task);
6051 return;
6052 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006053 dprintk("<-- %s\n", __func__);
6054}
6055
6056static void nfs4_free_reclaim_complete_data(void *data)
6057{
6058 struct nfs4_reclaim_complete_data *calldata = data;
6059
6060 kfree(calldata);
6061}
6062
6063static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6064 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6065 .rpc_call_done = nfs4_reclaim_complete_done,
6066 .rpc_release = nfs4_free_reclaim_complete_data,
6067};
6068
6069/*
6070 * Issue a global reclaim complete.
6071 */
6072static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6073{
6074 struct nfs4_reclaim_complete_data *calldata;
6075 struct rpc_task *task;
6076 struct rpc_message msg = {
6077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6078 };
6079 struct rpc_task_setup task_setup_data = {
6080 .rpc_client = clp->cl_rpcclient,
6081 .rpc_message = &msg,
6082 .callback_ops = &nfs4_reclaim_complete_call_ops,
6083 .flags = RPC_TASK_ASYNC,
6084 };
6085 int status = -ENOMEM;
6086
6087 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006088 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006089 if (calldata == NULL)
6090 goto out;
6091 calldata->clp = clp;
6092 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006093
Trond Myklebust9d12b212012-01-17 22:04:25 -05006094 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006095 msg.rpc_argp = &calldata->arg;
6096 msg.rpc_resp = &calldata->res;
6097 task_setup_data.callback_data = calldata;
6098 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006099 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006100 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006101 goto out;
6102 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006103 status = nfs4_wait_for_completion_rpc_task(task);
6104 if (status == 0)
6105 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006106 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006107 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006108out:
6109 dprintk("<-- %s status=%d\n", __func__, status);
6110 return status;
6111}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006112
6113static void
6114nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6115{
6116 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006117 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006118
6119 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006120 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6121 * right now covering the LAYOUTGET we are about to send.
6122 * However, that is not so catastrophic, and there seems
6123 * to be no way to prevent it completely.
6124 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006125 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006126 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006127 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006128 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6129 NFS_I(lgp->args.inode)->layout,
6130 lgp->args.ctx->state)) {
6131 rpc_exit(task, NFS4_OK);
6132 return;
6133 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006134 rpc_call_start(task);
6135}
6136
6137static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6138{
6139 struct nfs4_layoutget *lgp = calldata;
6140 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6141
6142 dprintk("--> %s\n", __func__);
6143
6144 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6145 return;
6146
6147 switch (task->tk_status) {
6148 case 0:
6149 break;
6150 case -NFS4ERR_LAYOUTTRYLATER:
6151 case -NFS4ERR_RECALLCONFLICT:
6152 task->tk_status = -NFS4ERR_DELAY;
6153 /* Fall through */
6154 default:
6155 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6156 rpc_restart_call_prepare(task);
6157 return;
6158 }
6159 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006160 dprintk("<-- %s\n", __func__);
6161}
6162
6163static void nfs4_layoutget_release(void *calldata)
6164{
6165 struct nfs4_layoutget *lgp = calldata;
6166
6167 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006168 put_nfs_open_context(lgp->args.ctx);
6169 kfree(calldata);
6170 dprintk("<-- %s\n", __func__);
6171}
6172
6173static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6174 .rpc_call_prepare = nfs4_layoutget_prepare,
6175 .rpc_call_done = nfs4_layoutget_done,
6176 .rpc_release = nfs4_layoutget_release,
6177};
6178
6179int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6180{
6181 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6182 struct rpc_task *task;
6183 struct rpc_message msg = {
6184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6185 .rpc_argp = &lgp->args,
6186 .rpc_resp = &lgp->res,
6187 };
6188 struct rpc_task_setup task_setup_data = {
6189 .rpc_client = server->client,
6190 .rpc_message = &msg,
6191 .callback_ops = &nfs4_layoutget_call_ops,
6192 .callback_data = lgp,
6193 .flags = RPC_TASK_ASYNC,
6194 };
6195 int status = 0;
6196
6197 dprintk("--> %s\n", __func__);
6198
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006199 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006200 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006201 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006202 task = rpc_run_task(&task_setup_data);
6203 if (IS_ERR(task))
6204 return PTR_ERR(task);
6205 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006206 if (status == 0)
6207 status = task->tk_status;
6208 if (status == 0)
6209 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006210 rpc_put_task(task);
6211 dprintk("<-- %s status=%d\n", __func__, status);
6212 return status;
6213}
6214
Benny Halevycbe82602011-05-22 19:52:37 +03006215static void
6216nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6217{
6218 struct nfs4_layoutreturn *lrp = calldata;
6219
6220 dprintk("--> %s\n", __func__);
6221 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006222 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006223 return;
6224 rpc_call_start(task);
6225}
6226
6227static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6228{
6229 struct nfs4_layoutreturn *lrp = calldata;
6230 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006231 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006232
6233 dprintk("--> %s\n", __func__);
6234
6235 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6236 return;
6237
6238 server = NFS_SERVER(lrp->args.inode);
6239 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006240 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006241 return;
6242 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006243 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006244 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006245 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006246 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006247 } else
6248 BUG_ON(!list_empty(&lo->plh_segs));
6249 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006250 lo->plh_block_lgets--;
6251 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006252 dprintk("<-- %s\n", __func__);
6253}
6254
6255static void nfs4_layoutreturn_release(void *calldata)
6256{
6257 struct nfs4_layoutreturn *lrp = calldata;
6258
6259 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006260 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006261 kfree(calldata);
6262 dprintk("<-- %s\n", __func__);
6263}
6264
6265static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6266 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6267 .rpc_call_done = nfs4_layoutreturn_done,
6268 .rpc_release = nfs4_layoutreturn_release,
6269};
6270
6271int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6272{
6273 struct rpc_task *task;
6274 struct rpc_message msg = {
6275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6276 .rpc_argp = &lrp->args,
6277 .rpc_resp = &lrp->res,
6278 };
6279 struct rpc_task_setup task_setup_data = {
6280 .rpc_client = lrp->clp->cl_rpcclient,
6281 .rpc_message = &msg,
6282 .callback_ops = &nfs4_layoutreturn_call_ops,
6283 .callback_data = lrp,
6284 };
6285 int status;
6286
6287 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006288 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006289 task = rpc_run_task(&task_setup_data);
6290 if (IS_ERR(task))
6291 return PTR_ERR(task);
6292 status = task->tk_status;
6293 dprintk("<-- %s status=%d\n", __func__, status);
6294 rpc_put_task(task);
6295 return status;
6296}
6297
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006298/*
6299 * Retrieve the list of Data Server devices from the MDS.
6300 */
6301static int _nfs4_getdevicelist(struct nfs_server *server,
6302 const struct nfs_fh *fh,
6303 struct pnfs_devicelist *devlist)
6304{
6305 struct nfs4_getdevicelist_args args = {
6306 .fh = fh,
6307 .layoutclass = server->pnfs_curr_ld->id,
6308 };
6309 struct nfs4_getdevicelist_res res = {
6310 .devlist = devlist,
6311 };
6312 struct rpc_message msg = {
6313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6314 .rpc_argp = &args,
6315 .rpc_resp = &res,
6316 };
6317 int status;
6318
6319 dprintk("--> %s\n", __func__);
6320 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6321 &res.seq_res, 0);
6322 dprintk("<-- %s status=%d\n", __func__, status);
6323 return status;
6324}
6325
6326int nfs4_proc_getdevicelist(struct nfs_server *server,
6327 const struct nfs_fh *fh,
6328 struct pnfs_devicelist *devlist)
6329{
6330 struct nfs4_exception exception = { };
6331 int err;
6332
6333 do {
6334 err = nfs4_handle_exception(server,
6335 _nfs4_getdevicelist(server, fh, devlist),
6336 &exception);
6337 } while (exception.retry);
6338
6339 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6340 err, devlist->num_devs);
6341
6342 return err;
6343}
6344EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6345
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006346static int
6347_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6348{
6349 struct nfs4_getdeviceinfo_args args = {
6350 .pdev = pdev,
6351 };
6352 struct nfs4_getdeviceinfo_res res = {
6353 .pdev = pdev,
6354 };
6355 struct rpc_message msg = {
6356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6357 .rpc_argp = &args,
6358 .rpc_resp = &res,
6359 };
6360 int status;
6361
6362 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006363 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006364 dprintk("<-- %s status=%d\n", __func__, status);
6365
6366 return status;
6367}
6368
6369int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6370{
6371 struct nfs4_exception exception = { };
6372 int err;
6373
6374 do {
6375 err = nfs4_handle_exception(server,
6376 _nfs4_proc_getdeviceinfo(server, pdev),
6377 &exception);
6378 } while (exception.retry);
6379 return err;
6380}
6381EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6382
Andy Adamson863a3c62011-03-23 13:27:54 +00006383static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6384{
6385 struct nfs4_layoutcommit_data *data = calldata;
6386 struct nfs_server *server = NFS_SERVER(data->args.inode);
6387
6388 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006389 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006390 return;
6391 rpc_call_start(task);
6392}
6393
6394static void
6395nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6396{
6397 struct nfs4_layoutcommit_data *data = calldata;
6398 struct nfs_server *server = NFS_SERVER(data->args.inode);
6399
6400 if (!nfs4_sequence_done(task, &data->res.seq_res))
6401 return;
6402
6403 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006404 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6405 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6406 case -NFS4ERR_BADLAYOUT: /* no layout */
6407 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006408 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006409 break;
6410 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006411 nfs_post_op_update_inode_force_wcc(data->args.inode,
6412 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006413 break;
6414 default:
6415 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6416 rpc_restart_call_prepare(task);
6417 return;
6418 }
6419 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006420}
6421
6422static void nfs4_layoutcommit_release(void *calldata)
6423{
6424 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006425 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006426 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006427
Andy Adamsondb29c082011-07-30 20:52:38 -04006428 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006429 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006430 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6431 list_del_init(&lseg->pls_lc_list);
6432 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6433 &lseg->pls_flags))
6434 put_lseg(lseg);
6435 }
Peng Tao92407e72011-10-23 20:21:17 -07006436
6437 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6438 smp_mb__after_clear_bit();
6439 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6440
Andy Adamson863a3c62011-03-23 13:27:54 +00006441 put_rpccred(data->cred);
6442 kfree(data);
6443}
6444
6445static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6446 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6447 .rpc_call_done = nfs4_layoutcommit_done,
6448 .rpc_release = nfs4_layoutcommit_release,
6449};
6450
6451int
Andy Adamsonef311532011-03-12 02:58:10 -05006452nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006453{
6454 struct rpc_message msg = {
6455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6456 .rpc_argp = &data->args,
6457 .rpc_resp = &data->res,
6458 .rpc_cred = data->cred,
6459 };
6460 struct rpc_task_setup task_setup_data = {
6461 .task = &data->task,
6462 .rpc_client = NFS_CLIENT(data->args.inode),
6463 .rpc_message = &msg,
6464 .callback_ops = &nfs4_layoutcommit_ops,
6465 .callback_data = data,
6466 .flags = RPC_TASK_ASYNC,
6467 };
6468 struct rpc_task *task;
6469 int status = 0;
6470
6471 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6472 "lbw: %llu inode %lu\n",
6473 data->task.tk_pid, sync,
6474 data->args.lastbytewritten,
6475 data->args.inode->i_ino);
6476
Trond Myklebust9d12b212012-01-17 22:04:25 -05006477 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006478 task = rpc_run_task(&task_setup_data);
6479 if (IS_ERR(task))
6480 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006481 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006482 goto out;
6483 status = nfs4_wait_for_completion_rpc_task(task);
6484 if (status != 0)
6485 goto out;
6486 status = task->tk_status;
6487out:
6488 dprintk("%s: status %d\n", __func__, status);
6489 rpc_put_task(task);
6490 return status;
6491}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006492
6493static int
6494_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6495 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6496{
6497 struct nfs41_secinfo_no_name_args args = {
6498 .style = SECINFO_STYLE_CURRENT_FH,
6499 };
6500 struct nfs4_secinfo_res res = {
6501 .flavors = flavors,
6502 };
6503 struct rpc_message msg = {
6504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6505 .rpc_argp = &args,
6506 .rpc_resp = &res,
6507 };
6508 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6509}
6510
6511static int
6512nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6513 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6514{
6515 struct nfs4_exception exception = { };
6516 int err;
6517 do {
6518 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6519 switch (err) {
6520 case 0:
6521 case -NFS4ERR_WRONGSEC:
6522 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006523 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006524 default:
6525 err = nfs4_handle_exception(server, err, &exception);
6526 }
6527 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006528out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006529 return err;
6530}
6531
6532static int
6533nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6534 struct nfs_fsinfo *info)
6535{
6536 int err;
6537 struct page *page;
6538 rpc_authflavor_t flavor;
6539 struct nfs4_secinfo_flavors *flavors;
6540
6541 page = alloc_page(GFP_KERNEL);
6542 if (!page) {
6543 err = -ENOMEM;
6544 goto out;
6545 }
6546
6547 flavors = page_address(page);
6548 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6549
6550 /*
6551 * Fall back on "guess and check" method if
6552 * the server doesn't support SECINFO_NO_NAME
6553 */
6554 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6555 err = nfs4_find_root_sec(server, fhandle, info);
6556 goto out_freepage;
6557 }
6558 if (err)
6559 goto out_freepage;
6560
6561 flavor = nfs_find_best_sec(flavors);
6562 if (err == 0)
6563 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6564
6565out_freepage:
6566 put_page(page);
6567 if (err == -EACCES)
6568 return -EPERM;
6569out:
6570 return err;
6571}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006572
6573static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006574{
6575 int status;
6576 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006577 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006578 };
6579 struct nfs41_test_stateid_res res;
6580 struct rpc_message msg = {
6581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6582 .rpc_argp = &args,
6583 .rpc_resp = &res,
6584 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006585
Trond Myklebust9d12b212012-01-17 22:04:25 -05006586 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006587 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6588
6589 if (status == NFS_OK)
6590 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006591 return status;
6592}
6593
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006594static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006595{
6596 struct nfs4_exception exception = { };
6597 int err;
6598 do {
6599 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006600 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006601 &exception);
6602 } while (exception.retry);
6603 return err;
6604}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006605
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006606static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006607{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006608 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006609 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006610 };
6611 struct nfs41_free_stateid_res res;
6612 struct rpc_message msg = {
6613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6614 .rpc_argp = &args,
6615 .rpc_resp = &res,
6616 };
6617
Trond Myklebust9d12b212012-01-17 22:04:25 -05006618 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6619 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006620}
6621
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006622static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006623{
6624 struct nfs4_exception exception = { };
6625 int err;
6626 do {
6627 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006628 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006629 &exception);
6630 } while (exception.retry);
6631 return err;
6632}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006633
6634static bool nfs41_match_stateid(const nfs4_stateid *s1,
6635 const nfs4_stateid *s2)
6636{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006637 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006638 return false;
6639
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006640 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006641 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006642 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006643 return true;
6644
6645 return false;
6646}
6647
Andy Adamson557134a2009-04-01 09:21:53 -04006648#endif /* CONFIG_NFS_V4_1 */
6649
Trond Myklebust36281ca2012-03-04 18:13:56 -05006650static bool nfs4_match_stateid(const nfs4_stateid *s1,
6651 const nfs4_stateid *s2)
6652{
Trond Myklebustf597c532012-03-04 18:13:56 -05006653 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006654}
6655
6656
Trond Myklebust17280172012-03-11 13:11:00 -04006657static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006658 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006659 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 .recover_open = nfs4_open_reclaim,
6661 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006662 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006663 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664};
6665
Andy Adamson591d71c2009-04-01 09:22:47 -04006666#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006667static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006668 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6669 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6670 .recover_open = nfs4_open_reclaim,
6671 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006672 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006673 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006674 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006675};
6676#endif /* CONFIG_NFS_V4_1 */
6677
Trond Myklebust17280172012-03-11 13:11:00 -04006678static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006679 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006680 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 .recover_open = nfs4_open_expired,
6682 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006683 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006684 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685};
6686
Andy Adamson591d71c2009-04-01 09:22:47 -04006687#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006688static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006689 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6690 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006691 .recover_open = nfs41_open_expired,
6692 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006693 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006694 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006695};
6696#endif /* CONFIG_NFS_V4_1 */
6697
Trond Myklebust17280172012-03-11 13:11:00 -04006698static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006699 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006700 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006701 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006702};
6703
6704#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006705static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006706 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006707 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006708 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006709};
6710#endif
6711
Trond Myklebust97dc1352010-06-16 09:52:26 -04006712static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6713 .minor_version = 0,
6714 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006715 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006716 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006717 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6718 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6719 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006720};
6721
6722#if defined(CONFIG_NFS_V4_1)
6723static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6724 .minor_version = 1,
6725 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006726 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006727 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006728 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6729 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6730 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006731};
6732#endif
6733
Trond Myklebust97dc1352010-06-16 09:52:26 -04006734const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6735 [0] = &nfs_v4_0_minor_ops,
6736#if defined(CONFIG_NFS_V4_1)
6737 [1] = &nfs_v4_1_minor_ops,
6738#endif
6739};
6740
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006741static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006742 .permission = nfs_permission,
6743 .getattr = nfs_getattr,
6744 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006745 .getxattr = generic_getxattr,
6746 .setxattr = generic_setxattr,
6747 .listxattr = generic_listxattr,
6748 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006749};
6750
David Howells509de812006-08-22 20:06:11 -04006751const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 .version = 4, /* protocol version */
6753 .dentry_ops = &nfs4_dentry_operations,
6754 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006755 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006756 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006758 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 .getattr = nfs4_proc_getattr,
6760 .setattr = nfs4_proc_setattr,
6761 .lookup = nfs4_proc_lookup,
6762 .access = nfs4_proc_access,
6763 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 .create = nfs4_proc_create,
6765 .remove = nfs4_proc_remove,
6766 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006767 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 .unlink_done = nfs4_proc_unlink_done,
6769 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006770 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006771 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006772 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 .link = nfs4_proc_link,
6774 .symlink = nfs4_proc_symlink,
6775 .mkdir = nfs4_proc_mkdir,
6776 .rmdir = nfs4_proc_remove,
6777 .readdir = nfs4_proc_readdir,
6778 .mknod = nfs4_proc_mknod,
6779 .statfs = nfs4_proc_statfs,
6780 .fsinfo = nfs4_proc_fsinfo,
6781 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006782 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 .decode_dirent = nfs4_decode_dirent,
6784 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006785 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006786 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006788 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006789 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006791 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006792 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006794 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006795 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006796 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006797 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798};
6799
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006800static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6801 .prefix = XATTR_NAME_NFSV4_ACL,
6802 .list = nfs4_xattr_list_nfs4_acl,
6803 .get = nfs4_xattr_get_nfs4_acl,
6804 .set = nfs4_xattr_set_nfs4_acl,
6805};
6806
6807const struct xattr_handler *nfs4_xattr_handlers[] = {
6808 &nfs4_xattr_nfs4_acl_handler,
6809 NULL
6810};
6811
Trond Myklebustef159e92012-02-06 19:50:40 -05006812module_param(max_session_slots, ushort, 0644);
6813MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6814 "requests the client will negotiate");
6815
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816/*
6817 * Local variables:
6818 * c-basic-offset: 8
6819 * End:
6820 */