blob: f99cf71f4e36bb44cd8d7cf484c13a5e78feca72 [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 Myklebust6926afd12012-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust035168ab2010-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 Myklebust6926afd12012-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 Myklebust6926afd12012-01-07 13:22:46 -05001665 nfs_fattr_map_and_free_names(server, &data->f_attr);
1666
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001667 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001668 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001669 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1670 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001672 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001674 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001677 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Andy Adamsond83217c2011-03-01 01:34:17 +00001681static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001682{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001683 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001684 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001685
Trond Myklebusta78cb572009-08-09 15:06:19 -04001686 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001687 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001688 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001689 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001690 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1691 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001692 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001693 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001694 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001695 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001696 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001697}
1698
Andy Adamsond83217c2011-03-01 01:34:17 +00001699static int nfs4_recover_expired_lease(struct nfs_server *server)
1700{
1701 return nfs4_client_recover_expired_lease(server->nfs_client);
1702}
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704/*
1705 * OPEN_EXPIRED:
1706 * reclaim state on the server after a network partition.
1707 * Assumes caller holds the appropriate lock
1708 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001709static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001711 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001712 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001714 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1715 if (IS_ERR(opendata))
1716 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001717 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001718 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001719 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001720 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001724static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001725{
Trond Myklebust539cd032007-06-05 11:46:42 -04001726 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727 struct nfs4_exception exception = { };
1728 int err;
1729
1730 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001731 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001732 switch (err) {
1733 default:
1734 goto out;
1735 case -NFS4ERR_GRACE:
1736 case -NFS4ERR_DELAY:
1737 nfs4_handle_exception(server, err, &exception);
1738 err = 0;
1739 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001740 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001741out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001742 return err;
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Trond Myklebust864472e2006-01-03 09:55:15 +01001750 ctx = nfs4_state_find_open_context(state);
1751 if (IS_ERR(ctx))
1752 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001753 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001754 put_nfs_open_context(ctx);
1755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001758#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001759static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001761 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762 struct nfs_server *server = NFS_SERVER(state->inode);
1763
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001764 if (state->flags & flags) {
1765 status = nfs41_test_stateid(server, stateid);
1766 if (status != NFS_OK) {
1767 nfs41_free_stateid(server, stateid);
1768 state->flags &= ~flags;
1769 }
1770 }
1771 return status;
1772}
1773
1774static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1775{
1776 int deleg_status, open_status;
1777 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1778 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1779
1780 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1781 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1782
1783 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1784 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001785 return nfs4_open_expired(sp, state);
1786}
1787#endif
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001790 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1791 * fields corresponding to attributes that were used to store the verifier.
1792 * Make sure we clobber those fields in the later setattr call
1793 */
1794static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1795{
1796 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1797 !(sattr->ia_valid & ATTR_ATIME_SET))
1798 sattr->ia_valid |= ATTR_ATIME;
1799
1800 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1801 !(sattr->ia_valid & ATTR_MTIME_SET))
1802 sattr->ia_valid |= ATTR_MTIME;
1803}
1804
1805/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001806 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Andy Adamson82be4172012-05-23 05:02:35 -04001808static int _nfs4_do_open(struct inode *dir,
1809 struct dentry *dentry,
1810 fmode_t fmode,
1811 int flags,
1812 struct iattr *sattr,
1813 struct rpc_cred *cred,
1814 struct nfs4_state **res,
1815 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 struct nfs4_state_owner *sp;
1818 struct nfs4_state *state = NULL;
1819 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001820 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001821 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001825 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1826 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1828 goto out_err;
1829 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001830 status = nfs4_recover_expired_lease(server);
1831 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001832 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001833 if (dentry->d_inode != NULL)
1834 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001835 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001836 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001837 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001838 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Andy Adamson82be4172012-05-23 05:02:35 -04001840 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1841 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1842 if (!opendata->f_attr.mdsthreshold)
1843 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001844 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001845 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001846 if (dentry->d_inode != NULL)
1847 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001848
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001849 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001851 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001853 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001854 status = PTR_ERR(state);
1855 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001856 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001857 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001858 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001859
1860 if (opendata->o_arg.open_flags & O_EXCL) {
1861 nfs4_exclusive_attrset(opendata, sattr);
1862
1863 nfs_fattr_init(opendata->o_res.f_attr);
1864 status = nfs4_do_setattr(state->inode, cred,
1865 opendata->o_res.f_attr, sattr,
1866 state);
1867 if (status == 0)
1868 nfs_setattr_update_inode(state->inode, sattr);
1869 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1870 }
Andy Adamson82be4172012-05-23 05:02:35 -04001871
1872 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1873 *ctx_th = opendata->f_attr.mdsthreshold;
1874 else
1875 kfree(opendata->f_attr.mdsthreshold);
1876 opendata->f_attr.mdsthreshold = NULL;
1877
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001878 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 *res = state;
1881 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001882err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001883 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001884 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001885err_put_state_owner:
1886 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 *res = NULL;
1889 return status;
1890}
1891
1892
Andy Adamson82be4172012-05-23 05:02:35 -04001893static struct nfs4_state *nfs4_do_open(struct inode *dir,
1894 struct dentry *dentry,
1895 fmode_t fmode,
1896 int flags,
1897 struct iattr *sattr,
1898 struct rpc_cred *cred,
1899 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 struct nfs4_exception exception = { };
1902 struct nfs4_state *res;
1903 int status;
1904
Trond Myklebust2d0dbc62012-06-08 10:58:09 -04001905 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001907 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1908 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (status == 0)
1910 break;
1911 /* NOTE: BAD_SEQID means the server and client disagree about the
1912 * book-keeping w.r.t. state-changing operations
1913 * (OPEN/CLOSE/LOCK/LOCKU...)
1914 * It is actually a sign of a bug on the client or on the server.
1915 *
1916 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001917 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 * have unhashed the old state_owner for us, and that we can
1919 * therefore safely retry using a new one. We should still warn
1920 * the user though...
1921 */
1922 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001923 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001924 " returned a bad sequence-id error!\n",
1925 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 exception.retry = 1;
1927 continue;
1928 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001929 /*
1930 * BAD_STATEID on OPEN means that the server cancelled our
1931 * state before it received the OPEN_CONFIRM.
1932 * Recover by retrying the request as per the discussion
1933 * on Page 181 of RFC3530.
1934 */
1935 if (status == -NFS4ERR_BAD_STATEID) {
1936 exception.retry = 1;
1937 continue;
1938 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001939 if (status == -EAGAIN) {
1940 /* We must have found a delegation */
1941 exception.retry = 1;
1942 continue;
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1945 status, &exception));
1946 } while (exception.retry);
1947 return res;
1948}
1949
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001950static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1951 struct nfs_fattr *fattr, struct iattr *sattr,
1952 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001954 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001956 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 .iap = sattr,
1958 .server = server,
1959 .bitmask = server->attr_bitmask,
1960 };
1961 struct nfs_setattrres res = {
1962 .fattr = fattr,
1963 .server = server,
1964 };
1965 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1967 .rpc_argp = &arg,
1968 .rpc_resp = &res,
1969 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001971 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001972 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Trond Myklebust0e574af2005-10-27 22:12:38 -04001974 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Trond Myklebust4fc87962012-03-08 17:42:01 -05001976 if (state != NULL) {
1977 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1978 current->files, current->tgid);
1979 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1980 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001981 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001982 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001983 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Bryan Schumaker7c513052011-03-24 17:12:24 +00001985 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001986 if (status == 0 && state != NULL)
1987 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001988 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001991static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1992 struct nfs_fattr *fattr, struct iattr *sattr,
1993 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001995 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001996 struct nfs4_exception exception = {
1997 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001998 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 int err;
2001 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002002 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2003 switch (err) {
2004 case -NFS4ERR_OPENMODE:
2005 if (state && !(state->state & FMODE_WRITE)) {
2006 err = -EBADF;
2007 if (sattr->ia_valid & ATTR_OPEN)
2008 err = -EACCES;
2009 goto out;
2010 }
2011 }
2012 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002014out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return err;
2016}
2017
2018struct nfs4_closedata {
2019 struct inode *inode;
2020 struct nfs4_state *state;
2021 struct nfs_closeargs arg;
2022 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002023 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002024 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002025 bool roc;
2026 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027};
2028
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002029static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002030{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002031 struct nfs4_closedata *calldata = data;
2032 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002033 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002034
Fred Isamanf7e89172011-01-06 11:36:32 +00002035 if (calldata->roc)
2036 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002037 nfs4_put_open_state(calldata->state);
2038 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002039 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002040 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002041 kfree(calldata);
2042}
2043
Trond Myklebust88069f72009-12-08 08:33:16 -05002044static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2045 fmode_t fmode)
2046{
2047 spin_lock(&state->owner->so_lock);
2048 if (!(fmode & FMODE_READ))
2049 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2050 if (!(fmode & FMODE_WRITE))
2051 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2052 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2053 spin_unlock(&state->owner->so_lock);
2054}
2055
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002056static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002058 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct nfs_server *server = NFS_SERVER(calldata->inode);
2061
Chuck Levera3ca5652012-03-01 17:00:40 -05002062 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002063 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2064 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 /* hmm. we are done with the inode, and in the process of freeing
2066 * the state_owner. we keep this around to process errors
2067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 switch (task->tk_status) {
2069 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002070 if (calldata->roc)
2071 pnfs_roc_set_barrier(state->inode,
2072 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002073 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002074 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002075 nfs4_close_clear_stateid_flags(state,
2076 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 break;
2078 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002079 case -NFS4ERR_OLD_STATEID:
2080 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002082 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002085 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2086 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002088 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002089 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002090 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002093static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002095 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002096 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002097 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002098
Chuck Levera3ca5652012-03-01 17:00:40 -05002099 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002100 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002101 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002102
Trond Myklebust88069f72009-12-08 08:33:16 -05002103 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2104 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002105 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002106 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002107 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002108 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002109 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2110 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2111 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002112 }
2113 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002114 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2115 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2116 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002117 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002118 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002119 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002120
2121 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002122 /* Note: exit _without_ calling nfs4_close_done */
2123 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002124 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002125 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002126
Fred Isamanf7e89172011-01-06 11:36:32 +00002127 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002128 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002129 if (calldata->roc &&
2130 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2131 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2132 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002133 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002134 }
2135 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002136
Trond Myklebust516a6af2005-10-27 22:12:41 -04002137 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002138 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002139 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002140 &calldata->arg.seq_args,
2141 &calldata->res.seq_res,
2142 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002143 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002144 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002145out:
2146 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002149static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002150 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002151 .rpc_call_done = nfs4_close_done,
2152 .rpc_release = nfs4_free_closedata,
2153};
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155/*
2156 * It is possible for data to be read/written from a mem-mapped file
2157 * after the sys_close call (which hits the vfs layer as a flush).
2158 * This means that we can't safely call nfsv4 close on a file until
2159 * the inode is cleared. This in turn means that we are not good
2160 * NFSv4 citizens - we do not indicate to the server to update the file's
2161 * share state even when we are done with one of the three share
2162 * stateid's in the inode.
2163 *
2164 * NOTE: Caller must be holding the sp->so_owner semaphore!
2165 */
Al Viro643168c2011-06-22 18:20:23 -04002166int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002168 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002170 struct nfs4_state_owner *sp = state->owner;
2171 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2174 .rpc_cred = state->owner->so_cred,
2175 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 struct rpc_task_setup task_setup_data = {
2177 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002178 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002180 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002181 .flags = RPC_TASK_ASYNC,
2182 };
Trond Myklebust95121352005-10-18 14:20:12 -07002183 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002185 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002187 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002188 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002189 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002191 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002192 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002194 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002195 if (calldata->arg.seqid == NULL)
2196 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002197 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002198 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002199 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002200 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002201 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002202 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002203 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002204
Trond Myklebust1174dd12010-12-21 10:52:24 -05002205 msg.rpc_argp = &calldata->arg;
2206 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002207 task_setup_data.callback_data = calldata;
2208 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002209 if (IS_ERR(task))
2210 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002211 status = 0;
2212 if (wait)
2213 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002214 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002215 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002216out_free_calldata:
2217 kfree(calldata);
2218out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002219 if (roc)
2220 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002221 nfs4_put_open_state(state);
2222 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002223 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Trond Myklebust2b484292010-09-17 10:56:51 -04002226static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002227nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 struct nfs4_state *state;
2230
Trond Myklebust565277f2007-10-15 18:17:53 -04002231 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002232 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2233 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002234 if (IS_ERR(state))
2235 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002236 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002237 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Trond Myklebust1185a552009-12-03 15:54:02 -05002240static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002241{
2242 if (ctx->state == NULL)
2243 return;
2244 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002245 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002246 else
Al Viro643168c2011-06-22 18:20:23 -04002247 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2251{
Benny Halevy43652ad2009-04-01 09:21:54 -04002252 struct nfs4_server_caps_arg args = {
2253 .fhandle = fhandle,
2254 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 struct nfs4_server_caps_res res = {};
2256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002258 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .rpc_resp = &res,
2260 };
2261 int status;
2262
Bryan Schumaker7c513052011-03-24 17:12:24 +00002263 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (status == 0) {
2265 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002266 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2267 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2268 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2269 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2270 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2272 server->caps |= NFS_CAP_ACLS;
2273 if (res.has_links != 0)
2274 server->caps |= NFS_CAP_HARDLINKS;
2275 if (res.has_symlinks != 0)
2276 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002277 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2278 server->caps |= NFS_CAP_FILEID;
2279 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2280 server->caps |= NFS_CAP_MODE;
2281 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2282 server->caps |= NFS_CAP_NLINK;
2283 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2284 server->caps |= NFS_CAP_OWNER;
2285 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2286 server->caps |= NFS_CAP_OWNER_GROUP;
2287 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2288 server->caps |= NFS_CAP_ATIME;
2289 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2290 server->caps |= NFS_CAP_CTIME;
2291 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2292 server->caps |= NFS_CAP_MTIME;
2293
Trond Myklebusta65318b2009-03-11 14:10:28 -04002294 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2295 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2296 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002298 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return status;
2302}
2303
Trond Myklebust55a97592006-06-09 09:34:19 -04002304int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
2306 struct nfs4_exception exception = { };
2307 int err;
2308 do {
2309 err = nfs4_handle_exception(server,
2310 _nfs4_server_capabilities(server, fhandle),
2311 &exception);
2312 } while (exception.retry);
2313 return err;
2314}
2315
2316static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2317 struct nfs_fsinfo *info)
2318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 struct nfs4_lookup_root_arg args = {
2320 .bitmask = nfs4_fattr_bitmap,
2321 };
2322 struct nfs4_lookup_res res = {
2323 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002324 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 .fh = fhandle,
2326 };
2327 struct rpc_message msg = {
2328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2329 .rpc_argp = &args,
2330 .rpc_resp = &res,
2331 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002332
Trond Myklebust0e574af2005-10-27 22:12:38 -04002333 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
2337static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2338 struct nfs_fsinfo *info)
2339{
2340 struct nfs4_exception exception = { };
2341 int err;
2342 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002343 err = _nfs4_lookup_root(server, fhandle, info);
2344 switch (err) {
2345 case 0:
2346 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002347 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002348 default:
2349 err = nfs4_handle_exception(server, err, &exception);
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return err;
2354}
2355
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002356static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2357 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2358{
2359 struct rpc_auth *auth;
2360 int ret;
2361
2362 auth = rpcauth_create(flavor, server->client);
2363 if (!auth) {
2364 ret = -EIO;
2365 goto out;
2366 }
2367 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002368out:
2369 return ret;
2370}
2371
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002372static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002373 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002375 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002376 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002378 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2379 flav_array[len] = RPC_AUTH_NULL;
2380 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002381
2382 for (i = 0; i < len; i++) {
2383 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002384 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002385 continue;
2386 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002387 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002388 /*
2389 * -EACCESS could mean that the user doesn't have correct permissions
2390 * to access the mount. It could also mean that we tried to mount
2391 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2392 * existing mount programs don't handle -EACCES very well so it should
2393 * be mapped to -EPERM instead.
2394 */
2395 if (status == -EACCES)
2396 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002397 return status;
2398}
2399
2400/*
2401 * get the file handle for the "/" directory on the server
2402 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002403int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2404 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002405{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002406 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002407 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002408 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2409 /*
2410 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2411 * by nfs4_map_errors() as this function exits.
2412 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002413 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (status == 0)
2415 status = nfs4_server_capabilities(server, fhandle);
2416 if (status == 0)
2417 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002418 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Bryan Schumakerbae36242012-05-10 15:07:31 -04002421static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2422 struct nfs_fsinfo *info)
2423{
2424 int error;
2425 struct nfs_fattr *fattr = info->fattr;
2426
2427 error = nfs4_server_capabilities(server, mntfh);
2428 if (error < 0) {
2429 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2430 return error;
2431 }
2432
2433 error = nfs4_proc_getattr(server, mntfh, fattr);
2434 if (error < 0) {
2435 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2436 return error;
2437 }
2438
2439 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2440 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2441 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2442
2443 return error;
2444}
2445
Manoj Naik6b97fd32006-06-09 09:34:29 -04002446/*
2447 * Get locations and (maybe) other attributes of a referral.
2448 * Note that we'll actually follow the referral later when
2449 * we detect fsid mismatch in inode revalidation
2450 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002451static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2452 const struct qstr *name, struct nfs_fattr *fattr,
2453 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002454{
2455 int status = -ENOMEM;
2456 struct page *page = NULL;
2457 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002458
2459 page = alloc_page(GFP_KERNEL);
2460 if (page == NULL)
2461 goto out;
2462 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2463 if (locations == NULL)
2464 goto out;
2465
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002466 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002467 if (status != 0)
2468 goto out;
2469 /* Make sure server returned a different fsid for the referral */
2470 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002471 dprintk("%s: server did not return a different fsid for"
2472 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002473 status = -EIO;
2474 goto out;
2475 }
Andy Adamson533eb462011-06-13 18:25:56 -04002476 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2477 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002478
Andy Adamson533eb462011-06-13 18:25:56 -04002479 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002480 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002481 memset(fhandle, 0, sizeof(struct nfs_fh));
2482out:
2483 if (page)
2484 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002485 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002486 return status;
2487}
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2490{
2491 struct nfs4_getattr_arg args = {
2492 .fh = fhandle,
2493 .bitmask = server->attr_bitmask,
2494 };
2495 struct nfs4_getattr_res res = {
2496 .fattr = fattr,
2497 .server = server,
2498 };
2499 struct rpc_message msg = {
2500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2501 .rpc_argp = &args,
2502 .rpc_resp = &res,
2503 };
2504
Trond Myklebust0e574af2005-10-27 22:12:38 -04002505 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002506 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
2509static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2510{
2511 struct nfs4_exception exception = { };
2512 int err;
2513 do {
2514 err = nfs4_handle_exception(server,
2515 _nfs4_proc_getattr(server, fhandle, fattr),
2516 &exception);
2517 } while (exception.retry);
2518 return err;
2519}
2520
2521/*
2522 * The file is not closed if it is opened due to the a request to change
2523 * the size of the file. The open call will not be needed once the
2524 * VFS layer lookup-intents are implemented.
2525 *
2526 * Close is called when the inode is destroyed.
2527 * If we haven't opened the file for O_WRONLY, we
2528 * need to in the size_change case to obtain a stateid.
2529 *
2530 * Got race?
2531 * Because OPEN is always done by name in nfsv4, it is
2532 * possible that we opened a different file by the same
2533 * name. We can recognize this race condition, but we
2534 * can't do anything about it besides returning an error.
2535 *
2536 * This will be fixed with VFS changes (lookup-intent).
2537 */
2538static int
2539nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2540 struct iattr *sattr)
2541{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002542 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002543 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002544 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 int status;
2546
Benny Halevy8a1636c2010-07-14 15:43:57 -04002547 if (pnfs_ld_layoutret_on_setattr(inode))
2548 pnfs_return_layout(inode);
2549
Trond Myklebust0e574af2005-10-27 22:12:38 -04002550 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Andy Adamson26699402012-05-30 16:12:24 -04002552 /* Deal with open(O_TRUNC) */
2553 if (sattr->ia_valid & ATTR_OPEN)
2554 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2555
2556 /* Optimization: if the end result is no change, don't RPC */
2557 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2558 return 0;
2559
Trond Myklebustd5308382005-11-04 15:33:38 -05002560 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002561 if (sattr->ia_valid & ATTR_FILE) {
2562 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002563
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002564 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002565 if (ctx) {
2566 cred = ctx->cred;
2567 state = ctx->state;
2568 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002569 }
2570
2571 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002572 if (status == 0)
2573 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return status;
2575}
2576
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002577static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2578 const struct qstr *name, struct nfs_fh *fhandle,
2579 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002580{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002581 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002582 int status;
2583 struct nfs4_lookup_arg args = {
2584 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002585 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002586 .name = name,
2587 };
2588 struct nfs4_lookup_res res = {
2589 .server = server,
2590 .fattr = fattr,
2591 .fh = fhandle,
2592 };
2593 struct rpc_message msg = {
2594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2595 .rpc_argp = &args,
2596 .rpc_resp = &res,
2597 };
2598
2599 nfs_fattr_init(fattr);
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002602 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 dprintk("NFS reply lookup: %d\n", status);
2604 return status;
2605}
2606
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002607static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002608{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002609 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002610 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002611 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2612 fattr->nlink = 2;
2613}
2614
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002615static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2616 struct qstr *name, struct nfs_fh *fhandle,
2617 struct nfs_fattr *fattr)
2618{
2619 struct nfs4_exception exception = { };
2620 struct rpc_clnt *client = *clnt;
2621 int err;
2622 do {
2623 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2624 switch (err) {
2625 case -NFS4ERR_BADNAME:
2626 err = -ENOENT;
2627 goto out;
2628 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002629 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002630 goto out;
2631 case -NFS4ERR_WRONGSEC:
2632 err = -EPERM;
2633 if (client != *clnt)
2634 goto out;
2635
2636 client = nfs4_create_sec_client(client, dir, name);
2637 if (IS_ERR(client))
2638 return PTR_ERR(client);
2639
2640 exception.retry = 1;
2641 break;
2642 default:
2643 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2644 }
2645 } while (exception.retry);
2646
2647out:
2648 if (err == 0)
2649 *clnt = client;
2650 else if (client != *clnt)
2651 rpc_shutdown_client(client);
2652
2653 return err;
2654}
2655
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002656static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002657 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002659 int status;
2660 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002661
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002662 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2663 if (client != NFS_CLIENT(dir)) {
2664 rpc_shutdown_client(client);
2665 nfs_fixup_secinfo_attributes(fattr);
2666 }
2667 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002670struct rpc_clnt *
2671nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2672 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2673{
2674 int status;
2675 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2676
2677 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2678 if (status < 0) {
2679 rpc_shutdown_client(client);
2680 return ERR_PTR(status);
2681 }
2682 return client;
2683}
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2686{
Trond Myklebust76b32992007-08-10 17:45:11 -04002687 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct nfs4_accessargs args = {
2689 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002690 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002692 struct nfs4_accessres res = {
2693 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002694 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 struct rpc_message msg = {
2696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2697 .rpc_argp = &args,
2698 .rpc_resp = &res,
2699 .rpc_cred = entry->cred,
2700 };
2701 int mode = entry->mask;
2702 int status;
2703
2704 /*
2705 * Determine which access bits we want to ask for...
2706 */
2707 if (mode & MAY_READ)
2708 args.access |= NFS4_ACCESS_READ;
2709 if (S_ISDIR(inode->i_mode)) {
2710 if (mode & MAY_WRITE)
2711 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2712 if (mode & MAY_EXEC)
2713 args.access |= NFS4_ACCESS_LOOKUP;
2714 } else {
2715 if (mode & MAY_WRITE)
2716 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2717 if (mode & MAY_EXEC)
2718 args.access |= NFS4_ACCESS_EXECUTE;
2719 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002720
2721 res.fattr = nfs_alloc_fattr();
2722 if (res.fattr == NULL)
2723 return -ENOMEM;
2724
Bryan Schumaker7c513052011-03-24 17:12:24 +00002725 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (!status) {
2727 entry->mask = 0;
2728 if (res.access & NFS4_ACCESS_READ)
2729 entry->mask |= MAY_READ;
2730 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2731 entry->mask |= MAY_WRITE;
2732 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2733 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002734 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002736 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return status;
2738}
2739
2740static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2741{
2742 struct nfs4_exception exception = { };
2743 int err;
2744 do {
2745 err = nfs4_handle_exception(NFS_SERVER(inode),
2746 _nfs4_proc_access(inode, entry),
2747 &exception);
2748 } while (exception.retry);
2749 return err;
2750}
2751
2752/*
2753 * TODO: For the time being, we don't try to get any attributes
2754 * along with any of the zero-copy operations READ, READDIR,
2755 * READLINK, WRITE.
2756 *
2757 * In the case of the first three, we want to put the GETATTR
2758 * after the read-type operation -- this is because it is hard
2759 * to predict the length of a GETATTR response in v4, and thus
2760 * align the READ data correctly. This means that the GETATTR
2761 * may end up partially falling into the page cache, and we should
2762 * shift it into the 'tail' of the xdr_buf before processing.
2763 * To do this efficiently, we need to know the total length
2764 * of data received, which doesn't seem to be available outside
2765 * of the RPC layer.
2766 *
2767 * In the case of WRITE, we also want to put the GETATTR after
2768 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002769 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 *
2771 * Both of these changes to the XDR layer would in fact be quite
2772 * minor, but I decided to leave them for a subsequent patch.
2773 */
2774static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2775 unsigned int pgbase, unsigned int pglen)
2776{
2777 struct nfs4_readlink args = {
2778 .fh = NFS_FH(inode),
2779 .pgbase = pgbase,
2780 .pglen = pglen,
2781 .pages = &page,
2782 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002783 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 struct rpc_message msg = {
2785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2786 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002787 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 };
2789
Bryan Schumaker7c513052011-03-24 17:12:24 +00002790 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 -07002791}
2792
2793static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2794 unsigned int pgbase, unsigned int pglen)
2795{
2796 struct nfs4_exception exception = { };
2797 int err;
2798 do {
2799 err = nfs4_handle_exception(NFS_SERVER(inode),
2800 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2801 &exception);
2802 } while (exception.retry);
2803 return err;
2804}
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806/*
2807 * Got race?
2808 * We will need to arrange for the VFS layer to provide an atomic open.
2809 * Until then, this create/open method is prone to inefficiency and race
2810 * conditions due to the lookup, create, and open VFS calls from sys_open()
2811 * placed on the wire.
2812 *
2813 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2814 * The file will be opened again in the subsequent VFS open call
2815 * (nfs4_proc_file_open).
2816 *
2817 * The open for read will just hang around to be used by any process that
2818 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2819 */
2820
2821static int
2822nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002823 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Al Viro82a2c1b2011-06-22 18:30:55 -04002825 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002827 struct rpc_cred *cred = NULL;
Andy Adamson6e5b5872012-05-31 15:16:03 -04002828 struct nfs4_threshold **thp = NULL;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002829 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 int status = 0;
2831
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002832 if (ctx != NULL) {
2833 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002834 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002835 fmode = ctx->mode;
Andy Adamson6e5b5872012-05-31 15:16:03 -04002836 thp = &ctx->mdsthreshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002838 sattr->ia_mode &= ~current_umask();
Andy Adamson6e5b5872012-05-31 15:16:03 -04002839 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred, thp);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002840 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (IS_ERR(state)) {
2842 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002845 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002846 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002847 if (ctx != NULL)
2848 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002849 else
Al Viro643168c2011-06-22 18:20:23 -04002850 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851out:
2852 return status;
2853}
2854
2855static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2856{
Trond Myklebust16e42952005-10-27 22:12:44 -04002857 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002858 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002860 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002862 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002863 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002864 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2867 .rpc_argp = &args,
2868 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002870 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002871
Bryan Schumaker7c513052011-03-24 17:12:24 +00002872 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002873 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002874 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return status;
2876}
2877
2878static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2879{
2880 struct nfs4_exception exception = { };
2881 int err;
2882 do {
2883 err = nfs4_handle_exception(NFS_SERVER(dir),
2884 _nfs4_proc_remove(dir, name),
2885 &exception);
2886 } while (exception.retry);
2887 return err;
2888}
2889
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002890static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002892 struct nfs_server *server = NFS_SERVER(dir);
2893 struct nfs_removeargs *args = msg->rpc_argp;
2894 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002896 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002898 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002901static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2902{
2903 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2904 &data->args.seq_args,
2905 &data->res.seq_res,
2906 task))
2907 return;
2908 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002911static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002913 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2914
Trond Myklebust14516c32010-07-31 14:29:06 -04002915 if (!nfs4_sequence_done(task, &res->seq_res))
2916 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002917 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002918 return 0;
2919 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002920 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
Jeff Laytond3d41522010-09-17 17:31:57 -04002923static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2924{
2925 struct nfs_server *server = NFS_SERVER(dir);
2926 struct nfs_renameargs *arg = msg->rpc_argp;
2927 struct nfs_renameres *res = msg->rpc_resp;
2928
2929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002930 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002931 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002932}
2933
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002934static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2935{
2936 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2937 &data->args.seq_args,
2938 &data->res.seq_res,
2939 task))
2940 return;
2941 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002942}
2943
2944static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2945 struct inode *new_dir)
2946{
2947 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2948
2949 if (!nfs4_sequence_done(task, &res->seq_res))
2950 return 0;
2951 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2952 return 0;
2953
2954 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002955 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002956 return 1;
2957}
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2960 struct inode *new_dir, struct qstr *new_name)
2961{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002962 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002963 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 .old_dir = NFS_FH(old_dir),
2965 .new_dir = NFS_FH(new_dir),
2966 .old_name = old_name,
2967 .new_name = new_name,
2968 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002969 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002970 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002971 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct rpc_message msg = {
2973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2974 .rpc_argp = &arg,
2975 .rpc_resp = &res,
2976 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002977 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Bryan Schumaker7c513052011-03-24 17:12:24 +00002979 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (!status) {
2981 update_changeattr(old_dir, &res.old_cinfo);
2982 update_changeattr(new_dir, &res.new_cinfo);
2983 }
2984 return status;
2985}
2986
2987static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2988 struct inode *new_dir, struct qstr *new_name)
2989{
2990 struct nfs4_exception exception = { };
2991 int err;
2992 do {
2993 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2994 _nfs4_proc_rename(old_dir, old_name,
2995 new_dir, new_name),
2996 &exception);
2997 } while (exception.retry);
2998 return err;
2999}
3000
3001static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3002{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003003 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct nfs4_link_arg arg = {
3005 .fh = NFS_FH(inode),
3006 .dir_fh = NFS_FH(dir),
3007 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003008 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003010 struct nfs4_link_res res = {
3011 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003012 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct rpc_message msg = {
3014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3015 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003016 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003018 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Trond Myklebust136f2622010-04-16 16:22:49 -04003020 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003021 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003022 goto out;
3023
Bryan Schumaker7c513052011-03-24 17:12:24 +00003024 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003025 if (!status) {
3026 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003027 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003028 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003029out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003030 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return status;
3032}
3033
3034static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3035{
3036 struct nfs4_exception exception = { };
3037 int err;
3038 do {
3039 err = nfs4_handle_exception(NFS_SERVER(inode),
3040 _nfs4_proc_link(inode, dir, name),
3041 &exception);
3042 } while (exception.retry);
3043 return err;
3044}
3045
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003046struct nfs4_createdata {
3047 struct rpc_message msg;
3048 struct nfs4_create_arg arg;
3049 struct nfs4_create_res res;
3050 struct nfs_fh fh;
3051 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003052};
3053
3054static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3055 struct qstr *name, struct iattr *sattr, u32 ftype)
3056{
3057 struct nfs4_createdata *data;
3058
3059 data = kzalloc(sizeof(*data), GFP_KERNEL);
3060 if (data != NULL) {
3061 struct nfs_server *server = NFS_SERVER(dir);
3062
3063 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3064 data->msg.rpc_argp = &data->arg;
3065 data->msg.rpc_resp = &data->res;
3066 data->arg.dir_fh = NFS_FH(dir);
3067 data->arg.server = server;
3068 data->arg.name = name;
3069 data->arg.attrs = sattr;
3070 data->arg.ftype = ftype;
3071 data->arg.bitmask = server->attr_bitmask;
3072 data->res.server = server;
3073 data->res.fh = &data->fh;
3074 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003075 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003076 }
3077 return data;
3078}
3079
3080static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3081{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003082 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003083 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003084 if (status == 0) {
3085 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003086 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3087 }
3088 return status;
3089}
3090
3091static void nfs4_free_createdata(struct nfs4_createdata *data)
3092{
3093 kfree(data);
3094}
3095
Chuck Lever4f390c12006-08-22 20:06:22 -04003096static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003097 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099 struct nfs4_createdata *data;
3100 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Chuck Lever94a6d752006-08-22 20:06:23 -04003102 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003104
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003105 status = -ENOMEM;
3106 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3107 if (data == NULL)
3108 goto out;
3109
3110 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3111 data->arg.u.symlink.pages = &page;
3112 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003114 status = nfs4_do_create(dir, dentry, data);
3115
3116 nfs4_free_createdata(data);
3117out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return status;
3119}
3120
Chuck Lever4f390c12006-08-22 20:06:22 -04003121static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003122 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
3124 struct nfs4_exception exception = { };
3125 int err;
3126 do {
3127 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003128 _nfs4_proc_symlink(dir, dentry, page,
3129 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 &exception);
3131 } while (exception.retry);
3132 return err;
3133}
3134
3135static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3136 struct iattr *sattr)
3137{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003138 struct nfs4_createdata *data;
3139 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003141 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3142 if (data == NULL)
3143 goto out;
3144
3145 status = nfs4_do_create(dir, dentry, data);
3146
3147 nfs4_free_createdata(data);
3148out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return status;
3150}
3151
3152static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3153 struct iattr *sattr)
3154{
3155 struct nfs4_exception exception = { };
3156 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003157
3158 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 do {
3160 err = nfs4_handle_exception(NFS_SERVER(dir),
3161 _nfs4_proc_mkdir(dir, dentry, sattr),
3162 &exception);
3163 } while (exception.retry);
3164 return err;
3165}
3166
3167static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003168 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 struct inode *dir = dentry->d_inode;
3171 struct nfs4_readdir_arg args = {
3172 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003173 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 .pgbase = 0,
3175 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003176 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003177 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 };
3179 struct nfs4_readdir_res res;
3180 struct rpc_message msg = {
3181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3182 .rpc_argp = &args,
3183 .rpc_resp = &res,
3184 .rpc_cred = cred,
3185 };
3186 int status;
3187
Harvey Harrison3110ff82008-05-02 13:42:44 -07003188 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003189 dentry->d_parent->d_name.name,
3190 dentry->d_name.name,
3191 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3193 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003194 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 -05003195 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003197 status += args.pgbase;
3198 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003199
3200 nfs_invalidate_atime(dir);
3201
Harvey Harrison3110ff82008-05-02 13:42:44 -07003202 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return status;
3204}
3205
3206static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003207 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 struct nfs4_exception exception = { };
3210 int err;
3211 do {
3212 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3213 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003214 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 &exception);
3216 } while (exception.retry);
3217 return err;
3218}
3219
3220static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3221 struct iattr *sattr, dev_t rdev)
3222{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003223 struct nfs4_createdata *data;
3224 int mode = sattr->ia_mode;
3225 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3228 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003229
3230 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3231 if (data == NULL)
3232 goto out;
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003235 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003237 data->arg.ftype = NF4BLK;
3238 data->arg.u.device.specdata1 = MAJOR(rdev);
3239 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
3241 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003242 data->arg.ftype = NF4CHR;
3243 data->arg.u.device.specdata1 = MAJOR(rdev);
3244 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003247 status = nfs4_do_create(dir, dentry, data);
3248
3249 nfs4_free_createdata(data);
3250out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return status;
3252}
3253
3254static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3255 struct iattr *sattr, dev_t rdev)
3256{
3257 struct nfs4_exception exception = { };
3258 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003259
3260 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 do {
3262 err = nfs4_handle_exception(NFS_SERVER(dir),
3263 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3264 &exception);
3265 } while (exception.retry);
3266 return err;
3267}
3268
3269static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3270 struct nfs_fsstat *fsstat)
3271{
3272 struct nfs4_statfs_arg args = {
3273 .fh = fhandle,
3274 .bitmask = server->attr_bitmask,
3275 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003276 struct nfs4_statfs_res res = {
3277 .fsstat = fsstat,
3278 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct rpc_message msg = {
3280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3281 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003282 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 };
3284
Trond Myklebust0e574af2005-10-27 22:12:38 -04003285 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003286 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
3289static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3290{
3291 struct nfs4_exception exception = { };
3292 int err;
3293 do {
3294 err = nfs4_handle_exception(server,
3295 _nfs4_proc_statfs(server, fhandle, fsstat),
3296 &exception);
3297 } while (exception.retry);
3298 return err;
3299}
3300
3301static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3302 struct nfs_fsinfo *fsinfo)
3303{
3304 struct nfs4_fsinfo_arg args = {
3305 .fh = fhandle,
3306 .bitmask = server->attr_bitmask,
3307 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003308 struct nfs4_fsinfo_res res = {
3309 .fsinfo = fsinfo,
3310 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 struct rpc_message msg = {
3312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3313 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003314 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 };
3316
Bryan Schumaker7c513052011-03-24 17:12:24 +00003317 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
3320static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3321{
3322 struct nfs4_exception exception = { };
3323 int err;
3324
3325 do {
3326 err = nfs4_handle_exception(server,
3327 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3328 &exception);
3329 } while (exception.retry);
3330 return err;
3331}
3332
3333static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3334{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003335 int error;
3336
Trond Myklebust0e574af2005-10-27 22:12:38 -04003337 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003338 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3339 if (error == 0)
3340 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3341
3342 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
3345static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3346 struct nfs_pathconf *pathconf)
3347{
3348 struct nfs4_pathconf_arg args = {
3349 .fh = fhandle,
3350 .bitmask = server->attr_bitmask,
3351 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003352 struct nfs4_pathconf_res res = {
3353 .pathconf = pathconf,
3354 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 struct rpc_message msg = {
3356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3357 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003358 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 };
3360
3361 /* None of the pathconf attributes are mandatory to implement */
3362 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3363 memset(pathconf, 0, sizeof(*pathconf));
3364 return 0;
3365 }
3366
Trond Myklebust0e574af2005-10-27 22:12:38 -04003367 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003368 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
3371static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3372 struct nfs_pathconf *pathconf)
3373{
3374 struct nfs4_exception exception = { };
3375 int err;
3376
3377 do {
3378 err = nfs4_handle_exception(server,
3379 _nfs4_proc_pathconf(server, fhandle, pathconf),
3380 &exception);
3381 } while (exception.retry);
3382 return err;
3383}
3384
Benny Halevyd20581a2011-05-22 19:52:03 +03003385void __nfs4_read_done_cb(struct nfs_read_data *data)
3386{
Fred Isamancd841602012-04-20 14:47:44 -04003387 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003388}
3389
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003390static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
Fred Isamancd841602012-04-20 14:47:44 -04003392 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003394 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003395 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003396 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003398
Benny Halevyd20581a2011-05-22 19:52:03 +03003399 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003401 renew_lease(server, data->timestamp);
3402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003405static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3406{
3407
3408 dprintk("--> %s\n", __func__);
3409
3410 if (!nfs4_sequence_done(task, &data->res.seq_res))
3411 return -EAGAIN;
3412
Benny Halevyd20581a2011-05-22 19:52:03 +03003413 return data->read_done_cb ? data->read_done_cb(task, data) :
3414 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003415}
3416
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003417static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003420 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003421 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003422 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003425static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3426{
Fred Isamancd841602012-04-20 14:47:44 -04003427 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003428 &data->args.seq_args,
3429 &data->res.seq_res,
3430 task))
3431 return;
3432 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433}
3434
Fred Isamanb029bc92011-03-03 15:13:42 +00003435static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
Fred Isamancd841602012-04-20 14:47:44 -04003437 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003439 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003440 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003441 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003443 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003445 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003446 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
Fred Isamanb029bc92011-03-03 15:13:42 +00003450static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3451{
3452 if (!nfs4_sequence_done(task, &data->res.seq_res))
3453 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003454 return data->write_done_cb ? data->write_done_cb(task, data) :
3455 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003456}
3457
Trond Myklebust5a37f852012-04-28 14:55:16 -04003458static
3459bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003460{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003461 const struct nfs_pgio_header *hdr = data->header;
3462
3463 /* Don't request attributes for pNFS or O_DIRECT writes */
3464 if (data->ds_clp != NULL || hdr->dreq != NULL)
3465 return false;
3466 /* Otherwise, request attributes if and only if we don't hold
3467 * a delegation
3468 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003469 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003470}
Fred Isamana69aef12011-03-03 15:13:47 +00003471
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003472static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Fred Isamancd841602012-04-20 14:47:44 -04003474 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003475
Trond Myklebust5a37f852012-04-28 14:55:16 -04003476 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003477 data->args.bitmask = NULL;
3478 data->res.fattr = NULL;
3479 } else
3480 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003481
Fred Isamanb029bc92011-03-03 15:13:42 +00003482 if (!data->write_done_cb)
3483 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003484 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 data->timestamp = jiffies;
3486
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003487 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003488 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003491static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3492{
Fred Isamancd841602012-04-20 14:47:44 -04003493 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003494 &data->args.seq_args,
3495 &data->res.seq_res,
3496 task))
3497 return;
3498 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
Fred Isaman0b7c0152012-04-20 14:47:39 -04003501static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3502{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3504 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003505 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return;
3508 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003509}
3510
Fred Isaman0b7c0152012-04-20 14:47:39 -04003511static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
3513 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003514
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003515 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003516 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003517 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520}
3521
Fred Isaman0b7c0152012-04-20 14:47:39 -04003522static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003523{
3524 if (!nfs4_sequence_done(task, &data->res.seq_res))
3525 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003526 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003527}
3528
Fred Isaman0b7c0152012-04-20 14:47:39 -04003529static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003531 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003532
Fred Isaman0b7c0152012-04-20 14:47:39 -04003533 if (data->commit_done_cb == NULL)
3534 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003535 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003536 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003537 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
3539
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003540struct nfs4_renewdata {
3541 struct nfs_client *client;
3542 unsigned long timestamp;
3543};
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545/*
3546 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3547 * standalone procedure for queueing an asynchronous RENEW.
3548 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003549static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003550{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003551 struct nfs4_renewdata *data = calldata;
3552 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003553
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003554 if (atomic_read(&clp->cl_count) > 1)
3555 nfs4_schedule_state_renewal(clp);
3556 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003557 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003558}
3559
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003560static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003562 struct nfs4_renewdata *data = calldata;
3563 struct nfs_client *clp = data->client;
3564 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003567 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003568 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3569 return;
3570 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003571 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003572 return;
3573 }
3574 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003576 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003579static const struct rpc_call_ops nfs4_renew_ops = {
3580 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003581 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003582};
3583
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003584static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
3586 struct rpc_message msg = {
3587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3588 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003589 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003591 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003593 if (renew_flags == 0)
3594 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003595 if (!atomic_inc_not_zero(&clp->cl_count))
3596 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003597 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003598 if (data == NULL)
3599 return -ENOMEM;
3600 data->client = clp;
3601 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003603 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003606static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
3608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3610 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003611 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 };
3613 unsigned long now = jiffies;
3614 int status;
3615
3616 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3617 if (status < 0)
3618 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003619 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 return 0;
3621}
3622
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003623static inline int nfs4_server_supports_acls(struct nfs_server *server)
3624{
3625 return (server->caps & NFS_CAP_ACLS)
3626 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3627 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3628}
3629
3630/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3631 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3632 * the stack.
3633 */
3634#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3635
Neil Hormane9e3d722011-03-04 19:26:03 -05003636static int buf_to_pages_noslab(const void *buf, size_t buflen,
3637 struct page **pages, unsigned int *pgbase)
3638{
3639 struct page *newpage, **spages;
3640 int rc = 0;
3641 size_t len;
3642 spages = pages;
3643
3644 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003645 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003646 newpage = alloc_page(GFP_KERNEL);
3647
3648 if (newpage == NULL)
3649 goto unwind;
3650 memcpy(page_address(newpage), buf, len);
3651 buf += len;
3652 buflen -= len;
3653 *pages++ = newpage;
3654 rc++;
3655 } while (buflen != 0);
3656
3657 return rc;
3658
3659unwind:
3660 for(; rc > 0; rc--)
3661 __free_page(spages[rc-1]);
3662 return -ENOMEM;
3663}
3664
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003665struct nfs4_cached_acl {
3666 int cached;
3667 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003668 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003669};
3670
3671static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003672{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003673 struct nfs_inode *nfsi = NFS_I(inode);
3674
3675 spin_lock(&inode->i_lock);
3676 kfree(nfsi->nfs4_acl);
3677 nfsi->nfs4_acl = acl;
3678 spin_unlock(&inode->i_lock);
3679}
3680
3681static void nfs4_zap_acl_attr(struct inode *inode)
3682{
3683 nfs4_set_cached_acl(inode, NULL);
3684}
3685
3686static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3687{
3688 struct nfs_inode *nfsi = NFS_I(inode);
3689 struct nfs4_cached_acl *acl;
3690 int ret = -ENOENT;
3691
3692 spin_lock(&inode->i_lock);
3693 acl = nfsi->nfs4_acl;
3694 if (acl == NULL)
3695 goto out;
3696 if (buf == NULL) /* user is just asking for length */
3697 goto out_len;
3698 if (acl->cached == 0)
3699 goto out;
3700 ret = -ERANGE; /* see getxattr(2) man page */
3701 if (acl->len > buflen)
3702 goto out;
3703 memcpy(buf, acl->data, acl->len);
3704out_len:
3705 ret = acl->len;
3706out:
3707 spin_unlock(&inode->i_lock);
3708 return ret;
3709}
3710
Sachin Prabhu5794d212012-04-17 14:36:40 +01003711static 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 +00003712{
3713 struct nfs4_cached_acl *acl;
3714
Sachin Prabhu5794d212012-04-17 14:36:40 +01003715 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003716 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3717 if (acl == NULL)
3718 goto out;
3719 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003720 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003721 } else {
3722 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3723 if (acl == NULL)
3724 goto out;
3725 acl->cached = 0;
3726 }
3727 acl->len = acl_len;
3728out:
3729 nfs4_set_cached_acl(inode, acl);
3730}
3731
Andy Adamsonbf118a32011-12-07 11:55:27 -05003732/*
3733 * The getxattr API returns the required buffer length when called with a
3734 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3735 * the required buf. On a NULL buf, we send a page of data to the server
3736 * guessing that the ACL request can be serviced by a page. If so, we cache
3737 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3738 * the cache. If not so, we throw away the page, and cache the required
3739 * length. The next getxattr call will then produce another round trip to
3740 * the server, this time with the input buf of the required size.
3741 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003742static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003743{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003744 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003745 struct nfs_getaclargs args = {
3746 .fh = NFS_FH(inode),
3747 .acl_pages = pages,
3748 .acl_len = buflen,
3749 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003750 struct nfs_getaclres res = {
3751 .acl_len = buflen,
3752 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003753 struct rpc_message msg = {
3754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3755 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003756 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003757 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003758 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003759
Andy Adamsonbf118a32011-12-07 11:55:27 -05003760 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3761 /* As long as we're doing a round trip to the server anyway,
3762 * let's be prepared for a page of acl data. */
3763 if (npages == 0)
3764 npages = 1;
3765
Sachin Prabhu5a006892012-04-17 14:35:39 +01003766 /* Add an extra page to handle the bitmap returned */
3767 npages++;
3768
Andy Adamsonbf118a32011-12-07 11:55:27 -05003769 for (i = 0; i < npages; i++) {
3770 pages[i] = alloc_page(GFP_KERNEL);
3771 if (!pages[i])
3772 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003774
3775 /* for decoding across pages */
3776 res.acl_scratch = alloc_page(GFP_KERNEL);
3777 if (!res.acl_scratch)
3778 goto out_free;
3779
Andy Adamsonbf118a32011-12-07 11:55:27 -05003780 args.acl_len = npages * PAGE_SIZE;
3781 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003782
Andy Adamsonbf118a32011-12-07 11:55:27 -05003783 /* Let decode_getfacl know not to fail if the ACL data is larger than
3784 * the page we send as a guess */
3785 if (buf == NULL)
3786 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787
Peng Taode040be2012-01-10 22:42:47 +08003788 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003789 __func__, buf, buflen, npages, args.acl_len);
3790 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3791 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003792 if (ret)
3793 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003794
3795 acl_len = res.acl_len - res.acl_data_offset;
3796 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003797 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003798 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003799 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003800 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003801 if (buf) {
3802 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003803 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003804 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003805 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003806 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003807 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003808 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003809out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003810 for (i = 0; i < npages; i++)
3811 if (pages[i])
3812 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003813 if (res.acl_scratch)
3814 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 return ret;
3816}
3817
Trond Myklebust16b42892006-08-24 12:27:15 -04003818static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3819{
3820 struct nfs4_exception exception = { };
3821 ssize_t ret;
3822 do {
3823 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3824 if (ret >= 0)
3825 break;
3826 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3827 } while (exception.retry);
3828 return ret;
3829}
3830
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3832{
3833 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003834 int ret;
3835
3836 if (!nfs4_server_supports_acls(server))
3837 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003838 ret = nfs_revalidate_inode(server, inode);
3839 if (ret < 0)
3840 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003841 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3842 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003843 ret = nfs4_read_cached_acl(inode, buf, buflen);
3844 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003845 /* -ENOENT is returned if there is no ACL or if there is an ACL
3846 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003847 return ret;
3848 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003849}
3850
Trond Myklebust16b42892006-08-24 12:27:15 -04003851static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003852{
3853 struct nfs_server *server = NFS_SERVER(inode);
3854 struct page *pages[NFS4ACL_MAXPAGES];
3855 struct nfs_setaclargs arg = {
3856 .fh = NFS_FH(inode),
3857 .acl_pages = pages,
3858 .acl_len = buflen,
3859 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003860 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003861 struct rpc_message msg = {
3862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3863 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003864 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003865 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003866 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003867
3868 if (!nfs4_server_supports_acls(server))
3869 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003870 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3871 if (i < 0)
3872 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003873 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003874 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003875
3876 /*
3877 * Free each page after tx, so the only ref left is
3878 * held by the network stack
3879 */
3880 for (; i > 0; i--)
3881 put_page(pages[i-1]);
3882
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003883 /*
3884 * Acl update can result in inode attribute update.
3885 * so mark the attribute cache invalid.
3886 */
3887 spin_lock(&inode->i_lock);
3888 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3889 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003890 nfs_access_zap_cache(inode);
3891 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003892 return ret;
3893}
3894
Trond Myklebust16b42892006-08-24 12:27:15 -04003895static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3896{
3897 struct nfs4_exception exception = { };
3898 int err;
3899 do {
3900 err = nfs4_handle_exception(NFS_SERVER(inode),
3901 __nfs4_proc_set_acl(inode, buf, buflen),
3902 &exception);
3903 } while (exception.retry);
3904 return err;
3905}
3906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003908nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003910 struct nfs_client *clp = server->nfs_client;
3911
3912 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 return 0;
3914 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003915 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003916 case -NFS4ERR_ADMIN_REVOKED:
3917 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003918 if (state == NULL)
3919 break;
3920 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003921 case -NFS4ERR_OPENMODE:
3922 if (state == NULL)
3923 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003924 nfs4_schedule_stateid_recovery(server, state);
3925 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003926 case -NFS4ERR_EXPIRED:
3927 if (state != NULL)
3928 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003930 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003931 nfs4_schedule_lease_recovery(clp);
3932 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003933#if defined(CONFIG_NFS_V4_1)
3934 case -NFS4ERR_BADSESSION:
3935 case -NFS4ERR_BADSLOT:
3936 case -NFS4ERR_BAD_HIGH_SLOT:
3937 case -NFS4ERR_DEADSESSION:
3938 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3939 case -NFS4ERR_SEQ_FALSE_RETRY:
3940 case -NFS4ERR_SEQ_MISORDERED:
3941 dprintk("%s ERROR %d, Reset session\n", __func__,
3942 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003943 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003944 task->tk_status = 0;
3945 return -EAGAIN;
3946#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003948 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003949 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003950 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3952 task->tk_status = 0;
3953 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003954 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 case -NFS4ERR_OLD_STATEID:
3956 task->tk_status = 0;
3957 return -EAGAIN;
3958 }
3959 task->tk_status = nfs4_map_errors(task->tk_status);
3960 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003961wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003962 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003963 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3964 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3965 task->tk_status = 0;
3966 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
Chuck Leverf0920752012-05-21 22:45:41 -04003969static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3970 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003971{
3972 __be32 verf[2];
3973
Chuck Lever2c820d92012-05-21 22:45:33 -04003974 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3975 /* An impossible timestamp guarantees this value
3976 * will never match a generated boot time. */
3977 verf[0] = 0;
3978 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3979 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003980 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3981 verf[0] = (__be32)nn->boot_time.tv_sec;
3982 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003983 }
Chuck Levercd937102012-03-02 17:14:31 -05003984 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3985}
3986
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003987int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3988 unsigned short port, struct rpc_cred *cred,
3989 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
3991 nfs4_verifier sc_verifier;
3992 struct nfs4_setclientid setclientid = {
3993 .sc_verifier = &sc_verifier,
3994 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003995 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 };
3997 struct rpc_message msg = {
3998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3999 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004000 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004001 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 int loop = 0;
4004 int status;
4005
Chuck Leverf0920752012-05-21 22:45:41 -04004006 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05004009 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05004011 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004012 clp->cl_ipaddr,
4013 rpc_peeraddr2str(clp->cl_rpcclient,
4014 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05004015 rpc_peeraddr2str(clp->cl_rpcclient,
4016 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04004017 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 clp->cl_id_uniquifier);
4019 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004020 sizeof(setclientid.sc_netid),
4021 rpc_peeraddr2str(clp->cl_rpcclient,
4022 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004024 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05004026 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004028 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 if (status != -NFS4ERR_CLID_INUSE)
4030 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004031 if (loop != 0) {
4032 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004034 }
4035 ++loop;
4036 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
4038 return status;
4039}
4040
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004041int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004042 struct nfs4_setclientid_res *arg,
4043 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
4045 struct nfs_fsinfo fsinfo;
4046 struct rpc_message msg = {
4047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004048 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004050 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 };
4052 unsigned long now;
4053 int status;
4054
4055 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004056 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 if (status == 0) {
4058 spin_lock(&clp->cl_lock);
4059 clp->cl_lease_time = fsinfo.lease_time * HZ;
4060 clp->cl_last_renewal = now;
4061 spin_unlock(&clp->cl_lock);
4062 }
4063 return status;
4064}
4065
Trond Myklebustfe650402006-01-03 09:55:18 +01004066struct nfs4_delegreturndata {
4067 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004068 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004069 struct nfs_fh fh;
4070 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004071 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004072 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004073 int rpc_status;
4074};
4075
Trond Myklebustfe650402006-01-03 09:55:18 +01004076static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4077{
4078 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004079
Trond Myklebust14516c32010-07-31 14:29:06 -04004080 if (!nfs4_sequence_done(task, &data->res.seq_res))
4081 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004082
Ricardo Labiaga79708862009-12-07 09:23:21 -05004083 switch (task->tk_status) {
4084 case -NFS4ERR_STALE_STATEID:
4085 case -NFS4ERR_EXPIRED:
4086 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004087 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004088 break;
4089 default:
4090 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4091 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004092 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004093 return;
4094 }
4095 }
4096 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004097}
4098
4099static void nfs4_delegreturn_release(void *calldata)
4100{
Trond Myklebustfe650402006-01-03 09:55:18 +01004101 kfree(calldata);
4102}
4103
Andy Adamson938e1012009-04-01 09:22:28 -04004104#if defined(CONFIG_NFS_V4_1)
4105static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4106{
4107 struct nfs4_delegreturndata *d_data;
4108
4109 d_data = (struct nfs4_delegreturndata *)data;
4110
Trond Myklebust035168ab2010-06-16 09:52:26 -04004111 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004112 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004113 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004114 return;
4115 rpc_call_start(task);
4116}
4117#endif /* CONFIG_NFS_V4_1 */
4118
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004119static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004120#if defined(CONFIG_NFS_V4_1)
4121 .rpc_call_prepare = nfs4_delegreturn_prepare,
4122#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004123 .rpc_call_done = nfs4_delegreturn_done,
4124 .rpc_release = nfs4_delegreturn_release,
4125};
4126
Trond Myklebuste6f81072008-01-24 18:14:34 -05004127static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004128{
4129 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004130 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004131 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004132 struct rpc_message msg = {
4133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4134 .rpc_cred = cred,
4135 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004136 struct rpc_task_setup task_setup_data = {
4137 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004138 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004139 .callback_ops = &nfs4_delegreturn_ops,
4140 .flags = RPC_TASK_ASYNC,
4141 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004142 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004143
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004144 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004145 if (data == NULL)
4146 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004147 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004148 data->args.fhandle = &data->fh;
4149 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004150 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004151 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004152 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004153 data->res.fattr = &data->fattr;
4154 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004155 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004156 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004157 data->rpc_status = 0;
4158
Trond Myklebustc970aa82007-07-14 15:39:59 -04004159 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004160 msg.rpc_argp = &data->args;
4161 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004162 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004163 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004164 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004165 if (!issync)
4166 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004167 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004168 if (status != 0)
4169 goto out;
4170 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004171 if (status == 0)
4172 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4173 else
4174 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004175out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004176 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004177 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178}
4179
Trond Myklebuste6f81072008-01-24 18:14:34 -05004180int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
4182 struct nfs_server *server = NFS_SERVER(inode);
4183 struct nfs4_exception exception = { };
4184 int err;
4185 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004186 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 switch (err) {
4188 case -NFS4ERR_STALE_STATEID:
4189 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 case 0:
4191 return 0;
4192 }
4193 err = nfs4_handle_exception(server, err, &exception);
4194 } while (exception.retry);
4195 return err;
4196}
4197
4198#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4199#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4200
4201/*
4202 * sleep, with exponential backoff, and retry the LOCK operation.
4203 */
4204static unsigned long
4205nfs4_set_lock_task_retry(unsigned long timeout)
4206{
Jeff Laytond3103102011-12-01 22:44:39 +01004207 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 timeout <<= 1;
4209 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4210 return NFS4_LOCK_MAXTIMEOUT;
4211 return timeout;
4212}
4213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4215{
4216 struct inode *inode = state->inode;
4217 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004218 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004219 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004221 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004223 struct nfs_lockt_res res = {
4224 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 };
4226 struct rpc_message msg = {
4227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4228 .rpc_argp = &arg,
4229 .rpc_resp = &res,
4230 .rpc_cred = state->owner->so_cred,
4231 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 struct nfs4_lock_state *lsp;
4233 int status;
4234
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004235 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004236 status = nfs4_set_lock_state(state, request);
4237 if (status != 0)
4238 goto out;
4239 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004240 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004241 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004242 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004243 switch (status) {
4244 case 0:
4245 request->fl_type = F_UNLCK;
4246 break;
4247 case -NFS4ERR_DENIED:
4248 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004250 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004251out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 return status;
4253}
4254
4255static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4256{
4257 struct nfs4_exception exception = { };
4258 int err;
4259
4260 do {
4261 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4262 _nfs4_proc_getlk(state, cmd, request),
4263 &exception);
4264 } while (exception.retry);
4265 return err;
4266}
4267
4268static int do_vfs_lock(struct file *file, struct file_lock *fl)
4269{
4270 int res = 0;
4271 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4272 case FL_POSIX:
4273 res = posix_lock_file_wait(file, fl);
4274 break;
4275 case FL_FLOCK:
4276 res = flock_lock_file_wait(file, fl);
4277 break;
4278 default:
4279 BUG();
4280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return res;
4282}
4283
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004284struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004285 struct nfs_locku_args arg;
4286 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004287 struct nfs4_lock_state *lsp;
4288 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004289 struct file_lock fl;
4290 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004291 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004292};
4293
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004294static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4295 struct nfs_open_context *ctx,
4296 struct nfs4_lock_state *lsp,
4297 struct nfs_seqid *seqid)
4298{
4299 struct nfs4_unlockdata *p;
4300 struct inode *inode = lsp->ls_state->inode;
4301
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004302 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004303 if (p == NULL)
4304 return NULL;
4305 p->arg.fh = NFS_FH(inode);
4306 p->arg.fl = &p->fl;
4307 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004308 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004309 p->arg.stateid = &lsp->ls_stateid;
4310 p->lsp = lsp;
4311 atomic_inc(&lsp->ls_count);
4312 /* Ensure we don't close file until we're done freeing locks! */
4313 p->ctx = get_nfs_open_context(ctx);
4314 memcpy(&p->fl, fl, sizeof(p->fl));
4315 p->server = NFS_SERVER(inode);
4316 return p;
4317}
4318
Trond Myklebust06f814a2006-01-03 09:55:07 +01004319static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004320{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004321 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004322 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004323 nfs4_put_lock_state(calldata->lsp);
4324 put_nfs_open_context(calldata->ctx);
4325 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004326}
4327
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004328static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004329{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004330 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004331
Trond Myklebust14516c32010-07-31 14:29:06 -04004332 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4333 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004334 switch (task->tk_status) {
4335 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004336 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4337 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004338 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004339 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004340 case -NFS4ERR_BAD_STATEID:
4341 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004342 case -NFS4ERR_STALE_STATEID:
4343 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004344 break;
4345 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004346 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004347 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004348 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004349}
4350
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004351static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004352{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004353 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004355 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004356 return;
4357 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004358 /* Note: exit _without_ running nfs4_locku_done */
4359 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004360 return;
4361 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004362 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004363 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004364 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004365 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004366 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004367 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368}
4369
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004370static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004371 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004372 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004373 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004374};
4375
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004376static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4377 struct nfs_open_context *ctx,
4378 struct nfs4_lock_state *lsp,
4379 struct nfs_seqid *seqid)
4380{
4381 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004382 struct rpc_message msg = {
4383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4384 .rpc_cred = ctx->cred,
4385 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004386 struct rpc_task_setup task_setup_data = {
4387 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004388 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004389 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004390 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004391 .flags = RPC_TASK_ASYNC,
4392 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004393
Frank Filz137d6ac2007-07-09 15:32:29 -07004394 /* Ensure this is an unlock - when canceling a lock, the
4395 * canceled lock is passed in, and it won't be an unlock.
4396 */
4397 fl->fl_type = F_UNLCK;
4398
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004399 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4400 if (data == NULL) {
4401 nfs_free_seqid(seqid);
4402 return ERR_PTR(-ENOMEM);
4403 }
4404
Trond Myklebust9d12b212012-01-17 22:04:25 -05004405 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004406 msg.rpc_argp = &data->arg;
4407 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004408 task_setup_data.callback_data = data;
4409 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410}
4411
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4413{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004414 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004415 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004416 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004417 struct rpc_task *task;
4418 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004419 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Trond Myklebust9b073572006-06-29 16:38:34 -04004421 status = nfs4_set_lock_state(state, request);
4422 /* Unlock _before_ we do the RPC call */
4423 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004424 down_read(&nfsi->rwsem);
4425 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4426 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004427 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004428 }
4429 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004430 if (status != 0)
4431 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004432 /* Is this a delegated lock? */
4433 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004434 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004435 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004436 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004437 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004438 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004439 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004440 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004441 status = PTR_ERR(task);
4442 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004443 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004444 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004445 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004446out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004447 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004448 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004451struct nfs4_lockdata {
4452 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004453 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004454 struct nfs4_lock_state *lsp;
4455 struct nfs_open_context *ctx;
4456 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004457 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004458 int rpc_status;
4459 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004460 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004461};
4462
4463static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004464 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4465 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004466{
4467 struct nfs4_lockdata *p;
4468 struct inode *inode = lsp->ls_state->inode;
4469 struct nfs_server *server = NFS_SERVER(inode);
4470
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004471 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004472 if (p == NULL)
4473 return NULL;
4474
4475 p->arg.fh = NFS_FH(inode);
4476 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004477 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004478 if (p->arg.open_seqid == NULL)
4479 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004480 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004482 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004483 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004484 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004485 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004486 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004487 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004488 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004489 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004490 atomic_inc(&lsp->ls_count);
4491 p->ctx = get_nfs_open_context(ctx);
4492 memcpy(&p->fl, fl, sizeof(p->fl));
4493 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004494out_free_seqid:
4495 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004496out_free:
4497 kfree(p);
4498 return NULL;
4499}
4500
4501static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4502{
4503 struct nfs4_lockdata *data = calldata;
4504 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Harvey Harrison3110ff82008-05-02 13:42:44 -07004506 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004507 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4508 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509 /* Do we need to do an open_to_lock_owner? */
4510 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004511 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4512 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004513 data->arg.open_stateid = &state->stateid;
4514 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004515 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004516 } else
4517 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004518 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004519 if (nfs4_setup_sequence(data->server,
4520 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004521 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004522 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004523 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004524 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525}
4526
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004527static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4528{
4529 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4530 nfs4_lock_prepare(task, calldata);
4531}
4532
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4534{
4535 struct nfs4_lockdata *data = calldata;
4536
Harvey Harrison3110ff82008-05-02 13:42:44 -07004537 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538
Trond Myklebust14516c32010-07-31 14:29:06 -04004539 if (!nfs4_sequence_done(task, &data->res.seq_res))
4540 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004541
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004542 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004543 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004544 if (data->rpc_status == 0)
4545 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4546 else
4547 goto out;
4548 }
4549 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004550 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004551 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004552 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004553 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004555 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004556}
4557
4558static void nfs4_lock_release(void *calldata)
4559{
4560 struct nfs4_lockdata *data = calldata;
4561
Harvey Harrison3110ff82008-05-02 13:42:44 -07004562 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004563 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564 if (data->cancelled != 0) {
4565 struct rpc_task *task;
4566 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4567 data->arg.lock_seqid);
4568 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004569 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004570 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004571 } else
4572 nfs_free_seqid(data->arg.lock_seqid);
4573 nfs4_put_lock_state(data->lsp);
4574 put_nfs_open_context(data->ctx);
4575 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004576 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577}
4578
4579static const struct rpc_call_ops nfs4_lock_ops = {
4580 .rpc_call_prepare = nfs4_lock_prepare,
4581 .rpc_call_done = nfs4_lock_done,
4582 .rpc_release = nfs4_lock_release,
4583};
4584
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004585static const struct rpc_call_ops nfs4_recover_lock_ops = {
4586 .rpc_call_prepare = nfs4_recover_lock_prepare,
4587 .rpc_call_done = nfs4_lock_done,
4588 .rpc_release = nfs4_lock_release,
4589};
4590
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004591static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4592{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004593 switch (error) {
4594 case -NFS4ERR_ADMIN_REVOKED:
4595 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004596 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004597 if (new_lock_owner != 0 ||
4598 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004599 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004600 break;
4601 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004602 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004603 case -NFS4ERR_EXPIRED:
4604 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004605 };
4606}
4607
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004608static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609{
4610 struct nfs4_lockdata *data;
4611 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004612 struct rpc_message msg = {
4613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4614 .rpc_cred = state->owner->so_cred,
4615 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004616 struct rpc_task_setup task_setup_data = {
4617 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004618 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004619 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004620 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004621 .flags = RPC_TASK_ASYNC,
4622 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623 int ret;
4624
Harvey Harrison3110ff82008-05-02 13:42:44 -07004625 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004626 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004627 fl->fl_u.nfs4_fl.owner,
4628 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004629 if (data == NULL)
4630 return -ENOMEM;
4631 if (IS_SETLKW(cmd))
4632 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004633 if (recovery_type > NFS_LOCK_NEW) {
4634 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004635 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004636 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4637 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004638 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004639 msg.rpc_argp = &data->arg;
4640 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004641 task_setup_data.callback_data = data;
4642 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004643 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004644 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004645 ret = nfs4_wait_for_completion_rpc_task(task);
4646 if (ret == 0) {
4647 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004648 if (ret)
4649 nfs4_handle_setlk_error(data->server, data->lsp,
4650 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651 } else
4652 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004653 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004654 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
4658static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4659{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004660 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004661 struct nfs4_exception exception = {
4662 .inode = state->inode,
4663 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004664 int err;
4665
4666 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004667 /* Cache the lock if possible... */
4668 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4669 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004670 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004671 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004672 break;
4673 nfs4_handle_exception(server, err, &exception);
4674 } while (exception.retry);
4675 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
4678static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4679{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004680 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004681 struct nfs4_exception exception = {
4682 .inode = state->inode,
4683 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004684 int err;
4685
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004686 err = nfs4_set_lock_state(state, request);
4687 if (err != 0)
4688 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004689 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004690 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4691 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004692 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004693 switch (err) {
4694 default:
4695 goto out;
4696 case -NFS4ERR_GRACE:
4697 case -NFS4ERR_DELAY:
4698 nfs4_handle_exception(server, err, &exception);
4699 err = 0;
4700 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004701 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004702out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004706#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004707static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004708{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004709 int status, ret = NFS_OK;
4710 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004711 struct nfs_server *server = NFS_SERVER(state->inode);
4712
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004713 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4714 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4715 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4716 if (status != NFS_OK) {
4717 nfs41_free_stateid(server, &lsp->ls_stateid);
4718 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4719 ret = status;
4720 }
4721 }
4722 };
4723
4724 return ret;
4725}
4726
4727static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4728{
4729 int status = NFS_OK;
4730
4731 if (test_bit(LK_STATE_IN_USE, &state->flags))
4732 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004733 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004734 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004735 return nfs4_lock_expired(state, request);
4736}
4737#endif
4738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4740{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004741 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004742 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004743 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004745 if ((fl_flags & FL_POSIX) &&
4746 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4747 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004748 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004749 status = nfs4_set_lock_state(state, request);
4750 if (status != 0)
4751 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004752 request->fl_flags |= FL_ACCESS;
4753 status = do_vfs_lock(request->fl_file, request);
4754 if (status < 0)
4755 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004756 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004757 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004758 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004759 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004760 request->fl_flags = fl_flags & ~FL_SLEEP;
4761 status = do_vfs_lock(request->fl_file, request);
4762 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004763 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004764 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004765 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004766 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004767 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004768 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004769 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004770 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4771 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004772out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004773 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004774out:
4775 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 return status;
4777}
4778
4779static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4780{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004781 struct nfs4_exception exception = {
4782 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004783 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004784 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 int err;
4786
4787 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004788 err = _nfs4_proc_setlk(state, cmd, request);
4789 if (err == -NFS4ERR_DENIED)
4790 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004792 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 } while (exception.retry);
4794 return err;
4795}
4796
4797static int
4798nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4799{
4800 struct nfs_open_context *ctx;
4801 struct nfs4_state *state;
4802 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4803 int status;
4804
4805 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004806 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 state = ctx->state;
4808
4809 if (request->fl_start < 0 || request->fl_end < 0)
4810 return -EINVAL;
4811
Trond Myklebustd9531262009-07-21 19:22:38 -04004812 if (IS_GETLK(cmd)) {
4813 if (state != NULL)
4814 return nfs4_proc_getlk(state, F_GETLK, request);
4815 return 0;
4816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
4818 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4819 return -EINVAL;
4820
Trond Myklebustd9531262009-07-21 19:22:38 -04004821 if (request->fl_type == F_UNLCK) {
4822 if (state != NULL)
4823 return nfs4_proc_unlck(state, cmd, request);
4824 return 0;
4825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Trond Myklebustd9531262009-07-21 19:22:38 -04004827 if (state == NULL)
4828 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004829 /*
4830 * Don't rely on the VFS having checked the file open mode,
4831 * since it won't do this for flock() locks.
4832 */
4833 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4834 case F_RDLCK:
4835 if (!(filp->f_mode & FMODE_READ))
4836 return -EBADF;
4837 break;
4838 case F_WRLCK:
4839 if (!(filp->f_mode & FMODE_WRITE))
4840 return -EBADF;
4841 }
4842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 do {
4844 status = nfs4_proc_setlk(state, cmd, request);
4845 if ((status != -EAGAIN) || IS_SETLK(cmd))
4846 break;
4847 timeout = nfs4_set_lock_task_retry(timeout);
4848 status = -ERESTARTSYS;
4849 if (signalled())
4850 break;
4851 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 return status;
4853}
4854
Trond Myklebust888e6942005-11-04 15:38:11 -05004855int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4856{
4857 struct nfs_server *server = NFS_SERVER(state->inode);
4858 struct nfs4_exception exception = { };
4859 int err;
4860
4861 err = nfs4_set_lock_state(state, fl);
4862 if (err != 0)
4863 goto out;
4864 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004865 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004866 switch (err) {
4867 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004868 printk(KERN_ERR "NFS: %s: unhandled error "
4869 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004870 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004871 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004872 goto out;
4873 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004874 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004875 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004876 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004877 nfs4_schedule_lease_recovery(server->nfs_client);
4878 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004879 case -NFS4ERR_BADSESSION:
4880 case -NFS4ERR_BADSLOT:
4881 case -NFS4ERR_BAD_HIGH_SLOT:
4882 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4883 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004884 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004885 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004886 case -ERESTARTSYS:
4887 /*
4888 * The show must go on: exit, but mark the
4889 * stateid as needing recovery.
4890 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004891 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004892 case -NFS4ERR_ADMIN_REVOKED:
4893 case -NFS4ERR_BAD_STATEID:
4894 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004895 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004896 err = 0;
4897 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004898 case -EKEYEXPIRED:
4899 /*
4900 * User RPCSEC_GSS context has expired.
4901 * We cannot recover this stateid now, so
4902 * skip it and allow recovery thread to
4903 * proceed.
4904 */
4905 err = 0;
4906 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004907 case -ENOMEM:
4908 case -NFS4ERR_DENIED:
4909 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4910 err = 0;
4911 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004912 case -NFS4ERR_DELAY:
4913 break;
4914 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004915 err = nfs4_handle_exception(server, err, &exception);
4916 } while (exception.retry);
4917out:
4918 return err;
4919}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004920
Trond Myklebustcf470c32012-03-07 13:49:12 -05004921struct nfs_release_lockowner_data {
4922 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004923 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004924 struct nfs_release_lockowner_args args;
4925};
4926
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004927static void nfs4_release_lockowner_release(void *calldata)
4928{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004929 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004930 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004931 kfree(calldata);
4932}
4933
Trond Myklebust17280172012-03-11 13:11:00 -04004934static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004935 .rpc_release = nfs4_release_lockowner_release,
4936};
4937
Trond Myklebustcf470c32012-03-07 13:49:12 -05004938int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004939{
4940 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004941 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004942 struct rpc_message msg = {
4943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4944 };
4945
4946 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004947 return -EINVAL;
4948 data = kmalloc(sizeof(*data), GFP_NOFS);
4949 if (!data)
4950 return -ENOMEM;
4951 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004952 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004953 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4954 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4955 data->args.lock_owner.s_dev = server->s_dev;
4956 msg.rpc_argp = &data->args;
4957 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4958 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004959}
4960
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004961#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4962
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004963static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4964 const void *buf, size_t buflen,
4965 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004966{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004967 if (strcmp(key, "") != 0)
4968 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004969
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004970 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004971}
4972
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004973static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4974 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004975{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004976 if (strcmp(key, "") != 0)
4977 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004978
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004979 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004980}
4981
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004982static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4983 size_t list_len, const char *name,
4984 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004985{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004986 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004987
J. Bruce Fields096455a2006-03-20 23:23:42 -05004988 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4989 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004990
4991 if (list && len <= list_len)
4992 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004993 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004994}
4995
Andy Adamson533eb462011-06-13 18:25:56 -04004996/*
4997 * nfs_fhget will use either the mounted_on_fileid or the fileid
4998 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004999static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5000{
Andy Adamson533eb462011-06-13 18:25:56 -04005001 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5002 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5003 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005004 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005005 return;
5006
5007 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005008 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005009 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5010 fattr->nlink = 2;
5011}
5012
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005013static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5014 const struct qstr *name,
5015 struct nfs4_fs_locations *fs_locations,
5016 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005017{
5018 struct nfs_server *server = NFS_SERVER(dir);
5019 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005020 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005021 };
5022 struct nfs4_fs_locations_arg args = {
5023 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005024 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005025 .page = page,
5026 .bitmask = bitmask,
5027 };
Benny Halevy22958462009-04-01 09:22:02 -04005028 struct nfs4_fs_locations_res res = {
5029 .fs_locations = fs_locations,
5030 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005031 struct rpc_message msg = {
5032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5033 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005034 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005035 };
5036 int status;
5037
Harvey Harrison3110ff82008-05-02 13:42:44 -07005038 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005039
5040 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5041 * is not supported */
5042 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5043 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5044 else
5045 bitmask[0] |= FATTR4_WORD0_FILEID;
5046
Trond Myklebustc228fd32007-01-13 02:28:11 -05005047 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005048 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005049 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005050 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005051 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005052 return status;
5053}
5054
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005055int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5056 const struct qstr *name,
5057 struct nfs4_fs_locations *fs_locations,
5058 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005059{
5060 struct nfs4_exception exception = { };
5061 int err;
5062 do {
5063 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005064 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005065 &exception);
5066 } while (exception.retry);
5067 return err;
5068}
5069
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005070static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5071{
5072 int status;
5073 struct nfs4_secinfo_arg args = {
5074 .dir_fh = NFS_FH(dir),
5075 .name = name,
5076 };
5077 struct nfs4_secinfo_res res = {
5078 .flavors = flavors,
5079 };
5080 struct rpc_message msg = {
5081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5082 .rpc_argp = &args,
5083 .rpc_resp = &res,
5084 };
5085
5086 dprintk("NFS call secinfo %s\n", name->name);
5087 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5088 dprintk("NFS reply secinfo: %d\n", status);
5089 return status;
5090}
5091
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005092int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5093 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005094{
5095 struct nfs4_exception exception = { };
5096 int err;
5097 do {
5098 err = nfs4_handle_exception(NFS_SERVER(dir),
5099 _nfs4_proc_secinfo(dir, name, flavors),
5100 &exception);
5101 } while (exception.retry);
5102 return err;
5103}
5104
Andy Adamson557134a2009-04-01 09:21:53 -04005105#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005106/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005107 * Check the exchange flags returned by the server for invalid flags, having
5108 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5109 * DS flags set.
5110 */
5111static int nfs4_check_cl_exchange_flags(u32 flags)
5112{
5113 if (flags & ~EXCHGID4_FLAG_MASK_R)
5114 goto out_inval;
5115 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5116 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5117 goto out_inval;
5118 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5119 goto out_inval;
5120 return NFS_OK;
5121out_inval:
5122 return -NFS4ERR_INVAL;
5123}
5124
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005125static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005126nfs41_same_server_scope(struct nfs41_server_scope *a,
5127 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005128{
5129 if (a->server_scope_sz == b->server_scope_sz &&
5130 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5131 return true;
5132
5133 return false;
5134}
5135
Andy Adamson357f54d2010-12-14 10:11:57 -05005136/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005137 * nfs4_proc_bind_conn_to_session()
5138 *
5139 * The 4.1 client currently uses the same TCP connection for the
5140 * fore and backchannel.
5141 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005142int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005143{
5144 int status;
5145 struct nfs41_bind_conn_to_session_res res;
5146 struct rpc_message msg = {
5147 .rpc_proc =
5148 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5149 .rpc_argp = clp,
5150 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005151 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005152 };
5153
5154 dprintk("--> %s\n", __func__);
5155 BUG_ON(clp == NULL);
5156
5157 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5158 if (unlikely(res.session == NULL)) {
5159 status = -ENOMEM;
5160 goto out;
5161 }
5162
5163 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5164 if (status == 0) {
5165 if (memcmp(res.session->sess_id.data,
5166 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5167 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5168 status = -EIO;
5169 goto out_session;
5170 }
5171 if (res.dir != NFS4_CDFS4_BOTH) {
5172 dprintk("NFS: %s: Unexpected direction from server\n",
5173 __func__);
5174 status = -EIO;
5175 goto out_session;
5176 }
5177 if (res.use_conn_in_rdma_mode) {
5178 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5179 __func__);
5180 status = -EIO;
5181 goto out_session;
5182 }
5183 }
5184out_session:
5185 kfree(res.session);
5186out:
5187 dprintk("<-- %s status= %d\n", __func__, status);
5188 return status;
5189}
5190
5191/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005192 * nfs4_proc_exchange_id()
5193 *
5194 * Since the clientid has expired, all compounds using sessions
5195 * associated with the stale clientid will be returning
5196 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5197 * be in some phase of session reset.
5198 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005199int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005200{
5201 nfs4_verifier verifier;
5202 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005203 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005204 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005205 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005206 };
5207 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005208 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005209 };
5210 int status;
5211 struct rpc_message msg = {
5212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5213 .rpc_argp = &args,
5214 .rpc_resp = &res,
5215 .rpc_cred = cred,
5216 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005217
5218 dprintk("--> %s\n", __func__);
5219 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005220
Chuck Leverf0920752012-05-21 22:45:41 -04005221 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005222
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005223 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005224 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005225 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005226 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005227 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005228
Chuck Leveracdeb692012-05-21 22:46:16 -04005229 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005230 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005231 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005232 status = -ENOMEM;
5233 goto out;
5234 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005235
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005236 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005237 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005238 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005239 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005240 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005241 }
5242
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005243 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005244 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005245 status = -ENOMEM;
5246 goto out_server_scope;
5247 }
5248
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005249 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005250 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005251 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005252
Chuck Lever177313f2012-05-21 22:44:58 -04005253 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005254 clp->cl_clientid = res.clientid;
5255 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5256 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5257 clp->cl_seqid = res.seqid;
5258
Chuck Leveracdeb692012-05-21 22:46:16 -04005259 kfree(clp->cl_serverowner);
5260 clp->cl_serverowner = res.server_owner;
5261 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005262
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005263 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005264 kfree(clp->cl_implid);
5265 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005266
Chuck Lever177313f2012-05-21 22:44:58 -04005267 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005268 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005269 res.server_scope)) {
5270 dprintk("%s: server_scope mismatch detected\n",
5271 __func__);
5272 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005273 kfree(clp->cl_serverscope);
5274 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005275 }
5276
Chuck Lever177313f2012-05-21 22:44:58 -04005277 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005278 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005279 goto out;
5280 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005281 } else
5282 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005283
Chuck Leveracdeb692012-05-21 22:46:16 -04005284out_server_owner:
5285 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005286out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005287 kfree(res.server_scope);
5288out:
Chuck Lever177313f2012-05-21 22:44:58 -04005289 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005290 dprintk("%s: Server Implementation ID: "
5291 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005292 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5293 clp->cl_implid->date.seconds,
5294 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005295 dprintk("<-- %s status= %d\n", __func__, status);
5296 return status;
5297}
5298
Trond Myklebust66245532012-05-25 17:18:09 -04005299static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5300 struct rpc_cred *cred)
5301{
5302 struct rpc_message msg = {
5303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5304 .rpc_argp = clp,
5305 .rpc_cred = cred,
5306 };
5307 int status;
5308
5309 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5310 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005311 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005312 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5313 return status;
5314}
5315
5316static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5317 struct rpc_cred *cred)
5318{
5319 unsigned int loop;
5320 int ret;
5321
5322 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5323 ret = _nfs4_proc_destroy_clientid(clp, cred);
5324 switch (ret) {
5325 case -NFS4ERR_DELAY:
5326 case -NFS4ERR_CLIENTID_BUSY:
5327 ssleep(1);
5328 break;
5329 default:
5330 return ret;
5331 }
5332 }
5333 return 0;
5334}
5335
5336int nfs4_destroy_clientid(struct nfs_client *clp)
5337{
5338 struct rpc_cred *cred;
5339 int ret = 0;
5340
5341 if (clp->cl_mvops->minor_version < 1)
5342 goto out;
5343 if (clp->cl_exchange_flags == 0)
5344 goto out;
5345 cred = nfs4_get_exchange_id_cred(clp);
5346 ret = nfs4_proc_destroy_clientid(clp, cred);
5347 if (cred)
5348 put_rpccred(cred);
5349 switch (ret) {
5350 case 0:
5351 case -NFS4ERR_STALE_CLIENTID:
5352 clp->cl_exchange_flags = 0;
5353 }
5354out:
5355 return ret;
5356}
5357
Andy Adamson2050f0c2009-04-01 09:22:30 -04005358struct nfs4_get_lease_time_data {
5359 struct nfs4_get_lease_time_args *args;
5360 struct nfs4_get_lease_time_res *res;
5361 struct nfs_client *clp;
5362};
5363
5364static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5365 void *calldata)
5366{
5367 int ret;
5368 struct nfs4_get_lease_time_data *data =
5369 (struct nfs4_get_lease_time_data *)calldata;
5370
5371 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005372 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005373 /* just setup sequence, do not trigger session recovery
5374 since we're invoked within one */
5375 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005376 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005377 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005378
5379 BUG_ON(ret == -EAGAIN);
5380 rpc_call_start(task);
5381 dprintk("<-- %s\n", __func__);
5382}
5383
5384/*
5385 * Called from nfs4_state_manager thread for session setup, so don't recover
5386 * from sequence operation or clientid errors.
5387 */
5388static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5389{
5390 struct nfs4_get_lease_time_data *data =
5391 (struct nfs4_get_lease_time_data *)calldata;
5392
5393 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005394 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5395 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005396 switch (task->tk_status) {
5397 case -NFS4ERR_DELAY:
5398 case -NFS4ERR_GRACE:
5399 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5400 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5401 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005402 /* fall through */
5403 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005404 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005405 return;
5406 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005407 dprintk("<-- %s\n", __func__);
5408}
5409
Trond Myklebust17280172012-03-11 13:11:00 -04005410static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005411 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5412 .rpc_call_done = nfs4_get_lease_time_done,
5413};
5414
5415int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5416{
5417 struct rpc_task *task;
5418 struct nfs4_get_lease_time_args args;
5419 struct nfs4_get_lease_time_res res = {
5420 .lr_fsinfo = fsinfo,
5421 };
5422 struct nfs4_get_lease_time_data data = {
5423 .args = &args,
5424 .res = &res,
5425 .clp = clp,
5426 };
5427 struct rpc_message msg = {
5428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5429 .rpc_argp = &args,
5430 .rpc_resp = &res,
5431 };
5432 struct rpc_task_setup task_setup = {
5433 .rpc_client = clp->cl_rpcclient,
5434 .rpc_message = &msg,
5435 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005436 .callback_data = &data,
5437 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005438 };
5439 int status;
5440
Trond Myklebust9d12b212012-01-17 22:04:25 -05005441 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005442 dprintk("--> %s\n", __func__);
5443 task = rpc_run_task(&task_setup);
5444
5445 if (IS_ERR(task))
5446 status = PTR_ERR(task);
5447 else {
5448 status = task->tk_status;
5449 rpc_put_task(task);
5450 }
5451 dprintk("<-- %s return %d\n", __func__, status);
5452
5453 return status;
5454}
5455
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005456static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5457{
5458 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5459}
5460
5461static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5462 struct nfs4_slot *new,
5463 u32 max_slots,
5464 u32 ivalue)
5465{
5466 struct nfs4_slot *old = NULL;
5467 u32 i;
5468
5469 spin_lock(&tbl->slot_tbl_lock);
5470 if (new) {
5471 old = tbl->slots;
5472 tbl->slots = new;
5473 tbl->max_slots = max_slots;
5474 }
5475 tbl->highest_used_slotid = -1; /* no slot is currently used */
5476 for (i = 0; i < tbl->max_slots; i++)
5477 tbl->slots[i].seq_nr = ivalue;
5478 spin_unlock(&tbl->slot_tbl_lock);
5479 kfree(old);
5480}
5481
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005482/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005483 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005484 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005485static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5486 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005487{
Andy Adamson104aeba2010-01-14 17:45:10 -05005488 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005489 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005490
Andy Adamson104aeba2010-01-14 17:45:10 -05005491 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5492 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005493
Andy Adamson104aeba2010-01-14 17:45:10 -05005494 /* Does the newly negotiated max_reqs match the existing slot table? */
5495 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005496 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005497 if (!new)
5498 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005499 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005500 ret = 0;
5501
5502 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005503 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5504 tbl, tbl->slots, tbl->max_slots);
5505out:
5506 dprintk("<-- %s: return %d\n", __func__, ret);
5507 return ret;
5508}
5509
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005510/* Destroy the slot table */
5511static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5512{
5513 if (session->fc_slot_table.slots != NULL) {
5514 kfree(session->fc_slot_table.slots);
5515 session->fc_slot_table.slots = NULL;
5516 }
5517 if (session->bc_slot_table.slots != NULL) {
5518 kfree(session->bc_slot_table.slots);
5519 session->bc_slot_table.slots = NULL;
5520 }
5521 return;
5522}
5523
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005524/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005525 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005526 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005527static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005528{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005529 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005530 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005531
Andy Adamsonaacd5532011-11-09 13:58:21 -05005532 dprintk("--> %s\n", __func__);
5533 /* Fore channel */
5534 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005535 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5536 if (status) /* -ENOMEM */
5537 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005538 /* Back channel */
5539 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005540 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5541 if (status && tbl->slots == NULL)
5542 /* Fore and back channel share a connection so get
5543 * both slot tables or neither */
5544 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005545 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005546}
5547
5548struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5549{
5550 struct nfs4_session *session;
5551 struct nfs4_slot_table *tbl;
5552
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005553 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005554 if (!session)
5555 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005556
Andy Adamson557134a2009-04-01 09:21:53 -04005557 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005558 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005559 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005560 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005561 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005562
5563 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005564 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005565 spin_lock_init(&tbl->slot_tbl_lock);
5566 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005567 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005568
Trond Myklebust1055d762010-06-16 09:52:27 -04005569 session->session_state = 1<<NFS4_SESSION_INITING;
5570
Andy Adamson557134a2009-04-01 09:21:53 -04005571 session->clp = clp;
5572 return session;
5573}
5574
5575void nfs4_destroy_session(struct nfs4_session *session)
5576{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005577 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005578 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005579
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005580 cred = nfs4_get_exchange_id_cred(session->clp);
5581 nfs4_proc_destroy_session(session, cred);
5582 if (cred)
5583 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005584
5585 rcu_read_lock();
5586 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5587 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005588 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005589 __func__, xprt);
5590 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005591 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005592 kfree(session);
5593}
5594
Andy Adamsonfc931582009-04-01 09:22:31 -04005595/*
5596 * Initialize the values to be used by the client in CREATE_SESSION
5597 * If nfs4_init_session set the fore channel request and response sizes,
5598 * use them.
5599 *
5600 * Set the back channel max_resp_sz_cached to zero to force the client to
5601 * always set csa_cachethis to FALSE because the current implementation
5602 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5603 */
5604static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5605{
5606 struct nfs4_session *session = args->client->cl_session;
5607 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5608 mxresp_sz = session->fc_attrs.max_resp_sz;
5609
5610 if (mxrqst_sz == 0)
5611 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5612 if (mxresp_sz == 0)
5613 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5614 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005615 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5616 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005617 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005618 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005619
5620 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005621 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005622 __func__,
5623 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005624 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005625
5626 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005627 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5628 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5629 args->bc_attrs.max_resp_sz_cached = 0;
5630 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5631 args->bc_attrs.max_reqs = 1;
5632
5633 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5634 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5635 __func__,
5636 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5637 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5638 args->bc_attrs.max_reqs);
5639}
5640
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005641static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005642{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005643 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5644 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5645
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005646 if (rcvd->max_resp_sz > sent->max_resp_sz)
5647 return -EINVAL;
5648 /*
5649 * Our requested max_ops is the minimum we need; we're not
5650 * prepared to break up compounds into smaller pieces than that.
5651 * So, no point even trying to continue if the server won't
5652 * cooperate:
5653 */
5654 if (rcvd->max_ops < sent->max_ops)
5655 return -EINVAL;
5656 if (rcvd->max_reqs == 0)
5657 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005658 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5659 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005660 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005661}
5662
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005663static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5664{
5665 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5666 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005667
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005668 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5669 return -EINVAL;
5670 if (rcvd->max_resp_sz < sent->max_resp_sz)
5671 return -EINVAL;
5672 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5673 return -EINVAL;
5674 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005675 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005676 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005677 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005678 return -EINVAL;
5679 return 0;
5680}
Andy Adamson8d353012009-04-01 09:22:32 -04005681
Andy Adamson8d353012009-04-01 09:22:32 -04005682static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5683 struct nfs4_session *session)
5684{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005685 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005686
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005687 ret = nfs4_verify_fore_channel_attrs(args, session);
5688 if (ret)
5689 return ret;
5690 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005691}
5692
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005693static int _nfs4_proc_create_session(struct nfs_client *clp,
5694 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005695{
5696 struct nfs4_session *session = clp->cl_session;
5697 struct nfs41_create_session_args args = {
5698 .client = clp,
5699 .cb_program = NFS4_CALLBACK,
5700 };
5701 struct nfs41_create_session_res res = {
5702 .client = clp,
5703 };
5704 struct rpc_message msg = {
5705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5706 .rpc_argp = &args,
5707 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005708 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005709 };
5710 int status;
5711
5712 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005713 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005714
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005715 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005716
Andy Adamson8d353012009-04-01 09:22:32 -04005717 if (!status)
5718 /* Verify the session's negotiated channel_attrs values */
5719 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005720 if (!status) {
5721 /* Increment the clientid slot sequence id */
5722 clp->cl_seqid++;
5723 }
5724
5725 return status;
5726}
5727
5728/*
5729 * Issues a CREATE_SESSION operation to the server.
5730 * It is the responsibility of the caller to verify the session is
5731 * expired before calling this routine.
5732 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005733int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005734{
5735 int status;
5736 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005737 struct nfs4_session *session = clp->cl_session;
5738
5739 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5740
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005741 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005742 if (status)
5743 goto out;
5744
Andy Adamsonaacd5532011-11-09 13:58:21 -05005745 /* Init or reset the session slot tables */
5746 status = nfs4_setup_session_slot_tables(session);
5747 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005748 if (status)
5749 goto out;
5750
5751 ptr = (unsigned *)&session->sess_id.data[0];
5752 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5753 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005754out:
5755 dprintk("<-- %s\n", __func__);
5756 return status;
5757}
5758
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005759/*
5760 * Issue the over-the-wire RPC DESTROY_SESSION.
5761 * The caller must serialize access to this routine.
5762 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005763int nfs4_proc_destroy_session(struct nfs4_session *session,
5764 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005765{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005766 struct rpc_message msg = {
5767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5768 .rpc_argp = session,
5769 .rpc_cred = cred,
5770 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005771 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005772
5773 dprintk("--> nfs4_proc_destroy_session\n");
5774
5775 /* session is still being setup */
5776 if (session->clp->cl_cons_state != NFS_CS_READY)
5777 return status;
5778
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005779 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005780
5781 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005782 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005783 "Session has been destroyed regardless...\n", status);
5784
5785 dprintk("<-- nfs4_proc_destroy_session\n");
5786 return status;
5787}
5788
Trond Myklebust7b38c362012-05-23 13:23:31 -04005789/*
5790 * With sessions, the client is not marked ready until after a
5791 * successful EXCHANGE_ID and CREATE_SESSION.
5792 *
5793 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5794 * other versions of NFS can be tried.
5795 */
5796static int nfs41_check_session_ready(struct nfs_client *clp)
5797{
5798 int ret;
5799
5800 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5801 ret = nfs4_client_recover_expired_lease(clp);
5802 if (ret)
5803 return ret;
5804 }
5805 if (clp->cl_cons_state < NFS_CS_READY)
5806 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005807 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005808 return 0;
5809}
5810
Trond Myklebustfccba802009-07-21 16:48:07 -04005811int nfs4_init_session(struct nfs_server *server)
5812{
5813 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005814 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005815 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005816
5817 if (!nfs4_has_session(clp))
5818 return 0;
5819
Trond Myklebust1055d762010-06-16 09:52:27 -04005820 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005821 spin_lock(&clp->cl_lock);
5822 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005823
Trond Myklebust7b38c362012-05-23 13:23:31 -04005824 rsize = server->rsize;
5825 if (rsize == 0)
5826 rsize = NFS_MAX_FILE_IO_SIZE;
5827 wsize = server->wsize;
5828 if (wsize == 0)
5829 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005830
Trond Myklebust7b38c362012-05-23 13:23:31 -04005831 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5832 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5833 }
5834 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005835
Trond Myklebust7b38c362012-05-23 13:23:31 -04005836 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005837}
5838
Trond Myklebust7b38c362012-05-23 13:23:31 -04005839int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005840{
5841 struct nfs4_session *session = clp->cl_session;
5842 int ret;
5843
Trond Myklebust7b38c362012-05-23 13:23:31 -04005844 spin_lock(&clp->cl_lock);
5845 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5846 /*
5847 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5848 * DS lease to be equal to the MDS lease.
5849 */
5850 clp->cl_lease_time = lease_time;
5851 clp->cl_last_renewal = jiffies;
5852 }
5853 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005854
Trond Myklebust7b38c362012-05-23 13:23:31 -04005855 ret = nfs41_check_session_ready(clp);
5856 if (ret)
5857 return ret;
5858 /* Test for the DS role */
5859 if (!is_ds_client(clp))
5860 return -ENODEV;
5861 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005862}
5863EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5864
5865
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005866/*
5867 * Renew the cl_session lease.
5868 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005869struct nfs4_sequence_data {
5870 struct nfs_client *clp;
5871 struct nfs4_sequence_args args;
5872 struct nfs4_sequence_res res;
5873};
5874
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005875static void nfs41_sequence_release(void *data)
5876{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005877 struct nfs4_sequence_data *calldata = data;
5878 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005879
Alexandros Batsakis71358402010-02-05 03:45:05 -08005880 if (atomic_read(&clp->cl_count) > 1)
5881 nfs4_schedule_state_renewal(clp);
5882 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005883 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005884}
5885
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005886static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5887{
5888 switch(task->tk_status) {
5889 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005890 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5891 return -EAGAIN;
5892 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005893 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005894 }
5895 return 0;
5896}
5897
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005898static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005899{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005900 struct nfs4_sequence_data *calldata = data;
5901 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005902
Trond Myklebust14516c32010-07-31 14:29:06 -04005903 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5904 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005905
5906 if (task->tk_status < 0) {
5907 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005908 if (atomic_read(&clp->cl_count) == 1)
5909 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005910
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005911 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5912 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005913 return;
5914 }
5915 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005916 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005917out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005918 dprintk("<-- %s\n", __func__);
5919}
5920
5921static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5922{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005923 struct nfs4_sequence_data *calldata = data;
5924 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005925 struct nfs4_sequence_args *args;
5926 struct nfs4_sequence_res *res;
5927
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005928 args = task->tk_msg.rpc_argp;
5929 res = task->tk_msg.rpc_resp;
5930
Trond Myklebust9d12b212012-01-17 22:04:25 -05005931 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005932 return;
5933 rpc_call_start(task);
5934}
5935
5936static const struct rpc_call_ops nfs41_sequence_ops = {
5937 .rpc_call_done = nfs41_sequence_call_done,
5938 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005939 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005940};
5941
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005942static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005943{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005944 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005945 struct rpc_message msg = {
5946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5947 .rpc_cred = cred,
5948 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005949 struct rpc_task_setup task_setup_data = {
5950 .rpc_client = clp->cl_rpcclient,
5951 .rpc_message = &msg,
5952 .callback_ops = &nfs41_sequence_ops,
5953 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5954 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005955
Alexandros Batsakis71358402010-02-05 03:45:05 -08005956 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005957 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005958 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005959 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005960 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005961 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005962 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005963 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005964 msg.rpc_argp = &calldata->args;
5965 msg.rpc_resp = &calldata->res;
5966 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005967 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005968
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005969 return rpc_run_task(&task_setup_data);
5970}
5971
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005972static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005973{
5974 struct rpc_task *task;
5975 int ret = 0;
5976
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005977 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5978 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005979 task = _nfs41_proc_sequence(clp, cred);
5980 if (IS_ERR(task))
5981 ret = PTR_ERR(task);
5982 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005983 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005984 dprintk("<-- %s status=%d\n", __func__, ret);
5985 return ret;
5986}
5987
5988static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5989{
5990 struct rpc_task *task;
5991 int ret;
5992
5993 task = _nfs41_proc_sequence(clp, cred);
5994 if (IS_ERR(task)) {
5995 ret = PTR_ERR(task);
5996 goto out;
5997 }
5998 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005999 if (!ret) {
6000 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6001
6002 if (task->tk_status == 0)
6003 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006004 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006005 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006006 rpc_put_task(task);
6007out:
6008 dprintk("<-- %s status=%d\n", __func__, ret);
6009 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006010}
6011
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006012struct nfs4_reclaim_complete_data {
6013 struct nfs_client *clp;
6014 struct nfs41_reclaim_complete_args arg;
6015 struct nfs41_reclaim_complete_res res;
6016};
6017
6018static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6019{
6020 struct nfs4_reclaim_complete_data *calldata = data;
6021
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006022 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04006023 if (nfs41_setup_sequence(calldata->clp->cl_session,
6024 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006025 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006026 return;
6027
6028 rpc_call_start(task);
6029}
6030
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006031static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6032{
6033 switch(task->tk_status) {
6034 case 0:
6035 case -NFS4ERR_COMPLETE_ALREADY:
6036 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6037 break;
6038 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006039 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006040 /* fall through */
6041 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006042 return -EAGAIN;
6043 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006044 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006045 }
6046 return 0;
6047}
6048
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006049static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6050{
6051 struct nfs4_reclaim_complete_data *calldata = data;
6052 struct nfs_client *clp = calldata->clp;
6053 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6054
6055 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006056 if (!nfs41_sequence_done(task, res))
6057 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006058
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006059 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6060 rpc_restart_call_prepare(task);
6061 return;
6062 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006063 dprintk("<-- %s\n", __func__);
6064}
6065
6066static void nfs4_free_reclaim_complete_data(void *data)
6067{
6068 struct nfs4_reclaim_complete_data *calldata = data;
6069
6070 kfree(calldata);
6071}
6072
6073static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6074 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6075 .rpc_call_done = nfs4_reclaim_complete_done,
6076 .rpc_release = nfs4_free_reclaim_complete_data,
6077};
6078
6079/*
6080 * Issue a global reclaim complete.
6081 */
6082static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6083{
6084 struct nfs4_reclaim_complete_data *calldata;
6085 struct rpc_task *task;
6086 struct rpc_message msg = {
6087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6088 };
6089 struct rpc_task_setup task_setup_data = {
6090 .rpc_client = clp->cl_rpcclient,
6091 .rpc_message = &msg,
6092 .callback_ops = &nfs4_reclaim_complete_call_ops,
6093 .flags = RPC_TASK_ASYNC,
6094 };
6095 int status = -ENOMEM;
6096
6097 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006098 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006099 if (calldata == NULL)
6100 goto out;
6101 calldata->clp = clp;
6102 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006103
Trond Myklebust9d12b212012-01-17 22:04:25 -05006104 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006105 msg.rpc_argp = &calldata->arg;
6106 msg.rpc_resp = &calldata->res;
6107 task_setup_data.callback_data = calldata;
6108 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006109 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006110 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006111 goto out;
6112 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006113 status = nfs4_wait_for_completion_rpc_task(task);
6114 if (status == 0)
6115 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006116 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006117 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006118out:
6119 dprintk("<-- %s status=%d\n", __func__, status);
6120 return status;
6121}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006122
6123static void
6124nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6125{
6126 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006127 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006128
6129 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006130 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6131 * right now covering the LAYOUTGET we are about to send.
6132 * However, that is not so catastrophic, and there seems
6133 * to be no way to prevent it completely.
6134 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006135 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006136 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006137 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006138 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6139 NFS_I(lgp->args.inode)->layout,
6140 lgp->args.ctx->state)) {
6141 rpc_exit(task, NFS4_OK);
6142 return;
6143 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006144 rpc_call_start(task);
6145}
6146
6147static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6148{
6149 struct nfs4_layoutget *lgp = calldata;
6150 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6151
6152 dprintk("--> %s\n", __func__);
6153
6154 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6155 return;
6156
6157 switch (task->tk_status) {
6158 case 0:
6159 break;
6160 case -NFS4ERR_LAYOUTTRYLATER:
6161 case -NFS4ERR_RECALLCONFLICT:
6162 task->tk_status = -NFS4ERR_DELAY;
6163 /* Fall through */
6164 default:
6165 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6166 rpc_restart_call_prepare(task);
6167 return;
6168 }
6169 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006170 dprintk("<-- %s\n", __func__);
6171}
6172
6173static void nfs4_layoutget_release(void *calldata)
6174{
6175 struct nfs4_layoutget *lgp = calldata;
6176
6177 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006178 put_nfs_open_context(lgp->args.ctx);
6179 kfree(calldata);
6180 dprintk("<-- %s\n", __func__);
6181}
6182
6183static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6184 .rpc_call_prepare = nfs4_layoutget_prepare,
6185 .rpc_call_done = nfs4_layoutget_done,
6186 .rpc_release = nfs4_layoutget_release,
6187};
6188
6189int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6190{
6191 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6192 struct rpc_task *task;
6193 struct rpc_message msg = {
6194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6195 .rpc_argp = &lgp->args,
6196 .rpc_resp = &lgp->res,
6197 };
6198 struct rpc_task_setup task_setup_data = {
6199 .rpc_client = server->client,
6200 .rpc_message = &msg,
6201 .callback_ops = &nfs4_layoutget_call_ops,
6202 .callback_data = lgp,
6203 .flags = RPC_TASK_ASYNC,
6204 };
6205 int status = 0;
6206
6207 dprintk("--> %s\n", __func__);
6208
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006209 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006210 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006211 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006212 task = rpc_run_task(&task_setup_data);
6213 if (IS_ERR(task))
6214 return PTR_ERR(task);
6215 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006216 if (status == 0)
6217 status = task->tk_status;
6218 if (status == 0)
6219 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006220 rpc_put_task(task);
6221 dprintk("<-- %s status=%d\n", __func__, status);
6222 return status;
6223}
6224
Benny Halevycbe82602011-05-22 19:52:37 +03006225static void
6226nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6227{
6228 struct nfs4_layoutreturn *lrp = calldata;
6229
6230 dprintk("--> %s\n", __func__);
6231 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006232 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006233 return;
6234 rpc_call_start(task);
6235}
6236
6237static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6238{
6239 struct nfs4_layoutreturn *lrp = calldata;
6240 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006241 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006242
6243 dprintk("--> %s\n", __func__);
6244
6245 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6246 return;
6247
6248 server = NFS_SERVER(lrp->args.inode);
6249 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006250 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006251 return;
6252 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006253 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006254 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006255 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006256 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006257 } else
6258 BUG_ON(!list_empty(&lo->plh_segs));
6259 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006260 lo->plh_block_lgets--;
6261 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006262 dprintk("<-- %s\n", __func__);
6263}
6264
6265static void nfs4_layoutreturn_release(void *calldata)
6266{
6267 struct nfs4_layoutreturn *lrp = calldata;
6268
6269 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006270 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006271 kfree(calldata);
6272 dprintk("<-- %s\n", __func__);
6273}
6274
6275static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6276 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6277 .rpc_call_done = nfs4_layoutreturn_done,
6278 .rpc_release = nfs4_layoutreturn_release,
6279};
6280
6281int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6282{
6283 struct rpc_task *task;
6284 struct rpc_message msg = {
6285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6286 .rpc_argp = &lrp->args,
6287 .rpc_resp = &lrp->res,
6288 };
6289 struct rpc_task_setup task_setup_data = {
6290 .rpc_client = lrp->clp->cl_rpcclient,
6291 .rpc_message = &msg,
6292 .callback_ops = &nfs4_layoutreturn_call_ops,
6293 .callback_data = lrp,
6294 };
6295 int status;
6296
6297 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006298 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006299 task = rpc_run_task(&task_setup_data);
6300 if (IS_ERR(task))
6301 return PTR_ERR(task);
6302 status = task->tk_status;
6303 dprintk("<-- %s status=%d\n", __func__, status);
6304 rpc_put_task(task);
6305 return status;
6306}
6307
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006308/*
6309 * Retrieve the list of Data Server devices from the MDS.
6310 */
6311static int _nfs4_getdevicelist(struct nfs_server *server,
6312 const struct nfs_fh *fh,
6313 struct pnfs_devicelist *devlist)
6314{
6315 struct nfs4_getdevicelist_args args = {
6316 .fh = fh,
6317 .layoutclass = server->pnfs_curr_ld->id,
6318 };
6319 struct nfs4_getdevicelist_res res = {
6320 .devlist = devlist,
6321 };
6322 struct rpc_message msg = {
6323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6324 .rpc_argp = &args,
6325 .rpc_resp = &res,
6326 };
6327 int status;
6328
6329 dprintk("--> %s\n", __func__);
6330 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6331 &res.seq_res, 0);
6332 dprintk("<-- %s status=%d\n", __func__, status);
6333 return status;
6334}
6335
6336int nfs4_proc_getdevicelist(struct nfs_server *server,
6337 const struct nfs_fh *fh,
6338 struct pnfs_devicelist *devlist)
6339{
6340 struct nfs4_exception exception = { };
6341 int err;
6342
6343 do {
6344 err = nfs4_handle_exception(server,
6345 _nfs4_getdevicelist(server, fh, devlist),
6346 &exception);
6347 } while (exception.retry);
6348
6349 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6350 err, devlist->num_devs);
6351
6352 return err;
6353}
6354EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6355
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006356static int
6357_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6358{
6359 struct nfs4_getdeviceinfo_args args = {
6360 .pdev = pdev,
6361 };
6362 struct nfs4_getdeviceinfo_res res = {
6363 .pdev = pdev,
6364 };
6365 struct rpc_message msg = {
6366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6367 .rpc_argp = &args,
6368 .rpc_resp = &res,
6369 };
6370 int status;
6371
6372 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006373 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006374 dprintk("<-- %s status=%d\n", __func__, status);
6375
6376 return status;
6377}
6378
6379int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6380{
6381 struct nfs4_exception exception = { };
6382 int err;
6383
6384 do {
6385 err = nfs4_handle_exception(server,
6386 _nfs4_proc_getdeviceinfo(server, pdev),
6387 &exception);
6388 } while (exception.retry);
6389 return err;
6390}
6391EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6392
Andy Adamson863a3c62011-03-23 13:27:54 +00006393static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6394{
6395 struct nfs4_layoutcommit_data *data = calldata;
6396 struct nfs_server *server = NFS_SERVER(data->args.inode);
6397
6398 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006399 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006400 return;
6401 rpc_call_start(task);
6402}
6403
6404static void
6405nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6406{
6407 struct nfs4_layoutcommit_data *data = calldata;
6408 struct nfs_server *server = NFS_SERVER(data->args.inode);
6409
6410 if (!nfs4_sequence_done(task, &data->res.seq_res))
6411 return;
6412
6413 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006414 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6415 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6416 case -NFS4ERR_BADLAYOUT: /* no layout */
6417 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006418 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006419 break;
6420 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006421 nfs_post_op_update_inode_force_wcc(data->args.inode,
6422 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006423 break;
6424 default:
6425 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6426 rpc_restart_call_prepare(task);
6427 return;
6428 }
6429 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006430}
6431
6432static void nfs4_layoutcommit_release(void *calldata)
6433{
6434 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006435 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006436 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006437
Andy Adamsondb29c082011-07-30 20:52:38 -04006438 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006439 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006440 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6441 list_del_init(&lseg->pls_lc_list);
6442 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6443 &lseg->pls_flags))
6444 put_lseg(lseg);
6445 }
Peng Tao92407e72011-10-23 20:21:17 -07006446
6447 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6448 smp_mb__after_clear_bit();
6449 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6450
Andy Adamson863a3c62011-03-23 13:27:54 +00006451 put_rpccred(data->cred);
6452 kfree(data);
6453}
6454
6455static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6456 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6457 .rpc_call_done = nfs4_layoutcommit_done,
6458 .rpc_release = nfs4_layoutcommit_release,
6459};
6460
6461int
Andy Adamsonef311532011-03-12 02:58:10 -05006462nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006463{
6464 struct rpc_message msg = {
6465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6466 .rpc_argp = &data->args,
6467 .rpc_resp = &data->res,
6468 .rpc_cred = data->cred,
6469 };
6470 struct rpc_task_setup task_setup_data = {
6471 .task = &data->task,
6472 .rpc_client = NFS_CLIENT(data->args.inode),
6473 .rpc_message = &msg,
6474 .callback_ops = &nfs4_layoutcommit_ops,
6475 .callback_data = data,
6476 .flags = RPC_TASK_ASYNC,
6477 };
6478 struct rpc_task *task;
6479 int status = 0;
6480
6481 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6482 "lbw: %llu inode %lu\n",
6483 data->task.tk_pid, sync,
6484 data->args.lastbytewritten,
6485 data->args.inode->i_ino);
6486
Trond Myklebust9d12b212012-01-17 22:04:25 -05006487 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006488 task = rpc_run_task(&task_setup_data);
6489 if (IS_ERR(task))
6490 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006491 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006492 goto out;
6493 status = nfs4_wait_for_completion_rpc_task(task);
6494 if (status != 0)
6495 goto out;
6496 status = task->tk_status;
6497out:
6498 dprintk("%s: status %d\n", __func__, status);
6499 rpc_put_task(task);
6500 return status;
6501}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006502
6503static int
6504_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6505 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6506{
6507 struct nfs41_secinfo_no_name_args args = {
6508 .style = SECINFO_STYLE_CURRENT_FH,
6509 };
6510 struct nfs4_secinfo_res res = {
6511 .flavors = flavors,
6512 };
6513 struct rpc_message msg = {
6514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6515 .rpc_argp = &args,
6516 .rpc_resp = &res,
6517 };
6518 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6519}
6520
6521static int
6522nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6523 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6524{
6525 struct nfs4_exception exception = { };
6526 int err;
6527 do {
6528 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6529 switch (err) {
6530 case 0:
6531 case -NFS4ERR_WRONGSEC:
6532 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006533 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006534 default:
6535 err = nfs4_handle_exception(server, err, &exception);
6536 }
6537 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006538out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006539 return err;
6540}
6541
6542static int
6543nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6544 struct nfs_fsinfo *info)
6545{
6546 int err;
6547 struct page *page;
6548 rpc_authflavor_t flavor;
6549 struct nfs4_secinfo_flavors *flavors;
6550
6551 page = alloc_page(GFP_KERNEL);
6552 if (!page) {
6553 err = -ENOMEM;
6554 goto out;
6555 }
6556
6557 flavors = page_address(page);
6558 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6559
6560 /*
6561 * Fall back on "guess and check" method if
6562 * the server doesn't support SECINFO_NO_NAME
6563 */
6564 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6565 err = nfs4_find_root_sec(server, fhandle, info);
6566 goto out_freepage;
6567 }
6568 if (err)
6569 goto out_freepage;
6570
6571 flavor = nfs_find_best_sec(flavors);
6572 if (err == 0)
6573 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6574
6575out_freepage:
6576 put_page(page);
6577 if (err == -EACCES)
6578 return -EPERM;
6579out:
6580 return err;
6581}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006582
6583static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006584{
6585 int status;
6586 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006587 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006588 };
6589 struct nfs41_test_stateid_res res;
6590 struct rpc_message msg = {
6591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6592 .rpc_argp = &args,
6593 .rpc_resp = &res,
6594 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006595
Trond Myklebust9d12b212012-01-17 22:04:25 -05006596 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006597 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6598
6599 if (status == NFS_OK)
6600 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006601 return status;
6602}
6603
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006604static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006605{
6606 struct nfs4_exception exception = { };
6607 int err;
6608 do {
6609 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006610 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006611 &exception);
6612 } while (exception.retry);
6613 return err;
6614}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006615
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006616static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006617{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006618 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006619 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006620 };
6621 struct nfs41_free_stateid_res res;
6622 struct rpc_message msg = {
6623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6624 .rpc_argp = &args,
6625 .rpc_resp = &res,
6626 };
6627
Trond Myklebust9d12b212012-01-17 22:04:25 -05006628 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6629 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006630}
6631
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006632static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006633{
6634 struct nfs4_exception exception = { };
6635 int err;
6636 do {
6637 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006638 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006639 &exception);
6640 } while (exception.retry);
6641 return err;
6642}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006643
6644static bool nfs41_match_stateid(const nfs4_stateid *s1,
6645 const nfs4_stateid *s2)
6646{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006647 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006648 return false;
6649
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006650 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006651 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006652 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006653 return true;
6654
6655 return false;
6656}
6657
Andy Adamson557134a2009-04-01 09:21:53 -04006658#endif /* CONFIG_NFS_V4_1 */
6659
Trond Myklebust36281ca2012-03-04 18:13:56 -05006660static bool nfs4_match_stateid(const nfs4_stateid *s1,
6661 const nfs4_stateid *s2)
6662{
Trond Myklebustf597c532012-03-04 18:13:56 -05006663 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006664}
6665
6666
Trond Myklebust17280172012-03-11 13:11:00 -04006667static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006668 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006669 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 .recover_open = nfs4_open_reclaim,
6671 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006672 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006673 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674};
6675
Andy Adamson591d71c2009-04-01 09:22:47 -04006676#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006677static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006678 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6679 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6680 .recover_open = nfs4_open_reclaim,
6681 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006682 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006683 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006684 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006685};
6686#endif /* CONFIG_NFS_V4_1 */
6687
Trond Myklebust17280172012-03-11 13:11:00 -04006688static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006689 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006690 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 .recover_open = nfs4_open_expired,
6692 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006693 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006694 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695};
6696
Andy Adamson591d71c2009-04-01 09:22:47 -04006697#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006698static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006699 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6700 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006701 .recover_open = nfs41_open_expired,
6702 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006703 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006704 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006705};
6706#endif /* CONFIG_NFS_V4_1 */
6707
Trond Myklebust17280172012-03-11 13:11:00 -04006708static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006709 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006710 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006711 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006712};
6713
6714#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006715static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006716 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006717 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006718 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006719};
6720#endif
6721
Trond Myklebust97dc1352010-06-16 09:52:26 -04006722static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6723 .minor_version = 0,
6724 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006725 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006726 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006727 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6728 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6729 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006730};
6731
6732#if defined(CONFIG_NFS_V4_1)
6733static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6734 .minor_version = 1,
6735 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006736 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006737 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006738 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6739 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6740 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006741};
6742#endif
6743
Trond Myklebust97dc1352010-06-16 09:52:26 -04006744const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6745 [0] = &nfs_v4_0_minor_ops,
6746#if defined(CONFIG_NFS_V4_1)
6747 [1] = &nfs_v4_1_minor_ops,
6748#endif
6749};
6750
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006751static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006752 .permission = nfs_permission,
6753 .getattr = nfs_getattr,
6754 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006755 .getxattr = generic_getxattr,
6756 .setxattr = generic_setxattr,
6757 .listxattr = generic_listxattr,
6758 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006759};
6760
David Howells509de812006-08-22 20:06:11 -04006761const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 .version = 4, /* protocol version */
6763 .dentry_ops = &nfs4_dentry_operations,
6764 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006765 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006766 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006768 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 .getattr = nfs4_proc_getattr,
6770 .setattr = nfs4_proc_setattr,
6771 .lookup = nfs4_proc_lookup,
6772 .access = nfs4_proc_access,
6773 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 .create = nfs4_proc_create,
6775 .remove = nfs4_proc_remove,
6776 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006777 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 .unlink_done = nfs4_proc_unlink_done,
6779 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006780 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006781 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006782 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 .link = nfs4_proc_link,
6784 .symlink = nfs4_proc_symlink,
6785 .mkdir = nfs4_proc_mkdir,
6786 .rmdir = nfs4_proc_remove,
6787 .readdir = nfs4_proc_readdir,
6788 .mknod = nfs4_proc_mknod,
6789 .statfs = nfs4_proc_statfs,
6790 .fsinfo = nfs4_proc_fsinfo,
6791 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006792 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 .decode_dirent = nfs4_decode_dirent,
6794 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006795 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006796 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006797 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006799 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006800 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006802 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006803 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006805 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006806 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006807 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006808 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006809 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006810 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006811 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006812 .free_client = nfs4_free_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813};
6814
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006815static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6816 .prefix = XATTR_NAME_NFSV4_ACL,
6817 .list = nfs4_xattr_list_nfs4_acl,
6818 .get = nfs4_xattr_get_nfs4_acl,
6819 .set = nfs4_xattr_set_nfs4_acl,
6820};
6821
6822const struct xattr_handler *nfs4_xattr_handlers[] = {
6823 &nfs4_xattr_nfs4_acl_handler,
6824 NULL
6825};
6826
Trond Myklebustef159e92012-02-06 19:50:40 -05006827module_param(max_session_slots, ushort, 0644);
6828MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6829 "requests the client will negotiate");
6830
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831/*
6832 * Local variables:
6833 * c-basic-offset: 8
6834 * End:
6835 */