blob: d1c1016cd505fe0d410edd5fbce8029edcb843f1 [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:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400297 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400298 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500299 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();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001068 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069}
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 Schumakerf062eb62011-06-02 14:59:10 -04001761 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Levereb64cf92012-07-11 16:30:05 -04001762 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001764 if (state->flags & flags) {
1765 status = nfs41_test_stateid(server, stateid);
1766 if (status != NFS_OK) {
Chuck Lever89af2732012-07-11 16:29:56 -04001767 if (status != -NFS4ERR_BAD_STATEID)
1768 nfs41_free_stateid(server, stateid);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001769 state->flags &= ~flags;
1770 }
1771 }
1772 return status;
1773}
1774
1775static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1776{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001777 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);
Chuck Levereb64cf92012-07-11 16:30:05 -04001779 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001780
Chuck Levereb64cf92012-07-11 16:30:05 -04001781 nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1782 status = nfs41_check_expired_stateid(state, &state->open_stateid,
1783 open_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001784
Chuck Levereb64cf92012-07-11 16:30:05 -04001785 if (status != NFS_OK)
1786 status = nfs4_open_expired(sp, state);
1787 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001788}
1789#endif
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001792 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1793 * fields corresponding to attributes that were used to store the verifier.
1794 * Make sure we clobber those fields in the later setattr call
1795 */
1796static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1797{
1798 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1799 !(sattr->ia_valid & ATTR_ATIME_SET))
1800 sattr->ia_valid |= ATTR_ATIME;
1801
1802 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1803 !(sattr->ia_valid & ATTR_MTIME_SET))
1804 sattr->ia_valid |= ATTR_MTIME;
1805}
1806
1807/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001808 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Andy Adamson82be4172012-05-23 05:02:35 -04001810static int _nfs4_do_open(struct inode *dir,
1811 struct dentry *dentry,
1812 fmode_t fmode,
1813 int flags,
1814 struct iattr *sattr,
1815 struct rpc_cred *cred,
1816 struct nfs4_state **res,
1817 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 struct nfs4_state_owner *sp;
1820 struct nfs4_state *state = NULL;
1821 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001822 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001823 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001827 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1828 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1830 goto out_err;
1831 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001832 status = nfs4_recover_expired_lease(server);
1833 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001834 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001835 if (dentry->d_inode != NULL)
1836 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001837 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001838 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001839 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001840 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Andy Adamson82be4172012-05-23 05:02:35 -04001842 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1843 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1844 if (!opendata->f_attr.mdsthreshold)
1845 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001846 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001847 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001848 if (dentry->d_inode != NULL)
1849 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001850
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001851 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001853 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001855 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001856 status = PTR_ERR(state);
1857 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001858 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001859 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001860 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001861
1862 if (opendata->o_arg.open_flags & O_EXCL) {
1863 nfs4_exclusive_attrset(opendata, sattr);
1864
1865 nfs_fattr_init(opendata->o_res.f_attr);
1866 status = nfs4_do_setattr(state->inode, cred,
1867 opendata->o_res.f_attr, sattr,
1868 state);
1869 if (status == 0)
1870 nfs_setattr_update_inode(state->inode, sattr);
1871 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1872 }
Andy Adamson82be4172012-05-23 05:02:35 -04001873
1874 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1875 *ctx_th = opendata->f_attr.mdsthreshold;
1876 else
1877 kfree(opendata->f_attr.mdsthreshold);
1878 opendata->f_attr.mdsthreshold = NULL;
1879
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001880 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 *res = state;
1883 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001884err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001885 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001886 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001887err_put_state_owner:
1888 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 *res = NULL;
1891 return status;
1892}
1893
1894
Andy Adamson82be4172012-05-23 05:02:35 -04001895static struct nfs4_state *nfs4_do_open(struct inode *dir,
1896 struct dentry *dentry,
1897 fmode_t fmode,
1898 int flags,
1899 struct iattr *sattr,
1900 struct rpc_cred *cred,
1901 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 struct nfs4_exception exception = { };
1904 struct nfs4_state *res;
1905 int status;
1906
Trond Myklebust2d0dbc62012-06-08 10:58:09 -04001907 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001909 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1910 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (status == 0)
1912 break;
1913 /* NOTE: BAD_SEQID means the server and client disagree about the
1914 * book-keeping w.r.t. state-changing operations
1915 * (OPEN/CLOSE/LOCK/LOCKU...)
1916 * It is actually a sign of a bug on the client or on the server.
1917 *
1918 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001919 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 * have unhashed the old state_owner for us, and that we can
1921 * therefore safely retry using a new one. We should still warn
1922 * the user though...
1923 */
1924 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001925 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001926 " returned a bad sequence-id error!\n",
1927 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 exception.retry = 1;
1929 continue;
1930 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001931 /*
1932 * BAD_STATEID on OPEN means that the server cancelled our
1933 * state before it received the OPEN_CONFIRM.
1934 * Recover by retrying the request as per the discussion
1935 * on Page 181 of RFC3530.
1936 */
1937 if (status == -NFS4ERR_BAD_STATEID) {
1938 exception.retry = 1;
1939 continue;
1940 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001941 if (status == -EAGAIN) {
1942 /* We must have found a delegation */
1943 exception.retry = 1;
1944 continue;
1945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1947 status, &exception));
1948 } while (exception.retry);
1949 return res;
1950}
1951
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001952static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1953 struct nfs_fattr *fattr, struct iattr *sattr,
1954 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001956 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001958 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 .iap = sattr,
1960 .server = server,
1961 .bitmask = server->attr_bitmask,
1962 };
1963 struct nfs_setattrres res = {
1964 .fattr = fattr,
1965 .server = server,
1966 };
1967 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1969 .rpc_argp = &arg,
1970 .rpc_resp = &res,
1971 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001973 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001974 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Trond Myklebust0e574af2005-10-27 22:12:38 -04001976 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Trond Myklebust4fc87962012-03-08 17:42:01 -05001978 if (state != NULL) {
1979 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1980 current->files, current->tgid);
1981 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1982 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001983 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001984 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001985 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Bryan Schumaker7c513052011-03-24 17:12:24 +00001987 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001988 if (status == 0 && state != NULL)
1989 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001990 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001993static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1994 struct nfs_fattr *fattr, struct iattr *sattr,
1995 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001997 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001998 struct nfs4_exception exception = {
1999 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002000 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002001 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int err;
2003 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002004 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2005 switch (err) {
2006 case -NFS4ERR_OPENMODE:
2007 if (state && !(state->state & FMODE_WRITE)) {
2008 err = -EBADF;
2009 if (sattr->ia_valid & ATTR_OPEN)
2010 err = -EACCES;
2011 goto out;
2012 }
2013 }
2014 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002016out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return err;
2018}
2019
2020struct nfs4_closedata {
2021 struct inode *inode;
2022 struct nfs4_state *state;
2023 struct nfs_closeargs arg;
2024 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002025 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002026 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002027 bool roc;
2028 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029};
2030
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002031static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002032{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002033 struct nfs4_closedata *calldata = data;
2034 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002035 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002036
Fred Isamanf7e89172011-01-06 11:36:32 +00002037 if (calldata->roc)
2038 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002039 nfs4_put_open_state(calldata->state);
2040 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002041 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002042 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002043 kfree(calldata);
2044}
2045
Trond Myklebust88069f72009-12-08 08:33:16 -05002046static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2047 fmode_t fmode)
2048{
2049 spin_lock(&state->owner->so_lock);
2050 if (!(fmode & FMODE_READ))
2051 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2052 if (!(fmode & FMODE_WRITE))
2053 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2054 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2055 spin_unlock(&state->owner->so_lock);
2056}
2057
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002058static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002060 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 struct nfs_server *server = NFS_SERVER(calldata->inode);
2063
Chuck Levera3ca5652012-03-01 17:00:40 -05002064 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002065 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2066 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 /* hmm. we are done with the inode, and in the process of freeing
2068 * the state_owner. we keep this around to process errors
2069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 switch (task->tk_status) {
2071 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002072 if (calldata->roc)
2073 pnfs_roc_set_barrier(state->inode,
2074 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002075 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002076 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002077 nfs4_close_clear_stateid_flags(state,
2078 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 break;
2080 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002081 case -NFS4ERR_OLD_STATEID:
2082 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002084 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002087 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2088 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002090 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002091 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002092 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002095static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002097 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002098 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002099 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002100
Chuck Levera3ca5652012-03-01 17:00:40 -05002101 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002102 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002103 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002104
Trond Myklebust88069f72009-12-08 08:33:16 -05002105 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2106 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002107 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002108 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002109 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002110 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002111 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2112 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2113 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002114 }
2115 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002116 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2117 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2118 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002119 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002120 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002121 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002122
2123 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002124 /* Note: exit _without_ calling nfs4_close_done */
2125 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002126 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002127 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002128
Fred Isamanf7e89172011-01-06 11:36:32 +00002129 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002130 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002131 if (calldata->roc &&
2132 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2133 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2134 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002135 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002136 }
2137 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002138
Trond Myklebust516a6af2005-10-27 22:12:41 -04002139 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002140 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002141 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002142 &calldata->arg.seq_args,
2143 &calldata->res.seq_res,
2144 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002145 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002146 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002147out:
2148 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002151static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002152 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002153 .rpc_call_done = nfs4_close_done,
2154 .rpc_release = nfs4_free_closedata,
2155};
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157/*
2158 * It is possible for data to be read/written from a mem-mapped file
2159 * after the sys_close call (which hits the vfs layer as a flush).
2160 * This means that we can't safely call nfsv4 close on a file until
2161 * the inode is cleared. This in turn means that we are not good
2162 * NFSv4 citizens - we do not indicate to the server to update the file's
2163 * share state even when we are done with one of the three share
2164 * stateid's in the inode.
2165 *
2166 * NOTE: Caller must be holding the sp->so_owner semaphore!
2167 */
Al Viro643168c2011-06-22 18:20:23 -04002168int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002170 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002172 struct nfs4_state_owner *sp = state->owner;
2173 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002174 struct rpc_message msg = {
2175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2176 .rpc_cred = state->owner->so_cred,
2177 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002178 struct rpc_task_setup task_setup_data = {
2179 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002180 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002181 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002182 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002183 .flags = RPC_TASK_ASYNC,
2184 };
Trond Myklebust95121352005-10-18 14:20:12 -07002185 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002187 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002189 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002190 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002191 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002193 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002194 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002196 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002197 if (calldata->arg.seqid == NULL)
2198 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002199 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002200 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002201 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002202 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002203 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002204 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002205 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002206
Trond Myklebust1174dd12010-12-21 10:52:24 -05002207 msg.rpc_argp = &calldata->arg;
2208 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002209 task_setup_data.callback_data = calldata;
2210 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002211 if (IS_ERR(task))
2212 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002213 status = 0;
2214 if (wait)
2215 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002216 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002217 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002218out_free_calldata:
2219 kfree(calldata);
2220out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002221 if (roc)
2222 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002223 nfs4_put_open_state(state);
2224 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002225 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Trond Myklebust2b484292010-09-17 10:56:51 -04002228static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002229nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 struct nfs4_state *state;
2232
Trond Myklebust565277f2007-10-15 18:17:53 -04002233 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002234 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2235 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002236 if (IS_ERR(state))
2237 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002238 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002239 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Trond Myklebust1185a552009-12-03 15:54:02 -05002242static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002243{
2244 if (ctx->state == NULL)
2245 return;
2246 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002247 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002248 else
Al Viro643168c2011-06-22 18:20:23 -04002249 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2253{
Benny Halevy43652ad2009-04-01 09:21:54 -04002254 struct nfs4_server_caps_arg args = {
2255 .fhandle = fhandle,
2256 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 struct nfs4_server_caps_res res = {};
2258 struct rpc_message msg = {
2259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002260 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 .rpc_resp = &res,
2262 };
2263 int status;
2264
Bryan Schumaker7c513052011-03-24 17:12:24 +00002265 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (status == 0) {
2267 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002268 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2269 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2270 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2271 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2272 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2274 server->caps |= NFS_CAP_ACLS;
2275 if (res.has_links != 0)
2276 server->caps |= NFS_CAP_HARDLINKS;
2277 if (res.has_symlinks != 0)
2278 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002279 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2280 server->caps |= NFS_CAP_FILEID;
2281 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2282 server->caps |= NFS_CAP_MODE;
2283 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2284 server->caps |= NFS_CAP_NLINK;
2285 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2286 server->caps |= NFS_CAP_OWNER;
2287 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2288 server->caps |= NFS_CAP_OWNER_GROUP;
2289 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2290 server->caps |= NFS_CAP_ATIME;
2291 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2292 server->caps |= NFS_CAP_CTIME;
2293 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2294 server->caps |= NFS_CAP_MTIME;
2295
Trond Myklebusta65318b2009-03-11 14:10:28 -04002296 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2297 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2298 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002300 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return status;
2304}
2305
Trond Myklebust55a97592006-06-09 09:34:19 -04002306int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct nfs4_exception exception = { };
2309 int err;
2310 do {
2311 err = nfs4_handle_exception(server,
2312 _nfs4_server_capabilities(server, fhandle),
2313 &exception);
2314 } while (exception.retry);
2315 return err;
2316}
2317
2318static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2319 struct nfs_fsinfo *info)
2320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 struct nfs4_lookup_root_arg args = {
2322 .bitmask = nfs4_fattr_bitmap,
2323 };
2324 struct nfs4_lookup_res res = {
2325 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002326 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 .fh = fhandle,
2328 };
2329 struct rpc_message msg = {
2330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2331 .rpc_argp = &args,
2332 .rpc_resp = &res,
2333 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002334
Trond Myklebust0e574af2005-10-27 22:12:38 -04002335 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002336 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
2339static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2340 struct nfs_fsinfo *info)
2341{
2342 struct nfs4_exception exception = { };
2343 int err;
2344 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002345 err = _nfs4_lookup_root(server, fhandle, info);
2346 switch (err) {
2347 case 0:
2348 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002349 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002350 default:
2351 err = nfs4_handle_exception(server, err, &exception);
2352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002354out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return err;
2356}
2357
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002358static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2359 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2360{
2361 struct rpc_auth *auth;
2362 int ret;
2363
2364 auth = rpcauth_create(flavor, server->client);
2365 if (!auth) {
2366 ret = -EIO;
2367 goto out;
2368 }
2369 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002370out:
2371 return ret;
2372}
2373
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002374static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002375 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002377 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002378 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002380 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2381 flav_array[len] = RPC_AUTH_NULL;
2382 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002383
2384 for (i = 0; i < len; i++) {
2385 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002386 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002387 continue;
2388 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002389 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002390 /*
2391 * -EACCESS could mean that the user doesn't have correct permissions
2392 * to access the mount. It could also mean that we tried to mount
2393 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2394 * existing mount programs don't handle -EACCES very well so it should
2395 * be mapped to -EPERM instead.
2396 */
2397 if (status == -EACCES)
2398 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002399 return status;
2400}
2401
2402/*
2403 * get the file handle for the "/" directory on the server
2404 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002405int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2406 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002407{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002408 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002409 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002410 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2411 /*
2412 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2413 * by nfs4_map_errors() as this function exits.
2414 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002415 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (status == 0)
2417 status = nfs4_server_capabilities(server, fhandle);
2418 if (status == 0)
2419 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002420 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Bryan Schumakerbae36242012-05-10 15:07:31 -04002423static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2424 struct nfs_fsinfo *info)
2425{
2426 int error;
2427 struct nfs_fattr *fattr = info->fattr;
2428
2429 error = nfs4_server_capabilities(server, mntfh);
2430 if (error < 0) {
2431 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2432 return error;
2433 }
2434
2435 error = nfs4_proc_getattr(server, mntfh, fattr);
2436 if (error < 0) {
2437 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2438 return error;
2439 }
2440
2441 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2442 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2443 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2444
2445 return error;
2446}
2447
Manoj Naik6b97fd32006-06-09 09:34:29 -04002448/*
2449 * Get locations and (maybe) other attributes of a referral.
2450 * Note that we'll actually follow the referral later when
2451 * we detect fsid mismatch in inode revalidation
2452 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002453static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2454 const struct qstr *name, struct nfs_fattr *fattr,
2455 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002456{
2457 int status = -ENOMEM;
2458 struct page *page = NULL;
2459 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002460
2461 page = alloc_page(GFP_KERNEL);
2462 if (page == NULL)
2463 goto out;
2464 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2465 if (locations == NULL)
2466 goto out;
2467
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002468 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002469 if (status != 0)
2470 goto out;
2471 /* Make sure server returned a different fsid for the referral */
2472 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002473 dprintk("%s: server did not return a different fsid for"
2474 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002475 status = -EIO;
2476 goto out;
2477 }
Andy Adamson533eb462011-06-13 18:25:56 -04002478 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2479 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002480
Andy Adamson533eb462011-06-13 18:25:56 -04002481 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002482 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002483 memset(fhandle, 0, sizeof(struct nfs_fh));
2484out:
2485 if (page)
2486 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002487 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002488 return status;
2489}
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2492{
2493 struct nfs4_getattr_arg args = {
2494 .fh = fhandle,
2495 .bitmask = server->attr_bitmask,
2496 };
2497 struct nfs4_getattr_res res = {
2498 .fattr = fattr,
2499 .server = server,
2500 };
2501 struct rpc_message msg = {
2502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2503 .rpc_argp = &args,
2504 .rpc_resp = &res,
2505 };
2506
Trond Myklebust0e574af2005-10-27 22:12:38 -04002507 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002508 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
2511static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2512{
2513 struct nfs4_exception exception = { };
2514 int err;
2515 do {
2516 err = nfs4_handle_exception(server,
2517 _nfs4_proc_getattr(server, fhandle, fattr),
2518 &exception);
2519 } while (exception.retry);
2520 return err;
2521}
2522
2523/*
2524 * The file is not closed if it is opened due to the a request to change
2525 * the size of the file. The open call will not be needed once the
2526 * VFS layer lookup-intents are implemented.
2527 *
2528 * Close is called when the inode is destroyed.
2529 * If we haven't opened the file for O_WRONLY, we
2530 * need to in the size_change case to obtain a stateid.
2531 *
2532 * Got race?
2533 * Because OPEN is always done by name in nfsv4, it is
2534 * possible that we opened a different file by the same
2535 * name. We can recognize this race condition, but we
2536 * can't do anything about it besides returning an error.
2537 *
2538 * This will be fixed with VFS changes (lookup-intent).
2539 */
2540static int
2541nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2542 struct iattr *sattr)
2543{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002544 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002545 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002546 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 int status;
2548
Benny Halevy8a1636c2010-07-14 15:43:57 -04002549 if (pnfs_ld_layoutret_on_setattr(inode))
2550 pnfs_return_layout(inode);
2551
Trond Myklebust0e574af2005-10-27 22:12:38 -04002552 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Andy Adamson26699402012-05-30 16:12:24 -04002554 /* Deal with open(O_TRUNC) */
2555 if (sattr->ia_valid & ATTR_OPEN)
2556 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2557
2558 /* Optimization: if the end result is no change, don't RPC */
2559 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2560 return 0;
2561
Trond Myklebustd5308382005-11-04 15:33:38 -05002562 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002563 if (sattr->ia_valid & ATTR_FILE) {
2564 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002565
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002566 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002567 if (ctx) {
2568 cred = ctx->cred;
2569 state = ctx->state;
2570 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002571 }
2572
2573 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002574 if (status == 0)
2575 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return status;
2577}
2578
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002579static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2580 const struct qstr *name, struct nfs_fh *fhandle,
2581 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002582{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002583 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002584 int status;
2585 struct nfs4_lookup_arg args = {
2586 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002587 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002588 .name = name,
2589 };
2590 struct nfs4_lookup_res res = {
2591 .server = server,
2592 .fattr = fattr,
2593 .fh = fhandle,
2594 };
2595 struct rpc_message msg = {
2596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2597 .rpc_argp = &args,
2598 .rpc_resp = &res,
2599 };
2600
2601 nfs_fattr_init(fattr);
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002604 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 dprintk("NFS reply lookup: %d\n", status);
2606 return status;
2607}
2608
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002609static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002610{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002611 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002612 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002613 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2614 fattr->nlink = 2;
2615}
2616
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002617static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2618 struct qstr *name, struct nfs_fh *fhandle,
2619 struct nfs_fattr *fattr)
2620{
2621 struct nfs4_exception exception = { };
2622 struct rpc_clnt *client = *clnt;
2623 int err;
2624 do {
2625 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2626 switch (err) {
2627 case -NFS4ERR_BADNAME:
2628 err = -ENOENT;
2629 goto out;
2630 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002631 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002632 goto out;
2633 case -NFS4ERR_WRONGSEC:
2634 err = -EPERM;
2635 if (client != *clnt)
2636 goto out;
2637
2638 client = nfs4_create_sec_client(client, dir, name);
2639 if (IS_ERR(client))
2640 return PTR_ERR(client);
2641
2642 exception.retry = 1;
2643 break;
2644 default:
2645 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2646 }
2647 } while (exception.retry);
2648
2649out:
2650 if (err == 0)
2651 *clnt = client;
2652 else if (client != *clnt)
2653 rpc_shutdown_client(client);
2654
2655 return err;
2656}
2657
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002658static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002659 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002661 int status;
2662 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002663
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002664 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2665 if (client != NFS_CLIENT(dir)) {
2666 rpc_shutdown_client(client);
2667 nfs_fixup_secinfo_attributes(fattr);
2668 }
2669 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002672struct rpc_clnt *
2673nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2674 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2675{
2676 int status;
2677 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2678
2679 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2680 if (status < 0) {
2681 rpc_shutdown_client(client);
2682 return ERR_PTR(status);
2683 }
2684 return client;
2685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2688{
Trond Myklebust76b32992007-08-10 17:45:11 -04002689 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct nfs4_accessargs args = {
2691 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002692 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002694 struct nfs4_accessres res = {
2695 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002696 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 struct rpc_message msg = {
2698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2699 .rpc_argp = &args,
2700 .rpc_resp = &res,
2701 .rpc_cred = entry->cred,
2702 };
2703 int mode = entry->mask;
2704 int status;
2705
2706 /*
2707 * Determine which access bits we want to ask for...
2708 */
2709 if (mode & MAY_READ)
2710 args.access |= NFS4_ACCESS_READ;
2711 if (S_ISDIR(inode->i_mode)) {
2712 if (mode & MAY_WRITE)
2713 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2714 if (mode & MAY_EXEC)
2715 args.access |= NFS4_ACCESS_LOOKUP;
2716 } else {
2717 if (mode & MAY_WRITE)
2718 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2719 if (mode & MAY_EXEC)
2720 args.access |= NFS4_ACCESS_EXECUTE;
2721 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002722
2723 res.fattr = nfs_alloc_fattr();
2724 if (res.fattr == NULL)
2725 return -ENOMEM;
2726
Bryan Schumaker7c513052011-03-24 17:12:24 +00002727 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 if (!status) {
2729 entry->mask = 0;
2730 if (res.access & NFS4_ACCESS_READ)
2731 entry->mask |= MAY_READ;
2732 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2733 entry->mask |= MAY_WRITE;
2734 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2735 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002736 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002738 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return status;
2740}
2741
2742static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2743{
2744 struct nfs4_exception exception = { };
2745 int err;
2746 do {
2747 err = nfs4_handle_exception(NFS_SERVER(inode),
2748 _nfs4_proc_access(inode, entry),
2749 &exception);
2750 } while (exception.retry);
2751 return err;
2752}
2753
2754/*
2755 * TODO: For the time being, we don't try to get any attributes
2756 * along with any of the zero-copy operations READ, READDIR,
2757 * READLINK, WRITE.
2758 *
2759 * In the case of the first three, we want to put the GETATTR
2760 * after the read-type operation -- this is because it is hard
2761 * to predict the length of a GETATTR response in v4, and thus
2762 * align the READ data correctly. This means that the GETATTR
2763 * may end up partially falling into the page cache, and we should
2764 * shift it into the 'tail' of the xdr_buf before processing.
2765 * To do this efficiently, we need to know the total length
2766 * of data received, which doesn't seem to be available outside
2767 * of the RPC layer.
2768 *
2769 * In the case of WRITE, we also want to put the GETATTR after
2770 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002771 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 *
2773 * Both of these changes to the XDR layer would in fact be quite
2774 * minor, but I decided to leave them for a subsequent patch.
2775 */
2776static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2777 unsigned int pgbase, unsigned int pglen)
2778{
2779 struct nfs4_readlink args = {
2780 .fh = NFS_FH(inode),
2781 .pgbase = pgbase,
2782 .pglen = pglen,
2783 .pages = &page,
2784 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002785 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 struct rpc_message msg = {
2787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2788 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002789 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 };
2791
Bryan Schumaker7c513052011-03-24 17:12:24 +00002792 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 -07002793}
2794
2795static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2796 unsigned int pgbase, unsigned int pglen)
2797{
2798 struct nfs4_exception exception = { };
2799 int err;
2800 do {
2801 err = nfs4_handle_exception(NFS_SERVER(inode),
2802 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2803 &exception);
2804 } while (exception.retry);
2805 return err;
2806}
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002809 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811static int
2812nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002813 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002815 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 int status = 0;
2818
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002819 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2820 if (IS_ERR(ctx))
2821 return PTR_ERR(ctx);
2822
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002823 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002824 state = nfs4_do_open(dir, dentry, ctx->mode,
2825 flags, sattr, ctx->cred,
2826 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002827 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (IS_ERR(state)) {
2829 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002830 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002832 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002833 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002834 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002836 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return status;
2838}
2839
2840static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2841{
Trond Myklebust16e42952005-10-27 22:12:44 -04002842 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002843 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002845 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002847 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002848 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002849 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2852 .rpc_argp = &args,
2853 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002855 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002856
Bryan Schumaker7c513052011-03-24 17:12:24 +00002857 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002858 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002859 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return status;
2861}
2862
2863static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2864{
2865 struct nfs4_exception exception = { };
2866 int err;
2867 do {
2868 err = nfs4_handle_exception(NFS_SERVER(dir),
2869 _nfs4_proc_remove(dir, name),
2870 &exception);
2871 } while (exception.retry);
2872 return err;
2873}
2874
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002875static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002877 struct nfs_server *server = NFS_SERVER(dir);
2878 struct nfs_removeargs *args = msg->rpc_argp;
2879 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002881 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002883 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002886static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2887{
2888 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2889 &data->args.seq_args,
2890 &data->res.seq_res,
2891 task))
2892 return;
2893 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002896static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002898 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2899
Trond Myklebust14516c32010-07-31 14:29:06 -04002900 if (!nfs4_sequence_done(task, &res->seq_res))
2901 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002902 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002903 return 0;
2904 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002905 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
Jeff Laytond3d41522010-09-17 17:31:57 -04002908static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2909{
2910 struct nfs_server *server = NFS_SERVER(dir);
2911 struct nfs_renameargs *arg = msg->rpc_argp;
2912 struct nfs_renameres *res = msg->rpc_resp;
2913
2914 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002915 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002916 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002917}
2918
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002919static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2920{
2921 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2922 &data->args.seq_args,
2923 &data->res.seq_res,
2924 task))
2925 return;
2926 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002927}
2928
2929static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2930 struct inode *new_dir)
2931{
2932 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2933
2934 if (!nfs4_sequence_done(task, &res->seq_res))
2935 return 0;
2936 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2937 return 0;
2938
2939 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002940 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002941 return 1;
2942}
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2945 struct inode *new_dir, struct qstr *new_name)
2946{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002947 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002948 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 .old_dir = NFS_FH(old_dir),
2950 .new_dir = NFS_FH(new_dir),
2951 .old_name = old_name,
2952 .new_name = new_name,
2953 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002954 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002955 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct rpc_message msg = {
2958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2959 .rpc_argp = &arg,
2960 .rpc_resp = &res,
2961 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002962 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Bryan Schumaker7c513052011-03-24 17:12:24 +00002964 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (!status) {
2966 update_changeattr(old_dir, &res.old_cinfo);
2967 update_changeattr(new_dir, &res.new_cinfo);
2968 }
2969 return status;
2970}
2971
2972static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2973 struct inode *new_dir, struct qstr *new_name)
2974{
2975 struct nfs4_exception exception = { };
2976 int err;
2977 do {
2978 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2979 _nfs4_proc_rename(old_dir, old_name,
2980 new_dir, new_name),
2981 &exception);
2982 } while (exception.retry);
2983 return err;
2984}
2985
2986static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2987{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002988 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct nfs4_link_arg arg = {
2990 .fh = NFS_FH(inode),
2991 .dir_fh = NFS_FH(dir),
2992 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002993 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002995 struct nfs4_link_res res = {
2996 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002997 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct rpc_message msg = {
2999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3000 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003001 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003003 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Trond Myklebust136f2622010-04-16 16:22:49 -04003005 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003006 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003007 goto out;
3008
Bryan Schumaker7c513052011-03-24 17:12:24 +00003009 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003010 if (!status) {
3011 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003012 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003013 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003014out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003015 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return status;
3017}
3018
3019static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3020{
3021 struct nfs4_exception exception = { };
3022 int err;
3023 do {
3024 err = nfs4_handle_exception(NFS_SERVER(inode),
3025 _nfs4_proc_link(inode, dir, name),
3026 &exception);
3027 } while (exception.retry);
3028 return err;
3029}
3030
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003031struct nfs4_createdata {
3032 struct rpc_message msg;
3033 struct nfs4_create_arg arg;
3034 struct nfs4_create_res res;
3035 struct nfs_fh fh;
3036 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003037};
3038
3039static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3040 struct qstr *name, struct iattr *sattr, u32 ftype)
3041{
3042 struct nfs4_createdata *data;
3043
3044 data = kzalloc(sizeof(*data), GFP_KERNEL);
3045 if (data != NULL) {
3046 struct nfs_server *server = NFS_SERVER(dir);
3047
3048 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3049 data->msg.rpc_argp = &data->arg;
3050 data->msg.rpc_resp = &data->res;
3051 data->arg.dir_fh = NFS_FH(dir);
3052 data->arg.server = server;
3053 data->arg.name = name;
3054 data->arg.attrs = sattr;
3055 data->arg.ftype = ftype;
3056 data->arg.bitmask = server->attr_bitmask;
3057 data->res.server = server;
3058 data->res.fh = &data->fh;
3059 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003060 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003061 }
3062 return data;
3063}
3064
3065static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3066{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003067 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003068 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003069 if (status == 0) {
3070 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3072 }
3073 return status;
3074}
3075
3076static void nfs4_free_createdata(struct nfs4_createdata *data)
3077{
3078 kfree(data);
3079}
3080
Chuck Lever4f390c12006-08-22 20:06:22 -04003081static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003082 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003084 struct nfs4_createdata *data;
3085 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Chuck Lever94a6d752006-08-22 20:06:23 -04003087 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003088 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003089
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003090 status = -ENOMEM;
3091 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3092 if (data == NULL)
3093 goto out;
3094
3095 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3096 data->arg.u.symlink.pages = &page;
3097 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099 status = nfs4_do_create(dir, dentry, data);
3100
3101 nfs4_free_createdata(data);
3102out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return status;
3104}
3105
Chuck Lever4f390c12006-08-22 20:06:22 -04003106static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003107 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
3109 struct nfs4_exception exception = { };
3110 int err;
3111 do {
3112 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003113 _nfs4_proc_symlink(dir, dentry, page,
3114 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 &exception);
3116 } while (exception.retry);
3117 return err;
3118}
3119
3120static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3121 struct iattr *sattr)
3122{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003123 struct nfs4_createdata *data;
3124 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003126 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3127 if (data == NULL)
3128 goto out;
3129
3130 status = nfs4_do_create(dir, dentry, data);
3131
3132 nfs4_free_createdata(data);
3133out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return status;
3135}
3136
3137static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3138 struct iattr *sattr)
3139{
3140 struct nfs4_exception exception = { };
3141 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003142
3143 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 do {
3145 err = nfs4_handle_exception(NFS_SERVER(dir),
3146 _nfs4_proc_mkdir(dir, dentry, sattr),
3147 &exception);
3148 } while (exception.retry);
3149 return err;
3150}
3151
3152static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003153 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
3155 struct inode *dir = dentry->d_inode;
3156 struct nfs4_readdir_arg args = {
3157 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003158 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 .pgbase = 0,
3160 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003161 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003162 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 };
3164 struct nfs4_readdir_res res;
3165 struct rpc_message msg = {
3166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3167 .rpc_argp = &args,
3168 .rpc_resp = &res,
3169 .rpc_cred = cred,
3170 };
3171 int status;
3172
Harvey Harrison3110ff82008-05-02 13:42:44 -07003173 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003174 dentry->d_parent->d_name.name,
3175 dentry->d_name.name,
3176 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3178 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003179 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 -05003180 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003182 status += args.pgbase;
3183 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003184
3185 nfs_invalidate_atime(dir);
3186
Harvey Harrison3110ff82008-05-02 13:42:44 -07003187 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return status;
3189}
3190
3191static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003192 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 struct nfs4_exception exception = { };
3195 int err;
3196 do {
3197 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3198 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003199 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 &exception);
3201 } while (exception.retry);
3202 return err;
3203}
3204
3205static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3206 struct iattr *sattr, dev_t rdev)
3207{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003208 struct nfs4_createdata *data;
3209 int mode = sattr->ia_mode;
3210 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3213 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003214
3215 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3216 if (data == NULL)
3217 goto out;
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003220 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003222 data->arg.ftype = NF4BLK;
3223 data->arg.u.device.specdata1 = MAJOR(rdev);
3224 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 }
3226 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003227 data->arg.ftype = NF4CHR;
3228 data->arg.u.device.specdata1 = MAJOR(rdev);
3229 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003232 status = nfs4_do_create(dir, dentry, data);
3233
3234 nfs4_free_createdata(data);
3235out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return status;
3237}
3238
3239static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3240 struct iattr *sattr, dev_t rdev)
3241{
3242 struct nfs4_exception exception = { };
3243 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003244
3245 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 do {
3247 err = nfs4_handle_exception(NFS_SERVER(dir),
3248 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3249 &exception);
3250 } while (exception.retry);
3251 return err;
3252}
3253
3254static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3255 struct nfs_fsstat *fsstat)
3256{
3257 struct nfs4_statfs_arg args = {
3258 .fh = fhandle,
3259 .bitmask = server->attr_bitmask,
3260 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003261 struct nfs4_statfs_res res = {
3262 .fsstat = fsstat,
3263 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 struct rpc_message msg = {
3265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3266 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003267 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 };
3269
Trond Myklebust0e574af2005-10-27 22:12:38 -04003270 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003271 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
3274static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3275{
3276 struct nfs4_exception exception = { };
3277 int err;
3278 do {
3279 err = nfs4_handle_exception(server,
3280 _nfs4_proc_statfs(server, fhandle, fsstat),
3281 &exception);
3282 } while (exception.retry);
3283 return err;
3284}
3285
3286static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3287 struct nfs_fsinfo *fsinfo)
3288{
3289 struct nfs4_fsinfo_arg args = {
3290 .fh = fhandle,
3291 .bitmask = server->attr_bitmask,
3292 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003293 struct nfs4_fsinfo_res res = {
3294 .fsinfo = fsinfo,
3295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 struct rpc_message msg = {
3297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3298 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003299 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 };
3301
Bryan Schumaker7c513052011-03-24 17:12:24 +00003302 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
3305static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3306{
3307 struct nfs4_exception exception = { };
3308 int err;
3309
3310 do {
3311 err = nfs4_handle_exception(server,
3312 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3313 &exception);
3314 } while (exception.retry);
3315 return err;
3316}
3317
3318static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3319{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003320 int error;
3321
Trond Myklebust0e574af2005-10-27 22:12:38 -04003322 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003323 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3324 if (error == 0)
3325 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3326
3327 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
3330static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3331 struct nfs_pathconf *pathconf)
3332{
3333 struct nfs4_pathconf_arg args = {
3334 .fh = fhandle,
3335 .bitmask = server->attr_bitmask,
3336 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003337 struct nfs4_pathconf_res res = {
3338 .pathconf = pathconf,
3339 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct rpc_message msg = {
3341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3342 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003343 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 };
3345
3346 /* None of the pathconf attributes are mandatory to implement */
3347 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3348 memset(pathconf, 0, sizeof(*pathconf));
3349 return 0;
3350 }
3351
Trond Myklebust0e574af2005-10-27 22:12:38 -04003352 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003353 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354}
3355
3356static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3357 struct nfs_pathconf *pathconf)
3358{
3359 struct nfs4_exception exception = { };
3360 int err;
3361
3362 do {
3363 err = nfs4_handle_exception(server,
3364 _nfs4_proc_pathconf(server, fhandle, pathconf),
3365 &exception);
3366 } while (exception.retry);
3367 return err;
3368}
3369
Benny Halevyd20581a2011-05-22 19:52:03 +03003370void __nfs4_read_done_cb(struct nfs_read_data *data)
3371{
Fred Isamancd841602012-04-20 14:47:44 -04003372 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003373}
3374
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003375static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Fred Isamancd841602012-04-20 14:47:44 -04003377 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003379 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003380 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003381 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003383
Benny Halevyd20581a2011-05-22 19:52:03 +03003384 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003386 renew_lease(server, data->timestamp);
3387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003390static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3391{
3392
3393 dprintk("--> %s\n", __func__);
3394
3395 if (!nfs4_sequence_done(task, &data->res.seq_res))
3396 return -EAGAIN;
3397
Benny Halevyd20581a2011-05-22 19:52:03 +03003398 return data->read_done_cb ? data->read_done_cb(task, data) :
3399 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003400}
3401
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003402static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003405 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003406 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003407 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003410static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3411{
Fred Isamancd841602012-04-20 14:47:44 -04003412 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003413 &data->args.seq_args,
3414 &data->res.seq_res,
3415 task))
3416 return;
3417 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Fred Isamanb029bc92011-03-03 15:13:42 +00003420static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
Fred Isamancd841602012-04-20 14:47:44 -04003422 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003424 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003425 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003426 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003428 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003430 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003431 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433}
3434
Fred Isamanb029bc92011-03-03 15:13:42 +00003435static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3436{
3437 if (!nfs4_sequence_done(task, &data->res.seq_res))
3438 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003439 return data->write_done_cb ? data->write_done_cb(task, data) :
3440 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003441}
3442
Trond Myklebust5a37f852012-04-28 14:55:16 -04003443static
3444bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003445{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003446 const struct nfs_pgio_header *hdr = data->header;
3447
3448 /* Don't request attributes for pNFS or O_DIRECT writes */
3449 if (data->ds_clp != NULL || hdr->dreq != NULL)
3450 return false;
3451 /* Otherwise, request attributes if and only if we don't hold
3452 * a delegation
3453 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003454 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003455}
Fred Isamana69aef12011-03-03 15:13:47 +00003456
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003457static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Fred Isamancd841602012-04-20 14:47:44 -04003459 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003460
Trond Myklebust5a37f852012-04-28 14:55:16 -04003461 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003462 data->args.bitmask = NULL;
3463 data->res.fattr = NULL;
3464 } else
3465 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003466
Fred Isamanb029bc92011-03-03 15:13:42 +00003467 if (!data->write_done_cb)
3468 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003469 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 data->timestamp = jiffies;
3471
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003472 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003473 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003476static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3477{
Fred Isamancd841602012-04-20 14:47:44 -04003478 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003479 &data->args.seq_args,
3480 &data->res.seq_res,
3481 task))
3482 return;
3483 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
Fred Isaman0b7c0152012-04-20 14:47:39 -04003486static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3489 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003490 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 return;
3493 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003494}
3495
Fred Isaman0b7c0152012-04-20 14:47:39 -04003496static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
3498 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003499
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003500 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003501 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003502 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505}
3506
Fred Isaman0b7c0152012-04-20 14:47:39 -04003507static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003508{
3509 if (!nfs4_sequence_done(task, &data->res.seq_res))
3510 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003511 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003512}
3513
Fred Isaman0b7c0152012-04-20 14:47:39 -04003514static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003516 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003517
Fred Isaman0b7c0152012-04-20 14:47:39 -04003518 if (data->commit_done_cb == NULL)
3519 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003520 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003521 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003522 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003525struct nfs4_renewdata {
3526 struct nfs_client *client;
3527 unsigned long timestamp;
3528};
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530/*
3531 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3532 * standalone procedure for queueing an asynchronous RENEW.
3533 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003534static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003535{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003536 struct nfs4_renewdata *data = calldata;
3537 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003538
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003539 if (atomic_read(&clp->cl_count) > 1)
3540 nfs4_schedule_state_renewal(clp);
3541 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003542 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003543}
3544
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003545static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003547 struct nfs4_renewdata *data = calldata;
3548 struct nfs_client *clp = data->client;
3549 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003552 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003553 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3554 return;
3555 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003556 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003557 return;
3558 }
3559 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003561 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003564static const struct rpc_call_ops nfs4_renew_ops = {
3565 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003566 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003567};
3568
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003569static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
3571 struct rpc_message msg = {
3572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3573 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003574 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003576 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003578 if (renew_flags == 0)
3579 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003580 if (!atomic_inc_not_zero(&clp->cl_count))
3581 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003582 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003583 if (data == NULL)
3584 return -ENOMEM;
3585 data->client = clp;
3586 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003588 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003591static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 struct rpc_message msg = {
3594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3595 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003596 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 };
3598 unsigned long now = jiffies;
3599 int status;
3600
3601 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3602 if (status < 0)
3603 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003604 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return 0;
3606}
3607
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003608static inline int nfs4_server_supports_acls(struct nfs_server *server)
3609{
3610 return (server->caps & NFS_CAP_ACLS)
3611 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3612 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3613}
3614
3615/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3616 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3617 * the stack.
3618 */
3619#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3620
Neil Hormane9e3d722011-03-04 19:26:03 -05003621static int buf_to_pages_noslab(const void *buf, size_t buflen,
3622 struct page **pages, unsigned int *pgbase)
3623{
3624 struct page *newpage, **spages;
3625 int rc = 0;
3626 size_t len;
3627 spages = pages;
3628
3629 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003630 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003631 newpage = alloc_page(GFP_KERNEL);
3632
3633 if (newpage == NULL)
3634 goto unwind;
3635 memcpy(page_address(newpage), buf, len);
3636 buf += len;
3637 buflen -= len;
3638 *pages++ = newpage;
3639 rc++;
3640 } while (buflen != 0);
3641
3642 return rc;
3643
3644unwind:
3645 for(; rc > 0; rc--)
3646 __free_page(spages[rc-1]);
3647 return -ENOMEM;
3648}
3649
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003650struct nfs4_cached_acl {
3651 int cached;
3652 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003653 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003654};
3655
3656static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003657{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003658 struct nfs_inode *nfsi = NFS_I(inode);
3659
3660 spin_lock(&inode->i_lock);
3661 kfree(nfsi->nfs4_acl);
3662 nfsi->nfs4_acl = acl;
3663 spin_unlock(&inode->i_lock);
3664}
3665
3666static void nfs4_zap_acl_attr(struct inode *inode)
3667{
3668 nfs4_set_cached_acl(inode, NULL);
3669}
3670
3671static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3672{
3673 struct nfs_inode *nfsi = NFS_I(inode);
3674 struct nfs4_cached_acl *acl;
3675 int ret = -ENOENT;
3676
3677 spin_lock(&inode->i_lock);
3678 acl = nfsi->nfs4_acl;
3679 if (acl == NULL)
3680 goto out;
3681 if (buf == NULL) /* user is just asking for length */
3682 goto out_len;
3683 if (acl->cached == 0)
3684 goto out;
3685 ret = -ERANGE; /* see getxattr(2) man page */
3686 if (acl->len > buflen)
3687 goto out;
3688 memcpy(buf, acl->data, acl->len);
3689out_len:
3690 ret = acl->len;
3691out:
3692 spin_unlock(&inode->i_lock);
3693 return ret;
3694}
3695
Sachin Prabhu5794d212012-04-17 14:36:40 +01003696static 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 +00003697{
3698 struct nfs4_cached_acl *acl;
3699
Sachin Prabhu5794d212012-04-17 14:36:40 +01003700 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003701 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3702 if (acl == NULL)
3703 goto out;
3704 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003705 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003706 } else {
3707 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3708 if (acl == NULL)
3709 goto out;
3710 acl->cached = 0;
3711 }
3712 acl->len = acl_len;
3713out:
3714 nfs4_set_cached_acl(inode, acl);
3715}
3716
Andy Adamsonbf118a32011-12-07 11:55:27 -05003717/*
3718 * The getxattr API returns the required buffer length when called with a
3719 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3720 * the required buf. On a NULL buf, we send a page of data to the server
3721 * guessing that the ACL request can be serviced by a page. If so, we cache
3722 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3723 * the cache. If not so, we throw away the page, and cache the required
3724 * length. The next getxattr call will then produce another round trip to
3725 * the server, this time with the input buf of the required size.
3726 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003727static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003728{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003729 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003730 struct nfs_getaclargs args = {
3731 .fh = NFS_FH(inode),
3732 .acl_pages = pages,
3733 .acl_len = buflen,
3734 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003735 struct nfs_getaclres res = {
3736 .acl_len = buflen,
3737 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003738 struct rpc_message msg = {
3739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3740 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003741 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003742 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003743 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744
Andy Adamsonbf118a32011-12-07 11:55:27 -05003745 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3746 /* As long as we're doing a round trip to the server anyway,
3747 * let's be prepared for a page of acl data. */
3748 if (npages == 0)
3749 npages = 1;
3750
Sachin Prabhu5a006892012-04-17 14:35:39 +01003751 /* Add an extra page to handle the bitmap returned */
3752 npages++;
3753
Andy Adamsonbf118a32011-12-07 11:55:27 -05003754 for (i = 0; i < npages; i++) {
3755 pages[i] = alloc_page(GFP_KERNEL);
3756 if (!pages[i])
3757 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003758 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003759
3760 /* for decoding across pages */
3761 res.acl_scratch = alloc_page(GFP_KERNEL);
3762 if (!res.acl_scratch)
3763 goto out_free;
3764
Andy Adamsonbf118a32011-12-07 11:55:27 -05003765 args.acl_len = npages * PAGE_SIZE;
3766 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003767
Andy Adamsonbf118a32011-12-07 11:55:27 -05003768 /* Let decode_getfacl know not to fail if the ACL data is larger than
3769 * the page we send as a guess */
3770 if (buf == NULL)
3771 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003772
Peng Taode040be2012-01-10 22:42:47 +08003773 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003774 __func__, buf, buflen, npages, args.acl_len);
3775 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3776 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003777 if (ret)
3778 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003779
3780 acl_len = res.acl_len - res.acl_data_offset;
3781 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003782 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003783 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003784 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003785 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003786 if (buf) {
3787 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003788 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003789 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003790 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003791 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003792 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003793 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003794out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003795 for (i = 0; i < npages; i++)
3796 if (pages[i])
3797 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003798 if (res.acl_scratch)
3799 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003800 return ret;
3801}
3802
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003803static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3804{
3805 struct nfs4_exception exception = { };
3806 ssize_t ret;
3807 do {
3808 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3809 if (ret >= 0)
3810 break;
3811 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3812 } while (exception.retry);
3813 return ret;
3814}
3815
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3817{
3818 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003819 int ret;
3820
3821 if (!nfs4_server_supports_acls(server))
3822 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003823 ret = nfs_revalidate_inode(server, inode);
3824 if (ret < 0)
3825 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003826 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3827 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828 ret = nfs4_read_cached_acl(inode, buf, buflen);
3829 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003830 /* -ENOENT is returned if there is no ACL or if there is an ACL
3831 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003832 return ret;
3833 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003834}
3835
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003836static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003837{
3838 struct nfs_server *server = NFS_SERVER(inode);
3839 struct page *pages[NFS4ACL_MAXPAGES];
3840 struct nfs_setaclargs arg = {
3841 .fh = NFS_FH(inode),
3842 .acl_pages = pages,
3843 .acl_len = buflen,
3844 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003845 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3848 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003849 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003850 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003851 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003852
3853 if (!nfs4_server_supports_acls(server))
3854 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003855 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3856 if (i < 0)
3857 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003858 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003859 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003860
3861 /*
3862 * Free each page after tx, so the only ref left is
3863 * held by the network stack
3864 */
3865 for (; i > 0; i--)
3866 put_page(pages[i-1]);
3867
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003868 /*
3869 * Acl update can result in inode attribute update.
3870 * so mark the attribute cache invalid.
3871 */
3872 spin_lock(&inode->i_lock);
3873 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3874 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003875 nfs_access_zap_cache(inode);
3876 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003877 return ret;
3878}
3879
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003880static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3881{
3882 struct nfs4_exception exception = { };
3883 int err;
3884 do {
3885 err = nfs4_handle_exception(NFS_SERVER(inode),
3886 __nfs4_proc_set_acl(inode, buf, buflen),
3887 &exception);
3888 } while (exception.retry);
3889 return err;
3890}
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003893nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003895 struct nfs_client *clp = server->nfs_client;
3896
3897 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 return 0;
3899 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003900 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003901 case -NFS4ERR_ADMIN_REVOKED:
3902 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003903 if (state == NULL)
3904 break;
3905 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003906 case -NFS4ERR_OPENMODE:
3907 if (state == NULL)
3908 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003909 nfs4_schedule_stateid_recovery(server, state);
3910 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003911 case -NFS4ERR_EXPIRED:
3912 if (state != NULL)
3913 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003915 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003916 nfs4_schedule_lease_recovery(clp);
3917 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003918#if defined(CONFIG_NFS_V4_1)
3919 case -NFS4ERR_BADSESSION:
3920 case -NFS4ERR_BADSLOT:
3921 case -NFS4ERR_BAD_HIGH_SLOT:
3922 case -NFS4ERR_DEADSESSION:
3923 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3924 case -NFS4ERR_SEQ_FALSE_RETRY:
3925 case -NFS4ERR_SEQ_MISORDERED:
3926 dprintk("%s ERROR %d, Reset session\n", __func__,
3927 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003928 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003929 task->tk_status = 0;
3930 return -EAGAIN;
3931#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003933 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003934 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003935 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3937 task->tk_status = 0;
3938 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003939 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 case -NFS4ERR_OLD_STATEID:
3941 task->tk_status = 0;
3942 return -EAGAIN;
3943 }
3944 task->tk_status = nfs4_map_errors(task->tk_status);
3945 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003946wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003947 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003948 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3949 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3950 task->tk_status = 0;
3951 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
3953
Chuck Leverf0920752012-05-21 22:45:41 -04003954static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3955 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003956{
3957 __be32 verf[2];
3958
Chuck Lever2c820d92012-05-21 22:45:33 -04003959 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3960 /* An impossible timestamp guarantees this value
3961 * will never match a generated boot time. */
3962 verf[0] = 0;
3963 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3964 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003965 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3966 verf[0] = (__be32)nn->boot_time.tv_sec;
3967 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003968 }
Chuck Levercd937102012-03-02 17:14:31 -05003969 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3970}
3971
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003972int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3973 unsigned short port, struct rpc_cred *cred,
3974 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
3976 nfs4_verifier sc_verifier;
3977 struct nfs4_setclientid setclientid = {
3978 .sc_verifier = &sc_verifier,
3979 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003980 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 };
3982 struct rpc_message msg = {
3983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3984 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003985 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003986 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 int loop = 0;
3989 int status;
3990
Chuck Leverf0920752012-05-21 22:45:41 -04003991 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003994 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003996 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003997 clp->cl_ipaddr,
3998 rpc_peeraddr2str(clp->cl_rpcclient,
3999 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05004000 rpc_peeraddr2str(clp->cl_rpcclient,
4001 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04004002 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 clp->cl_id_uniquifier);
4004 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004005 sizeof(setclientid.sc_netid),
4006 rpc_peeraddr2str(clp->cl_rpcclient,
4007 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004009 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05004011 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004013 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (status != -NFS4ERR_CLID_INUSE)
4015 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004016 if (loop != 0) {
4017 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004019 }
4020 ++loop;
4021 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 }
4023 return status;
4024}
4025
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004026int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004027 struct nfs4_setclientid_res *arg,
4028 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
4030 struct nfs_fsinfo fsinfo;
4031 struct rpc_message msg = {
4032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004033 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004035 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 };
4037 unsigned long now;
4038 int status;
4039
4040 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004041 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 if (status == 0) {
4043 spin_lock(&clp->cl_lock);
4044 clp->cl_lease_time = fsinfo.lease_time * HZ;
4045 clp->cl_last_renewal = now;
4046 spin_unlock(&clp->cl_lock);
4047 }
4048 return status;
4049}
4050
Trond Myklebustfe650402006-01-03 09:55:18 +01004051struct nfs4_delegreturndata {
4052 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004053 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004054 struct nfs_fh fh;
4055 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004056 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004057 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004058 int rpc_status;
4059};
4060
Trond Myklebustfe650402006-01-03 09:55:18 +01004061static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4062{
4063 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004064
Trond Myklebust14516c32010-07-31 14:29:06 -04004065 if (!nfs4_sequence_done(task, &data->res.seq_res))
4066 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004067
Ricardo Labiaga79708862009-12-07 09:23:21 -05004068 switch (task->tk_status) {
4069 case -NFS4ERR_STALE_STATEID:
4070 case -NFS4ERR_EXPIRED:
4071 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004072 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004073 break;
4074 default:
4075 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4076 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004077 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004078 return;
4079 }
4080 }
4081 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004082}
4083
4084static void nfs4_delegreturn_release(void *calldata)
4085{
Trond Myklebustfe650402006-01-03 09:55:18 +01004086 kfree(calldata);
4087}
4088
Andy Adamson938e1012009-04-01 09:22:28 -04004089#if defined(CONFIG_NFS_V4_1)
4090static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4091{
4092 struct nfs4_delegreturndata *d_data;
4093
4094 d_data = (struct nfs4_delegreturndata *)data;
4095
Trond Myklebust035168a2010-06-16 09:52:26 -04004096 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004097 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004098 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004099 return;
4100 rpc_call_start(task);
4101}
4102#endif /* CONFIG_NFS_V4_1 */
4103
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004104static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004105#if defined(CONFIG_NFS_V4_1)
4106 .rpc_call_prepare = nfs4_delegreturn_prepare,
4107#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004108 .rpc_call_done = nfs4_delegreturn_done,
4109 .rpc_release = nfs4_delegreturn_release,
4110};
4111
Trond Myklebuste6f81072008-01-24 18:14:34 -05004112static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004113{
4114 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004115 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004116 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004117 struct rpc_message msg = {
4118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4119 .rpc_cred = cred,
4120 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004121 struct rpc_task_setup task_setup_data = {
4122 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004123 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004124 .callback_ops = &nfs4_delegreturn_ops,
4125 .flags = RPC_TASK_ASYNC,
4126 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004127 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004128
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004129 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004130 if (data == NULL)
4131 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004132 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004133 data->args.fhandle = &data->fh;
4134 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004135 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004136 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004137 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004138 data->res.fattr = &data->fattr;
4139 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004140 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004141 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004142 data->rpc_status = 0;
4143
Trond Myklebustc970aa82007-07-14 15:39:59 -04004144 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004145 msg.rpc_argp = &data->args;
4146 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004147 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004148 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004149 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004150 if (!issync)
4151 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004152 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004153 if (status != 0)
4154 goto out;
4155 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004156 if (status == 0)
4157 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4158 else
4159 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004160out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004161 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004162 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163}
4164
Trond Myklebuste6f81072008-01-24 18:14:34 -05004165int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
4167 struct nfs_server *server = NFS_SERVER(inode);
4168 struct nfs4_exception exception = { };
4169 int err;
4170 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004171 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 switch (err) {
4173 case -NFS4ERR_STALE_STATEID:
4174 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 case 0:
4176 return 0;
4177 }
4178 err = nfs4_handle_exception(server, err, &exception);
4179 } while (exception.retry);
4180 return err;
4181}
4182
4183#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4184#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4185
4186/*
4187 * sleep, with exponential backoff, and retry the LOCK operation.
4188 */
4189static unsigned long
4190nfs4_set_lock_task_retry(unsigned long timeout)
4191{
Jeff Laytond3103102011-12-01 22:44:39 +01004192 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 timeout <<= 1;
4194 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4195 return NFS4_LOCK_MAXTIMEOUT;
4196 return timeout;
4197}
4198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4200{
4201 struct inode *inode = state->inode;
4202 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004203 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004204 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004206 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004208 struct nfs_lockt_res res = {
4209 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 };
4211 struct rpc_message msg = {
4212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4213 .rpc_argp = &arg,
4214 .rpc_resp = &res,
4215 .rpc_cred = state->owner->so_cred,
4216 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 struct nfs4_lock_state *lsp;
4218 int status;
4219
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004220 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004221 status = nfs4_set_lock_state(state, request);
4222 if (status != 0)
4223 goto out;
4224 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004225 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004226 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004227 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004228 switch (status) {
4229 case 0:
4230 request->fl_type = F_UNLCK;
4231 break;
4232 case -NFS4ERR_DENIED:
4233 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004235 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004236out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 return status;
4238}
4239
4240static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4241{
4242 struct nfs4_exception exception = { };
4243 int err;
4244
4245 do {
4246 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4247 _nfs4_proc_getlk(state, cmd, request),
4248 &exception);
4249 } while (exception.retry);
4250 return err;
4251}
4252
4253static int do_vfs_lock(struct file *file, struct file_lock *fl)
4254{
4255 int res = 0;
4256 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4257 case FL_POSIX:
4258 res = posix_lock_file_wait(file, fl);
4259 break;
4260 case FL_FLOCK:
4261 res = flock_lock_file_wait(file, fl);
4262 break;
4263 default:
4264 BUG();
4265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return res;
4267}
4268
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004269struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004270 struct nfs_locku_args arg;
4271 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004272 struct nfs4_lock_state *lsp;
4273 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004274 struct file_lock fl;
4275 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004276 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004277};
4278
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004279static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4280 struct nfs_open_context *ctx,
4281 struct nfs4_lock_state *lsp,
4282 struct nfs_seqid *seqid)
4283{
4284 struct nfs4_unlockdata *p;
4285 struct inode *inode = lsp->ls_state->inode;
4286
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004287 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004288 if (p == NULL)
4289 return NULL;
4290 p->arg.fh = NFS_FH(inode);
4291 p->arg.fl = &p->fl;
4292 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004293 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004294 p->arg.stateid = &lsp->ls_stateid;
4295 p->lsp = lsp;
4296 atomic_inc(&lsp->ls_count);
4297 /* Ensure we don't close file until we're done freeing locks! */
4298 p->ctx = get_nfs_open_context(ctx);
4299 memcpy(&p->fl, fl, sizeof(p->fl));
4300 p->server = NFS_SERVER(inode);
4301 return p;
4302}
4303
Trond Myklebust06f814a2006-01-03 09:55:07 +01004304static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004305{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004306 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004307 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004308 nfs4_put_lock_state(calldata->lsp);
4309 put_nfs_open_context(calldata->ctx);
4310 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004311}
4312
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004313static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004314{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004315 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316
Trond Myklebust14516c32010-07-31 14:29:06 -04004317 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4318 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004319 switch (task->tk_status) {
4320 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004321 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4322 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004323 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004324 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004325 case -NFS4ERR_BAD_STATEID:
4326 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004327 case -NFS4ERR_STALE_STATEID:
4328 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004329 break;
4330 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004331 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004332 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004333 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004334}
4335
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004336static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004337{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004338 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004340 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004341 return;
4342 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004343 /* Note: exit _without_ running nfs4_locku_done */
4344 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004345 return;
4346 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004347 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004348 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004349 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004350 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004351 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004352 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353}
4354
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004355static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004356 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004357 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004358 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004359};
4360
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004361static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4362 struct nfs_open_context *ctx,
4363 struct nfs4_lock_state *lsp,
4364 struct nfs_seqid *seqid)
4365{
4366 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004367 struct rpc_message msg = {
4368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4369 .rpc_cred = ctx->cred,
4370 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004371 struct rpc_task_setup task_setup_data = {
4372 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004373 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004374 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004375 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004376 .flags = RPC_TASK_ASYNC,
4377 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004378
Frank Filz137d6ac2007-07-09 15:32:29 -07004379 /* Ensure this is an unlock - when canceling a lock, the
4380 * canceled lock is passed in, and it won't be an unlock.
4381 */
4382 fl->fl_type = F_UNLCK;
4383
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004384 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4385 if (data == NULL) {
4386 nfs_free_seqid(seqid);
4387 return ERR_PTR(-ENOMEM);
4388 }
4389
Trond Myklebust9d12b212012-01-17 22:04:25 -05004390 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004391 msg.rpc_argp = &data->arg;
4392 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004393 task_setup_data.callback_data = data;
4394 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004395}
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4398{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004399 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004400 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004401 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004402 struct rpc_task *task;
4403 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004404 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Trond Myklebust9b073572006-06-29 16:38:34 -04004406 status = nfs4_set_lock_state(state, request);
4407 /* Unlock _before_ we do the RPC call */
4408 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004409 down_read(&nfsi->rwsem);
4410 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4411 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004412 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004413 }
4414 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004415 if (status != 0)
4416 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004417 /* Is this a delegated lock? */
4418 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004419 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004420 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004421 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004422 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004423 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004424 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004425 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004426 status = PTR_ERR(task);
4427 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004428 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004429 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004430 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004431out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004432 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004433 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004436struct nfs4_lockdata {
4437 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004438 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004439 struct nfs4_lock_state *lsp;
4440 struct nfs_open_context *ctx;
4441 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004442 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443 int rpc_status;
4444 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004445 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004446};
4447
4448static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004449 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4450 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004451{
4452 struct nfs4_lockdata *p;
4453 struct inode *inode = lsp->ls_state->inode;
4454 struct nfs_server *server = NFS_SERVER(inode);
4455
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004456 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004457 if (p == NULL)
4458 return NULL;
4459
4460 p->arg.fh = NFS_FH(inode);
4461 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004462 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004463 if (p->arg.open_seqid == NULL)
4464 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004465 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004466 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004467 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004468 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004469 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004470 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004471 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004472 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004473 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004474 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475 atomic_inc(&lsp->ls_count);
4476 p->ctx = get_nfs_open_context(ctx);
4477 memcpy(&p->fl, fl, sizeof(p->fl));
4478 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004479out_free_seqid:
4480 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481out_free:
4482 kfree(p);
4483 return NULL;
4484}
4485
4486static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4487{
4488 struct nfs4_lockdata *data = calldata;
4489 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Harvey Harrison3110ff82008-05-02 13:42:44 -07004491 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004492 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4493 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004494 /* Do we need to do an open_to_lock_owner? */
4495 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004496 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4497 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004498 data->arg.open_stateid = &state->stateid;
4499 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004500 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004501 } else
4502 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004503 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004504 if (nfs4_setup_sequence(data->server,
4505 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004506 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004507 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004508 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004509 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510}
4511
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004512static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4513{
4514 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4515 nfs4_lock_prepare(task, calldata);
4516}
4517
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4519{
4520 struct nfs4_lockdata *data = calldata;
4521
Harvey Harrison3110ff82008-05-02 13:42:44 -07004522 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523
Trond Myklebust14516c32010-07-31 14:29:06 -04004524 if (!nfs4_sequence_done(task, &data->res.seq_res))
4525 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004526
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004528 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529 if (data->rpc_status == 0)
4530 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4531 else
4532 goto out;
4533 }
4534 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004535 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004536 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004537 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004539out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004540 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004541}
4542
4543static void nfs4_lock_release(void *calldata)
4544{
4545 struct nfs4_lockdata *data = calldata;
4546
Harvey Harrison3110ff82008-05-02 13:42:44 -07004547 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004548 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004549 if (data->cancelled != 0) {
4550 struct rpc_task *task;
4551 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4552 data->arg.lock_seqid);
4553 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004554 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004555 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004556 } else
4557 nfs_free_seqid(data->arg.lock_seqid);
4558 nfs4_put_lock_state(data->lsp);
4559 put_nfs_open_context(data->ctx);
4560 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004561 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562}
4563
4564static const struct rpc_call_ops nfs4_lock_ops = {
4565 .rpc_call_prepare = nfs4_lock_prepare,
4566 .rpc_call_done = nfs4_lock_done,
4567 .rpc_release = nfs4_lock_release,
4568};
4569
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004570static const struct rpc_call_ops nfs4_recover_lock_ops = {
4571 .rpc_call_prepare = nfs4_recover_lock_prepare,
4572 .rpc_call_done = nfs4_lock_done,
4573 .rpc_release = nfs4_lock_release,
4574};
4575
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004576static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4577{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004578 switch (error) {
4579 case -NFS4ERR_ADMIN_REVOKED:
4580 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004581 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004582 if (new_lock_owner != 0 ||
4583 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004584 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004585 break;
4586 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004587 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004588 case -NFS4ERR_EXPIRED:
4589 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004590 };
4591}
4592
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004593static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594{
4595 struct nfs4_lockdata *data;
4596 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004597 struct rpc_message msg = {
4598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4599 .rpc_cred = state->owner->so_cred,
4600 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004601 struct rpc_task_setup task_setup_data = {
4602 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004603 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004604 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004605 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004606 .flags = RPC_TASK_ASYNC,
4607 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004608 int ret;
4609
Harvey Harrison3110ff82008-05-02 13:42:44 -07004610 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004611 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004612 fl->fl_u.nfs4_fl.owner,
4613 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614 if (data == NULL)
4615 return -ENOMEM;
4616 if (IS_SETLKW(cmd))
4617 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004618 if (recovery_type > NFS_LOCK_NEW) {
4619 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004620 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004621 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4622 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004623 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004624 msg.rpc_argp = &data->arg;
4625 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004626 task_setup_data.callback_data = data;
4627 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004628 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004629 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004630 ret = nfs4_wait_for_completion_rpc_task(task);
4631 if (ret == 0) {
4632 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004633 if (ret)
4634 nfs4_handle_setlk_error(data->server, data->lsp,
4635 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004636 } else
4637 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004638 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004639 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
4643static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4644{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004645 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004646 struct nfs4_exception exception = {
4647 .inode = state->inode,
4648 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004649 int err;
4650
4651 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004652 /* Cache the lock if possible... */
4653 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4654 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004655 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004656 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004657 break;
4658 nfs4_handle_exception(server, err, &exception);
4659 } while (exception.retry);
4660 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
4663static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4664{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004665 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004666 struct nfs4_exception exception = {
4667 .inode = state->inode,
4668 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004669 int err;
4670
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004671 err = nfs4_set_lock_state(state, request);
4672 if (err != 0)
4673 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004674 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004675 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4676 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004677 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004678 switch (err) {
4679 default:
4680 goto out;
4681 case -NFS4ERR_GRACE:
4682 case -NFS4ERR_DELAY:
4683 nfs4_handle_exception(server, err, &exception);
4684 err = 0;
4685 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004686 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004687out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689}
4690
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004691#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004692static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004693{
Chuck Levereb64cf92012-07-11 16:30:05 -04004694 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004695 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004696 struct nfs_server *server = NFS_SERVER(state->inode);
4697
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004698 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4699 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4700 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4701 if (status != NFS_OK) {
Chuck Lever89af2732012-07-11 16:29:56 -04004702 if (status != -NFS4ERR_BAD_STATEID)
4703 nfs41_free_stateid(server,
4704 &lsp->ls_stateid);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004705 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4706 ret = status;
4707 }
4708 }
4709 };
4710
4711 return ret;
4712}
4713
4714static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4715{
4716 int status = NFS_OK;
4717
4718 if (test_bit(LK_STATE_IN_USE, &state->flags))
4719 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004720 if (status != NFS_OK)
4721 status = nfs4_lock_expired(state, request);
4722 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004723}
4724#endif
4725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4727{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004728 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004729 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004730 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004732 if ((fl_flags & FL_POSIX) &&
4733 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4734 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004735 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004736 status = nfs4_set_lock_state(state, request);
4737 if (status != 0)
4738 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004739 request->fl_flags |= FL_ACCESS;
4740 status = do_vfs_lock(request->fl_file, request);
4741 if (status < 0)
4742 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004743 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004744 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004745 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004746 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004747 request->fl_flags = fl_flags & ~FL_SLEEP;
4748 status = do_vfs_lock(request->fl_file, request);
4749 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004750 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004751 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004752 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004753 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004754 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004755 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004756 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004757 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4758 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004759out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004760 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004761out:
4762 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 return status;
4764}
4765
4766static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4767{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004768 struct nfs4_exception exception = {
4769 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004770 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004771 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 int err;
4773
4774 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004775 err = _nfs4_proc_setlk(state, cmd, request);
4776 if (err == -NFS4ERR_DENIED)
4777 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004779 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 } while (exception.retry);
4781 return err;
4782}
4783
4784static int
4785nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4786{
4787 struct nfs_open_context *ctx;
4788 struct nfs4_state *state;
4789 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4790 int status;
4791
4792 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004793 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 state = ctx->state;
4795
4796 if (request->fl_start < 0 || request->fl_end < 0)
4797 return -EINVAL;
4798
Trond Myklebustd9531262009-07-21 19:22:38 -04004799 if (IS_GETLK(cmd)) {
4800 if (state != NULL)
4801 return nfs4_proc_getlk(state, F_GETLK, request);
4802 return 0;
4803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4806 return -EINVAL;
4807
Trond Myklebustd9531262009-07-21 19:22:38 -04004808 if (request->fl_type == F_UNLCK) {
4809 if (state != NULL)
4810 return nfs4_proc_unlck(state, cmd, request);
4811 return 0;
4812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Trond Myklebustd9531262009-07-21 19:22:38 -04004814 if (state == NULL)
4815 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004816 /*
4817 * Don't rely on the VFS having checked the file open mode,
4818 * since it won't do this for flock() locks.
4819 */
4820 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4821 case F_RDLCK:
4822 if (!(filp->f_mode & FMODE_READ))
4823 return -EBADF;
4824 break;
4825 case F_WRLCK:
4826 if (!(filp->f_mode & FMODE_WRITE))
4827 return -EBADF;
4828 }
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 do {
4831 status = nfs4_proc_setlk(state, cmd, request);
4832 if ((status != -EAGAIN) || IS_SETLK(cmd))
4833 break;
4834 timeout = nfs4_set_lock_task_retry(timeout);
4835 status = -ERESTARTSYS;
4836 if (signalled())
4837 break;
4838 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return status;
4840}
4841
Trond Myklebust888e6942005-11-04 15:38:11 -05004842int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4843{
4844 struct nfs_server *server = NFS_SERVER(state->inode);
4845 struct nfs4_exception exception = { };
4846 int err;
4847
4848 err = nfs4_set_lock_state(state, fl);
4849 if (err != 0)
4850 goto out;
4851 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004852 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004853 switch (err) {
4854 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004855 printk(KERN_ERR "NFS: %s: unhandled error "
4856 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004857 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004858 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004859 goto out;
4860 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004861 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004862 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004863 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004864 nfs4_schedule_lease_recovery(server->nfs_client);
4865 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004866 case -NFS4ERR_BADSESSION:
4867 case -NFS4ERR_BADSLOT:
4868 case -NFS4ERR_BAD_HIGH_SLOT:
4869 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4870 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004871 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004872 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004873 case -ERESTARTSYS:
4874 /*
4875 * The show must go on: exit, but mark the
4876 * stateid as needing recovery.
4877 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004878 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004879 case -NFS4ERR_ADMIN_REVOKED:
4880 case -NFS4ERR_BAD_STATEID:
4881 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004882 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004883 err = 0;
4884 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004885 case -EKEYEXPIRED:
4886 /*
4887 * User RPCSEC_GSS context has expired.
4888 * We cannot recover this stateid now, so
4889 * skip it and allow recovery thread to
4890 * proceed.
4891 */
4892 err = 0;
4893 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004894 case -ENOMEM:
4895 case -NFS4ERR_DENIED:
4896 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4897 err = 0;
4898 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004899 case -NFS4ERR_DELAY:
4900 break;
4901 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004902 err = nfs4_handle_exception(server, err, &exception);
4903 } while (exception.retry);
4904out:
4905 return err;
4906}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004907
Trond Myklebustcf470c32012-03-07 13:49:12 -05004908struct nfs_release_lockowner_data {
4909 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004910 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004911 struct nfs_release_lockowner_args args;
4912};
4913
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004914static void nfs4_release_lockowner_release(void *calldata)
4915{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004916 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004917 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004918 kfree(calldata);
4919}
4920
Trond Myklebust17280172012-03-11 13:11:00 -04004921static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004922 .rpc_release = nfs4_release_lockowner_release,
4923};
4924
Trond Myklebustcf470c32012-03-07 13:49:12 -05004925int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004926{
4927 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004928 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004929 struct rpc_message msg = {
4930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4931 };
4932
4933 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004934 return -EINVAL;
4935 data = kmalloc(sizeof(*data), GFP_NOFS);
4936 if (!data)
4937 return -ENOMEM;
4938 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004939 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004940 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4941 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4942 data->args.lock_owner.s_dev = server->s_dev;
4943 msg.rpc_argp = &data->args;
4944 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4945 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004946}
4947
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004948#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4949
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004950static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4951 const void *buf, size_t buflen,
4952 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004953{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004954 if (strcmp(key, "") != 0)
4955 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004956
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004957 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004958}
4959
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004960static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4961 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004962{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004963 if (strcmp(key, "") != 0)
4964 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004965
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004966 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004967}
4968
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004969static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4970 size_t list_len, const char *name,
4971 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004972{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004973 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004974
J. Bruce Fields096455a2006-03-20 23:23:42 -05004975 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4976 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004977
4978 if (list && len <= list_len)
4979 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004980 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004981}
4982
Andy Adamson533eb462011-06-13 18:25:56 -04004983/*
4984 * nfs_fhget will use either the mounted_on_fileid or the fileid
4985 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004986static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4987{
Andy Adamson533eb462011-06-13 18:25:56 -04004988 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4989 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4990 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004991 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004992 return;
4993
4994 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004995 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004996 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4997 fattr->nlink = 2;
4998}
4999
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005000static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5001 const struct qstr *name,
5002 struct nfs4_fs_locations *fs_locations,
5003 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005004{
5005 struct nfs_server *server = NFS_SERVER(dir);
5006 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005007 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005008 };
5009 struct nfs4_fs_locations_arg args = {
5010 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005011 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005012 .page = page,
5013 .bitmask = bitmask,
5014 };
Benny Halevy22958462009-04-01 09:22:02 -04005015 struct nfs4_fs_locations_res res = {
5016 .fs_locations = fs_locations,
5017 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005018 struct rpc_message msg = {
5019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5020 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005021 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005022 };
5023 int status;
5024
Harvey Harrison3110ff82008-05-02 13:42:44 -07005025 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005026
5027 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5028 * is not supported */
5029 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5030 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5031 else
5032 bitmask[0] |= FATTR4_WORD0_FILEID;
5033
Trond Myklebustc228fd32007-01-13 02:28:11 -05005034 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005035 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005036 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005037 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005038 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005039 return status;
5040}
5041
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005042int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5043 const struct qstr *name,
5044 struct nfs4_fs_locations *fs_locations,
5045 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005046{
5047 struct nfs4_exception exception = { };
5048 int err;
5049 do {
5050 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005051 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005052 &exception);
5053 } while (exception.retry);
5054 return err;
5055}
5056
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005057static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5058{
5059 int status;
5060 struct nfs4_secinfo_arg args = {
5061 .dir_fh = NFS_FH(dir),
5062 .name = name,
5063 };
5064 struct nfs4_secinfo_res res = {
5065 .flavors = flavors,
5066 };
5067 struct rpc_message msg = {
5068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5069 .rpc_argp = &args,
5070 .rpc_resp = &res,
5071 };
5072
5073 dprintk("NFS call secinfo %s\n", name->name);
5074 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5075 dprintk("NFS reply secinfo: %d\n", status);
5076 return status;
5077}
5078
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005079int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5080 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005081{
5082 struct nfs4_exception exception = { };
5083 int err;
5084 do {
5085 err = nfs4_handle_exception(NFS_SERVER(dir),
5086 _nfs4_proc_secinfo(dir, name, flavors),
5087 &exception);
5088 } while (exception.retry);
5089 return err;
5090}
5091
Andy Adamson557134a2009-04-01 09:21:53 -04005092#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005093/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005094 * Check the exchange flags returned by the server for invalid flags, having
5095 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5096 * DS flags set.
5097 */
5098static int nfs4_check_cl_exchange_flags(u32 flags)
5099{
5100 if (flags & ~EXCHGID4_FLAG_MASK_R)
5101 goto out_inval;
5102 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5103 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5104 goto out_inval;
5105 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5106 goto out_inval;
5107 return NFS_OK;
5108out_inval:
5109 return -NFS4ERR_INVAL;
5110}
5111
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005112static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005113nfs41_same_server_scope(struct nfs41_server_scope *a,
5114 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005115{
5116 if (a->server_scope_sz == b->server_scope_sz &&
5117 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5118 return true;
5119
5120 return false;
5121}
5122
Andy Adamson357f54d2010-12-14 10:11:57 -05005123/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005124 * nfs4_proc_bind_conn_to_session()
5125 *
5126 * The 4.1 client currently uses the same TCP connection for the
5127 * fore and backchannel.
5128 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005129int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005130{
5131 int status;
5132 struct nfs41_bind_conn_to_session_res res;
5133 struct rpc_message msg = {
5134 .rpc_proc =
5135 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5136 .rpc_argp = clp,
5137 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005138 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005139 };
5140
5141 dprintk("--> %s\n", __func__);
5142 BUG_ON(clp == NULL);
5143
5144 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5145 if (unlikely(res.session == NULL)) {
5146 status = -ENOMEM;
5147 goto out;
5148 }
5149
5150 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5151 if (status == 0) {
5152 if (memcmp(res.session->sess_id.data,
5153 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5154 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5155 status = -EIO;
5156 goto out_session;
5157 }
5158 if (res.dir != NFS4_CDFS4_BOTH) {
5159 dprintk("NFS: %s: Unexpected direction from server\n",
5160 __func__);
5161 status = -EIO;
5162 goto out_session;
5163 }
5164 if (res.use_conn_in_rdma_mode) {
5165 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5166 __func__);
5167 status = -EIO;
5168 goto out_session;
5169 }
5170 }
5171out_session:
5172 kfree(res.session);
5173out:
5174 dprintk("<-- %s status= %d\n", __func__, status);
5175 return status;
5176}
5177
5178/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005179 * nfs4_proc_exchange_id()
5180 *
5181 * Since the clientid has expired, all compounds using sessions
5182 * associated with the stale clientid will be returning
5183 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5184 * be in some phase of session reset.
5185 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005186int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005187{
5188 nfs4_verifier verifier;
5189 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005190 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005191 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005192 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005193 };
5194 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005195 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005196 };
5197 int status;
5198 struct rpc_message msg = {
5199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5200 .rpc_argp = &args,
5201 .rpc_resp = &res,
5202 .rpc_cred = cred,
5203 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005204
5205 dprintk("--> %s\n", __func__);
5206 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005207
Chuck Leverf0920752012-05-21 22:45:41 -04005208 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005209
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005210 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005211 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005212 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005213 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005214 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005215
Chuck Leveracdeb692012-05-21 22:46:16 -04005216 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005217 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005218 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005219 status = -ENOMEM;
5220 goto out;
5221 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005222
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005223 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005224 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005225 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005226 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005227 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005228 }
5229
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005230 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005231 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005232 status = -ENOMEM;
5233 goto out_server_scope;
5234 }
5235
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005236 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005237 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005238 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005239
Chuck Lever177313f2012-05-21 22:44:58 -04005240 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005241 clp->cl_clientid = res.clientid;
5242 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5243 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5244 clp->cl_seqid = res.seqid;
5245
Chuck Leveracdeb692012-05-21 22:46:16 -04005246 kfree(clp->cl_serverowner);
5247 clp->cl_serverowner = res.server_owner;
5248 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005249
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005250 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005251 kfree(clp->cl_implid);
5252 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005253
Chuck Lever177313f2012-05-21 22:44:58 -04005254 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005255 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005256 res.server_scope)) {
5257 dprintk("%s: server_scope mismatch detected\n",
5258 __func__);
5259 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005260 kfree(clp->cl_serverscope);
5261 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005262 }
5263
Chuck Lever177313f2012-05-21 22:44:58 -04005264 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005265 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005266 goto out;
5267 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005268 } else
5269 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005270
Chuck Leveracdeb692012-05-21 22:46:16 -04005271out_server_owner:
5272 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005273out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005274 kfree(res.server_scope);
5275out:
Chuck Lever177313f2012-05-21 22:44:58 -04005276 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005277 dprintk("%s: Server Implementation ID: "
5278 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005279 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5280 clp->cl_implid->date.seconds,
5281 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005282 dprintk("<-- %s status= %d\n", __func__, status);
5283 return status;
5284}
5285
Trond Myklebust66245532012-05-25 17:18:09 -04005286static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5287 struct rpc_cred *cred)
5288{
5289 struct rpc_message msg = {
5290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5291 .rpc_argp = clp,
5292 .rpc_cred = cred,
5293 };
5294 int status;
5295
5296 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5297 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005298 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005299 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5300 return status;
5301}
5302
5303static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5304 struct rpc_cred *cred)
5305{
5306 unsigned int loop;
5307 int ret;
5308
5309 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5310 ret = _nfs4_proc_destroy_clientid(clp, cred);
5311 switch (ret) {
5312 case -NFS4ERR_DELAY:
5313 case -NFS4ERR_CLIENTID_BUSY:
5314 ssleep(1);
5315 break;
5316 default:
5317 return ret;
5318 }
5319 }
5320 return 0;
5321}
5322
5323int nfs4_destroy_clientid(struct nfs_client *clp)
5324{
5325 struct rpc_cred *cred;
5326 int ret = 0;
5327
5328 if (clp->cl_mvops->minor_version < 1)
5329 goto out;
5330 if (clp->cl_exchange_flags == 0)
5331 goto out;
5332 cred = nfs4_get_exchange_id_cred(clp);
5333 ret = nfs4_proc_destroy_clientid(clp, cred);
5334 if (cred)
5335 put_rpccred(cred);
5336 switch (ret) {
5337 case 0:
5338 case -NFS4ERR_STALE_CLIENTID:
5339 clp->cl_exchange_flags = 0;
5340 }
5341out:
5342 return ret;
5343}
5344
Andy Adamson2050f0c2009-04-01 09:22:30 -04005345struct nfs4_get_lease_time_data {
5346 struct nfs4_get_lease_time_args *args;
5347 struct nfs4_get_lease_time_res *res;
5348 struct nfs_client *clp;
5349};
5350
5351static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5352 void *calldata)
5353{
5354 int ret;
5355 struct nfs4_get_lease_time_data *data =
5356 (struct nfs4_get_lease_time_data *)calldata;
5357
5358 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005359 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005360 /* just setup sequence, do not trigger session recovery
5361 since we're invoked within one */
5362 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005363 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005364 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005365
5366 BUG_ON(ret == -EAGAIN);
5367 rpc_call_start(task);
5368 dprintk("<-- %s\n", __func__);
5369}
5370
5371/*
5372 * Called from nfs4_state_manager thread for session setup, so don't recover
5373 * from sequence operation or clientid errors.
5374 */
5375static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5376{
5377 struct nfs4_get_lease_time_data *data =
5378 (struct nfs4_get_lease_time_data *)calldata;
5379
5380 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005381 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5382 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005383 switch (task->tk_status) {
5384 case -NFS4ERR_DELAY:
5385 case -NFS4ERR_GRACE:
5386 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5387 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5388 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005389 /* fall through */
5390 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005391 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005392 return;
5393 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005394 dprintk("<-- %s\n", __func__);
5395}
5396
Trond Myklebust17280172012-03-11 13:11:00 -04005397static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005398 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5399 .rpc_call_done = nfs4_get_lease_time_done,
5400};
5401
5402int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5403{
5404 struct rpc_task *task;
5405 struct nfs4_get_lease_time_args args;
5406 struct nfs4_get_lease_time_res res = {
5407 .lr_fsinfo = fsinfo,
5408 };
5409 struct nfs4_get_lease_time_data data = {
5410 .args = &args,
5411 .res = &res,
5412 .clp = clp,
5413 };
5414 struct rpc_message msg = {
5415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5416 .rpc_argp = &args,
5417 .rpc_resp = &res,
5418 };
5419 struct rpc_task_setup task_setup = {
5420 .rpc_client = clp->cl_rpcclient,
5421 .rpc_message = &msg,
5422 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005423 .callback_data = &data,
5424 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005425 };
5426 int status;
5427
Trond Myklebust9d12b212012-01-17 22:04:25 -05005428 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005429 dprintk("--> %s\n", __func__);
5430 task = rpc_run_task(&task_setup);
5431
5432 if (IS_ERR(task))
5433 status = PTR_ERR(task);
5434 else {
5435 status = task->tk_status;
5436 rpc_put_task(task);
5437 }
5438 dprintk("<-- %s return %d\n", __func__, status);
5439
5440 return status;
5441}
5442
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005443static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5444{
5445 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5446}
5447
5448static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5449 struct nfs4_slot *new,
5450 u32 max_slots,
5451 u32 ivalue)
5452{
5453 struct nfs4_slot *old = NULL;
5454 u32 i;
5455
5456 spin_lock(&tbl->slot_tbl_lock);
5457 if (new) {
5458 old = tbl->slots;
5459 tbl->slots = new;
5460 tbl->max_slots = max_slots;
5461 }
5462 tbl->highest_used_slotid = -1; /* no slot is currently used */
5463 for (i = 0; i < tbl->max_slots; i++)
5464 tbl->slots[i].seq_nr = ivalue;
5465 spin_unlock(&tbl->slot_tbl_lock);
5466 kfree(old);
5467}
5468
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005469/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005470 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005471 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005472static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5473 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005474{
Andy Adamson104aeba2010-01-14 17:45:10 -05005475 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005476 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005477
Andy Adamson104aeba2010-01-14 17:45:10 -05005478 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5479 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005480
Andy Adamson104aeba2010-01-14 17:45:10 -05005481 /* Does the newly negotiated max_reqs match the existing slot table? */
5482 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005483 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005484 if (!new)
5485 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005486 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005487 ret = 0;
5488
5489 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005490 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5491 tbl, tbl->slots, tbl->max_slots);
5492out:
5493 dprintk("<-- %s: return %d\n", __func__, ret);
5494 return ret;
5495}
5496
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005497/* Destroy the slot table */
5498static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5499{
5500 if (session->fc_slot_table.slots != NULL) {
5501 kfree(session->fc_slot_table.slots);
5502 session->fc_slot_table.slots = NULL;
5503 }
5504 if (session->bc_slot_table.slots != NULL) {
5505 kfree(session->bc_slot_table.slots);
5506 session->bc_slot_table.slots = NULL;
5507 }
5508 return;
5509}
5510
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005511/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005512 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005513 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005514static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005515{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005516 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005517 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005518
Andy Adamsonaacd5532011-11-09 13:58:21 -05005519 dprintk("--> %s\n", __func__);
5520 /* Fore channel */
5521 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005522 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5523 if (status) /* -ENOMEM */
5524 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005525 /* Back channel */
5526 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005527 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5528 if (status && tbl->slots == NULL)
5529 /* Fore and back channel share a connection so get
5530 * both slot tables or neither */
5531 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005532 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005533}
5534
5535struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5536{
5537 struct nfs4_session *session;
5538 struct nfs4_slot_table *tbl;
5539
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005540 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005541 if (!session)
5542 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005543
Andy Adamson557134a2009-04-01 09:21:53 -04005544 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005545 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005546 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005547 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005548 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005549
5550 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005551 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005552 spin_lock_init(&tbl->slot_tbl_lock);
5553 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005554 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005555
Trond Myklebust1055d762010-06-16 09:52:27 -04005556 session->session_state = 1<<NFS4_SESSION_INITING;
5557
Andy Adamson557134a2009-04-01 09:21:53 -04005558 session->clp = clp;
5559 return session;
5560}
5561
5562void nfs4_destroy_session(struct nfs4_session *session)
5563{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005564 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005565 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005566
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005567 cred = nfs4_get_exchange_id_cred(session->clp);
5568 nfs4_proc_destroy_session(session, cred);
5569 if (cred)
5570 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005571
5572 rcu_read_lock();
5573 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5574 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005575 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005576 __func__, xprt);
5577 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005578 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005579 kfree(session);
5580}
5581
Andy Adamsonfc931582009-04-01 09:22:31 -04005582/*
5583 * Initialize the values to be used by the client in CREATE_SESSION
5584 * If nfs4_init_session set the fore channel request and response sizes,
5585 * use them.
5586 *
5587 * Set the back channel max_resp_sz_cached to zero to force the client to
5588 * always set csa_cachethis to FALSE because the current implementation
5589 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5590 */
5591static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5592{
5593 struct nfs4_session *session = args->client->cl_session;
5594 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5595 mxresp_sz = session->fc_attrs.max_resp_sz;
5596
5597 if (mxrqst_sz == 0)
5598 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5599 if (mxresp_sz == 0)
5600 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5601 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005602 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5603 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005604 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005605 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005606
5607 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005608 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005609 __func__,
5610 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005611 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005612
5613 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005614 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5615 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5616 args->bc_attrs.max_resp_sz_cached = 0;
5617 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5618 args->bc_attrs.max_reqs = 1;
5619
5620 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5621 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5622 __func__,
5623 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5624 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5625 args->bc_attrs.max_reqs);
5626}
5627
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005628static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005629{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005630 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5631 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5632
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005633 if (rcvd->max_resp_sz > sent->max_resp_sz)
5634 return -EINVAL;
5635 /*
5636 * Our requested max_ops is the minimum we need; we're not
5637 * prepared to break up compounds into smaller pieces than that.
5638 * So, no point even trying to continue if the server won't
5639 * cooperate:
5640 */
5641 if (rcvd->max_ops < sent->max_ops)
5642 return -EINVAL;
5643 if (rcvd->max_reqs == 0)
5644 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005645 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5646 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005647 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005648}
5649
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005650static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5651{
5652 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5653 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005654
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005655 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5656 return -EINVAL;
5657 if (rcvd->max_resp_sz < sent->max_resp_sz)
5658 return -EINVAL;
5659 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5660 return -EINVAL;
5661 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005662 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005663 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005664 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005665 return -EINVAL;
5666 return 0;
5667}
Andy Adamson8d353012009-04-01 09:22:32 -04005668
Andy Adamson8d353012009-04-01 09:22:32 -04005669static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5670 struct nfs4_session *session)
5671{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005672 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005673
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005674 ret = nfs4_verify_fore_channel_attrs(args, session);
5675 if (ret)
5676 return ret;
5677 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005678}
5679
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005680static int _nfs4_proc_create_session(struct nfs_client *clp,
5681 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005682{
5683 struct nfs4_session *session = clp->cl_session;
5684 struct nfs41_create_session_args args = {
5685 .client = clp,
5686 .cb_program = NFS4_CALLBACK,
5687 };
5688 struct nfs41_create_session_res res = {
5689 .client = clp,
5690 };
5691 struct rpc_message msg = {
5692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5693 .rpc_argp = &args,
5694 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005695 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005696 };
5697 int status;
5698
5699 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005700 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005701
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005702 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005703
Andy Adamson8d353012009-04-01 09:22:32 -04005704 if (!status)
5705 /* Verify the session's negotiated channel_attrs values */
5706 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005707 if (!status) {
5708 /* Increment the clientid slot sequence id */
5709 clp->cl_seqid++;
5710 }
5711
5712 return status;
5713}
5714
5715/*
5716 * Issues a CREATE_SESSION operation to the server.
5717 * It is the responsibility of the caller to verify the session is
5718 * expired before calling this routine.
5719 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005720int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005721{
5722 int status;
5723 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005724 struct nfs4_session *session = clp->cl_session;
5725
5726 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5727
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005728 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005729 if (status)
5730 goto out;
5731
Andy Adamsonaacd5532011-11-09 13:58:21 -05005732 /* Init or reset the session slot tables */
5733 status = nfs4_setup_session_slot_tables(session);
5734 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005735 if (status)
5736 goto out;
5737
5738 ptr = (unsigned *)&session->sess_id.data[0];
5739 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5740 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005741out:
5742 dprintk("<-- %s\n", __func__);
5743 return status;
5744}
5745
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005746/*
5747 * Issue the over-the-wire RPC DESTROY_SESSION.
5748 * The caller must serialize access to this routine.
5749 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005750int nfs4_proc_destroy_session(struct nfs4_session *session,
5751 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005752{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005753 struct rpc_message msg = {
5754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5755 .rpc_argp = session,
5756 .rpc_cred = cred,
5757 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005758 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005759
5760 dprintk("--> nfs4_proc_destroy_session\n");
5761
5762 /* session is still being setup */
5763 if (session->clp->cl_cons_state != NFS_CS_READY)
5764 return status;
5765
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005766 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005767
5768 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005769 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005770 "Session has been destroyed regardless...\n", status);
5771
5772 dprintk("<-- nfs4_proc_destroy_session\n");
5773 return status;
5774}
5775
Trond Myklebust7b38c362012-05-23 13:23:31 -04005776/*
5777 * With sessions, the client is not marked ready until after a
5778 * successful EXCHANGE_ID and CREATE_SESSION.
5779 *
5780 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5781 * other versions of NFS can be tried.
5782 */
5783static int nfs41_check_session_ready(struct nfs_client *clp)
5784{
5785 int ret;
5786
5787 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5788 ret = nfs4_client_recover_expired_lease(clp);
5789 if (ret)
5790 return ret;
5791 }
5792 if (clp->cl_cons_state < NFS_CS_READY)
5793 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005794 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005795 return 0;
5796}
5797
Trond Myklebustfccba802009-07-21 16:48:07 -04005798int nfs4_init_session(struct nfs_server *server)
5799{
5800 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005801 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005802 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005803
5804 if (!nfs4_has_session(clp))
5805 return 0;
5806
Trond Myklebust1055d762010-06-16 09:52:27 -04005807 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005808 spin_lock(&clp->cl_lock);
5809 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005810
Trond Myklebust7b38c362012-05-23 13:23:31 -04005811 rsize = server->rsize;
5812 if (rsize == 0)
5813 rsize = NFS_MAX_FILE_IO_SIZE;
5814 wsize = server->wsize;
5815 if (wsize == 0)
5816 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005817
Trond Myklebust7b38c362012-05-23 13:23:31 -04005818 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5819 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5820 }
5821 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005822
Trond Myklebust7b38c362012-05-23 13:23:31 -04005823 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005824}
5825
Trond Myklebust7b38c362012-05-23 13:23:31 -04005826int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005827{
5828 struct nfs4_session *session = clp->cl_session;
5829 int ret;
5830
Trond Myklebust7b38c362012-05-23 13:23:31 -04005831 spin_lock(&clp->cl_lock);
5832 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5833 /*
5834 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5835 * DS lease to be equal to the MDS lease.
5836 */
5837 clp->cl_lease_time = lease_time;
5838 clp->cl_last_renewal = jiffies;
5839 }
5840 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005841
Trond Myklebust7b38c362012-05-23 13:23:31 -04005842 ret = nfs41_check_session_ready(clp);
5843 if (ret)
5844 return ret;
5845 /* Test for the DS role */
5846 if (!is_ds_client(clp))
5847 return -ENODEV;
5848 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005849}
5850EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5851
5852
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005853/*
5854 * Renew the cl_session lease.
5855 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005856struct nfs4_sequence_data {
5857 struct nfs_client *clp;
5858 struct nfs4_sequence_args args;
5859 struct nfs4_sequence_res res;
5860};
5861
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005862static void nfs41_sequence_release(void *data)
5863{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005864 struct nfs4_sequence_data *calldata = data;
5865 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005866
Alexandros Batsakis71358402010-02-05 03:45:05 -08005867 if (atomic_read(&clp->cl_count) > 1)
5868 nfs4_schedule_state_renewal(clp);
5869 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005870 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005871}
5872
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005873static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5874{
5875 switch(task->tk_status) {
5876 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005877 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5878 return -EAGAIN;
5879 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005880 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005881 }
5882 return 0;
5883}
5884
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005885static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005886{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005887 struct nfs4_sequence_data *calldata = data;
5888 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005889
Trond Myklebust14516c32010-07-31 14:29:06 -04005890 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5891 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005892
5893 if (task->tk_status < 0) {
5894 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005895 if (atomic_read(&clp->cl_count) == 1)
5896 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005897
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005898 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5899 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005900 return;
5901 }
5902 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005903 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005904out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005905 dprintk("<-- %s\n", __func__);
5906}
5907
5908static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5909{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005910 struct nfs4_sequence_data *calldata = data;
5911 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005912 struct nfs4_sequence_args *args;
5913 struct nfs4_sequence_res *res;
5914
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005915 args = task->tk_msg.rpc_argp;
5916 res = task->tk_msg.rpc_resp;
5917
Trond Myklebust9d12b212012-01-17 22:04:25 -05005918 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005919 return;
5920 rpc_call_start(task);
5921}
5922
5923static const struct rpc_call_ops nfs41_sequence_ops = {
5924 .rpc_call_done = nfs41_sequence_call_done,
5925 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005926 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005927};
5928
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005929static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005930{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005931 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005932 struct rpc_message msg = {
5933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5934 .rpc_cred = cred,
5935 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005936 struct rpc_task_setup task_setup_data = {
5937 .rpc_client = clp->cl_rpcclient,
5938 .rpc_message = &msg,
5939 .callback_ops = &nfs41_sequence_ops,
5940 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5941 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005942
Alexandros Batsakis71358402010-02-05 03:45:05 -08005943 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005944 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005945 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005946 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005947 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005948 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005949 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005950 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005951 msg.rpc_argp = &calldata->args;
5952 msg.rpc_resp = &calldata->res;
5953 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005954 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005955
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005956 return rpc_run_task(&task_setup_data);
5957}
5958
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005959static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005960{
5961 struct rpc_task *task;
5962 int ret = 0;
5963
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005964 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5965 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005966 task = _nfs41_proc_sequence(clp, cred);
5967 if (IS_ERR(task))
5968 ret = PTR_ERR(task);
5969 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005970 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005971 dprintk("<-- %s status=%d\n", __func__, ret);
5972 return ret;
5973}
5974
5975static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5976{
5977 struct rpc_task *task;
5978 int ret;
5979
5980 task = _nfs41_proc_sequence(clp, cred);
5981 if (IS_ERR(task)) {
5982 ret = PTR_ERR(task);
5983 goto out;
5984 }
5985 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005986 if (!ret) {
5987 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5988
5989 if (task->tk_status == 0)
5990 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005991 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005992 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005993 rpc_put_task(task);
5994out:
5995 dprintk("<-- %s status=%d\n", __func__, ret);
5996 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005997}
5998
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005999struct nfs4_reclaim_complete_data {
6000 struct nfs_client *clp;
6001 struct nfs41_reclaim_complete_args arg;
6002 struct nfs41_reclaim_complete_res res;
6003};
6004
6005static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6006{
6007 struct nfs4_reclaim_complete_data *calldata = data;
6008
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006009 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006010 if (nfs41_setup_sequence(calldata->clp->cl_session,
6011 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006012 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006013 return;
6014
6015 rpc_call_start(task);
6016}
6017
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006018static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6019{
6020 switch(task->tk_status) {
6021 case 0:
6022 case -NFS4ERR_COMPLETE_ALREADY:
6023 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6024 break;
6025 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006026 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006027 /* fall through */
6028 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006029 return -EAGAIN;
6030 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006031 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006032 }
6033 return 0;
6034}
6035
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006036static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6037{
6038 struct nfs4_reclaim_complete_data *calldata = data;
6039 struct nfs_client *clp = calldata->clp;
6040 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6041
6042 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006043 if (!nfs41_sequence_done(task, res))
6044 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006045
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006046 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6047 rpc_restart_call_prepare(task);
6048 return;
6049 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006050 dprintk("<-- %s\n", __func__);
6051}
6052
6053static void nfs4_free_reclaim_complete_data(void *data)
6054{
6055 struct nfs4_reclaim_complete_data *calldata = data;
6056
6057 kfree(calldata);
6058}
6059
6060static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6061 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6062 .rpc_call_done = nfs4_reclaim_complete_done,
6063 .rpc_release = nfs4_free_reclaim_complete_data,
6064};
6065
6066/*
6067 * Issue a global reclaim complete.
6068 */
6069static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6070{
6071 struct nfs4_reclaim_complete_data *calldata;
6072 struct rpc_task *task;
6073 struct rpc_message msg = {
6074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6075 };
6076 struct rpc_task_setup task_setup_data = {
6077 .rpc_client = clp->cl_rpcclient,
6078 .rpc_message = &msg,
6079 .callback_ops = &nfs4_reclaim_complete_call_ops,
6080 .flags = RPC_TASK_ASYNC,
6081 };
6082 int status = -ENOMEM;
6083
6084 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006085 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006086 if (calldata == NULL)
6087 goto out;
6088 calldata->clp = clp;
6089 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006090
Trond Myklebust9d12b212012-01-17 22:04:25 -05006091 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006092 msg.rpc_argp = &calldata->arg;
6093 msg.rpc_resp = &calldata->res;
6094 task_setup_data.callback_data = calldata;
6095 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006096 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006097 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006098 goto out;
6099 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006100 status = nfs4_wait_for_completion_rpc_task(task);
6101 if (status == 0)
6102 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006103 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006104 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006105out:
6106 dprintk("<-- %s status=%d\n", __func__, status);
6107 return status;
6108}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006109
6110static void
6111nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6112{
6113 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006114 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006115
6116 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006117 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6118 * right now covering the LAYOUTGET we are about to send.
6119 * However, that is not so catastrophic, and there seems
6120 * to be no way to prevent it completely.
6121 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006122 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006123 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006124 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006125 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6126 NFS_I(lgp->args.inode)->layout,
6127 lgp->args.ctx->state)) {
6128 rpc_exit(task, NFS4_OK);
6129 return;
6130 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006131 rpc_call_start(task);
6132}
6133
6134static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6135{
6136 struct nfs4_layoutget *lgp = calldata;
6137 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6138
6139 dprintk("--> %s\n", __func__);
6140
6141 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6142 return;
6143
6144 switch (task->tk_status) {
6145 case 0:
6146 break;
6147 case -NFS4ERR_LAYOUTTRYLATER:
6148 case -NFS4ERR_RECALLCONFLICT:
6149 task->tk_status = -NFS4ERR_DELAY;
6150 /* Fall through */
6151 default:
6152 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6153 rpc_restart_call_prepare(task);
6154 return;
6155 }
6156 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006157 dprintk("<-- %s\n", __func__);
6158}
6159
6160static void nfs4_layoutget_release(void *calldata)
6161{
6162 struct nfs4_layoutget *lgp = calldata;
6163
6164 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006165 put_nfs_open_context(lgp->args.ctx);
6166 kfree(calldata);
6167 dprintk("<-- %s\n", __func__);
6168}
6169
6170static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6171 .rpc_call_prepare = nfs4_layoutget_prepare,
6172 .rpc_call_done = nfs4_layoutget_done,
6173 .rpc_release = nfs4_layoutget_release,
6174};
6175
6176int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6177{
6178 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6179 struct rpc_task *task;
6180 struct rpc_message msg = {
6181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6182 .rpc_argp = &lgp->args,
6183 .rpc_resp = &lgp->res,
6184 };
6185 struct rpc_task_setup task_setup_data = {
6186 .rpc_client = server->client,
6187 .rpc_message = &msg,
6188 .callback_ops = &nfs4_layoutget_call_ops,
6189 .callback_data = lgp,
6190 .flags = RPC_TASK_ASYNC,
6191 };
6192 int status = 0;
6193
6194 dprintk("--> %s\n", __func__);
6195
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006196 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006197 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006198 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006199 task = rpc_run_task(&task_setup_data);
6200 if (IS_ERR(task))
6201 return PTR_ERR(task);
6202 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006203 if (status == 0)
6204 status = task->tk_status;
6205 if (status == 0)
6206 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006207 rpc_put_task(task);
6208 dprintk("<-- %s status=%d\n", __func__, status);
6209 return status;
6210}
6211
Benny Halevycbe82602011-05-22 19:52:37 +03006212static void
6213nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6214{
6215 struct nfs4_layoutreturn *lrp = calldata;
6216
6217 dprintk("--> %s\n", __func__);
6218 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006219 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006220 return;
6221 rpc_call_start(task);
6222}
6223
6224static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6225{
6226 struct nfs4_layoutreturn *lrp = calldata;
6227 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006228 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006229
6230 dprintk("--> %s\n", __func__);
6231
6232 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6233 return;
6234
6235 server = NFS_SERVER(lrp->args.inode);
6236 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006237 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006238 return;
6239 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006240 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006241 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006242 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006243 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006244 } else
6245 BUG_ON(!list_empty(&lo->plh_segs));
6246 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006247 lo->plh_block_lgets--;
6248 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006249 dprintk("<-- %s\n", __func__);
6250}
6251
6252static void nfs4_layoutreturn_release(void *calldata)
6253{
6254 struct nfs4_layoutreturn *lrp = calldata;
6255
6256 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006257 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006258 kfree(calldata);
6259 dprintk("<-- %s\n", __func__);
6260}
6261
6262static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6263 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6264 .rpc_call_done = nfs4_layoutreturn_done,
6265 .rpc_release = nfs4_layoutreturn_release,
6266};
6267
6268int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6269{
6270 struct rpc_task *task;
6271 struct rpc_message msg = {
6272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6273 .rpc_argp = &lrp->args,
6274 .rpc_resp = &lrp->res,
6275 };
6276 struct rpc_task_setup task_setup_data = {
6277 .rpc_client = lrp->clp->cl_rpcclient,
6278 .rpc_message = &msg,
6279 .callback_ops = &nfs4_layoutreturn_call_ops,
6280 .callback_data = lrp,
6281 };
6282 int status;
6283
6284 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006285 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006286 task = rpc_run_task(&task_setup_data);
6287 if (IS_ERR(task))
6288 return PTR_ERR(task);
6289 status = task->tk_status;
6290 dprintk("<-- %s status=%d\n", __func__, status);
6291 rpc_put_task(task);
6292 return status;
6293}
6294
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006295/*
6296 * Retrieve the list of Data Server devices from the MDS.
6297 */
6298static int _nfs4_getdevicelist(struct nfs_server *server,
6299 const struct nfs_fh *fh,
6300 struct pnfs_devicelist *devlist)
6301{
6302 struct nfs4_getdevicelist_args args = {
6303 .fh = fh,
6304 .layoutclass = server->pnfs_curr_ld->id,
6305 };
6306 struct nfs4_getdevicelist_res res = {
6307 .devlist = devlist,
6308 };
6309 struct rpc_message msg = {
6310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6311 .rpc_argp = &args,
6312 .rpc_resp = &res,
6313 };
6314 int status;
6315
6316 dprintk("--> %s\n", __func__);
6317 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6318 &res.seq_res, 0);
6319 dprintk("<-- %s status=%d\n", __func__, status);
6320 return status;
6321}
6322
6323int nfs4_proc_getdevicelist(struct nfs_server *server,
6324 const struct nfs_fh *fh,
6325 struct pnfs_devicelist *devlist)
6326{
6327 struct nfs4_exception exception = { };
6328 int err;
6329
6330 do {
6331 err = nfs4_handle_exception(server,
6332 _nfs4_getdevicelist(server, fh, devlist),
6333 &exception);
6334 } while (exception.retry);
6335
6336 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6337 err, devlist->num_devs);
6338
6339 return err;
6340}
6341EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6342
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006343static int
6344_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6345{
6346 struct nfs4_getdeviceinfo_args args = {
6347 .pdev = pdev,
6348 };
6349 struct nfs4_getdeviceinfo_res res = {
6350 .pdev = pdev,
6351 };
6352 struct rpc_message msg = {
6353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6354 .rpc_argp = &args,
6355 .rpc_resp = &res,
6356 };
6357 int status;
6358
6359 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006360 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006361 dprintk("<-- %s status=%d\n", __func__, status);
6362
6363 return status;
6364}
6365
6366int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6367{
6368 struct nfs4_exception exception = { };
6369 int err;
6370
6371 do {
6372 err = nfs4_handle_exception(server,
6373 _nfs4_proc_getdeviceinfo(server, pdev),
6374 &exception);
6375 } while (exception.retry);
6376 return err;
6377}
6378EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6379
Andy Adamson863a3c62011-03-23 13:27:54 +00006380static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6381{
6382 struct nfs4_layoutcommit_data *data = calldata;
6383 struct nfs_server *server = NFS_SERVER(data->args.inode);
6384
6385 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006386 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006387 return;
6388 rpc_call_start(task);
6389}
6390
6391static void
6392nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6393{
6394 struct nfs4_layoutcommit_data *data = calldata;
6395 struct nfs_server *server = NFS_SERVER(data->args.inode);
6396
6397 if (!nfs4_sequence_done(task, &data->res.seq_res))
6398 return;
6399
6400 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006401 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6402 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6403 case -NFS4ERR_BADLAYOUT: /* no layout */
6404 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006405 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006406 break;
6407 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006408 nfs_post_op_update_inode_force_wcc(data->args.inode,
6409 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006410 break;
6411 default:
6412 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6413 rpc_restart_call_prepare(task);
6414 return;
6415 }
6416 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006417}
6418
6419static void nfs4_layoutcommit_release(void *calldata)
6420{
6421 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006422 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006423 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006424
Andy Adamsondb29c082011-07-30 20:52:38 -04006425 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006426 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006427 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6428 list_del_init(&lseg->pls_lc_list);
6429 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6430 &lseg->pls_flags))
6431 put_lseg(lseg);
6432 }
Peng Tao92407e72011-10-23 20:21:17 -07006433
6434 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6435 smp_mb__after_clear_bit();
6436 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6437
Andy Adamson863a3c62011-03-23 13:27:54 +00006438 put_rpccred(data->cred);
6439 kfree(data);
6440}
6441
6442static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6443 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6444 .rpc_call_done = nfs4_layoutcommit_done,
6445 .rpc_release = nfs4_layoutcommit_release,
6446};
6447
6448int
Andy Adamsonef311532011-03-12 02:58:10 -05006449nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006450{
6451 struct rpc_message msg = {
6452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6453 .rpc_argp = &data->args,
6454 .rpc_resp = &data->res,
6455 .rpc_cred = data->cred,
6456 };
6457 struct rpc_task_setup task_setup_data = {
6458 .task = &data->task,
6459 .rpc_client = NFS_CLIENT(data->args.inode),
6460 .rpc_message = &msg,
6461 .callback_ops = &nfs4_layoutcommit_ops,
6462 .callback_data = data,
6463 .flags = RPC_TASK_ASYNC,
6464 };
6465 struct rpc_task *task;
6466 int status = 0;
6467
6468 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6469 "lbw: %llu inode %lu\n",
6470 data->task.tk_pid, sync,
6471 data->args.lastbytewritten,
6472 data->args.inode->i_ino);
6473
Trond Myklebust9d12b212012-01-17 22:04:25 -05006474 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006475 task = rpc_run_task(&task_setup_data);
6476 if (IS_ERR(task))
6477 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006478 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006479 goto out;
6480 status = nfs4_wait_for_completion_rpc_task(task);
6481 if (status != 0)
6482 goto out;
6483 status = task->tk_status;
6484out:
6485 dprintk("%s: status %d\n", __func__, status);
6486 rpc_put_task(task);
6487 return status;
6488}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006489
6490static int
6491_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6492 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6493{
6494 struct nfs41_secinfo_no_name_args args = {
6495 .style = SECINFO_STYLE_CURRENT_FH,
6496 };
6497 struct nfs4_secinfo_res res = {
6498 .flavors = flavors,
6499 };
6500 struct rpc_message msg = {
6501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6502 .rpc_argp = &args,
6503 .rpc_resp = &res,
6504 };
6505 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6506}
6507
6508static int
6509nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6510 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6511{
6512 struct nfs4_exception exception = { };
6513 int err;
6514 do {
6515 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6516 switch (err) {
6517 case 0:
6518 case -NFS4ERR_WRONGSEC:
6519 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006520 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006521 default:
6522 err = nfs4_handle_exception(server, err, &exception);
6523 }
6524 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006525out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006526 return err;
6527}
6528
6529static int
6530nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6531 struct nfs_fsinfo *info)
6532{
6533 int err;
6534 struct page *page;
6535 rpc_authflavor_t flavor;
6536 struct nfs4_secinfo_flavors *flavors;
6537
6538 page = alloc_page(GFP_KERNEL);
6539 if (!page) {
6540 err = -ENOMEM;
6541 goto out;
6542 }
6543
6544 flavors = page_address(page);
6545 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6546
6547 /*
6548 * Fall back on "guess and check" method if
6549 * the server doesn't support SECINFO_NO_NAME
6550 */
6551 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6552 err = nfs4_find_root_sec(server, fhandle, info);
6553 goto out_freepage;
6554 }
6555 if (err)
6556 goto out_freepage;
6557
6558 flavor = nfs_find_best_sec(flavors);
6559 if (err == 0)
6560 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6561
6562out_freepage:
6563 put_page(page);
6564 if (err == -EACCES)
6565 return -EPERM;
6566out:
6567 return err;
6568}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006569
6570static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006571{
6572 int status;
6573 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006574 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006575 };
6576 struct nfs41_test_stateid_res res;
6577 struct rpc_message msg = {
6578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6579 .rpc_argp = &args,
6580 .rpc_resp = &res,
6581 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006582
Trond Myklebust9d12b212012-01-17 22:04:25 -05006583 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006584 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever377e5072012-07-11 16:29:45 -04006585 if (status != NFS_OK)
6586 return status;
6587 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006588}
6589
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006590static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006591{
6592 struct nfs4_exception exception = { };
6593 int err;
6594 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006595 err = _nfs41_test_stateid(server, stateid);
6596 if (err != -NFS4ERR_DELAY)
6597 break;
6598 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006599 } while (exception.retry);
6600 return err;
6601}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006602
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006603static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006604{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006605 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006606 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006607 };
6608 struct nfs41_free_stateid_res res;
6609 struct rpc_message msg = {
6610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6611 .rpc_argp = &args,
6612 .rpc_resp = &res,
6613 };
6614
Trond Myklebust9d12b212012-01-17 22:04:25 -05006615 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever377e5072012-07-11 16:29:45 -04006616 return nfs4_call_sync_sequence(server->client, server, &msg,
6617 &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006618}
6619
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006620static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006621{
6622 struct nfs4_exception exception = { };
6623 int err;
6624 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006625 err = _nfs4_free_stateid(server, stateid);
6626 if (err != -NFS4ERR_DELAY)
6627 break;
6628 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006629 } while (exception.retry);
6630 return err;
6631}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006632
6633static bool nfs41_match_stateid(const nfs4_stateid *s1,
6634 const nfs4_stateid *s2)
6635{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006636 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006637 return false;
6638
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006639 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006640 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006641 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006642 return true;
6643
6644 return false;
6645}
6646
Andy Adamson557134a2009-04-01 09:21:53 -04006647#endif /* CONFIG_NFS_V4_1 */
6648
Trond Myklebust36281ca2012-03-04 18:13:56 -05006649static bool nfs4_match_stateid(const nfs4_stateid *s1,
6650 const nfs4_stateid *s2)
6651{
Trond Myklebustf597c532012-03-04 18:13:56 -05006652 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006653}
6654
6655
Trond Myklebust17280172012-03-11 13:11:00 -04006656static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006657 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006658 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 .recover_open = nfs4_open_reclaim,
6660 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006661 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006662 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663};
6664
Andy Adamson591d71c2009-04-01 09:22:47 -04006665#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006666static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006667 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6668 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6669 .recover_open = nfs4_open_reclaim,
6670 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006671 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006672 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006673 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006674};
6675#endif /* CONFIG_NFS_V4_1 */
6676
Trond Myklebust17280172012-03-11 13:11:00 -04006677static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006678 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006679 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 .recover_open = nfs4_open_expired,
6681 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006682 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006683 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684};
6685
Andy Adamson591d71c2009-04-01 09:22:47 -04006686#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006687static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006688 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6689 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006690 .recover_open = nfs41_open_expired,
6691 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006692 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006693 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006694};
6695#endif /* CONFIG_NFS_V4_1 */
6696
Trond Myklebust17280172012-03-11 13:11:00 -04006697static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006698 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006699 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006700 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006701};
6702
6703#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006704static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006705 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006706 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006707 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006708};
6709#endif
6710
Trond Myklebust97dc1352010-06-16 09:52:26 -04006711static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6712 .minor_version = 0,
6713 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006714 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006715 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006716 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6717 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6718 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006719};
6720
6721#if defined(CONFIG_NFS_V4_1)
6722static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6723 .minor_version = 1,
6724 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006725 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006726 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006727 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6728 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6729 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006730};
6731#endif
6732
Trond Myklebust97dc1352010-06-16 09:52:26 -04006733const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6734 [0] = &nfs_v4_0_minor_ops,
6735#if defined(CONFIG_NFS_V4_1)
6736 [1] = &nfs_v4_1_minor_ops,
6737#endif
6738};
6739
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006740static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006741 .permission = nfs_permission,
6742 .getattr = nfs_getattr,
6743 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006744 .getxattr = generic_getxattr,
6745 .setxattr = generic_setxattr,
6746 .listxattr = generic_listxattr,
6747 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006748};
6749
David Howells509de812006-08-22 20:06:11 -04006750const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 .version = 4, /* protocol version */
6752 .dentry_ops = &nfs4_dentry_operations,
6753 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006754 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006755 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006757 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 .getattr = nfs4_proc_getattr,
6759 .setattr = nfs4_proc_setattr,
6760 .lookup = nfs4_proc_lookup,
6761 .access = nfs4_proc_access,
6762 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 .create = nfs4_proc_create,
6764 .remove = nfs4_proc_remove,
6765 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006766 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 .unlink_done = nfs4_proc_unlink_done,
6768 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006769 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006770 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006771 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 .link = nfs4_proc_link,
6773 .symlink = nfs4_proc_symlink,
6774 .mkdir = nfs4_proc_mkdir,
6775 .rmdir = nfs4_proc_remove,
6776 .readdir = nfs4_proc_readdir,
6777 .mknod = nfs4_proc_mknod,
6778 .statfs = nfs4_proc_statfs,
6779 .fsinfo = nfs4_proc_fsinfo,
6780 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006781 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 .decode_dirent = nfs4_decode_dirent,
6783 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006784 .read_pageio_init = pnfs_pageio_init_read,
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 Schumaker57208fa2012-06-20 15:53:48 -04006788 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006789 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006790 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006792 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006793 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006795 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006796 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006797 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006798 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006799 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006800 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006801 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006802 .free_client = nfs4_free_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803};
6804
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006805static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6806 .prefix = XATTR_NAME_NFSV4_ACL,
6807 .list = nfs4_xattr_list_nfs4_acl,
6808 .get = nfs4_xattr_get_nfs4_acl,
6809 .set = nfs4_xattr_set_nfs4_acl,
6810};
6811
6812const struct xattr_handler *nfs4_xattr_handlers[] = {
6813 &nfs4_xattr_nfs4_acl_handler,
6814 NULL
6815};
6816
Trond Myklebustef159e92012-02-06 19:50:40 -05006817module_param(max_session_slots, ushort, 0644);
6818MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6819 "requests the client will negotiate");
6820
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821/*
6822 * Local variables:
6823 * c-basic-offset: 8
6824 * End:
6825 */