blob: ce1ebff49fd704258ac80e137a3a9b414cc55b68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Trond Myklebust52567b02009-10-23 14:46:42 -0400107 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700109 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * This is our standard bitmap for GETATTR requests.
117 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400118const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 FATTR4_WORD0_TYPE
120 | FATTR4_WORD0_CHANGE
121 | FATTR4_WORD0_SIZE
122 | FATTR4_WORD0_FSID
123 | FATTR4_WORD0_FILEID,
124 FATTR4_WORD1_MODE
125 | FATTR4_WORD1_NUMLINKS
126 | FATTR4_WORD1_OWNER
127 | FATTR4_WORD1_OWNER_GROUP
128 | FATTR4_WORD1_RAWDEV
129 | FATTR4_WORD1_SPACE_USED
130 | FATTR4_WORD1_TIME_ACCESS
131 | FATTR4_WORD1_TIME_METADATA
132 | FATTR4_WORD1_TIME_MODIFY
133};
134
Trond Myklebust1549210f2012-06-05 09:16:47 -0400135static const u32 nfs4_pnfs_open_bitmap[3] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID,
141 FATTR4_WORD1_MODE
142 | FATTR4_WORD1_NUMLINKS
143 | FATTR4_WORD1_OWNER
144 | FATTR4_WORD1_OWNER_GROUP
145 | FATTR4_WORD1_RAWDEV
146 | FATTR4_WORD1_SPACE_USED
147 | FATTR4_WORD1_TIME_ACCESS
148 | FATTR4_WORD1_TIME_METADATA
149 | FATTR4_WORD1_TIME_MODIFY,
150 FATTR4_WORD2_MDSTHRESHOLD
151};
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153const u32 nfs4_statfs_bitmap[2] = {
154 FATTR4_WORD0_FILES_AVAIL
155 | FATTR4_WORD0_FILES_FREE
156 | FATTR4_WORD0_FILES_TOTAL,
157 FATTR4_WORD1_SPACE_AVAIL
158 | FATTR4_WORD1_SPACE_FREE
159 | FATTR4_WORD1_SPACE_TOTAL
160};
161
Trond Myklebust4ce79712005-06-22 17:16:21 +0000162const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 FATTR4_WORD0_MAXLINK
164 | FATTR4_WORD0_MAXNAME,
165 0
166};
167
Fred Isamandae100c2011-07-30 20:52:37 -0400168const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 | FATTR4_WORD0_MAXREAD
170 | FATTR4_WORD0_MAXWRITE
171 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700172 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400173 | FATTR4_WORD1_FS_LAYOUT_TYPES,
174 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Manoj Naik830b8e32006-06-09 09:34:25 -0400177const u32 nfs4_fs_locations_bitmap[2] = {
178 FATTR4_WORD0_TYPE
179 | FATTR4_WORD0_CHANGE
180 | FATTR4_WORD0_SIZE
181 | FATTR4_WORD0_FSID
182 | FATTR4_WORD0_FILEID
183 | FATTR4_WORD0_FS_LOCATIONS,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
192 | FATTR4_WORD1_TIME_MODIFY
193 | FATTR4_WORD1_MOUNTED_ON_FILEID
194};
195
Al Virobc4785c2006-10-19 23:28:51 -0700196static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct nfs4_readdir_arg *readdir)
198{
Al Viro0dbb4c62006-10-19 23:28:49 -0700199 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 BUG_ON(readdir->count < 80);
202 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000203 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
205 return;
206 }
207
208 readdir->cookie = 0;
209 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
210 if (cookie == 2)
211 return;
212
213 /*
214 * NFSv4 servers do not return entries for '.' and '..'
215 * Therefore, we fake these entries here. We let '.'
216 * have cookie 0 and '..' have cookie 1. Note that
217 * when talking to the server, we always send cookie 0
218 * instead of 1 or 2.
219 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800220 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (cookie == 0) {
223 *p++ = xdr_one; /* next */
224 *p++ = xdr_zero; /* cookie, first word */
225 *p++ = xdr_one; /* cookie, second word */
226 *p++ = xdr_one; /* entry len */
227 memcpy(p, ".\0\0\0", 4); /* entry */
228 p++;
229 *p++ = xdr_one; /* bitmap length */
230 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
231 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400232 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 *p++ = xdr_one; /* next */
236 *p++ = xdr_zero; /* cookie, first word */
237 *p++ = xdr_two; /* cookie, second word */
238 *p++ = xdr_two; /* entry len */
239 memcpy(p, "..\0\0", 4); /* entry */
240 p++;
241 *p++ = xdr_one; /* bitmap length */
242 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
243 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400244 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 readdir->pgbase = (char *)p - (char *)start;
247 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800248 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Trond Myklebust65de8722008-12-23 15:21:44 -0500251static int nfs4_wait_clnt_recover(struct nfs_client *clp)
252{
253 int res;
254
255 might_sleep();
256
Trond Myklebuste005e802008-12-23 15:21:48 -0500257 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400258 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400259 if (res)
260 return res;
261
262 if (clp->cl_cons_state < 0)
263 return clp->cl_cons_state;
264 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500265}
266
267static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
268{
269 int res = 0;
270
271 might_sleep();
272
273 if (*timeout <= 0)
274 *timeout = NFS4_POLL_RETRY_MIN;
275 if (*timeout > NFS4_POLL_RETRY_MAX)
276 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100277 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 if (fatal_signal_pending(current))
279 res = -ERESTARTSYS;
280 *timeout <<= 1;
281 return res;
282}
283
284/* This is the error handling routine for processes that are allowed
285 * to sleep.
286 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800287static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500288{
289 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500290 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500292 int ret = errorcode;
293
294 exception->retry = 0;
295 switch(errorcode) {
296 case 0:
297 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500298 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400299 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400300 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500301 exception->retry = 1;
302 return 0;
303 }
304 if (state == NULL)
305 break;
306 nfs4_schedule_stateid_recovery(server, state);
307 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500308 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500309 case -NFS4ERR_ADMIN_REVOKED:
310 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500311 if (state == NULL)
312 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400313 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500314 nfs4_schedule_stateid_recovery(server, state);
315 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400316 case -NFS4ERR_EXPIRED:
317 if (state != NULL)
318 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500319 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500320 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500321 nfs4_schedule_lease_recovery(clp);
322 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500323#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400324 case -NFS4ERR_BADSESSION:
325 case -NFS4ERR_BADSLOT:
326 case -NFS4ERR_BAD_HIGH_SLOT:
327 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
328 case -NFS4ERR_DEADSESSION:
329 case -NFS4ERR_SEQ_FALSE_RETRY:
330 case -NFS4ERR_SEQ_MISORDERED:
331 dprintk("%s ERROR: %d Reset session\n", __func__,
332 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400333 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400334 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500335 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500336#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500338 if (exception->timeout > HZ) {
339 /* We have retried a decent amount, time to
340 * fail
341 */
342 ret = -EBUSY;
343 break;
344 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 case -NFS4ERR_GRACE:
346 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500347 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500348 ret = nfs4_delay(server->client, &exception->timeout);
349 if (ret != 0)
350 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400351 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 case -NFS4ERR_OLD_STATEID:
353 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800354 break;
355 case -NFS4ERR_BADOWNER:
356 /* The following works around a Linux server bug! */
357 case -NFS4ERR_BADNAME:
358 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
359 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
360 exception->retry = 1;
361 printk(KERN_WARNING "NFS: v4 server %s "
362 "does not accept raw "
363 "uid/gids. "
364 "Reenabling the idmapper.\n",
365 server->nfs_client->cl_hostname);
366 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500367 }
368 /* We failed to handle the error */
369 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500370wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500371 ret = nfs4_wait_clnt_recover(clp);
372 if (ret == 0)
373 exception->retry = 1;
374 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500375}
376
377
Trond Myklebust452e9352010-07-31 14:29:06 -0400378static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_lock(&clp->cl_lock);
381 if (time_before(clp->cl_last_renewal,timestamp))
382 clp->cl_last_renewal = timestamp;
383 spin_unlock(&clp->cl_lock);
384}
385
Trond Myklebust452e9352010-07-31 14:29:06 -0400386static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
387{
388 do_renew_lease(server->nfs_client, timestamp);
389}
390
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400391#if defined(CONFIG_NFS_V4_1)
392
Benny Halevy510b8172009-04-01 09:22:14 -0400393/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400394 * nfs4_free_slot - free a slot and efficiently update slot table.
395 *
396 * freeing a slot is trivially done by clearing its respective bit
397 * in the bitmap.
398 * If the freed slotid equals highest_used_slotid we want to update it
399 * so that the server would be able to size down the slot table if needed,
400 * otherwise we know that the highest_used_slotid is still in use.
401 * When updating highest_used_slotid there may be "holes" in the bitmap
402 * so we need to scan down from highest_used_slotid to 0 looking for the now
403 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500404 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500405 *
406 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400407 */
408static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500409nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400410{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500411 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400412 /* clear used bit in bitmap */
413 __clear_bit(slotid, tbl->used_slots);
414
415 /* update highest_used_slotid when it is freed */
416 if (slotid == tbl->highest_used_slotid) {
417 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500418 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400419 tbl->highest_used_slotid = slotid;
420 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500421 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400422 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500423 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
424 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400425}
426
Trond Myklebust961a8282012-01-17 22:57:37 -0500427bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
428{
429 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
430 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400431}
432
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800433/*
Andy Adamson42acd022011-01-06 02:04:34 +0000434 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435 */
Andy Adamson42acd022011-01-06 02:04:34 +0000436static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400438 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500439 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
440 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800441 return;
442 }
443
Trond Myklebust45d43c22012-02-06 19:38:51 -0500444 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800445 return;
446
Andy Adamson42acd022011-01-06 02:04:34 +0000447 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
448 complete(&ses->fc_slot_table.complete);
449}
450
451/*
452 * Signal state manager thread if session back channel is drained
453 */
454void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
455{
456 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500457 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000458 return;
459 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
460 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800461}
462
Trond Myklebustd185a332010-06-16 09:52:25 -0400463static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400464{
465 struct nfs4_slot_table *tbl;
466
Trond Myklebustd185a332010-06-16 09:52:25 -0400467 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400468 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400469 /* just wake up the next guy waiting since
470 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500471 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500472 return;
Andy Adamson13615872009-04-01 09:22:17 -0400473 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500474
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500475 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500476 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000477 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500478 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400479 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400480}
481
Trond Myklebust14516c32010-07-31 14:29:06 -0400482static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483{
484 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400486
487 /*
488 * sr_status remains 1 if an RPC level error occurred. The server
489 * may or may not have processed the sequence operation..
490 * Proceed as if the server received and processed the sequence
491 * operation.
492 */
493 if (res->sr_status == 1)
494 res->sr_status = NFS_OK;
495
Bryan Schumaker468f8612011-04-18 15:57:32 -0400496 /* don't increment the sequence number if the task wasn't sent */
497 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400498 goto out;
499
Andy Adamson691daf32009-12-04 15:55:39 -0500500 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400501 switch (res->sr_status) {
502 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400503 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400504 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400505 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400506 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400507 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500508 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500509 if (res->sr_status_flags != 0)
510 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400511 break;
512 case -NFS4ERR_DELAY:
513 /* The server detected a resend of the RPC call and
514 * returned NFS4ERR_DELAY as per Section 2.10.6.2
515 * of RFC5661.
516 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200517 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400518 __func__,
519 res->sr_slot - res->sr_session->fc_slot_table.slots,
520 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400521 goto out_retry;
522 default:
523 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400524 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400525 }
526out:
527 /* The session may be reset by one of the error handlers. */
528 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400529 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400530 return 1;
531out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400532 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 goto out;
534 rpc_delay(task, NFS4_POLL_RETRY_MAX);
535 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400536}
537
Trond Myklebust14516c32010-07-31 14:29:06 -0400538static int nfs4_sequence_done(struct rpc_task *task,
539 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400540{
Trond Myklebust14516c32010-07-31 14:29:06 -0400541 if (res->sr_session == NULL)
542 return 1;
543 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400544}
545
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400546/*
Benny Halevy510b8172009-04-01 09:22:14 -0400547 * nfs4_find_slot - efficiently look for a free slot
548 *
549 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
550 * If found, we mark the slot as used, update the highest_used_slotid,
551 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500552 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400553 *
554 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400555 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500556static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800557nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400558{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500559 u32 slotid;
560 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400561
Trond Myklebust45d43c22012-02-06 19:38:51 -0500562 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400563 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
564 tbl->max_slots);
565 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
566 if (slotid >= tbl->max_slots)
567 goto out;
568 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500569 if (slotid > tbl->highest_used_slotid ||
570 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400571 tbl->highest_used_slotid = slotid;
572 ret_id = slotid;
573out:
574 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
575 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400576 return ret_id;
577}
578
Trond Myklebust9d12b212012-01-17 22:04:25 -0500579static void nfs41_init_sequence(struct nfs4_sequence_args *args,
580 struct nfs4_sequence_res *res, int cache_reply)
581{
582 args->sa_session = NULL;
583 args->sa_cache_this = 0;
584 if (cache_reply)
585 args->sa_cache_this = 1;
586 res->sr_session = NULL;
587 res->sr_slot = NULL;
588}
589
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000590int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 struct nfs4_sequence_args *args,
592 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct rpc_task *task)
594{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595 struct nfs4_slot *slot;
596 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500597 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400598
599 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400601 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 return 0;
603
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400604 tbl = &session->fc_slot_table;
605
606 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400607 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800608 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500609 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500610 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400611 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500612 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500613 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400614 }
615
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800616 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
617 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
618 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
619 spin_unlock(&tbl->slot_tbl_lock);
620 dprintk("%s enforce FIFO order\n", __func__);
621 return -EAGAIN;
622 }
623
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800624 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500625 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400626 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
627 spin_unlock(&tbl->slot_tbl_lock);
628 dprintk("<-- %s: no free slots\n", __func__);
629 return -EAGAIN;
630 }
631 spin_unlock(&tbl->slot_tbl_lock);
632
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800633 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400634 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400635 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400636 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400637
638 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
639
Benny Halevy99fe60d2009-04-01 09:22:29 -0400640 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400641 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400643 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644 /*
645 * sr_status is only set in decode_sequence, and so will remain
646 * set to 1 if an rpc level failure occurs.
647 */
648 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649 return 0;
650}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000651EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652
Trond Myklebust035168a2010-06-16 09:52:26 -0400653int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 struct nfs4_sequence_args *args,
655 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 struct rpc_task *task)
657{
Trond Myklebust035168a2010-06-16 09:52:26 -0400658 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659 int ret = 0;
660
Trond Myklebust9d12b212012-01-17 22:04:25 -0500661 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400663
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200664 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400665 __func__, session->clp, session, res->sr_slot ?
666 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400667
Trond Myklebust9d12b212012-01-17 22:04:25 -0500668 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669out:
670 dprintk("<-- %s status=%d\n", __func__, ret);
671 return ret;
672}
673
674struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400675 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676 struct nfs4_sequence_args *seq_args;
677 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678};
679
680static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
681{
682 struct nfs41_call_sync_data *data = calldata;
683
Trond Myklebust035168a2010-06-16 09:52:26 -0400684 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
685
686 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500687 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 return;
689 rpc_call_start(task);
690}
691
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800692static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
693{
694 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
695 nfs41_call_sync_prepare(task, calldata);
696}
697
Andy Adamson69ab40c2009-04-01 09:22:19 -0400698static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
699{
700 struct nfs41_call_sync_data *data = calldata;
701
Trond Myklebust14516c32010-07-31 14:29:06 -0400702 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400703}
704
Trond Myklebust17280172012-03-11 13:11:00 -0400705static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400707 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708};
709
Trond Myklebust17280172012-03-11 13:11:00 -0400710static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800711 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
712 .rpc_call_done = nfs41_call_sync_done,
713};
714
Bryan Schumaker7c513052011-03-24 17:12:24 +0000715static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
716 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717 struct rpc_message *msg,
718 struct nfs4_sequence_args *args,
719 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800720 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721{
722 int ret;
723 struct rpc_task *task;
724 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400725 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726 .seq_args = args,
727 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 };
729 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000730 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731 .rpc_message = msg,
732 .callback_ops = &nfs41_call_sync_ops,
733 .callback_data = &data
734 };
735
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800736 if (privileged)
737 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400738 task = rpc_run_task(&task_setup);
739 if (IS_ERR(task))
740 ret = PTR_ERR(task);
741 else {
742 ret = task->tk_status;
743 rpc_put_task(task);
744 }
745 return ret;
746}
747
Bryan Schumaker7c513052011-03-24 17:12:24 +0000748int _nfs4_call_sync_session(struct rpc_clnt *clnt,
749 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400750 struct rpc_message *msg,
751 struct nfs4_sequence_args *args,
752 struct nfs4_sequence_res *res,
753 int cache_reply)
754{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500755 nfs41_init_sequence(args, res, cache_reply);
756 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400757}
758
Trond Myklebustdf896452010-06-16 09:52:26 -0400759#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500760static inline
761void nfs41_init_sequence(struct nfs4_sequence_args *args,
762 struct nfs4_sequence_res *res, int cache_reply)
763{
764}
765
Trond Myklebust14516c32010-07-31 14:29:06 -0400766static int nfs4_sequence_done(struct rpc_task *task,
767 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400768{
Trond Myklebust14516c32010-07-31 14:29:06 -0400769 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400770}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400771#endif /* CONFIG_NFS_V4_1 */
772
Bryan Schumaker7c513052011-03-24 17:12:24 +0000773int _nfs4_call_sync(struct rpc_clnt *clnt,
774 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400775 struct rpc_message *msg,
776 struct nfs4_sequence_args *args,
777 struct nfs4_sequence_res *res,
778 int cache_reply)
779{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500780 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400782}
783
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000784static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000785int nfs4_call_sync(struct rpc_clnt *clnt,
786 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000787 struct rpc_message *msg,
788 struct nfs4_sequence_args *args,
789 struct nfs4_sequence_res *res,
790 int cache_reply)
791{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000792 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
793 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000794}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400795
Trond Myklebust38478b22006-05-25 01:40:57 -0400796static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Trond Myklebust38478b22006-05-25 01:40:57 -0400798 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Trond Myklebust38478b22006-05-25 01:40:57 -0400800 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400801 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700802 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400803 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700804 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400809 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810 struct nfs_openargs o_arg;
811 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100812 struct nfs_open_confirmargs c_arg;
813 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 struct nfs4_string owner_name;
815 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400820 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100822 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400823 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100824 int rpc_status;
825 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826};
827
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400828
829static void nfs4_init_opendata_res(struct nfs4_opendata *p)
830{
831 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400832 p->o_res.seqid = p->o_arg.seqid;
833 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400834 p->o_res.server = p->o_arg.server;
835 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500836 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400837}
838
Al Viro82a2c1b2011-06-22 18:30:55 -0400839static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400841 const struct iattr *attrs,
842 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843{
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct inode *dir = parent->d_inode;
846 struct nfs_server *server = NFS_SERVER(dir);
847 struct nfs4_opendata *p;
848
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400849 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 if (p == NULL)
851 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400852 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 if (p->o_arg.seqid == NULL)
854 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400855 nfs_sb_active(dentry->d_sb);
856 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 p->dir = parent;
858 p->owner = sp;
859 atomic_inc(&sp->so_count);
860 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861 p->o_arg.open_flags = flags;
862 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400863 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400864 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
865 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400866 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 p->o_arg.server = server;
868 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400869 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500871 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500872 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400873
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100874 p->o_arg.u.attrs = &p->attrs;
875 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500876
877 verf[0] = jiffies;
878 verf[1] = current->pid;
879 memcpy(p->o_arg.u.verifier.data, verf,
880 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100882 p->c_arg.fh = &p->o_res.fh;
883 p->c_arg.stateid = &p->o_res.stateid;
884 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400885 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400886 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100887 return p;
888err_free:
889 kfree(p);
890err:
891 dput(parent);
892 return NULL;
893}
894
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400895static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100896{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400897 struct nfs4_opendata *p = container_of(kref,
898 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400899 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400900
901 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 if (p->state != NULL)
903 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400904 nfs4_put_state_owner(p->owner);
905 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400906 dput(p->dentry);
907 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500908 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400909 kfree(p);
910}
911
912static void nfs4_opendata_put(struct nfs4_opendata *p)
913{
914 if (p != NULL)
915 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100916}
917
Trond Myklebust06f814a2006-01-03 09:55:07 +0100918static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
919{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920 int ret;
921
Trond Myklebust06f814a2006-01-03 09:55:07 +0100922 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100923 return ret;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400927{
928 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929
Trond Myklebust536e43d2012-01-17 22:04:26 -0500930 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 goto out;
932 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400933 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500934 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
935 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400936 break;
937 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500938 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
939 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 break;
941 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500942 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
943 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400946 return ret;
947}
948
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500951 if (delegation == NULL)
952 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500955 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400956 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500957 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 return 1;
959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100962{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100964 case FMODE_WRITE:
965 state->n_wronly++;
966 break;
967 case FMODE_READ:
968 state->n_rdonly++;
969 break;
970 case FMODE_READ|FMODE_WRITE:
971 state->n_rdwr++;
972 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100974}
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400977{
978 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500979 nfs4_stateid_copy(&state->stateid, stateid);
980 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400982 case FMODE_READ:
983 set_bit(NFS_O_RDONLY_STATE, &state->flags);
984 break;
985 case FMODE_WRITE:
986 set_bit(NFS_O_WRONLY_STATE, &state->flags);
987 break;
988 case FMODE_READ|FMODE_WRITE:
989 set_bit(NFS_O_RDWR_STATE, &state->flags);
990 }
991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400994{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400995 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400997 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400998}
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static 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 -07001001{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001002 /*
1003 * Protect the call to nfs4_state_set_mode_locked and
1004 * serialise the stateid update
1005 */
1006 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001007 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001008 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009 set_bit(NFS_DELEGATED_STATE, &state->flags);
1010 }
1011 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 write_sequnlock(&state->seqlock);
1014 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001016 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019static 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 -05001020{
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *deleg_cur;
1023 int ret = 0;
1024
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001026
1027 rcu_read_lock();
1028 deleg_cur = rcu_dereference(nfsi->delegation);
1029 if (deleg_cur == NULL)
1030 goto no_delegation;
1031
1032 spin_lock(&deleg_cur->lock);
1033 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001035 goto no_delegation_unlock;
1036
1037 if (delegation == NULL)
1038 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001039 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001040 goto no_delegation_unlock;
1041
Trond Myklebustb7391f42008-12-23 15:21:52 -05001042 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001044 ret = 1;
1045no_delegation_unlock:
1046 spin_unlock(&deleg_cur->lock);
1047no_delegation:
1048 rcu_read_unlock();
1049
1050 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001052 ret = 1;
1053 }
1054
1055 return ret;
1056}
1057
1058
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060{
1061 struct nfs_delegation *delegation;
1062
1063 rcu_read_lock();
1064 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066 rcu_read_unlock();
1067 return;
1068 }
1069 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001070 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071}
1072
Trond Myklebust6ee41262007-07-08 14:11:36 -04001073static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074{
1075 struct nfs4_state *state = opendata->state;
1076 struct nfs_inode *nfsi = NFS_I(state->inode);
1077 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001078 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001079 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 nfs4_stateid stateid;
1081 int ret = -EAGAIN;
1082
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (can_open_cached(state, fmode, open_mode)) {
1087 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001089 goto out_return_state;
1090 }
1091 spin_unlock(&state->owner->so_lock);
1092 }
Trond Myklebust34310432008-12-23 15:21:38 -05001093 rcu_read_lock();
1094 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001095 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001096 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001098 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001100 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001102 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 if (ret != 0)
1104 goto out;
1105 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001106
1107 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001109 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111out:
1112 return ERR_PTR(ret);
1113out_return_state:
1114 atomic_inc(&state->count);
1115 return state;
1116}
1117
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001118static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1119{
1120 struct inode *inode;
1121 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001122 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001123 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124
Trond Myklebustaac00a82007-07-05 19:02:21 -04001125 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001126 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 goto out;
1128 }
1129
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001133 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001135 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001136 goto err;
1137 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138 state = nfs4_get_open_state(inode, data->owner);
1139 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001140 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001141 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001142 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001143 int delegation_flags = 0;
1144
Trond Myklebust003707c2007-07-05 18:07:55 -04001145 rcu_read_lock();
1146 delegation = rcu_dereference(NFS_I(inode)->delegation);
1147 if (delegation)
1148 delegation_flags = delegation->flags;
1149 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001150 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1151 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1152 "returning a delegation for "
1153 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001154 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001155 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001156 nfs_inode_set_delegation(state->inode,
1157 data->owner->so_cred,
1158 &data->o_res);
1159 else
1160 nfs_inode_reclaim_delegation(state->inode,
1161 data->owner->so_cred,
1162 &data->o_res);
1163 }
Trond Myklebust34310432008-12-23 15:21:38 -05001164
1165 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001166 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001167 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001168out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001169 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001170err_put_inode:
1171 iput(inode);
1172err:
1173 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001174}
1175
Trond Myklebust864472e2006-01-03 09:55:15 +01001176static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1177{
1178 struct nfs_inode *nfsi = NFS_I(state->inode);
1179 struct nfs_open_context *ctx;
1180
1181 spin_lock(&state->inode->i_lock);
1182 list_for_each_entry(ctx, &nfsi->open_files, list) {
1183 if (ctx->state != state)
1184 continue;
1185 get_nfs_open_context(ctx);
1186 spin_unlock(&state->inode->i_lock);
1187 return ctx;
1188 }
1189 spin_unlock(&state->inode->i_lock);
1190 return ERR_PTR(-ENOENT);
1191}
1192
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001193static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1194{
1195 struct nfs4_opendata *opendata;
1196
Al Viro3d4ff432011-06-22 18:40:12 -04001197 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001198 if (opendata == NULL)
1199 return ERR_PTR(-ENOMEM);
1200 opendata->state = state;
1201 atomic_inc(&state->count);
1202 return opendata;
1203}
1204
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001205static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001206{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 int ret;
1209
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 opendata->o_arg.open_flags = 0;
1211 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1213 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1214 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001215 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 if (ret != 0)
1217 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219 if (IS_ERR(newstate))
1220 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001221 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 return 0;
1224}
1225
1226static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1227{
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 int ret;
1230
1231 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 smp_rmb();
1234 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001235 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 if (ret != 0)
1238 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001239 if (newstate != state)
1240 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 }
1242 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001243 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 if (ret != 0)
1246 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 if (newstate != state)
1248 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 }
1250 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001251 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 if (ret != 0)
1254 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 if (newstate != state)
1256 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001258 /*
1259 * We may have performed cached opens for all three recoveries.
1260 * Check if we need to update the current stateid.
1261 */
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001263 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001264 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001266 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001267 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 return 0;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
1273 * OPEN_RECLAIM:
1274 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001278 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001280 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int status;
1282
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001283 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1284 if (IS_ERR(opendata))
1285 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1287 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001290 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001291 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001292 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 opendata->o_arg.u.delegation_type = delegation_type;
1294 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return status;
1297}
1298
Trond Myklebust539cd032007-06-05 11:46:42 -04001299static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs_server *server = NFS_SERVER(state->inode);
1302 struct nfs4_exception exception = { };
1303 int err;
1304 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001305 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001306 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001307 break;
1308 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 } while (exception.retry);
1310 return err;
1311}
1312
Trond Myklebust864472e2006-01-03 09:55:15 +01001313static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1314{
1315 struct nfs_open_context *ctx;
1316 int ret;
1317
1318 ctx = nfs4_state_find_open_context(state);
1319 if (IS_ERR(ctx))
1320 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 put_nfs_open_context(ctx);
1323 return ret;
1324}
1325
Trond Myklebust13437e12007-07-06 15:10:43 -04001326static 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 -07001327{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001328 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001331 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1332 if (IS_ERR(opendata))
1333 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001334 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001336 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001337 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Trond Myklebust13437e12007-07-06 15:10:43 -04001341int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001344 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int err;
1346 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001347 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 switch (err) {
1349 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350 case -ENOENT:
1351 case -ESTALE:
1352 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001353 case -NFS4ERR_BADSESSION:
1354 case -NFS4ERR_BADSLOT:
1355 case -NFS4ERR_BAD_HIGH_SLOT:
1356 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1357 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001358 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 case -NFS4ERR_STALE_CLIENTID:
1361 case -NFS4ERR_STALE_STATEID:
1362 case -NFS4ERR_EXPIRED:
1363 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001364 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001365 goto out;
1366 case -ERESTARTSYS:
1367 /*
1368 * The show must go on: exit, but mark the
1369 * stateid as needing recovery.
1370 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001371 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001372 case -NFS4ERR_ADMIN_REVOKED:
1373 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001374 nfs_inode_find_state_and_recover(state->inode,
1375 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001376 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001377 case -EKEYEXPIRED:
1378 /*
1379 * User RPCSEC_GSS context has expired.
1380 * We cannot recover this stateid now, so
1381 * skip it and allow recovery thread to
1382 * proceed.
1383 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001384 case -ENOMEM:
1385 err = 0;
1386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 err = nfs4_handle_exception(server, err, &exception);
1389 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001390out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return err;
1392}
1393
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1395{
1396 struct nfs4_opendata *data = calldata;
1397
1398 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001399 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001400 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001401 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001402 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001403 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001404 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405}
1406
1407static void nfs4_open_confirm_release(void *calldata)
1408{
1409 struct nfs4_opendata *data = calldata;
1410 struct nfs4_state *state = NULL;
1411
1412 /* If this request hasn't been cancelled, do nothing */
1413 if (data->cancelled == 0)
1414 goto out_free;
1415 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001416 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001419 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001420 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001421out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001422 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423}
1424
1425static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426 .rpc_call_done = nfs4_open_confirm_done,
1427 .rpc_release = nfs4_open_confirm_release,
1428};
1429
1430/*
1431 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1432 */
1433static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1434{
1435 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1436 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001437 struct rpc_message msg = {
1438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1439 .rpc_argp = &data->c_arg,
1440 .rpc_resp = &data->c_res,
1441 .rpc_cred = data->owner->so_cred,
1442 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001443 struct rpc_task_setup task_setup_data = {
1444 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001445 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001446 .callback_ops = &nfs4_open_confirm_ops,
1447 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001448 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001449 .flags = RPC_TASK_ASYNC,
1450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int status;
1452
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001453 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001454 data->rpc_done = 0;
1455 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001457 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001458 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001459 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001460 status = nfs4_wait_for_completion_rpc_task(task);
1461 if (status != 0) {
1462 data->cancelled = 1;
1463 smp_wmb();
1464 } else
1465 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001466 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return status;
1468}
1469
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 struct nfs4_opendata *data = calldata;
1473 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001474
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1476 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001477 /*
1478 * Check if we still need to send an OPEN call, or if we can use
1479 * a delegation instead.
1480 */
1481 if (data->state != NULL) {
1482 struct nfs_delegation *delegation;
1483
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001484 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001485 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001486 rcu_read_lock();
1487 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001488 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1489 can_open_delegated(delegation, data->o_arg.fmode))
1490 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001491 rcu_read_unlock();
1492 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001493 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001494 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001495 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001496 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001497 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1498 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001499 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001500 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001501 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001502 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001503 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001504 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001505 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001506unlock_no_action:
1507 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001508out_no_action:
1509 task->tk_action = NULL;
1510
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001513static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1514{
1515 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1516 nfs4_open_prepare(task, calldata);
1517}
1518
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519static void nfs4_open_done(struct rpc_task *task, void *calldata)
1520{
1521 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001524
Trond Myklebust14516c32010-07-31 14:29:06 -04001525 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1526 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001527
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 if (task->tk_status == 0) {
1529 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 case S_IFREG:
1531 break;
1532 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534 break;
1535 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001537 break;
1538 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001540 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001541 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001542 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1543 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001544 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001545 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001547
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548static void nfs4_open_release(void *calldata)
1549{
1550 struct nfs4_opendata *data = calldata;
1551 struct nfs4_state *state = NULL;
1552
1553 /* If this request hasn't been cancelled, do nothing */
1554 if (data->cancelled == 0)
1555 goto out_free;
1556 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001557 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 goto out_free;
1559 /* In case we need an open_confirm, no cleanup! */
1560 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1561 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001563 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001564 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001565out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001566 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567}
1568
1569static const struct rpc_call_ops nfs4_open_ops = {
1570 .rpc_call_prepare = nfs4_open_prepare,
1571 .rpc_call_done = nfs4_open_done,
1572 .rpc_release = nfs4_open_release,
1573};
1574
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001575static const struct rpc_call_ops nfs4_recover_open_ops = {
1576 .rpc_call_prepare = nfs4_recover_open_prepare,
1577 .rpc_call_done = nfs4_open_done,
1578 .rpc_release = nfs4_open_release,
1579};
1580
1581static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001582{
1583 struct inode *dir = data->dir->d_inode;
1584 struct nfs_server *server = NFS_SERVER(dir);
1585 struct nfs_openargs *o_arg = &data->o_arg;
1586 struct nfs_openres *o_res = &data->o_res;
1587 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001588 struct rpc_message msg = {
1589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1590 .rpc_argp = o_arg,
1591 .rpc_resp = o_res,
1592 .rpc_cred = data->owner->so_cred,
1593 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001594 struct rpc_task_setup task_setup_data = {
1595 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001596 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001597 .callback_ops = &nfs4_open_ops,
1598 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001599 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001600 .flags = RPC_TASK_ASYNC,
1601 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001602 int status;
1603
Trond Myklebust9d12b212012-01-17 22:04:25 -05001604 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001605 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001606 data->rpc_done = 0;
1607 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001608 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001609 if (isrecover)
1610 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001611 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001612 if (IS_ERR(task))
1613 return PTR_ERR(task);
1614 status = nfs4_wait_for_completion_rpc_task(task);
1615 if (status != 0) {
1616 data->cancelled = 1;
1617 smp_wmb();
1618 } else
1619 status = data->rpc_status;
1620 rpc_put_task(task);
1621
1622 return status;
1623}
1624
1625static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1626{
1627 struct inode *dir = data->dir->d_inode;
1628 struct nfs_openres *o_res = &data->o_res;
1629 int status;
1630
1631 status = nfs4_run_open_task(data, 1);
1632 if (status != 0 || !data->rpc_done)
1633 return status;
1634
Trond Myklebust6926afd2012-01-07 13:22:46 -05001635 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1636
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001637 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1638 status = _nfs4_proc_open_confirm(data);
1639 if (status != 0)
1640 return status;
1641 }
1642
1643 return status;
1644}
1645
1646/*
1647 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1648 */
1649static int _nfs4_proc_open(struct nfs4_opendata *data)
1650{
1651 struct inode *dir = data->dir->d_inode;
1652 struct nfs_server *server = NFS_SERVER(dir);
1653 struct nfs_openargs *o_arg = &data->o_arg;
1654 struct nfs_openres *o_res = &data->o_res;
1655 int status;
1656
1657 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001658 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001660 if (status != 0) {
1661 if (status == -NFS4ERR_BADNAME &&
1662 !(o_arg->open_flags & O_CREAT))
1663 return -ENOENT;
1664 return status;
1665 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666
Trond Myklebust6926afd2012-01-07 13:22:46 -05001667 nfs_fattr_map_and_free_names(server, &data->f_attr);
1668
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001669 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001670 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001671 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1672 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001674 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001679 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Andy Adamsond83217c2011-03-01 01:34:17 +00001683static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001684{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001685 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001686 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001687
Trond Myklebusta78cb572009-08-09 15:06:19 -04001688 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001689 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001690 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001691 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001692 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1693 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001694 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001695 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001696 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001697 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001698 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001699}
1700
Andy Adamsond83217c2011-03-01 01:34:17 +00001701static int nfs4_recover_expired_lease(struct nfs_server *server)
1702{
1703 return nfs4_client_recover_expired_lease(server->nfs_client);
1704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * OPEN_EXPIRED:
1708 * reclaim state on the server after a network partition.
1709 * Assumes caller holds the appropriate lock
1710 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001711static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001713 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001716 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1717 if (IS_ERR(opendata))
1718 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001720 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001721 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001722 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001726static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727{
Trond Myklebust539cd032007-06-05 11:46:42 -04001728 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729 struct nfs4_exception exception = { };
1730 int err;
1731
1732 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001733 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001734 switch (err) {
1735 default:
1736 goto out;
1737 case -NFS4ERR_GRACE:
1738 case -NFS4ERR_DELAY:
1739 nfs4_handle_exception(server, err, &exception);
1740 err = 0;
1741 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001742 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001743out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001744 return err;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001750 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 ctx = nfs4_state_find_open_context(state);
1753 if (IS_ERR(ctx))
1754 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001755 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001756 put_nfs_open_context(ctx);
1757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001761static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001764 nfs4_stateid *stateid = &state->stateid;
1765 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001766
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001767 /* If a state reset has been done, test_stateid is unneeded */
1768 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1769 return;
1770
1771 status = nfs41_test_stateid(server, stateid);
1772 if (status != NFS_OK) {
1773 /* Free the stateid unless the server explicitly
1774 * informs us the stateid is unrecognized. */
1775 if (status != -NFS4ERR_BAD_STATEID)
1776 nfs41_free_stateid(server, stateid);
1777
1778 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1779 }
1780}
1781
1782/**
1783 * nfs41_check_open_stateid - possibly free an open stateid
1784 *
1785 * @state: NFSv4 state for an inode
1786 *
1787 * Returns NFS_OK if recovery for this stateid is now finished.
1788 * Otherwise a negative NFS4ERR value is returned.
1789 */
1790static int nfs41_check_open_stateid(struct nfs4_state *state)
1791{
1792 struct nfs_server *server = NFS_SERVER(state->inode);
1793 nfs4_stateid *stateid = &state->stateid;
1794 int status;
1795
1796 /* If a state reset has been done, test_stateid is unneeded */
1797 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1798 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1799 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1800 return -NFS4ERR_BAD_STATEID;
1801
1802 status = nfs41_test_stateid(server, stateid);
1803 if (status != NFS_OK) {
1804 /* Free the stateid unless the server explicitly
1805 * informs us the stateid is unrecognized. */
1806 if (status != -NFS4ERR_BAD_STATEID)
1807 nfs41_free_stateid(server, stateid);
1808
1809 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1810 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1811 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001812 }
1813 return status;
1814}
1815
1816static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1817{
Chuck Levereb64cf92012-07-11 16:30:05 -04001818 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001819
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001820 nfs41_clear_delegation_stateid(state);
1821 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001822 if (status != NFS_OK)
1823 status = nfs4_open_expired(sp, state);
1824 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001825}
1826#endif
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001829 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1830 * fields corresponding to attributes that were used to store the verifier.
1831 * Make sure we clobber those fields in the later setattr call
1832 */
1833static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1834{
1835 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1836 !(sattr->ia_valid & ATTR_ATIME_SET))
1837 sattr->ia_valid |= ATTR_ATIME;
1838
1839 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1840 !(sattr->ia_valid & ATTR_MTIME_SET))
1841 sattr->ia_valid |= ATTR_MTIME;
1842}
1843
1844/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
Andy Adamson82be4172012-05-23 05:02:35 -04001847static int _nfs4_do_open(struct inode *dir,
1848 struct dentry *dentry,
1849 fmode_t fmode,
1850 int flags,
1851 struct iattr *sattr,
1852 struct rpc_cred *cred,
1853 struct nfs4_state **res,
1854 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 struct nfs4_state_owner *sp;
1857 struct nfs4_state *state = NULL;
1858 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001859 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001860 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001864 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1865 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1867 goto out_err;
1868 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001869 status = nfs4_recover_expired_lease(server);
1870 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001871 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001872 if (dentry->d_inode != NULL)
1873 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001874 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001875 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001876 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001877 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Andy Adamson82be4172012-05-23 05:02:35 -04001879 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1880 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1881 if (!opendata->f_attr.mdsthreshold)
1882 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001883 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001884 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001885 if (dentry->d_inode != NULL)
1886 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001887
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001888 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001890 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001892 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001893 status = PTR_ERR(state);
1894 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001895 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001896 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001897 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001898
1899 if (opendata->o_arg.open_flags & O_EXCL) {
1900 nfs4_exclusive_attrset(opendata, sattr);
1901
1902 nfs_fattr_init(opendata->o_res.f_attr);
1903 status = nfs4_do_setattr(state->inode, cred,
1904 opendata->o_res.f_attr, sattr,
1905 state);
1906 if (status == 0)
1907 nfs_setattr_update_inode(state->inode, sattr);
1908 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1909 }
Andy Adamson82be4172012-05-23 05:02:35 -04001910
1911 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1912 *ctx_th = opendata->f_attr.mdsthreshold;
1913 else
1914 kfree(opendata->f_attr.mdsthreshold);
1915 opendata->f_attr.mdsthreshold = NULL;
1916
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001917 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 *res = state;
1920 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001921err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001922 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001923 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001924err_put_state_owner:
1925 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *res = NULL;
1928 return status;
1929}
1930
1931
Andy Adamson82be4172012-05-23 05:02:35 -04001932static struct nfs4_state *nfs4_do_open(struct inode *dir,
1933 struct dentry *dentry,
1934 fmode_t fmode,
1935 int flags,
1936 struct iattr *sattr,
1937 struct rpc_cred *cred,
1938 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct nfs4_exception exception = { };
1941 struct nfs4_state *res;
1942 int status;
1943
Trond Myklebust2d0dbc62012-06-08 10:58:09 -04001944 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001946 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1947 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (status == 0)
1949 break;
1950 /* NOTE: BAD_SEQID means the server and client disagree about the
1951 * book-keeping w.r.t. state-changing operations
1952 * (OPEN/CLOSE/LOCK/LOCKU...)
1953 * It is actually a sign of a bug on the client or on the server.
1954 *
1955 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001956 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * have unhashed the old state_owner for us, and that we can
1958 * therefore safely retry using a new one. We should still warn
1959 * the user though...
1960 */
1961 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001962 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001963 " returned a bad sequence-id error!\n",
1964 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 exception.retry = 1;
1966 continue;
1967 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001968 /*
1969 * BAD_STATEID on OPEN means that the server cancelled our
1970 * state before it received the OPEN_CONFIRM.
1971 * Recover by retrying the request as per the discussion
1972 * on Page 181 of RFC3530.
1973 */
1974 if (status == -NFS4ERR_BAD_STATEID) {
1975 exception.retry = 1;
1976 continue;
1977 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001978 if (status == -EAGAIN) {
1979 /* We must have found a delegation */
1980 exception.retry = 1;
1981 continue;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1984 status, &exception));
1985 } while (exception.retry);
1986 return res;
1987}
1988
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001989static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1990 struct nfs_fattr *fattr, struct iattr *sattr,
1991 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001993 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001995 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 .iap = sattr,
1997 .server = server,
1998 .bitmask = server->attr_bitmask,
1999 };
2000 struct nfs_setattrres res = {
2001 .fattr = fattr,
2002 .server = server,
2003 };
2004 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2006 .rpc_argp = &arg,
2007 .rpc_resp = &res,
2008 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002010 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002011 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Trond Myklebust0e574af2005-10-27 22:12:38 -04002013 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Trond Myklebust4fc87962012-03-08 17:42:01 -05002015 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002016 struct nfs_lockowner lockowner = {
2017 .l_owner = current->files,
2018 .l_pid = current->tgid,
2019 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002020 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002021 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002022 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2023 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002024 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002025 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002026 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Bryan Schumaker7c513052011-03-24 17:12:24 +00002028 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002029 if (status == 0 && state != NULL)
2030 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002031 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002034static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2035 struct nfs_fattr *fattr, struct iattr *sattr,
2036 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002038 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002039 struct nfs4_exception exception = {
2040 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002041 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002042 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 int err;
2044 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002045 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2046 switch (err) {
2047 case -NFS4ERR_OPENMODE:
2048 if (state && !(state->state & FMODE_WRITE)) {
2049 err = -EBADF;
2050 if (sattr->ia_valid & ATTR_OPEN)
2051 err = -EACCES;
2052 goto out;
2053 }
2054 }
2055 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002057out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return err;
2059}
2060
2061struct nfs4_closedata {
2062 struct inode *inode;
2063 struct nfs4_state *state;
2064 struct nfs_closeargs arg;
2065 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002066 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002067 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002068 bool roc;
2069 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070};
2071
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002072static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002073{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002074 struct nfs4_closedata *calldata = data;
2075 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002076 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002077
Fred Isamanf7e89172011-01-06 11:36:32 +00002078 if (calldata->roc)
2079 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002080 nfs4_put_open_state(calldata->state);
2081 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002082 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002083 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002084 kfree(calldata);
2085}
2086
Trond Myklebust88069f72009-12-08 08:33:16 -05002087static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2088 fmode_t fmode)
2089{
2090 spin_lock(&state->owner->so_lock);
2091 if (!(fmode & FMODE_READ))
2092 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2093 if (!(fmode & FMODE_WRITE))
2094 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2095 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2096 spin_unlock(&state->owner->so_lock);
2097}
2098
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002099static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002101 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct nfs_server *server = NFS_SERVER(calldata->inode);
2104
Chuck Levera3ca5652012-03-01 17:00:40 -05002105 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002106 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2107 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 /* hmm. we are done with the inode, and in the process of freeing
2109 * the state_owner. we keep this around to process errors
2110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 switch (task->tk_status) {
2112 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002113 if (calldata->roc)
2114 pnfs_roc_set_barrier(state->inode,
2115 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002116 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002117 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002118 nfs4_close_clear_stateid_flags(state,
2119 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 break;
2121 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002122 case -NFS4ERR_OLD_STATEID:
2123 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002125 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002128 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2129 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002131 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002132 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002133 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002136static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002138 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002139 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002140 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002141 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002142
Chuck Levera3ca5652012-03-01 17:00:40 -05002143 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002144 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002145 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002146
Trond Myklebust88069f72009-12-08 08:33:16 -05002147 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2148 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002149 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002150 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002151 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002152 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002153 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2154 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2155 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002156 }
2157 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002158 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2159 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2160 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002161 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002162 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002163 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002164
2165 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002166 /* Note: exit _without_ calling nfs4_close_done */
2167 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002168 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002169 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002170
Fred Isamanf7e89172011-01-06 11:36:32 +00002171 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002172 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002173 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002174 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002175 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002176 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002177
Trond Myklebust516a6af2005-10-27 22:12:41 -04002178 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002179 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002180 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002181 &calldata->arg.seq_args,
2182 &calldata->res.seq_res,
2183 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002184 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002186out:
2187 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002191 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002192 .rpc_call_done = nfs4_close_done,
2193 .rpc_release = nfs4_free_closedata,
2194};
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196/*
2197 * It is possible for data to be read/written from a mem-mapped file
2198 * after the sys_close call (which hits the vfs layer as a flush).
2199 * This means that we can't safely call nfsv4 close on a file until
2200 * the inode is cleared. This in turn means that we are not good
2201 * NFSv4 citizens - we do not indicate to the server to update the file's
2202 * share state even when we are done with one of the three share
2203 * stateid's in the inode.
2204 *
2205 * NOTE: Caller must be holding the sp->so_owner semaphore!
2206 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002207int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002209 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002211 struct nfs4_state_owner *sp = state->owner;
2212 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002213 struct rpc_message msg = {
2214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2215 .rpc_cred = state->owner->so_cred,
2216 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002217 struct rpc_task_setup task_setup_data = {
2218 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002219 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002221 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002222 .flags = RPC_TASK_ASYNC,
2223 };
Trond Myklebust95121352005-10-18 14:20:12 -07002224 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002226 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002228 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002229 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002230 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002232 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002233 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002235 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002236 if (calldata->arg.seqid == NULL)
2237 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002238 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002239 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002240 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002241 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002242 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002243 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002244 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002245
Trond Myklebust1174dd12010-12-21 10:52:24 -05002246 msg.rpc_argp = &calldata->arg;
2247 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002248 task_setup_data.callback_data = calldata;
2249 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002250 if (IS_ERR(task))
2251 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002252 status = 0;
2253 if (wait)
2254 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002255 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002256 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002257out_free_calldata:
2258 kfree(calldata);
2259out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002260 nfs4_put_open_state(state);
2261 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002262 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Trond Myklebust2b484292010-09-17 10:56:51 -04002265static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002266nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 struct nfs4_state *state;
2269
Trond Myklebust565277f2007-10-15 18:17:53 -04002270 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002271 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2272 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002273 if (IS_ERR(state))
2274 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002275 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002276 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Trond Myklebust1185a552009-12-03 15:54:02 -05002279static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002280{
2281 if (ctx->state == NULL)
2282 return;
2283 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002284 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002285 else
Al Viro643168c2011-06-22 18:20:23 -04002286 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2290{
Benny Halevy43652ad2009-04-01 09:21:54 -04002291 struct nfs4_server_caps_arg args = {
2292 .fhandle = fhandle,
2293 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 struct nfs4_server_caps_res res = {};
2295 struct rpc_message msg = {
2296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002297 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 .rpc_resp = &res,
2299 };
2300 int status;
2301
Bryan Schumaker7c513052011-03-24 17:12:24 +00002302 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (status == 0) {
2304 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002305 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2306 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2307 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2308 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2309 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2311 server->caps |= NFS_CAP_ACLS;
2312 if (res.has_links != 0)
2313 server->caps |= NFS_CAP_HARDLINKS;
2314 if (res.has_symlinks != 0)
2315 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002316 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2317 server->caps |= NFS_CAP_FILEID;
2318 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2319 server->caps |= NFS_CAP_MODE;
2320 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2321 server->caps |= NFS_CAP_NLINK;
2322 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2323 server->caps |= NFS_CAP_OWNER;
2324 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2325 server->caps |= NFS_CAP_OWNER_GROUP;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2327 server->caps |= NFS_CAP_ATIME;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2329 server->caps |= NFS_CAP_CTIME;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2331 server->caps |= NFS_CAP_MTIME;
2332
Trond Myklebusta65318b2009-03-11 14:10:28 -04002333 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2334 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2335 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002337 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return status;
2341}
2342
Trond Myklebust55a97592006-06-09 09:34:19 -04002343int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 struct nfs4_exception exception = { };
2346 int err;
2347 do {
2348 err = nfs4_handle_exception(server,
2349 _nfs4_server_capabilities(server, fhandle),
2350 &exception);
2351 } while (exception.retry);
2352 return err;
2353}
2354
2355static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2356 struct nfs_fsinfo *info)
2357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 struct nfs4_lookup_root_arg args = {
2359 .bitmask = nfs4_fattr_bitmap,
2360 };
2361 struct nfs4_lookup_res res = {
2362 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002363 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 .fh = fhandle,
2365 };
2366 struct rpc_message msg = {
2367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2368 .rpc_argp = &args,
2369 .rpc_resp = &res,
2370 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002371
Trond Myklebust0e574af2005-10-27 22:12:38 -04002372 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002373 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
2376static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2377 struct nfs_fsinfo *info)
2378{
2379 struct nfs4_exception exception = { };
2380 int err;
2381 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002382 err = _nfs4_lookup_root(server, fhandle, info);
2383 switch (err) {
2384 case 0:
2385 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002386 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002387 default:
2388 err = nfs4_handle_exception(server, err, &exception);
2389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002391out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return err;
2393}
2394
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002395static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2396 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2397{
2398 struct rpc_auth *auth;
2399 int ret;
2400
2401 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002402 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002403 ret = -EIO;
2404 goto out;
2405 }
2406 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002407out:
2408 return ret;
2409}
2410
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002411static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002412 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002414 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002415 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002417 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2418 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002419
2420 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002421 /* AUTH_UNIX is the default flavor if none was specified,
2422 * thus has already been tried. */
2423 if (flav_array[i] == RPC_AUTH_UNIX)
2424 continue;
2425
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002426 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002427 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002428 continue;
2429 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002430 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002431 /*
2432 * -EACCESS could mean that the user doesn't have correct permissions
2433 * to access the mount. It could also mean that we tried to mount
2434 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2435 * existing mount programs don't handle -EACCES very well so it should
2436 * be mapped to -EPERM instead.
2437 */
2438 if (status == -EACCES)
2439 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002440 return status;
2441}
2442
2443/*
2444 * get the file handle for the "/" directory on the server
2445 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002446int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2447 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002448{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002449 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002450 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002451 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2452 /*
2453 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2454 * by nfs4_map_errors() as this function exits.
2455 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002456 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (status == 0)
2458 status = nfs4_server_capabilities(server, fhandle);
2459 if (status == 0)
2460 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002461 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Bryan Schumakerbae36242012-05-10 15:07:31 -04002464static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2465 struct nfs_fsinfo *info)
2466{
2467 int error;
2468 struct nfs_fattr *fattr = info->fattr;
2469
2470 error = nfs4_server_capabilities(server, mntfh);
2471 if (error < 0) {
2472 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2473 return error;
2474 }
2475
2476 error = nfs4_proc_getattr(server, mntfh, fattr);
2477 if (error < 0) {
2478 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2479 return error;
2480 }
2481
2482 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2483 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2484 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2485
2486 return error;
2487}
2488
Manoj Naik6b97fd32006-06-09 09:34:29 -04002489/*
2490 * Get locations and (maybe) other attributes of a referral.
2491 * Note that we'll actually follow the referral later when
2492 * we detect fsid mismatch in inode revalidation
2493 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002494static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2495 const struct qstr *name, struct nfs_fattr *fattr,
2496 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002497{
2498 int status = -ENOMEM;
2499 struct page *page = NULL;
2500 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002501
2502 page = alloc_page(GFP_KERNEL);
2503 if (page == NULL)
2504 goto out;
2505 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2506 if (locations == NULL)
2507 goto out;
2508
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002509 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002510 if (status != 0)
2511 goto out;
2512 /* Make sure server returned a different fsid for the referral */
2513 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002514 dprintk("%s: server did not return a different fsid for"
2515 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002516 status = -EIO;
2517 goto out;
2518 }
Andy Adamson533eb462011-06-13 18:25:56 -04002519 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2520 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002521
Andy Adamson533eb462011-06-13 18:25:56 -04002522 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002523 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002524 memset(fhandle, 0, sizeof(struct nfs_fh));
2525out:
2526 if (page)
2527 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002528 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002529 return status;
2530}
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2533{
2534 struct nfs4_getattr_arg args = {
2535 .fh = fhandle,
2536 .bitmask = server->attr_bitmask,
2537 };
2538 struct nfs4_getattr_res res = {
2539 .fattr = fattr,
2540 .server = server,
2541 };
2542 struct rpc_message msg = {
2543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2544 .rpc_argp = &args,
2545 .rpc_resp = &res,
2546 };
2547
Trond Myklebust0e574af2005-10-27 22:12:38 -04002548 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002549 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2553{
2554 struct nfs4_exception exception = { };
2555 int err;
2556 do {
2557 err = nfs4_handle_exception(server,
2558 _nfs4_proc_getattr(server, fhandle, fattr),
2559 &exception);
2560 } while (exception.retry);
2561 return err;
2562}
2563
2564/*
2565 * The file is not closed if it is opened due to the a request to change
2566 * the size of the file. The open call will not be needed once the
2567 * VFS layer lookup-intents are implemented.
2568 *
2569 * Close is called when the inode is destroyed.
2570 * If we haven't opened the file for O_WRONLY, we
2571 * need to in the size_change case to obtain a stateid.
2572 *
2573 * Got race?
2574 * Because OPEN is always done by name in nfsv4, it is
2575 * possible that we opened a different file by the same
2576 * name. We can recognize this race condition, but we
2577 * can't do anything about it besides returning an error.
2578 *
2579 * This will be fixed with VFS changes (lookup-intent).
2580 */
2581static int
2582nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2583 struct iattr *sattr)
2584{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002585 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002586 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002587 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 int status;
2589
Benny Halevy8a1636c2010-07-14 15:43:57 -04002590 if (pnfs_ld_layoutret_on_setattr(inode))
2591 pnfs_return_layout(inode);
2592
Trond Myklebust0e574af2005-10-27 22:12:38 -04002593 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Andy Adamson26699402012-05-30 16:12:24 -04002595 /* Deal with open(O_TRUNC) */
2596 if (sattr->ia_valid & ATTR_OPEN)
2597 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2598
2599 /* Optimization: if the end result is no change, don't RPC */
2600 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2601 return 0;
2602
Trond Myklebustd5308382005-11-04 15:33:38 -05002603 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002604 if (sattr->ia_valid & ATTR_FILE) {
2605 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002606
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002607 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002608 if (ctx) {
2609 cred = ctx->cred;
2610 state = ctx->state;
2611 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002612 }
2613
2614 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002615 if (status == 0)
2616 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return status;
2618}
2619
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002620static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2621 const struct qstr *name, struct nfs_fh *fhandle,
2622 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002623{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002624 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002625 int status;
2626 struct nfs4_lookup_arg args = {
2627 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002628 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002629 .name = name,
2630 };
2631 struct nfs4_lookup_res res = {
2632 .server = server,
2633 .fattr = fattr,
2634 .fh = fhandle,
2635 };
2636 struct rpc_message msg = {
2637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2638 .rpc_argp = &args,
2639 .rpc_resp = &res,
2640 };
2641
2642 nfs_fattr_init(fattr);
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002645 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 dprintk("NFS reply lookup: %d\n", status);
2647 return status;
2648}
2649
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002650static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002651{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002652 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002653 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002654 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2655 fattr->nlink = 2;
2656}
2657
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002658static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2659 struct qstr *name, struct nfs_fh *fhandle,
2660 struct nfs_fattr *fattr)
2661{
2662 struct nfs4_exception exception = { };
2663 struct rpc_clnt *client = *clnt;
2664 int err;
2665 do {
2666 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2667 switch (err) {
2668 case -NFS4ERR_BADNAME:
2669 err = -ENOENT;
2670 goto out;
2671 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002672 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002673 goto out;
2674 case -NFS4ERR_WRONGSEC:
2675 err = -EPERM;
2676 if (client != *clnt)
2677 goto out;
2678
2679 client = nfs4_create_sec_client(client, dir, name);
2680 if (IS_ERR(client))
2681 return PTR_ERR(client);
2682
2683 exception.retry = 1;
2684 break;
2685 default:
2686 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2687 }
2688 } while (exception.retry);
2689
2690out:
2691 if (err == 0)
2692 *clnt = client;
2693 else if (client != *clnt)
2694 rpc_shutdown_client(client);
2695
2696 return err;
2697}
2698
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002699static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002700 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002702 int status;
2703 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002704
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002705 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2706 if (client != NFS_CLIENT(dir)) {
2707 rpc_shutdown_client(client);
2708 nfs_fixup_secinfo_attributes(fattr);
2709 }
2710 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002713struct rpc_clnt *
2714nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2715 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2716{
2717 int status;
2718 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2719
2720 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2721 if (status < 0) {
2722 rpc_shutdown_client(client);
2723 return ERR_PTR(status);
2724 }
2725 return client;
2726}
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2729{
Trond Myklebust76b32992007-08-10 17:45:11 -04002730 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct nfs4_accessargs args = {
2732 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002733 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002735 struct nfs4_accessres res = {
2736 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002737 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 struct rpc_message msg = {
2739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2740 .rpc_argp = &args,
2741 .rpc_resp = &res,
2742 .rpc_cred = entry->cred,
2743 };
2744 int mode = entry->mask;
2745 int status;
2746
2747 /*
2748 * Determine which access bits we want to ask for...
2749 */
2750 if (mode & MAY_READ)
2751 args.access |= NFS4_ACCESS_READ;
2752 if (S_ISDIR(inode->i_mode)) {
2753 if (mode & MAY_WRITE)
2754 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2755 if (mode & MAY_EXEC)
2756 args.access |= NFS4_ACCESS_LOOKUP;
2757 } else {
2758 if (mode & MAY_WRITE)
2759 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2760 if (mode & MAY_EXEC)
2761 args.access |= NFS4_ACCESS_EXECUTE;
2762 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002763
2764 res.fattr = nfs_alloc_fattr();
2765 if (res.fattr == NULL)
2766 return -ENOMEM;
2767
Bryan Schumaker7c513052011-03-24 17:12:24 +00002768 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (!status) {
2770 entry->mask = 0;
2771 if (res.access & NFS4_ACCESS_READ)
2772 entry->mask |= MAY_READ;
2773 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2774 entry->mask |= MAY_WRITE;
2775 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2776 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002777 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002779 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return status;
2781}
2782
2783static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2784{
2785 struct nfs4_exception exception = { };
2786 int err;
2787 do {
2788 err = nfs4_handle_exception(NFS_SERVER(inode),
2789 _nfs4_proc_access(inode, entry),
2790 &exception);
2791 } while (exception.retry);
2792 return err;
2793}
2794
2795/*
2796 * TODO: For the time being, we don't try to get any attributes
2797 * along with any of the zero-copy operations READ, READDIR,
2798 * READLINK, WRITE.
2799 *
2800 * In the case of the first three, we want to put the GETATTR
2801 * after the read-type operation -- this is because it is hard
2802 * to predict the length of a GETATTR response in v4, and thus
2803 * align the READ data correctly. This means that the GETATTR
2804 * may end up partially falling into the page cache, and we should
2805 * shift it into the 'tail' of the xdr_buf before processing.
2806 * To do this efficiently, we need to know the total length
2807 * of data received, which doesn't seem to be available outside
2808 * of the RPC layer.
2809 *
2810 * In the case of WRITE, we also want to put the GETATTR after
2811 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002812 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 *
2814 * Both of these changes to the XDR layer would in fact be quite
2815 * minor, but I decided to leave them for a subsequent patch.
2816 */
2817static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2818 unsigned int pgbase, unsigned int pglen)
2819{
2820 struct nfs4_readlink args = {
2821 .fh = NFS_FH(inode),
2822 .pgbase = pgbase,
2823 .pglen = pglen,
2824 .pages = &page,
2825 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002826 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 struct rpc_message msg = {
2828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2829 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002830 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 };
2832
Bryan Schumaker7c513052011-03-24 17:12:24 +00002833 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 -07002834}
2835
2836static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2837 unsigned int pgbase, unsigned int pglen)
2838{
2839 struct nfs4_exception exception = { };
2840 int err;
2841 do {
2842 err = nfs4_handle_exception(NFS_SERVER(inode),
2843 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2844 &exception);
2845 } while (exception.retry);
2846 return err;
2847}
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002850 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852static int
2853nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002854 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 int status = 0;
2859
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002860 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2861 if (IS_ERR(ctx))
2862 return PTR_ERR(ctx);
2863
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002864 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002865 state = nfs4_do_open(dir, dentry, ctx->mode,
2866 flags, sattr, ctx->cred,
2867 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002868 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (IS_ERR(state)) {
2870 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002873 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002874 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002875 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002877 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return status;
2879}
2880
2881static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2882{
Trond Myklebust16e42952005-10-27 22:12:44 -04002883 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002884 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002886 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002888 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002889 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002890 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2893 .rpc_argp = &args,
2894 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002896 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002897
Bryan Schumaker7c513052011-03-24 17:12:24 +00002898 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002899 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002900 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902}
2903
2904static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(NFS_SERVER(dir),
2910 _nfs4_proc_remove(dir, name),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002916static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002918 struct nfs_server *server = NFS_SERVER(dir);
2919 struct nfs_removeargs *args = msg->rpc_argp;
2920 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002922 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002924 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002927static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2928{
2929 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2930 &data->args.seq_args,
2931 &data->res.seq_res,
2932 task))
2933 return;
2934 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002937static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002939 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2940
Trond Myklebust14516c32010-07-31 14:29:06 -04002941 if (!nfs4_sequence_done(task, &res->seq_res))
2942 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002943 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002944 return 0;
2945 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002946 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
Jeff Laytond3d41522010-09-17 17:31:57 -04002949static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2950{
2951 struct nfs_server *server = NFS_SERVER(dir);
2952 struct nfs_renameargs *arg = msg->rpc_argp;
2953 struct nfs_renameres *res = msg->rpc_resp;
2954
2955 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002956 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002957 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002958}
2959
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002960static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2961{
2962 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2963 &data->args.seq_args,
2964 &data->res.seq_res,
2965 task))
2966 return;
2967 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002968}
2969
2970static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2971 struct inode *new_dir)
2972{
2973 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2974
2975 if (!nfs4_sequence_done(task, &res->seq_res))
2976 return 0;
2977 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2978 return 0;
2979
2980 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002981 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002982 return 1;
2983}
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2986 struct inode *new_dir, struct qstr *new_name)
2987{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002988 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002989 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 .old_dir = NFS_FH(old_dir),
2991 .new_dir = NFS_FH(new_dir),
2992 .old_name = old_name,
2993 .new_name = new_name,
2994 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002995 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002996 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002997 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct rpc_message msg = {
2999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3000 .rpc_argp = &arg,
3001 .rpc_resp = &res,
3002 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003003 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Bryan Schumaker7c513052011-03-24 17:12:24 +00003005 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (!status) {
3007 update_changeattr(old_dir, &res.old_cinfo);
3008 update_changeattr(new_dir, &res.new_cinfo);
3009 }
3010 return status;
3011}
3012
3013static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3014 struct inode *new_dir, struct qstr *new_name)
3015{
3016 struct nfs4_exception exception = { };
3017 int err;
3018 do {
3019 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3020 _nfs4_proc_rename(old_dir, old_name,
3021 new_dir, new_name),
3022 &exception);
3023 } while (exception.retry);
3024 return err;
3025}
3026
3027static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3028{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003029 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct nfs4_link_arg arg = {
3031 .fh = NFS_FH(inode),
3032 .dir_fh = NFS_FH(dir),
3033 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003034 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003036 struct nfs4_link_res res = {
3037 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 struct rpc_message msg = {
3040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3041 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003042 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003044 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Trond Myklebust136f2622010-04-16 16:22:49 -04003046 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003047 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003048 goto out;
3049
Bryan Schumaker7c513052011-03-24 17:12:24 +00003050 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003051 if (!status) {
3052 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003053 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003054 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003055out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003056 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return status;
3058}
3059
3060static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3061{
3062 struct nfs4_exception exception = { };
3063 int err;
3064 do {
3065 err = nfs4_handle_exception(NFS_SERVER(inode),
3066 _nfs4_proc_link(inode, dir, name),
3067 &exception);
3068 } while (exception.retry);
3069 return err;
3070}
3071
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072struct nfs4_createdata {
3073 struct rpc_message msg;
3074 struct nfs4_create_arg arg;
3075 struct nfs4_create_res res;
3076 struct nfs_fh fh;
3077 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003078};
3079
3080static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3081 struct qstr *name, struct iattr *sattr, u32 ftype)
3082{
3083 struct nfs4_createdata *data;
3084
3085 data = kzalloc(sizeof(*data), GFP_KERNEL);
3086 if (data != NULL) {
3087 struct nfs_server *server = NFS_SERVER(dir);
3088
3089 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3090 data->msg.rpc_argp = &data->arg;
3091 data->msg.rpc_resp = &data->res;
3092 data->arg.dir_fh = NFS_FH(dir);
3093 data->arg.server = server;
3094 data->arg.name = name;
3095 data->arg.attrs = sattr;
3096 data->arg.ftype = ftype;
3097 data->arg.bitmask = server->attr_bitmask;
3098 data->res.server = server;
3099 data->res.fh = &data->fh;
3100 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003102 }
3103 return data;
3104}
3105
3106static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3107{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003108 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003109 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110 if (status == 0) {
3111 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3113 }
3114 return status;
3115}
3116
3117static void nfs4_free_createdata(struct nfs4_createdata *data)
3118{
3119 kfree(data);
3120}
3121
Chuck Lever4f390c12006-08-22 20:06:22 -04003122static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003123 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003125 struct nfs4_createdata *data;
3126 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Chuck Lever94a6d752006-08-22 20:06:23 -04003128 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003129 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003130
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 status = -ENOMEM;
3132 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3133 if (data == NULL)
3134 goto out;
3135
3136 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3137 data->arg.u.symlink.pages = &page;
3138 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003140 status = nfs4_do_create(dir, dentry, data);
3141
3142 nfs4_free_createdata(data);
3143out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return status;
3145}
3146
Chuck Lever4f390c12006-08-22 20:06:22 -04003147static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003148 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 struct nfs4_exception exception = { };
3151 int err;
3152 do {
3153 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003154 _nfs4_proc_symlink(dir, dentry, page,
3155 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 &exception);
3157 } while (exception.retry);
3158 return err;
3159}
3160
3161static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3162 struct iattr *sattr)
3163{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003164 struct nfs4_createdata *data;
3165 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003167 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3168 if (data == NULL)
3169 goto out;
3170
3171 status = nfs4_do_create(dir, dentry, data);
3172
3173 nfs4_free_createdata(data);
3174out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return status;
3176}
3177
3178static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3179 struct iattr *sattr)
3180{
3181 struct nfs4_exception exception = { };
3182 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003183
3184 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 do {
3186 err = nfs4_handle_exception(NFS_SERVER(dir),
3187 _nfs4_proc_mkdir(dir, dentry, sattr),
3188 &exception);
3189 } while (exception.retry);
3190 return err;
3191}
3192
3193static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003194 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
3196 struct inode *dir = dentry->d_inode;
3197 struct nfs4_readdir_arg args = {
3198 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003199 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 .pgbase = 0,
3201 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003202 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003203 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 };
3205 struct nfs4_readdir_res res;
3206 struct rpc_message msg = {
3207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3208 .rpc_argp = &args,
3209 .rpc_resp = &res,
3210 .rpc_cred = cred,
3211 };
3212 int status;
3213
Harvey Harrison3110ff82008-05-02 13:42:44 -07003214 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003215 dentry->d_parent->d_name.name,
3216 dentry->d_name.name,
3217 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003218 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003220 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 -05003221 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003222 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003223 status += args.pgbase;
3224 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003225
3226 nfs_invalidate_atime(dir);
3227
Harvey Harrison3110ff82008-05-02 13:42:44 -07003228 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return status;
3230}
3231
3232static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003233 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 struct nfs4_exception exception = { };
3236 int err;
3237 do {
3238 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3239 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003240 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 &exception);
3242 } while (exception.retry);
3243 return err;
3244}
3245
3246static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3247 struct iattr *sattr, dev_t rdev)
3248{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003249 struct nfs4_createdata *data;
3250 int mode = sattr->ia_mode;
3251 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3254 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003255
3256 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3257 if (data == NULL)
3258 goto out;
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003261 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003263 data->arg.ftype = NF4BLK;
3264 data->arg.u.device.specdata1 = MAJOR(rdev);
3265 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
3267 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003268 data->arg.ftype = NF4CHR;
3269 data->arg.u.device.specdata1 = MAJOR(rdev);
3270 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003273 status = nfs4_do_create(dir, dentry, data);
3274
3275 nfs4_free_createdata(data);
3276out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return status;
3278}
3279
3280static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3281 struct iattr *sattr, dev_t rdev)
3282{
3283 struct nfs4_exception exception = { };
3284 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003285
3286 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 do {
3288 err = nfs4_handle_exception(NFS_SERVER(dir),
3289 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3290 &exception);
3291 } while (exception.retry);
3292 return err;
3293}
3294
3295static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3296 struct nfs_fsstat *fsstat)
3297{
3298 struct nfs4_statfs_arg args = {
3299 .fh = fhandle,
3300 .bitmask = server->attr_bitmask,
3301 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003302 struct nfs4_statfs_res res = {
3303 .fsstat = fsstat,
3304 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 struct rpc_message msg = {
3306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3307 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003308 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 };
3310
Trond Myklebust0e574af2005-10-27 22:12:38 -04003311 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003312 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
3315static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3316{
3317 struct nfs4_exception exception = { };
3318 int err;
3319 do {
3320 err = nfs4_handle_exception(server,
3321 _nfs4_proc_statfs(server, fhandle, fsstat),
3322 &exception);
3323 } while (exception.retry);
3324 return err;
3325}
3326
3327static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3328 struct nfs_fsinfo *fsinfo)
3329{
3330 struct nfs4_fsinfo_arg args = {
3331 .fh = fhandle,
3332 .bitmask = server->attr_bitmask,
3333 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003334 struct nfs4_fsinfo_res res = {
3335 .fsinfo = fsinfo,
3336 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct rpc_message msg = {
3338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3339 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003340 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 };
3342
Bryan Schumaker7c513052011-03-24 17:12:24 +00003343 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
3346static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3347{
3348 struct nfs4_exception exception = { };
3349 int err;
3350
3351 do {
3352 err = nfs4_handle_exception(server,
3353 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3354 &exception);
3355 } while (exception.retry);
3356 return err;
3357}
3358
3359static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3360{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003361 int error;
3362
Trond Myklebust0e574af2005-10-27 22:12:38 -04003363 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003364 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3365 if (error == 0)
3366 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3367
3368 return error;
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_pathconf_arg args = {
3375 .fh = fhandle,
3376 .bitmask = server->attr_bitmask,
3377 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003378 struct nfs4_pathconf_res res = {
3379 .pathconf = pathconf,
3380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 struct rpc_message msg = {
3382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3383 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003384 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 };
3386
3387 /* None of the pathconf attributes are mandatory to implement */
3388 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3389 memset(pathconf, 0, sizeof(*pathconf));
3390 return 0;
3391 }
3392
Trond Myklebust0e574af2005-10-27 22:12:38 -04003393 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003394 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3398 struct nfs_pathconf *pathconf)
3399{
3400 struct nfs4_exception exception = { };
3401 int err;
3402
3403 do {
3404 err = nfs4_handle_exception(server,
3405 _nfs4_proc_pathconf(server, fhandle, pathconf),
3406 &exception);
3407 } while (exception.retry);
3408 return err;
3409}
3410
Benny Halevyd20581a2011-05-22 19:52:03 +03003411void __nfs4_read_done_cb(struct nfs_read_data *data)
3412{
Fred Isamancd841602012-04-20 14:47:44 -04003413 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003414}
3415
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003416static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
Fred Isamancd841602012-04-20 14:47:44 -04003418 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003420 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003421 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003422 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003424
Benny Halevyd20581a2011-05-22 19:52:03 +03003425 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003427 renew_lease(server, data->timestamp);
3428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003431static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3432{
3433
3434 dprintk("--> %s\n", __func__);
3435
3436 if (!nfs4_sequence_done(task, &data->res.seq_res))
3437 return -EAGAIN;
3438
Benny Halevyd20581a2011-05-22 19:52:03 +03003439 return data->read_done_cb ? data->read_done_cb(task, data) :
3440 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003441}
3442
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003443static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003446 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003447 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003448 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003451static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3452{
Fred Isamancd841602012-04-20 14:47:44 -04003453 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003454 &data->args.seq_args,
3455 &data->res.seq_res,
3456 task))
3457 return;
3458 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
Fred Isamanb029bc92011-03-03 15:13:42 +00003461static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Fred Isamancd841602012-04-20 14:47:44 -04003463 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003465 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003466 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003467 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003469 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003471 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003472 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Fred Isamanb029bc92011-03-03 15:13:42 +00003476static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3477{
3478 if (!nfs4_sequence_done(task, &data->res.seq_res))
3479 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003480 return data->write_done_cb ? data->write_done_cb(task, data) :
3481 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003482}
3483
Trond Myklebust5a37f852012-04-28 14:55:16 -04003484static
3485bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003486{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003487 const struct nfs_pgio_header *hdr = data->header;
3488
3489 /* Don't request attributes for pNFS or O_DIRECT writes */
3490 if (data->ds_clp != NULL || hdr->dreq != NULL)
3491 return false;
3492 /* Otherwise, request attributes if and only if we don't hold
3493 * a delegation
3494 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003495 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003496}
Fred Isamana69aef12011-03-03 15:13:47 +00003497
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003498static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Fred Isamancd841602012-04-20 14:47:44 -04003500 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003501
Trond Myklebust5a37f852012-04-28 14:55:16 -04003502 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003503 data->args.bitmask = NULL;
3504 data->res.fattr = NULL;
3505 } else
3506 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003507
Fred Isamanb029bc92011-03-03 15:13:42 +00003508 if (!data->write_done_cb)
3509 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003510 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 data->timestamp = jiffies;
3512
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003514 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003517static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3518{
Fred Isamancd841602012-04-20 14:47:44 -04003519 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003520 &data->args.seq_args,
3521 &data->res.seq_res,
3522 task))
3523 return;
3524 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
Fred Isaman0b7c0152012-04-20 14:47:39 -04003527static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3530 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003531 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return;
3534 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003535}
3536
Fred Isaman0b7c0152012-04-20 14:47:39 -04003537static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
3539 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003540
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003541 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003542 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003543 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Fred Isaman0b7c0152012-04-20 14:47:39 -04003548static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003549{
3550 if (!nfs4_sequence_done(task, &data->res.seq_res))
3551 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003552 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003553}
3554
Fred Isaman0b7c0152012-04-20 14:47:39 -04003555static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003557 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003558
Fred Isaman0b7c0152012-04-20 14:47:39 -04003559 if (data->commit_done_cb == NULL)
3560 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003561 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003562 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003563 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003566struct nfs4_renewdata {
3567 struct nfs_client *client;
3568 unsigned long timestamp;
3569};
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571/*
3572 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3573 * standalone procedure for queueing an asynchronous RENEW.
3574 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003575static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003576{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003577 struct nfs4_renewdata *data = calldata;
3578 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003579
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003580 if (atomic_read(&clp->cl_count) > 1)
3581 nfs4_schedule_state_renewal(clp);
3582 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003583 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003584}
3585
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003586static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003588 struct nfs4_renewdata *data = calldata;
3589 struct nfs_client *clp = data->client;
3590 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003593 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003594 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3595 return;
3596 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003597 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003598 return;
3599 }
3600 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003602 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003605static const struct rpc_call_ops nfs4_renew_ops = {
3606 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003607 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003608};
3609
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003610static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3614 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003615 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003617 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003619 if (renew_flags == 0)
3620 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003621 if (!atomic_inc_not_zero(&clp->cl_count))
3622 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003623 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003624 if (data == NULL)
3625 return -ENOMEM;
3626 data->client = clp;
3627 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003629 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003632static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
3634 struct rpc_message msg = {
3635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3636 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003637 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 };
3639 unsigned long now = jiffies;
3640 int status;
3641
3642 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3643 if (status < 0)
3644 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003645 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return 0;
3647}
3648
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003649static inline int nfs4_server_supports_acls(struct nfs_server *server)
3650{
3651 return (server->caps & NFS_CAP_ACLS)
3652 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3653 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3654}
3655
Trond Myklebust21f498c2012-08-24 10:59:25 -04003656/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3657 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003658 * the stack.
3659 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003660#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003661
Neil Hormane9e3d722011-03-04 19:26:03 -05003662static int buf_to_pages_noslab(const void *buf, size_t buflen,
3663 struct page **pages, unsigned int *pgbase)
3664{
3665 struct page *newpage, **spages;
3666 int rc = 0;
3667 size_t len;
3668 spages = pages;
3669
3670 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003671 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003672 newpage = alloc_page(GFP_KERNEL);
3673
3674 if (newpage == NULL)
3675 goto unwind;
3676 memcpy(page_address(newpage), buf, len);
3677 buf += len;
3678 buflen -= len;
3679 *pages++ = newpage;
3680 rc++;
3681 } while (buflen != 0);
3682
3683 return rc;
3684
3685unwind:
3686 for(; rc > 0; rc--)
3687 __free_page(spages[rc-1]);
3688 return -ENOMEM;
3689}
3690
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003691struct nfs4_cached_acl {
3692 int cached;
3693 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003694 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003695};
3696
3697static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003698{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003699 struct nfs_inode *nfsi = NFS_I(inode);
3700
3701 spin_lock(&inode->i_lock);
3702 kfree(nfsi->nfs4_acl);
3703 nfsi->nfs4_acl = acl;
3704 spin_unlock(&inode->i_lock);
3705}
3706
3707static void nfs4_zap_acl_attr(struct inode *inode)
3708{
3709 nfs4_set_cached_acl(inode, NULL);
3710}
3711
3712static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3713{
3714 struct nfs_inode *nfsi = NFS_I(inode);
3715 struct nfs4_cached_acl *acl;
3716 int ret = -ENOENT;
3717
3718 spin_lock(&inode->i_lock);
3719 acl = nfsi->nfs4_acl;
3720 if (acl == NULL)
3721 goto out;
3722 if (buf == NULL) /* user is just asking for length */
3723 goto out_len;
3724 if (acl->cached == 0)
3725 goto out;
3726 ret = -ERANGE; /* see getxattr(2) man page */
3727 if (acl->len > buflen)
3728 goto out;
3729 memcpy(buf, acl->data, acl->len);
3730out_len:
3731 ret = acl->len;
3732out:
3733 spin_unlock(&inode->i_lock);
3734 return ret;
3735}
3736
Sachin Prabhu5794d212012-04-17 14:36:40 +01003737static 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 +00003738{
3739 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003740 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003741
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003742 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003743 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744 if (acl == NULL)
3745 goto out;
3746 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003747 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003748 } else {
3749 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3750 if (acl == NULL)
3751 goto out;
3752 acl->cached = 0;
3753 }
3754 acl->len = acl_len;
3755out:
3756 nfs4_set_cached_acl(inode, acl);
3757}
3758
Andy Adamsonbf118a32011-12-07 11:55:27 -05003759/*
3760 * The getxattr API returns the required buffer length when called with a
3761 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3762 * the required buf. On a NULL buf, we send a page of data to the server
3763 * guessing that the ACL request can be serviced by a page. If so, we cache
3764 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3765 * the cache. If not so, we throw away the page, and cache the required
3766 * length. The next getxattr call will then produce another round trip to
3767 * the server, this time with the input buf of the required size.
3768 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003769static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003771 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003772 struct nfs_getaclargs args = {
3773 .fh = NFS_FH(inode),
3774 .acl_pages = pages,
3775 .acl_len = buflen,
3776 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003777 struct nfs_getaclres res = {
3778 .acl_len = buflen,
3779 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003780 struct rpc_message msg = {
3781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3782 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003783 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003784 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003785 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3786 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003787
Andy Adamsonbf118a32011-12-07 11:55:27 -05003788 /* As long as we're doing a round trip to the server anyway,
3789 * let's be prepared for a page of acl data. */
3790 if (npages == 0)
3791 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003792 if (npages > ARRAY_SIZE(pages))
3793 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003794
Andy Adamsonbf118a32011-12-07 11:55:27 -05003795 for (i = 0; i < npages; i++) {
3796 pages[i] = alloc_page(GFP_KERNEL);
3797 if (!pages[i])
3798 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003799 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003800
3801 /* for decoding across pages */
3802 res.acl_scratch = alloc_page(GFP_KERNEL);
3803 if (!res.acl_scratch)
3804 goto out_free;
3805
Andy Adamsonbf118a32011-12-07 11:55:27 -05003806 args.acl_len = npages * PAGE_SIZE;
3807 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003808
Peng Taode040be2012-01-10 22:42:47 +08003809 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003810 __func__, buf, buflen, npages, args.acl_len);
3811 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3812 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003813 if (ret)
3814 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003815
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003816 /* Handle the case where the passed-in buffer is too short */
3817 if (res.acl_flags & NFS4_ACL_TRUNC) {
3818 /* Did the user only issue a request for the acl length? */
3819 if (buf == NULL)
3820 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003821 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003822 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003823 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003824 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3825 if (buf)
3826 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3827out_ok:
3828 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003829out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003830 for (i = 0; i < npages; i++)
3831 if (pages[i])
3832 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003833 if (res.acl_scratch)
3834 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003835 return ret;
3836}
3837
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003838static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3839{
3840 struct nfs4_exception exception = { };
3841 ssize_t ret;
3842 do {
3843 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3844 if (ret >= 0)
3845 break;
3846 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3847 } while (exception.retry);
3848 return ret;
3849}
3850
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003851static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3852{
3853 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003854 int ret;
3855
3856 if (!nfs4_server_supports_acls(server))
3857 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003858 ret = nfs_revalidate_inode(server, inode);
3859 if (ret < 0)
3860 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003861 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3862 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003863 ret = nfs4_read_cached_acl(inode, buf, buflen);
3864 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003865 /* -ENOENT is returned if there is no ACL or if there is an ACL
3866 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867 return ret;
3868 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003869}
3870
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003871static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003872{
3873 struct nfs_server *server = NFS_SERVER(inode);
3874 struct page *pages[NFS4ACL_MAXPAGES];
3875 struct nfs_setaclargs arg = {
3876 .fh = NFS_FH(inode),
3877 .acl_pages = pages,
3878 .acl_len = buflen,
3879 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003880 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003881 struct rpc_message msg = {
3882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3883 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003884 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003885 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003886 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003887 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003888
3889 if (!nfs4_server_supports_acls(server))
3890 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003891 if (npages > ARRAY_SIZE(pages))
3892 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003893 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3894 if (i < 0)
3895 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003896 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003897 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003898
3899 /*
3900 * Free each page after tx, so the only ref left is
3901 * held by the network stack
3902 */
3903 for (; i > 0; i--)
3904 put_page(pages[i-1]);
3905
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003906 /*
3907 * Acl update can result in inode attribute update.
3908 * so mark the attribute cache invalid.
3909 */
3910 spin_lock(&inode->i_lock);
3911 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3912 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003913 nfs_access_zap_cache(inode);
3914 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003915 return ret;
3916}
3917
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003918static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3919{
3920 struct nfs4_exception exception = { };
3921 int err;
3922 do {
3923 err = nfs4_handle_exception(NFS_SERVER(inode),
3924 __nfs4_proc_set_acl(inode, buf, buflen),
3925 &exception);
3926 } while (exception.retry);
3927 return err;
3928}
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003931nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003933 struct nfs_client *clp = server->nfs_client;
3934
3935 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 return 0;
3937 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003938 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003939 case -NFS4ERR_ADMIN_REVOKED:
3940 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003941 if (state == NULL)
3942 break;
3943 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003944 case -NFS4ERR_OPENMODE:
3945 if (state == NULL)
3946 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003947 nfs4_schedule_stateid_recovery(server, state);
3948 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003949 case -NFS4ERR_EXPIRED:
3950 if (state != NULL)
3951 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003953 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003954 nfs4_schedule_lease_recovery(clp);
3955 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003956#if defined(CONFIG_NFS_V4_1)
3957 case -NFS4ERR_BADSESSION:
3958 case -NFS4ERR_BADSLOT:
3959 case -NFS4ERR_BAD_HIGH_SLOT:
3960 case -NFS4ERR_DEADSESSION:
3961 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3962 case -NFS4ERR_SEQ_FALSE_RETRY:
3963 case -NFS4ERR_SEQ_MISORDERED:
3964 dprintk("%s ERROR %d, Reset session\n", __func__,
3965 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003966 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003967 task->tk_status = 0;
3968 return -EAGAIN;
3969#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003971 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003972 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003973 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3975 task->tk_status = 0;
3976 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003977 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 case -NFS4ERR_OLD_STATEID:
3979 task->tk_status = 0;
3980 return -EAGAIN;
3981 }
3982 task->tk_status = nfs4_map_errors(task->tk_status);
3983 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003984wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003985 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003986 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3987 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3988 task->tk_status = 0;
3989 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990}
3991
Chuck Leverf0920752012-05-21 22:45:41 -04003992static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3993 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003994{
3995 __be32 verf[2];
3996
Chuck Lever2c820d92012-05-21 22:45:33 -04003997 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3998 /* An impossible timestamp guarantees this value
3999 * will never match a generated boot time. */
4000 verf[0] = 0;
4001 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4002 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004003 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4004 verf[0] = (__be32)nn->boot_time.tv_sec;
4005 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004006 }
Chuck Levercd937102012-03-02 17:14:31 -05004007 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4008}
4009
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004010/**
4011 * nfs4_proc_setclientid - Negotiate client ID
4012 * @clp: state data structure
4013 * @program: RPC program for NFSv4 callback service
4014 * @port: IP port number for NFS4 callback service
4015 * @cred: RPC credential to use for this call
4016 * @res: where to place the result
4017 *
4018 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4019 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004020int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4021 unsigned short port, struct rpc_cred *cred,
4022 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
4024 nfs4_verifier sc_verifier;
4025 struct nfs4_setclientid setclientid = {
4026 .sc_verifier = &sc_verifier,
4027 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004028 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 };
4030 struct rpc_message msg = {
4031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4032 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004033 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004034 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004036 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Chuck Leverde734832012-07-11 16:30:50 -04004038 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004039 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Leverde734832012-07-11 16:30:50 -04004040 rcu_read_lock();
4041 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
4042 sizeof(setclientid.sc_name), "%s/%s %s",
4043 clp->cl_ipaddr,
4044 rpc_peeraddr2str(clp->cl_rpcclient,
4045 RPC_DISPLAY_ADDR),
4046 rpc_peeraddr2str(clp->cl_rpcclient,
4047 RPC_DISPLAY_PROTO));
4048 /* cb_client4 */
4049 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004050 sizeof(setclientid.sc_netid),
4051 rpc_peeraddr2str(clp->cl_rpcclient,
4052 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004053 rcu_read_unlock();
4054 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004055 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 clp->cl_ipaddr, port >> 8, port & 255);
4057
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004058 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4059 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4060 setclientid.sc_name_len, setclientid.sc_name);
4061 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4062 dprintk("NFS reply setclientid: %d\n", status);
4063 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004066/**
4067 * nfs4_proc_setclientid_confirm - Confirm client ID
4068 * @clp: state data structure
4069 * @res: result of a previous SETCLIENTID
4070 * @cred: RPC credential to use for this call
4071 *
4072 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4073 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004074int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004075 struct nfs4_setclientid_res *arg,
4076 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
4078 struct nfs_fsinfo fsinfo;
4079 struct rpc_message msg = {
4080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004081 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004083 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 };
4085 unsigned long now;
4086 int status;
4087
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004088 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4089 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4090 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004092 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 if (status == 0) {
4094 spin_lock(&clp->cl_lock);
4095 clp->cl_lease_time = fsinfo.lease_time * HZ;
4096 clp->cl_last_renewal = now;
4097 spin_unlock(&clp->cl_lock);
4098 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004099 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 return status;
4101}
4102
Trond Myklebustfe650402006-01-03 09:55:18 +01004103struct nfs4_delegreturndata {
4104 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004105 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004106 struct nfs_fh fh;
4107 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004108 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004109 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004110 int rpc_status;
4111};
4112
Trond Myklebustfe650402006-01-03 09:55:18 +01004113static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4114{
4115 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004116
Trond Myklebust14516c32010-07-31 14:29:06 -04004117 if (!nfs4_sequence_done(task, &data->res.seq_res))
4118 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004119
Ricardo Labiaga79708862009-12-07 09:23:21 -05004120 switch (task->tk_status) {
4121 case -NFS4ERR_STALE_STATEID:
4122 case -NFS4ERR_EXPIRED:
4123 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004124 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004125 break;
4126 default:
4127 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4128 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004129 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004130 return;
4131 }
4132 }
4133 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004134}
4135
4136static void nfs4_delegreturn_release(void *calldata)
4137{
Trond Myklebustfe650402006-01-03 09:55:18 +01004138 kfree(calldata);
4139}
4140
Andy Adamson938e1012009-04-01 09:22:28 -04004141#if defined(CONFIG_NFS_V4_1)
4142static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4143{
4144 struct nfs4_delegreturndata *d_data;
4145
4146 d_data = (struct nfs4_delegreturndata *)data;
4147
Trond Myklebust035168a2010-06-16 09:52:26 -04004148 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004149 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004150 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004151 return;
4152 rpc_call_start(task);
4153}
4154#endif /* CONFIG_NFS_V4_1 */
4155
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004156static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004157#if defined(CONFIG_NFS_V4_1)
4158 .rpc_call_prepare = nfs4_delegreturn_prepare,
4159#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004160 .rpc_call_done = nfs4_delegreturn_done,
4161 .rpc_release = nfs4_delegreturn_release,
4162};
4163
Trond Myklebuste6f81072008-01-24 18:14:34 -05004164static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004165{
4166 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004167 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004168 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004169 struct rpc_message msg = {
4170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4171 .rpc_cred = cred,
4172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004173 struct rpc_task_setup task_setup_data = {
4174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004176 .callback_ops = &nfs4_delegreturn_ops,
4177 .flags = RPC_TASK_ASYNC,
4178 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004179 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004180
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004181 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004182 if (data == NULL)
4183 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004184 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004185 data->args.fhandle = &data->fh;
4186 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004187 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004188 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004189 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004190 data->res.fattr = &data->fattr;
4191 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004192 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004193 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004194 data->rpc_status = 0;
4195
Trond Myklebustc970aa82007-07-14 15:39:59 -04004196 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004197 msg.rpc_argp = &data->args;
4198 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004199 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004200 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004201 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004202 if (!issync)
4203 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004204 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004205 if (status != 0)
4206 goto out;
4207 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004208 if (status == 0)
4209 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4210 else
4211 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004212out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004213 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004214 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
Trond Myklebuste6f81072008-01-24 18:14:34 -05004217int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
4219 struct nfs_server *server = NFS_SERVER(inode);
4220 struct nfs4_exception exception = { };
4221 int err;
4222 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004223 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 switch (err) {
4225 case -NFS4ERR_STALE_STATEID:
4226 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 case 0:
4228 return 0;
4229 }
4230 err = nfs4_handle_exception(server, err, &exception);
4231 } while (exception.retry);
4232 return err;
4233}
4234
4235#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4236#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4237
4238/*
4239 * sleep, with exponential backoff, and retry the LOCK operation.
4240 */
4241static unsigned long
4242nfs4_set_lock_task_retry(unsigned long timeout)
4243{
Jeff Laytond3103102011-12-01 22:44:39 +01004244 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 timeout <<= 1;
4246 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4247 return NFS4_LOCK_MAXTIMEOUT;
4248 return timeout;
4249}
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4252{
4253 struct inode *inode = state->inode;
4254 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004255 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004256 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004258 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004260 struct nfs_lockt_res res = {
4261 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 };
4263 struct rpc_message msg = {
4264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4265 .rpc_argp = &arg,
4266 .rpc_resp = &res,
4267 .rpc_cred = state->owner->so_cred,
4268 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 struct nfs4_lock_state *lsp;
4270 int status;
4271
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004272 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004273 status = nfs4_set_lock_state(state, request);
4274 if (status != 0)
4275 goto out;
4276 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004277 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004278 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004279 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004280 switch (status) {
4281 case 0:
4282 request->fl_type = F_UNLCK;
4283 break;
4284 case -NFS4ERR_DENIED:
4285 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004287 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 return status;
4290}
4291
4292static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4293{
4294 struct nfs4_exception exception = { };
4295 int err;
4296
4297 do {
4298 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4299 _nfs4_proc_getlk(state, cmd, request),
4300 &exception);
4301 } while (exception.retry);
4302 return err;
4303}
4304
4305static int do_vfs_lock(struct file *file, struct file_lock *fl)
4306{
4307 int res = 0;
4308 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4309 case FL_POSIX:
4310 res = posix_lock_file_wait(file, fl);
4311 break;
4312 case FL_FLOCK:
4313 res = flock_lock_file_wait(file, fl);
4314 break;
4315 default:
4316 BUG();
4317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return res;
4319}
4320
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004322 struct nfs_locku_args arg;
4323 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004324 struct nfs4_lock_state *lsp;
4325 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004326 struct file_lock fl;
4327 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004328 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004329};
4330
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004331static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4332 struct nfs_open_context *ctx,
4333 struct nfs4_lock_state *lsp,
4334 struct nfs_seqid *seqid)
4335{
4336 struct nfs4_unlockdata *p;
4337 struct inode *inode = lsp->ls_state->inode;
4338
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004339 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004340 if (p == NULL)
4341 return NULL;
4342 p->arg.fh = NFS_FH(inode);
4343 p->arg.fl = &p->fl;
4344 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004345 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004346 p->arg.stateid = &lsp->ls_stateid;
4347 p->lsp = lsp;
4348 atomic_inc(&lsp->ls_count);
4349 /* Ensure we don't close file until we're done freeing locks! */
4350 p->ctx = get_nfs_open_context(ctx);
4351 memcpy(&p->fl, fl, sizeof(p->fl));
4352 p->server = NFS_SERVER(inode);
4353 return p;
4354}
4355
Trond Myklebust06f814a2006-01-03 09:55:07 +01004356static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004357{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004358 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004359 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004360 nfs4_put_lock_state(calldata->lsp);
4361 put_nfs_open_context(calldata->ctx);
4362 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004363}
4364
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004365static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004366{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004367 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004368
Trond Myklebust14516c32010-07-31 14:29:06 -04004369 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4370 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004371 switch (task->tk_status) {
4372 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004373 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4374 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004375 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004376 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004377 case -NFS4ERR_BAD_STATEID:
4378 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004379 case -NFS4ERR_STALE_STATEID:
4380 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004381 break;
4382 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004383 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004384 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004385 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004386}
4387
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004388static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004389{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004390 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004392 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004394 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004395 /* Note: exit _without_ running nfs4_locku_done */
4396 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004397 return;
4398 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004399 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004400 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004401 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004402 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004403 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004404 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004407static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004408 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004409 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004410 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004411};
4412
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004413static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4414 struct nfs_open_context *ctx,
4415 struct nfs4_lock_state *lsp,
4416 struct nfs_seqid *seqid)
4417{
4418 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004419 struct rpc_message msg = {
4420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4421 .rpc_cred = ctx->cred,
4422 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004423 struct rpc_task_setup task_setup_data = {
4424 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004425 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004426 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004427 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004428 .flags = RPC_TASK_ASYNC,
4429 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004430
Frank Filz137d6ac2007-07-09 15:32:29 -07004431 /* Ensure this is an unlock - when canceling a lock, the
4432 * canceled lock is passed in, and it won't be an unlock.
4433 */
4434 fl->fl_type = F_UNLCK;
4435
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004436 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4437 if (data == NULL) {
4438 nfs_free_seqid(seqid);
4439 return ERR_PTR(-ENOMEM);
4440 }
4441
Trond Myklebust9d12b212012-01-17 22:04:25 -05004442 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004443 msg.rpc_argp = &data->arg;
4444 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004445 task_setup_data.callback_data = data;
4446 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447}
4448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4450{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004451 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004452 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004453 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004454 struct rpc_task *task;
4455 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004456 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Trond Myklebust9b073572006-06-29 16:38:34 -04004458 status = nfs4_set_lock_state(state, request);
4459 /* Unlock _before_ we do the RPC call */
4460 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004461 down_read(&nfsi->rwsem);
4462 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4463 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004464 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004465 }
4466 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004467 if (status != 0)
4468 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004469 /* Is this a delegated lock? */
4470 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004471 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004472 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004473 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004474 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004475 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004476 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004477 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004478 status = PTR_ERR(task);
4479 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004480 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004482 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004483out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004484 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004485 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486}
4487
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004488struct nfs4_lockdata {
4489 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004490 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004491 struct nfs4_lock_state *lsp;
4492 struct nfs_open_context *ctx;
4493 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004494 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004495 int rpc_status;
4496 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004497 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004498};
4499
4500static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004501 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4502 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503{
4504 struct nfs4_lockdata *p;
4505 struct inode *inode = lsp->ls_state->inode;
4506 struct nfs_server *server = NFS_SERVER(inode);
4507
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004508 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509 if (p == NULL)
4510 return NULL;
4511
4512 p->arg.fh = NFS_FH(inode);
4513 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004514 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004515 if (p->arg.open_seqid == NULL)
4516 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004517 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004519 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004521 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004522 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004523 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004524 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004526 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527 atomic_inc(&lsp->ls_count);
4528 p->ctx = get_nfs_open_context(ctx);
4529 memcpy(&p->fl, fl, sizeof(p->fl));
4530 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004531out_free_seqid:
4532 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533out_free:
4534 kfree(p);
4535 return NULL;
4536}
4537
4538static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4539{
4540 struct nfs4_lockdata *data = calldata;
4541 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Harvey Harrison3110ff82008-05-02 13:42:44 -07004543 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004544 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4545 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004546 /* Do we need to do an open_to_lock_owner? */
4547 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004548 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4549 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004550 data->arg.open_stateid = &state->stateid;
4551 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004552 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004553 } else
4554 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004555 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004556 if (nfs4_setup_sequence(data->server,
4557 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004558 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004559 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004560 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004561 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562}
4563
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004564static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4565{
4566 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4567 nfs4_lock_prepare(task, calldata);
4568}
4569
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004570static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4571{
4572 struct nfs4_lockdata *data = calldata;
4573
Harvey Harrison3110ff82008-05-02 13:42:44 -07004574 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575
Trond Myklebust14516c32010-07-31 14:29:06 -04004576 if (!nfs4_sequence_done(task, &data->res.seq_res))
4577 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004578
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004581 if (data->rpc_status == 0)
4582 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4583 else
4584 goto out;
4585 }
4586 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004587 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004588 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004589 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004590 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004592 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593}
4594
4595static void nfs4_lock_release(void *calldata)
4596{
4597 struct nfs4_lockdata *data = calldata;
4598
Harvey Harrison3110ff82008-05-02 13:42:44 -07004599 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004600 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004601 if (data->cancelled != 0) {
4602 struct rpc_task *task;
4603 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4604 data->arg.lock_seqid);
4605 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004606 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004607 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004608 } else
4609 nfs_free_seqid(data->arg.lock_seqid);
4610 nfs4_put_lock_state(data->lsp);
4611 put_nfs_open_context(data->ctx);
4612 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004613 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614}
4615
4616static const struct rpc_call_ops nfs4_lock_ops = {
4617 .rpc_call_prepare = nfs4_lock_prepare,
4618 .rpc_call_done = nfs4_lock_done,
4619 .rpc_release = nfs4_lock_release,
4620};
4621
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004622static const struct rpc_call_ops nfs4_recover_lock_ops = {
4623 .rpc_call_prepare = nfs4_recover_lock_prepare,
4624 .rpc_call_done = nfs4_lock_done,
4625 .rpc_release = nfs4_lock_release,
4626};
4627
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004628static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4629{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004630 switch (error) {
4631 case -NFS4ERR_ADMIN_REVOKED:
4632 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004633 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004634 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004635 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004636 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004637 break;
4638 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004639 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004640 case -NFS4ERR_EXPIRED:
4641 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004642 };
4643}
4644
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004645static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646{
4647 struct nfs4_lockdata *data;
4648 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004649 struct rpc_message msg = {
4650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4651 .rpc_cred = state->owner->so_cred,
4652 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004653 struct rpc_task_setup task_setup_data = {
4654 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004655 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004656 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004657 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004658 .flags = RPC_TASK_ASYNC,
4659 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004660 int ret;
4661
Harvey Harrison3110ff82008-05-02 13:42:44 -07004662 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004663 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004664 fl->fl_u.nfs4_fl.owner,
4665 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666 if (data == NULL)
4667 return -ENOMEM;
4668 if (IS_SETLKW(cmd))
4669 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004670 if (recovery_type > NFS_LOCK_NEW) {
4671 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004672 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004673 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4674 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004675 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004676 msg.rpc_argp = &data->arg;
4677 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004678 task_setup_data.callback_data = data;
4679 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004680 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004681 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004682 ret = nfs4_wait_for_completion_rpc_task(task);
4683 if (ret == 0) {
4684 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004685 if (ret)
4686 nfs4_handle_setlk_error(data->server, data->lsp,
4687 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688 } else
4689 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004690 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004691 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
4694
4695static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4696{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004697 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004698 struct nfs4_exception exception = {
4699 .inode = state->inode,
4700 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004701 int err;
4702
4703 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004704 /* Cache the lock if possible... */
4705 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4706 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004707 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004708 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004709 break;
4710 nfs4_handle_exception(server, err, &exception);
4711 } while (exception.retry);
4712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
4715static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4716{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004717 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004718 struct nfs4_exception exception = {
4719 .inode = state->inode,
4720 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004721 int err;
4722
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004723 err = nfs4_set_lock_state(state, request);
4724 if (err != 0)
4725 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004726 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004727 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4728 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004729 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004730 switch (err) {
4731 default:
4732 goto out;
4733 case -NFS4ERR_GRACE:
4734 case -NFS4ERR_DELAY:
4735 nfs4_handle_exception(server, err, &exception);
4736 err = 0;
4737 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004738 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004739out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741}
4742
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004743#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004744/**
4745 * nfs41_check_expired_locks - possibly free a lock stateid
4746 *
4747 * @state: NFSv4 state for an inode
4748 *
4749 * Returns NFS_OK if recovery for this stateid is now finished.
4750 * Otherwise a negative NFS4ERR value is returned.
4751 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004752static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004753{
Chuck Levereb64cf92012-07-11 16:30:05 -04004754 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004755 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004756 struct nfs_server *server = NFS_SERVER(state->inode);
4757
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004758 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004759 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004760 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4761 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004762 /* Free the stateid unless the server
4763 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004764 if (status != -NFS4ERR_BAD_STATEID)
4765 nfs41_free_stateid(server,
4766 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004767 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004768 ret = status;
4769 }
4770 }
4771 };
4772
4773 return ret;
4774}
4775
4776static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4777{
4778 int status = NFS_OK;
4779
4780 if (test_bit(LK_STATE_IN_USE, &state->flags))
4781 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004782 if (status != NFS_OK)
4783 status = nfs4_lock_expired(state, request);
4784 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004785}
4786#endif
4787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4789{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004790 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004791 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004792 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004794 if ((fl_flags & FL_POSIX) &&
4795 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4796 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004797 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004798 status = nfs4_set_lock_state(state, request);
4799 if (status != 0)
4800 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004801 request->fl_flags |= FL_ACCESS;
4802 status = do_vfs_lock(request->fl_file, request);
4803 if (status < 0)
4804 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004805 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004806 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004807 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004808 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004809 request->fl_flags = fl_flags & ~FL_SLEEP;
4810 status = do_vfs_lock(request->fl_file, request);
4811 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004812 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004813 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004814 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004815 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004816 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004817 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004818 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004819 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4820 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004821out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004822 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004823out:
4824 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 return status;
4826}
4827
4828static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4829{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004830 struct nfs4_exception exception = {
4831 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004832 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 int err;
4835
4836 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004837 err = _nfs4_proc_setlk(state, cmd, request);
4838 if (err == -NFS4ERR_DENIED)
4839 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004841 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 } while (exception.retry);
4843 return err;
4844}
4845
4846static int
4847nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4848{
4849 struct nfs_open_context *ctx;
4850 struct nfs4_state *state;
4851 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4852 int status;
4853
4854 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004855 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 state = ctx->state;
4857
4858 if (request->fl_start < 0 || request->fl_end < 0)
4859 return -EINVAL;
4860
Trond Myklebustd9531262009-07-21 19:22:38 -04004861 if (IS_GETLK(cmd)) {
4862 if (state != NULL)
4863 return nfs4_proc_getlk(state, F_GETLK, request);
4864 return 0;
4865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
4867 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4868 return -EINVAL;
4869
Trond Myklebustd9531262009-07-21 19:22:38 -04004870 if (request->fl_type == F_UNLCK) {
4871 if (state != NULL)
4872 return nfs4_proc_unlck(state, cmd, request);
4873 return 0;
4874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Trond Myklebustd9531262009-07-21 19:22:38 -04004876 if (state == NULL)
4877 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004878 /*
4879 * Don't rely on the VFS having checked the file open mode,
4880 * since it won't do this for flock() locks.
4881 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004882 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004883 case F_RDLCK:
4884 if (!(filp->f_mode & FMODE_READ))
4885 return -EBADF;
4886 break;
4887 case F_WRLCK:
4888 if (!(filp->f_mode & FMODE_WRITE))
4889 return -EBADF;
4890 }
4891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 do {
4893 status = nfs4_proc_setlk(state, cmd, request);
4894 if ((status != -EAGAIN) || IS_SETLK(cmd))
4895 break;
4896 timeout = nfs4_set_lock_task_retry(timeout);
4897 status = -ERESTARTSYS;
4898 if (signalled())
4899 break;
4900 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 return status;
4902}
4903
Trond Myklebust888e6942005-11-04 15:38:11 -05004904int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4905{
4906 struct nfs_server *server = NFS_SERVER(state->inode);
4907 struct nfs4_exception exception = { };
4908 int err;
4909
4910 err = nfs4_set_lock_state(state, fl);
4911 if (err != 0)
4912 goto out;
4913 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004914 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004915 switch (err) {
4916 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004917 printk(KERN_ERR "NFS: %s: unhandled error "
4918 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004919 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004920 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004921 goto out;
4922 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004923 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004924 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004925 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004926 nfs4_schedule_lease_recovery(server->nfs_client);
4927 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004928 case -NFS4ERR_BADSESSION:
4929 case -NFS4ERR_BADSLOT:
4930 case -NFS4ERR_BAD_HIGH_SLOT:
4931 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4932 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004933 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004934 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004935 case -ERESTARTSYS:
4936 /*
4937 * The show must go on: exit, but mark the
4938 * stateid as needing recovery.
4939 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004940 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004941 case -NFS4ERR_ADMIN_REVOKED:
4942 case -NFS4ERR_BAD_STATEID:
4943 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004944 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004945 err = 0;
4946 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004947 case -EKEYEXPIRED:
4948 /*
4949 * User RPCSEC_GSS context has expired.
4950 * We cannot recover this stateid now, so
4951 * skip it and allow recovery thread to
4952 * proceed.
4953 */
4954 err = 0;
4955 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004956 case -ENOMEM:
4957 case -NFS4ERR_DENIED:
4958 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4959 err = 0;
4960 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004961 case -NFS4ERR_DELAY:
4962 break;
4963 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004964 err = nfs4_handle_exception(server, err, &exception);
4965 } while (exception.retry);
4966out:
4967 return err;
4968}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004969
Trond Myklebustcf470c32012-03-07 13:49:12 -05004970struct nfs_release_lockowner_data {
4971 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004972 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004973 struct nfs_release_lockowner_args args;
4974};
4975
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004976static void nfs4_release_lockowner_release(void *calldata)
4977{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004978 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004979 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004980 kfree(calldata);
4981}
4982
Trond Myklebust17280172012-03-11 13:11:00 -04004983static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004984 .rpc_release = nfs4_release_lockowner_release,
4985};
4986
Trond Myklebustcf470c32012-03-07 13:49:12 -05004987int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004988{
4989 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004990 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004991 struct rpc_message msg = {
4992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4993 };
4994
4995 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004996 return -EINVAL;
4997 data = kmalloc(sizeof(*data), GFP_NOFS);
4998 if (!data)
4999 return -ENOMEM;
5000 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005001 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005002 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5003 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5004 data->args.lock_owner.s_dev = server->s_dev;
5005 msg.rpc_argp = &data->args;
5006 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5007 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005008}
5009
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005010#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5011
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005012static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5013 const void *buf, size_t buflen,
5014 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005015{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005016 if (strcmp(key, "") != 0)
5017 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005018
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005019 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005020}
5021
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005022static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5023 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005024{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005025 if (strcmp(key, "") != 0)
5026 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005027
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005028 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005029}
5030
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005031static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5032 size_t list_len, const char *name,
5033 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005034{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005035 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005036
J. Bruce Fields096455a2006-03-20 23:23:42 -05005037 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5038 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005039
5040 if (list && len <= list_len)
5041 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005042 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005043}
5044
Andy Adamson533eb462011-06-13 18:25:56 -04005045/*
5046 * nfs_fhget will use either the mounted_on_fileid or the fileid
5047 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005048static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5049{
Andy Adamson533eb462011-06-13 18:25:56 -04005050 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5051 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5052 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005053 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005054 return;
5055
5056 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005057 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005058 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5059 fattr->nlink = 2;
5060}
5061
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005062static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5063 const struct qstr *name,
5064 struct nfs4_fs_locations *fs_locations,
5065 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005066{
5067 struct nfs_server *server = NFS_SERVER(dir);
5068 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005069 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005070 };
5071 struct nfs4_fs_locations_arg args = {
5072 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005073 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005074 .page = page,
5075 .bitmask = bitmask,
5076 };
Benny Halevy22958462009-04-01 09:22:02 -04005077 struct nfs4_fs_locations_res res = {
5078 .fs_locations = fs_locations,
5079 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005080 struct rpc_message msg = {
5081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5082 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005083 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005084 };
5085 int status;
5086
Harvey Harrison3110ff82008-05-02 13:42:44 -07005087 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005088
5089 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5090 * is not supported */
5091 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5092 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5093 else
5094 bitmask[0] |= FATTR4_WORD0_FILEID;
5095
Trond Myklebustc228fd32007-01-13 02:28:11 -05005096 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005097 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005098 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005099 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005100 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005101 return status;
5102}
5103
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005104int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5105 const struct qstr *name,
5106 struct nfs4_fs_locations *fs_locations,
5107 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005108{
5109 struct nfs4_exception exception = { };
5110 int err;
5111 do {
5112 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005113 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005114 &exception);
5115 } while (exception.retry);
5116 return err;
5117}
5118
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005119static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5120{
5121 int status;
5122 struct nfs4_secinfo_arg args = {
5123 .dir_fh = NFS_FH(dir),
5124 .name = name,
5125 };
5126 struct nfs4_secinfo_res res = {
5127 .flavors = flavors,
5128 };
5129 struct rpc_message msg = {
5130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5131 .rpc_argp = &args,
5132 .rpc_resp = &res,
5133 };
5134
5135 dprintk("NFS call secinfo %s\n", name->name);
5136 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5137 dprintk("NFS reply secinfo: %d\n", status);
5138 return status;
5139}
5140
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005141int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5142 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005143{
5144 struct nfs4_exception exception = { };
5145 int err;
5146 do {
5147 err = nfs4_handle_exception(NFS_SERVER(dir),
5148 _nfs4_proc_secinfo(dir, name, flavors),
5149 &exception);
5150 } while (exception.retry);
5151 return err;
5152}
5153
Andy Adamson557134a2009-04-01 09:21:53 -04005154#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005155/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005156 * Check the exchange flags returned by the server for invalid flags, having
5157 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5158 * DS flags set.
5159 */
5160static int nfs4_check_cl_exchange_flags(u32 flags)
5161{
5162 if (flags & ~EXCHGID4_FLAG_MASK_R)
5163 goto out_inval;
5164 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5165 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5166 goto out_inval;
5167 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5168 goto out_inval;
5169 return NFS_OK;
5170out_inval:
5171 return -NFS4ERR_INVAL;
5172}
5173
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005174static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005175nfs41_same_server_scope(struct nfs41_server_scope *a,
5176 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005177{
5178 if (a->server_scope_sz == b->server_scope_sz &&
5179 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5180 return true;
5181
5182 return false;
5183}
5184
Andy Adamson357f54d2010-12-14 10:11:57 -05005185/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005186 * nfs4_proc_bind_conn_to_session()
5187 *
5188 * The 4.1 client currently uses the same TCP connection for the
5189 * fore and backchannel.
5190 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005191int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005192{
5193 int status;
5194 struct nfs41_bind_conn_to_session_res res;
5195 struct rpc_message msg = {
5196 .rpc_proc =
5197 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5198 .rpc_argp = clp,
5199 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005200 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005201 };
5202
5203 dprintk("--> %s\n", __func__);
5204 BUG_ON(clp == NULL);
5205
5206 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5207 if (unlikely(res.session == NULL)) {
5208 status = -ENOMEM;
5209 goto out;
5210 }
5211
5212 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5213 if (status == 0) {
5214 if (memcmp(res.session->sess_id.data,
5215 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5216 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5217 status = -EIO;
5218 goto out_session;
5219 }
5220 if (res.dir != NFS4_CDFS4_BOTH) {
5221 dprintk("NFS: %s: Unexpected direction from server\n",
5222 __func__);
5223 status = -EIO;
5224 goto out_session;
5225 }
5226 if (res.use_conn_in_rdma_mode) {
5227 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5228 __func__);
5229 status = -EIO;
5230 goto out_session;
5231 }
5232 }
5233out_session:
5234 kfree(res.session);
5235out:
5236 dprintk("<-- %s status= %d\n", __func__, status);
5237 return status;
5238}
5239
5240/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005241 * nfs4_proc_exchange_id()
5242 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005243 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5244 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005245 * Since the clientid has expired, all compounds using sessions
5246 * associated with the stale clientid will be returning
5247 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5248 * be in some phase of session reset.
5249 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005250int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005251{
5252 nfs4_verifier verifier;
5253 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005254 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005255 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005256 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005257 };
5258 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005259 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005260 };
5261 int status;
5262 struct rpc_message msg = {
5263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5264 .rpc_argp = &args,
5265 .rpc_resp = &res,
5266 .rpc_cred = cred,
5267 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005268
Chuck Leverf0920752012-05-21 22:45:41 -04005269 nfs4_init_boot_verifier(clp, &verifier);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005270 args.id_len = scnprintf(args.id, sizeof(args.id),
Chuck Leverde734832012-07-11 16:30:50 -04005271 "%s/%s",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005272 clp->cl_ipaddr,
Chuck Leverde734832012-07-11 16:30:50 -04005273 clp->cl_rpcclient->cl_nodename);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005274 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5275 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5276 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005277
Chuck Leveracdeb692012-05-21 22:46:16 -04005278 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005279 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005280 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005281 status = -ENOMEM;
5282 goto out;
5283 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005284
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005285 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005286 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005287 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005288 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005289 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005290 }
5291
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005292 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005293 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005294 status = -ENOMEM;
5295 goto out_server_scope;
5296 }
5297
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005298 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005299 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005300 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005301
Chuck Lever177313f2012-05-21 22:44:58 -04005302 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005303 clp->cl_clientid = res.clientid;
5304 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5305 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5306 clp->cl_seqid = res.seqid;
5307
Chuck Leveracdeb692012-05-21 22:46:16 -04005308 kfree(clp->cl_serverowner);
5309 clp->cl_serverowner = res.server_owner;
5310 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005311
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005312 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005313 kfree(clp->cl_implid);
5314 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005315
Chuck Lever177313f2012-05-21 22:44:58 -04005316 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005317 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005318 res.server_scope)) {
5319 dprintk("%s: server_scope mismatch detected\n",
5320 __func__);
5321 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005322 kfree(clp->cl_serverscope);
5323 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005324 }
5325
Chuck Lever177313f2012-05-21 22:44:58 -04005326 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005327 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005328 goto out;
5329 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005330 } else
5331 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005332
Chuck Leveracdeb692012-05-21 22:46:16 -04005333out_server_owner:
5334 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005335out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005336 kfree(res.server_scope);
5337out:
Chuck Lever177313f2012-05-21 22:44:58 -04005338 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005339 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005340 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005341 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005342 clp->cl_implid->date.seconds,
5343 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005344 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005345 return status;
5346}
5347
Trond Myklebust66245532012-05-25 17:18:09 -04005348static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5349 struct rpc_cred *cred)
5350{
5351 struct rpc_message msg = {
5352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5353 .rpc_argp = clp,
5354 .rpc_cred = cred,
5355 };
5356 int status;
5357
5358 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5359 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005360 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005361 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5362 return status;
5363}
5364
5365static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5366 struct rpc_cred *cred)
5367{
5368 unsigned int loop;
5369 int ret;
5370
5371 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5372 ret = _nfs4_proc_destroy_clientid(clp, cred);
5373 switch (ret) {
5374 case -NFS4ERR_DELAY:
5375 case -NFS4ERR_CLIENTID_BUSY:
5376 ssleep(1);
5377 break;
5378 default:
5379 return ret;
5380 }
5381 }
5382 return 0;
5383}
5384
5385int nfs4_destroy_clientid(struct nfs_client *clp)
5386{
5387 struct rpc_cred *cred;
5388 int ret = 0;
5389
5390 if (clp->cl_mvops->minor_version < 1)
5391 goto out;
5392 if (clp->cl_exchange_flags == 0)
5393 goto out;
5394 cred = nfs4_get_exchange_id_cred(clp);
5395 ret = nfs4_proc_destroy_clientid(clp, cred);
5396 if (cred)
5397 put_rpccred(cred);
5398 switch (ret) {
5399 case 0:
5400 case -NFS4ERR_STALE_CLIENTID:
5401 clp->cl_exchange_flags = 0;
5402 }
5403out:
5404 return ret;
5405}
5406
Andy Adamson2050f0c2009-04-01 09:22:30 -04005407struct nfs4_get_lease_time_data {
5408 struct nfs4_get_lease_time_args *args;
5409 struct nfs4_get_lease_time_res *res;
5410 struct nfs_client *clp;
5411};
5412
5413static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5414 void *calldata)
5415{
5416 int ret;
5417 struct nfs4_get_lease_time_data *data =
5418 (struct nfs4_get_lease_time_data *)calldata;
5419
5420 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005421 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005422 /* just setup sequence, do not trigger session recovery
5423 since we're invoked within one */
5424 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005425 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005426 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005427
5428 BUG_ON(ret == -EAGAIN);
5429 rpc_call_start(task);
5430 dprintk("<-- %s\n", __func__);
5431}
5432
5433/*
5434 * Called from nfs4_state_manager thread for session setup, so don't recover
5435 * from sequence operation or clientid errors.
5436 */
5437static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5438{
5439 struct nfs4_get_lease_time_data *data =
5440 (struct nfs4_get_lease_time_data *)calldata;
5441
5442 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005443 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5444 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005445 switch (task->tk_status) {
5446 case -NFS4ERR_DELAY:
5447 case -NFS4ERR_GRACE:
5448 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5449 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5450 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005451 /* fall through */
5452 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005453 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005454 return;
5455 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005456 dprintk("<-- %s\n", __func__);
5457}
5458
Trond Myklebust17280172012-03-11 13:11:00 -04005459static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005460 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5461 .rpc_call_done = nfs4_get_lease_time_done,
5462};
5463
5464int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5465{
5466 struct rpc_task *task;
5467 struct nfs4_get_lease_time_args args;
5468 struct nfs4_get_lease_time_res res = {
5469 .lr_fsinfo = fsinfo,
5470 };
5471 struct nfs4_get_lease_time_data data = {
5472 .args = &args,
5473 .res = &res,
5474 .clp = clp,
5475 };
5476 struct rpc_message msg = {
5477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5478 .rpc_argp = &args,
5479 .rpc_resp = &res,
5480 };
5481 struct rpc_task_setup task_setup = {
5482 .rpc_client = clp->cl_rpcclient,
5483 .rpc_message = &msg,
5484 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005485 .callback_data = &data,
5486 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005487 };
5488 int status;
5489
Trond Myklebust9d12b212012-01-17 22:04:25 -05005490 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005491 dprintk("--> %s\n", __func__);
5492 task = rpc_run_task(&task_setup);
5493
5494 if (IS_ERR(task))
5495 status = PTR_ERR(task);
5496 else {
5497 status = task->tk_status;
5498 rpc_put_task(task);
5499 }
5500 dprintk("<-- %s return %d\n", __func__, status);
5501
5502 return status;
5503}
5504
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005505static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5506{
5507 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5508}
5509
5510static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5511 struct nfs4_slot *new,
5512 u32 max_slots,
5513 u32 ivalue)
5514{
5515 struct nfs4_slot *old = NULL;
5516 u32 i;
5517
5518 spin_lock(&tbl->slot_tbl_lock);
5519 if (new) {
5520 old = tbl->slots;
5521 tbl->slots = new;
5522 tbl->max_slots = max_slots;
5523 }
5524 tbl->highest_used_slotid = -1; /* no slot is currently used */
5525 for (i = 0; i < tbl->max_slots; i++)
5526 tbl->slots[i].seq_nr = ivalue;
5527 spin_unlock(&tbl->slot_tbl_lock);
5528 kfree(old);
5529}
5530
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005531/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005532 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005533 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005534static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5535 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005536{
Andy Adamson104aeba2010-01-14 17:45:10 -05005537 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005538 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005539
Andy Adamson104aeba2010-01-14 17:45:10 -05005540 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5541 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005542
Andy Adamson104aeba2010-01-14 17:45:10 -05005543 /* Does the newly negotiated max_reqs match the existing slot table? */
5544 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005545 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005546 if (!new)
5547 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005548 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005549 ret = 0;
5550
5551 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005552 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5553 tbl, tbl->slots, tbl->max_slots);
5554out:
5555 dprintk("<-- %s: return %d\n", __func__, ret);
5556 return ret;
5557}
5558
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005559/* Destroy the slot table */
5560static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5561{
5562 if (session->fc_slot_table.slots != NULL) {
5563 kfree(session->fc_slot_table.slots);
5564 session->fc_slot_table.slots = NULL;
5565 }
5566 if (session->bc_slot_table.slots != NULL) {
5567 kfree(session->bc_slot_table.slots);
5568 session->bc_slot_table.slots = NULL;
5569 }
5570 return;
5571}
5572
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005573/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005574 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005575 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005576static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005577{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005578 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005579 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005580
Andy Adamsonaacd5532011-11-09 13:58:21 -05005581 dprintk("--> %s\n", __func__);
5582 /* Fore channel */
5583 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005584 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5585 if (status) /* -ENOMEM */
5586 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005587 /* Back channel */
5588 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005589 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5590 if (status && tbl->slots == NULL)
5591 /* Fore and back channel share a connection so get
5592 * both slot tables or neither */
5593 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005594 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005595}
5596
5597struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5598{
5599 struct nfs4_session *session;
5600 struct nfs4_slot_table *tbl;
5601
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005602 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005603 if (!session)
5604 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005605
Andy Adamson557134a2009-04-01 09:21:53 -04005606 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005607 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005608 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005609 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005610 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005611
5612 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005613 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005614 spin_lock_init(&tbl->slot_tbl_lock);
5615 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005616 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005617
Trond Myklebust1055d762010-06-16 09:52:27 -04005618 session->session_state = 1<<NFS4_SESSION_INITING;
5619
Andy Adamson557134a2009-04-01 09:21:53 -04005620 session->clp = clp;
5621 return session;
5622}
5623
5624void nfs4_destroy_session(struct nfs4_session *session)
5625{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005626 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005627 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005628
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005629 cred = nfs4_get_exchange_id_cred(session->clp);
5630 nfs4_proc_destroy_session(session, cred);
5631 if (cred)
5632 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005633
5634 rcu_read_lock();
5635 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5636 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005637 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005638 __func__, xprt);
5639 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005640 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005641 kfree(session);
5642}
5643
Andy Adamsonfc931582009-04-01 09:22:31 -04005644/*
5645 * Initialize the values to be used by the client in CREATE_SESSION
5646 * If nfs4_init_session set the fore channel request and response sizes,
5647 * use them.
5648 *
5649 * Set the back channel max_resp_sz_cached to zero to force the client to
5650 * always set csa_cachethis to FALSE because the current implementation
5651 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5652 */
5653static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5654{
5655 struct nfs4_session *session = args->client->cl_session;
5656 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5657 mxresp_sz = session->fc_attrs.max_resp_sz;
5658
5659 if (mxrqst_sz == 0)
5660 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5661 if (mxresp_sz == 0)
5662 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5663 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005664 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5665 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005666 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005667 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005668
5669 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005670 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005671 __func__,
5672 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005673 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005674
5675 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005676 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5677 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5678 args->bc_attrs.max_resp_sz_cached = 0;
5679 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5680 args->bc_attrs.max_reqs = 1;
5681
5682 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5683 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5684 __func__,
5685 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5686 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5687 args->bc_attrs.max_reqs);
5688}
5689
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005690static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005691{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005692 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5693 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5694
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005695 if (rcvd->max_resp_sz > sent->max_resp_sz)
5696 return -EINVAL;
5697 /*
5698 * Our requested max_ops is the minimum we need; we're not
5699 * prepared to break up compounds into smaller pieces than that.
5700 * So, no point even trying to continue if the server won't
5701 * cooperate:
5702 */
5703 if (rcvd->max_ops < sent->max_ops)
5704 return -EINVAL;
5705 if (rcvd->max_reqs == 0)
5706 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005707 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5708 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005709 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005710}
5711
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005712static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5713{
5714 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5715 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005716
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005717 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5718 return -EINVAL;
5719 if (rcvd->max_resp_sz < sent->max_resp_sz)
5720 return -EINVAL;
5721 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5722 return -EINVAL;
5723 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005724 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005725 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005726 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005727 return -EINVAL;
5728 return 0;
5729}
Andy Adamson8d353012009-04-01 09:22:32 -04005730
Andy Adamson8d353012009-04-01 09:22:32 -04005731static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5732 struct nfs4_session *session)
5733{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005734 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005735
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005736 ret = nfs4_verify_fore_channel_attrs(args, session);
5737 if (ret)
5738 return ret;
5739 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005740}
5741
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005742static int _nfs4_proc_create_session(struct nfs_client *clp,
5743 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005744{
5745 struct nfs4_session *session = clp->cl_session;
5746 struct nfs41_create_session_args args = {
5747 .client = clp,
5748 .cb_program = NFS4_CALLBACK,
5749 };
5750 struct nfs41_create_session_res res = {
5751 .client = clp,
5752 };
5753 struct rpc_message msg = {
5754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5755 .rpc_argp = &args,
5756 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005757 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005758 };
5759 int status;
5760
5761 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005762 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005763
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005764 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005765
Andy Adamson8d353012009-04-01 09:22:32 -04005766 if (!status)
5767 /* Verify the session's negotiated channel_attrs values */
5768 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005769 if (!status) {
5770 /* Increment the clientid slot sequence id */
5771 clp->cl_seqid++;
5772 }
5773
5774 return status;
5775}
5776
5777/*
5778 * Issues a CREATE_SESSION operation to the server.
5779 * It is the responsibility of the caller to verify the session is
5780 * expired before calling this routine.
5781 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005782int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005783{
5784 int status;
5785 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005786 struct nfs4_session *session = clp->cl_session;
5787
5788 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5789
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005790 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005791 if (status)
5792 goto out;
5793
Andy Adamsonaacd5532011-11-09 13:58:21 -05005794 /* Init or reset the session slot tables */
5795 status = nfs4_setup_session_slot_tables(session);
5796 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005797 if (status)
5798 goto out;
5799
5800 ptr = (unsigned *)&session->sess_id.data[0];
5801 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5802 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005803out:
5804 dprintk("<-- %s\n", __func__);
5805 return status;
5806}
5807
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005808/*
5809 * Issue the over-the-wire RPC DESTROY_SESSION.
5810 * The caller must serialize access to this routine.
5811 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005812int nfs4_proc_destroy_session(struct nfs4_session *session,
5813 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005814{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005815 struct rpc_message msg = {
5816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5817 .rpc_argp = session,
5818 .rpc_cred = cred,
5819 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005820 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005821
5822 dprintk("--> nfs4_proc_destroy_session\n");
5823
5824 /* session is still being setup */
5825 if (session->clp->cl_cons_state != NFS_CS_READY)
5826 return status;
5827
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005828 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005829
5830 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005831 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005832 "Session has been destroyed regardless...\n", status);
5833
5834 dprintk("<-- nfs4_proc_destroy_session\n");
5835 return status;
5836}
5837
Trond Myklebust7b38c362012-05-23 13:23:31 -04005838/*
5839 * With sessions, the client is not marked ready until after a
5840 * successful EXCHANGE_ID and CREATE_SESSION.
5841 *
5842 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5843 * other versions of NFS can be tried.
5844 */
5845static int nfs41_check_session_ready(struct nfs_client *clp)
5846{
5847 int ret;
5848
5849 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5850 ret = nfs4_client_recover_expired_lease(clp);
5851 if (ret)
5852 return ret;
5853 }
5854 if (clp->cl_cons_state < NFS_CS_READY)
5855 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005856 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005857 return 0;
5858}
5859
Trond Myklebustfccba802009-07-21 16:48:07 -04005860int nfs4_init_session(struct nfs_server *server)
5861{
5862 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005863 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005864 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005865
5866 if (!nfs4_has_session(clp))
5867 return 0;
5868
Trond Myklebust1055d762010-06-16 09:52:27 -04005869 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005870 spin_lock(&clp->cl_lock);
5871 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005872
Trond Myklebust7b38c362012-05-23 13:23:31 -04005873 rsize = server->rsize;
5874 if (rsize == 0)
5875 rsize = NFS_MAX_FILE_IO_SIZE;
5876 wsize = server->wsize;
5877 if (wsize == 0)
5878 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005879
Trond Myklebust7b38c362012-05-23 13:23:31 -04005880 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5881 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5882 }
5883 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005884
Trond Myklebust7b38c362012-05-23 13:23:31 -04005885 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005886}
5887
Trond Myklebust7b38c362012-05-23 13:23:31 -04005888int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005889{
5890 struct nfs4_session *session = clp->cl_session;
5891 int ret;
5892
Trond Myklebust7b38c362012-05-23 13:23:31 -04005893 spin_lock(&clp->cl_lock);
5894 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5895 /*
5896 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5897 * DS lease to be equal to the MDS lease.
5898 */
5899 clp->cl_lease_time = lease_time;
5900 clp->cl_last_renewal = jiffies;
5901 }
5902 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005903
Trond Myklebust7b38c362012-05-23 13:23:31 -04005904 ret = nfs41_check_session_ready(clp);
5905 if (ret)
5906 return ret;
5907 /* Test for the DS role */
5908 if (!is_ds_client(clp))
5909 return -ENODEV;
5910 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005911}
5912EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5913
5914
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005915/*
5916 * Renew the cl_session lease.
5917 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005918struct nfs4_sequence_data {
5919 struct nfs_client *clp;
5920 struct nfs4_sequence_args args;
5921 struct nfs4_sequence_res res;
5922};
5923
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005924static void nfs41_sequence_release(void *data)
5925{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005926 struct nfs4_sequence_data *calldata = data;
5927 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005928
Alexandros Batsakis71358402010-02-05 03:45:05 -08005929 if (atomic_read(&clp->cl_count) > 1)
5930 nfs4_schedule_state_renewal(clp);
5931 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005932 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005933}
5934
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005935static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5936{
5937 switch(task->tk_status) {
5938 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005939 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5940 return -EAGAIN;
5941 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005942 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005943 }
5944 return 0;
5945}
5946
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005947static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005948{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005949 struct nfs4_sequence_data *calldata = data;
5950 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005951
Trond Myklebust14516c32010-07-31 14:29:06 -04005952 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5953 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005954
5955 if (task->tk_status < 0) {
5956 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005957 if (atomic_read(&clp->cl_count) == 1)
5958 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005959
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005960 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5961 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005962 return;
5963 }
5964 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005965 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005966out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005967 dprintk("<-- %s\n", __func__);
5968}
5969
5970static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5971{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005972 struct nfs4_sequence_data *calldata = data;
5973 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005974 struct nfs4_sequence_args *args;
5975 struct nfs4_sequence_res *res;
5976
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005977 args = task->tk_msg.rpc_argp;
5978 res = task->tk_msg.rpc_resp;
5979
Trond Myklebust9d12b212012-01-17 22:04:25 -05005980 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005981 return;
5982 rpc_call_start(task);
5983}
5984
5985static const struct rpc_call_ops nfs41_sequence_ops = {
5986 .rpc_call_done = nfs41_sequence_call_done,
5987 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005988 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005989};
5990
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005991static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005992{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005993 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005994 struct rpc_message msg = {
5995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5996 .rpc_cred = cred,
5997 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005998 struct rpc_task_setup task_setup_data = {
5999 .rpc_client = clp->cl_rpcclient,
6000 .rpc_message = &msg,
6001 .callback_ops = &nfs41_sequence_ops,
6002 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6003 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006004
Alexandros Batsakis71358402010-02-05 03:45:05 -08006005 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006006 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006007 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006008 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006009 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006010 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006011 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006012 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006013 msg.rpc_argp = &calldata->args;
6014 msg.rpc_resp = &calldata->res;
6015 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006016 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006017
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006018 return rpc_run_task(&task_setup_data);
6019}
6020
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006021static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006022{
6023 struct rpc_task *task;
6024 int ret = 0;
6025
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006026 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6027 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006028 task = _nfs41_proc_sequence(clp, cred);
6029 if (IS_ERR(task))
6030 ret = PTR_ERR(task);
6031 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006032 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006033 dprintk("<-- %s status=%d\n", __func__, ret);
6034 return ret;
6035}
6036
6037static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6038{
6039 struct rpc_task *task;
6040 int ret;
6041
6042 task = _nfs41_proc_sequence(clp, cred);
6043 if (IS_ERR(task)) {
6044 ret = PTR_ERR(task);
6045 goto out;
6046 }
6047 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006048 if (!ret) {
6049 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6050
6051 if (task->tk_status == 0)
6052 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006053 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006054 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006055 rpc_put_task(task);
6056out:
6057 dprintk("<-- %s status=%d\n", __func__, ret);
6058 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006059}
6060
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006061struct nfs4_reclaim_complete_data {
6062 struct nfs_client *clp;
6063 struct nfs41_reclaim_complete_args arg;
6064 struct nfs41_reclaim_complete_res res;
6065};
6066
6067static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6068{
6069 struct nfs4_reclaim_complete_data *calldata = data;
6070
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006071 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006072 if (nfs41_setup_sequence(calldata->clp->cl_session,
6073 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006074 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006075 return;
6076
6077 rpc_call_start(task);
6078}
6079
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006080static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6081{
6082 switch(task->tk_status) {
6083 case 0:
6084 case -NFS4ERR_COMPLETE_ALREADY:
6085 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6086 break;
6087 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006088 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006089 /* fall through */
6090 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006091 return -EAGAIN;
6092 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006093 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006094 }
6095 return 0;
6096}
6097
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006098static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6099{
6100 struct nfs4_reclaim_complete_data *calldata = data;
6101 struct nfs_client *clp = calldata->clp;
6102 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6103
6104 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006105 if (!nfs41_sequence_done(task, res))
6106 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006107
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006108 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6109 rpc_restart_call_prepare(task);
6110 return;
6111 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006112 dprintk("<-- %s\n", __func__);
6113}
6114
6115static void nfs4_free_reclaim_complete_data(void *data)
6116{
6117 struct nfs4_reclaim_complete_data *calldata = data;
6118
6119 kfree(calldata);
6120}
6121
6122static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6123 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6124 .rpc_call_done = nfs4_reclaim_complete_done,
6125 .rpc_release = nfs4_free_reclaim_complete_data,
6126};
6127
6128/*
6129 * Issue a global reclaim complete.
6130 */
6131static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6132{
6133 struct nfs4_reclaim_complete_data *calldata;
6134 struct rpc_task *task;
6135 struct rpc_message msg = {
6136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6137 };
6138 struct rpc_task_setup task_setup_data = {
6139 .rpc_client = clp->cl_rpcclient,
6140 .rpc_message = &msg,
6141 .callback_ops = &nfs4_reclaim_complete_call_ops,
6142 .flags = RPC_TASK_ASYNC,
6143 };
6144 int status = -ENOMEM;
6145
6146 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006147 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006148 if (calldata == NULL)
6149 goto out;
6150 calldata->clp = clp;
6151 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006152
Trond Myklebust9d12b212012-01-17 22:04:25 -05006153 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006154 msg.rpc_argp = &calldata->arg;
6155 msg.rpc_resp = &calldata->res;
6156 task_setup_data.callback_data = calldata;
6157 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006158 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006159 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006160 goto out;
6161 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006162 status = nfs4_wait_for_completion_rpc_task(task);
6163 if (status == 0)
6164 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006165 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006166 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006167out:
6168 dprintk("<-- %s status=%d\n", __func__, status);
6169 return status;
6170}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006171
6172static void
6173nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6174{
6175 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006176 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006177
6178 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006179 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6180 * right now covering the LAYOUTGET we are about to send.
6181 * However, that is not so catastrophic, and there seems
6182 * to be no way to prevent it completely.
6183 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006184 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006185 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006186 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006187 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6188 NFS_I(lgp->args.inode)->layout,
6189 lgp->args.ctx->state)) {
6190 rpc_exit(task, NFS4_OK);
6191 return;
6192 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006193 rpc_call_start(task);
6194}
6195
6196static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6197{
6198 struct nfs4_layoutget *lgp = calldata;
6199 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6200
6201 dprintk("--> %s\n", __func__);
6202
6203 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6204 return;
6205
6206 switch (task->tk_status) {
6207 case 0:
6208 break;
6209 case -NFS4ERR_LAYOUTTRYLATER:
6210 case -NFS4ERR_RECALLCONFLICT:
6211 task->tk_status = -NFS4ERR_DELAY;
6212 /* Fall through */
6213 default:
6214 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6215 rpc_restart_call_prepare(task);
6216 return;
6217 }
6218 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006219 dprintk("<-- %s\n", __func__);
6220}
6221
Idan Kedar85541162012-08-02 11:47:10 +03006222static size_t max_response_pages(struct nfs_server *server)
6223{
6224 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6225 return nfs_page_array_len(0, max_resp_sz);
6226}
6227
6228static void nfs4_free_pages(struct page **pages, size_t size)
6229{
6230 int i;
6231
6232 if (!pages)
6233 return;
6234
6235 for (i = 0; i < size; i++) {
6236 if (!pages[i])
6237 break;
6238 __free_page(pages[i]);
6239 }
6240 kfree(pages);
6241}
6242
6243static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6244{
6245 struct page **pages;
6246 int i;
6247
6248 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6249 if (!pages) {
6250 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6251 return NULL;
6252 }
6253
6254 for (i = 0; i < size; i++) {
6255 pages[i] = alloc_page(gfp_flags);
6256 if (!pages[i]) {
6257 dprintk("%s: failed to allocate page\n", __func__);
6258 nfs4_free_pages(pages, size);
6259 return NULL;
6260 }
6261 }
6262
6263 return pages;
6264}
6265
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006266static void nfs4_layoutget_release(void *calldata)
6267{
6268 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006269 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6270 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006271
6272 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006273 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006274 put_nfs_open_context(lgp->args.ctx);
6275 kfree(calldata);
6276 dprintk("<-- %s\n", __func__);
6277}
6278
6279static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6280 .rpc_call_prepare = nfs4_layoutget_prepare,
6281 .rpc_call_done = nfs4_layoutget_done,
6282 .rpc_release = nfs4_layoutget_release,
6283};
6284
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006285struct pnfs_layout_segment *
6286nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006287{
6288 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006289 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006290 struct rpc_task *task;
6291 struct rpc_message msg = {
6292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6293 .rpc_argp = &lgp->args,
6294 .rpc_resp = &lgp->res,
6295 };
6296 struct rpc_task_setup task_setup_data = {
6297 .rpc_client = server->client,
6298 .rpc_message = &msg,
6299 .callback_ops = &nfs4_layoutget_call_ops,
6300 .callback_data = lgp,
6301 .flags = RPC_TASK_ASYNC,
6302 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006303 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006304 int status = 0;
6305
6306 dprintk("--> %s\n", __func__);
6307
Idan Kedar85541162012-08-02 11:47:10 +03006308 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6309 if (!lgp->args.layout.pages) {
6310 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006311 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006312 }
6313 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6314
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006315 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006316 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006317 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006318 task = rpc_run_task(&task_setup_data);
6319 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006320 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006321 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006322 if (status == 0)
6323 status = task->tk_status;
6324 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006325 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006326 rpc_put_task(task);
6327 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006328 if (status)
6329 return ERR_PTR(status);
6330 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006331}
6332
Benny Halevycbe82602011-05-22 19:52:37 +03006333static void
6334nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6335{
6336 struct nfs4_layoutreturn *lrp = calldata;
6337
6338 dprintk("--> %s\n", __func__);
6339 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006340 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006341 return;
6342 rpc_call_start(task);
6343}
6344
6345static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6346{
6347 struct nfs4_layoutreturn *lrp = calldata;
6348 struct nfs_server *server;
6349
6350 dprintk("--> %s\n", __func__);
6351
6352 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6353 return;
6354
6355 server = NFS_SERVER(lrp->args.inode);
6356 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006357 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006358 return;
6359 }
Benny Halevycbe82602011-05-22 19:52:37 +03006360 dprintk("<-- %s\n", __func__);
6361}
6362
6363static void nfs4_layoutreturn_release(void *calldata)
6364{
6365 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006366 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006367
6368 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006369 spin_lock(&lo->plh_inode->i_lock);
6370 if (lrp->res.lrs_present)
6371 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6372 lo->plh_block_lgets--;
6373 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006374 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006375 kfree(calldata);
6376 dprintk("<-- %s\n", __func__);
6377}
6378
6379static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6380 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6381 .rpc_call_done = nfs4_layoutreturn_done,
6382 .rpc_release = nfs4_layoutreturn_release,
6383};
6384
6385int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6386{
6387 struct rpc_task *task;
6388 struct rpc_message msg = {
6389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6390 .rpc_argp = &lrp->args,
6391 .rpc_resp = &lrp->res,
6392 };
6393 struct rpc_task_setup task_setup_data = {
6394 .rpc_client = lrp->clp->cl_rpcclient,
6395 .rpc_message = &msg,
6396 .callback_ops = &nfs4_layoutreturn_call_ops,
6397 .callback_data = lrp,
6398 };
6399 int status;
6400
6401 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006402 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006403 task = rpc_run_task(&task_setup_data);
6404 if (IS_ERR(task))
6405 return PTR_ERR(task);
6406 status = task->tk_status;
6407 dprintk("<-- %s status=%d\n", __func__, status);
6408 rpc_put_task(task);
6409 return status;
6410}
6411
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006412/*
6413 * Retrieve the list of Data Server devices from the MDS.
6414 */
6415static int _nfs4_getdevicelist(struct nfs_server *server,
6416 const struct nfs_fh *fh,
6417 struct pnfs_devicelist *devlist)
6418{
6419 struct nfs4_getdevicelist_args args = {
6420 .fh = fh,
6421 .layoutclass = server->pnfs_curr_ld->id,
6422 };
6423 struct nfs4_getdevicelist_res res = {
6424 .devlist = devlist,
6425 };
6426 struct rpc_message msg = {
6427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6428 .rpc_argp = &args,
6429 .rpc_resp = &res,
6430 };
6431 int status;
6432
6433 dprintk("--> %s\n", __func__);
6434 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6435 &res.seq_res, 0);
6436 dprintk("<-- %s status=%d\n", __func__, status);
6437 return status;
6438}
6439
6440int nfs4_proc_getdevicelist(struct nfs_server *server,
6441 const struct nfs_fh *fh,
6442 struct pnfs_devicelist *devlist)
6443{
6444 struct nfs4_exception exception = { };
6445 int err;
6446
6447 do {
6448 err = nfs4_handle_exception(server,
6449 _nfs4_getdevicelist(server, fh, devlist),
6450 &exception);
6451 } while (exception.retry);
6452
6453 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6454 err, devlist->num_devs);
6455
6456 return err;
6457}
6458EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6459
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006460static int
6461_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6462{
6463 struct nfs4_getdeviceinfo_args args = {
6464 .pdev = pdev,
6465 };
6466 struct nfs4_getdeviceinfo_res res = {
6467 .pdev = pdev,
6468 };
6469 struct rpc_message msg = {
6470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6471 .rpc_argp = &args,
6472 .rpc_resp = &res,
6473 };
6474 int status;
6475
6476 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006477 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006478 dprintk("<-- %s status=%d\n", __func__, status);
6479
6480 return status;
6481}
6482
6483int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6484{
6485 struct nfs4_exception exception = { };
6486 int err;
6487
6488 do {
6489 err = nfs4_handle_exception(server,
6490 _nfs4_proc_getdeviceinfo(server, pdev),
6491 &exception);
6492 } while (exception.retry);
6493 return err;
6494}
6495EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6496
Andy Adamson863a3c62011-03-23 13:27:54 +00006497static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6498{
6499 struct nfs4_layoutcommit_data *data = calldata;
6500 struct nfs_server *server = NFS_SERVER(data->args.inode);
6501
6502 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006503 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006504 return;
6505 rpc_call_start(task);
6506}
6507
6508static void
6509nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6510{
6511 struct nfs4_layoutcommit_data *data = calldata;
6512 struct nfs_server *server = NFS_SERVER(data->args.inode);
6513
6514 if (!nfs4_sequence_done(task, &data->res.seq_res))
6515 return;
6516
6517 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006518 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6519 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6520 case -NFS4ERR_BADLAYOUT: /* no layout */
6521 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006522 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006523 break;
6524 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006525 nfs_post_op_update_inode_force_wcc(data->args.inode,
6526 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006527 break;
6528 default:
6529 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6530 rpc_restart_call_prepare(task);
6531 return;
6532 }
6533 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006534}
6535
6536static void nfs4_layoutcommit_release(void *calldata)
6537{
6538 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006539 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006540 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006541
Andy Adamsondb29c082011-07-30 20:52:38 -04006542 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006543 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006544 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6545 list_del_init(&lseg->pls_lc_list);
6546 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6547 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006548 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006549 }
Peng Tao92407e72011-10-23 20:21:17 -07006550
6551 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6552 smp_mb__after_clear_bit();
6553 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6554
Andy Adamson863a3c62011-03-23 13:27:54 +00006555 put_rpccred(data->cred);
6556 kfree(data);
6557}
6558
6559static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6560 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6561 .rpc_call_done = nfs4_layoutcommit_done,
6562 .rpc_release = nfs4_layoutcommit_release,
6563};
6564
6565int
Andy Adamsonef311532011-03-12 02:58:10 -05006566nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006567{
6568 struct rpc_message msg = {
6569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6570 .rpc_argp = &data->args,
6571 .rpc_resp = &data->res,
6572 .rpc_cred = data->cred,
6573 };
6574 struct rpc_task_setup task_setup_data = {
6575 .task = &data->task,
6576 .rpc_client = NFS_CLIENT(data->args.inode),
6577 .rpc_message = &msg,
6578 .callback_ops = &nfs4_layoutcommit_ops,
6579 .callback_data = data,
6580 .flags = RPC_TASK_ASYNC,
6581 };
6582 struct rpc_task *task;
6583 int status = 0;
6584
6585 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6586 "lbw: %llu inode %lu\n",
6587 data->task.tk_pid, sync,
6588 data->args.lastbytewritten,
6589 data->args.inode->i_ino);
6590
Trond Myklebust9d12b212012-01-17 22:04:25 -05006591 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006592 task = rpc_run_task(&task_setup_data);
6593 if (IS_ERR(task))
6594 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006595 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006596 goto out;
6597 status = nfs4_wait_for_completion_rpc_task(task);
6598 if (status != 0)
6599 goto out;
6600 status = task->tk_status;
6601out:
6602 dprintk("%s: status %d\n", __func__, status);
6603 rpc_put_task(task);
6604 return status;
6605}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006606
6607static int
6608_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6609 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6610{
6611 struct nfs41_secinfo_no_name_args args = {
6612 .style = SECINFO_STYLE_CURRENT_FH,
6613 };
6614 struct nfs4_secinfo_res res = {
6615 .flavors = flavors,
6616 };
6617 struct rpc_message msg = {
6618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6619 .rpc_argp = &args,
6620 .rpc_resp = &res,
6621 };
6622 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6623}
6624
6625static int
6626nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6627 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6628{
6629 struct nfs4_exception exception = { };
6630 int err;
6631 do {
6632 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6633 switch (err) {
6634 case 0:
6635 case -NFS4ERR_WRONGSEC:
6636 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006637 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006638 default:
6639 err = nfs4_handle_exception(server, err, &exception);
6640 }
6641 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006642out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006643 return err;
6644}
6645
6646static int
6647nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6648 struct nfs_fsinfo *info)
6649{
6650 int err;
6651 struct page *page;
6652 rpc_authflavor_t flavor;
6653 struct nfs4_secinfo_flavors *flavors;
6654
6655 page = alloc_page(GFP_KERNEL);
6656 if (!page) {
6657 err = -ENOMEM;
6658 goto out;
6659 }
6660
6661 flavors = page_address(page);
6662 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6663
6664 /*
6665 * Fall back on "guess and check" method if
6666 * the server doesn't support SECINFO_NO_NAME
6667 */
6668 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6669 err = nfs4_find_root_sec(server, fhandle, info);
6670 goto out_freepage;
6671 }
6672 if (err)
6673 goto out_freepage;
6674
6675 flavor = nfs_find_best_sec(flavors);
6676 if (err == 0)
6677 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6678
6679out_freepage:
6680 put_page(page);
6681 if (err == -EACCES)
6682 return -EPERM;
6683out:
6684 return err;
6685}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006686
6687static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006688{
6689 int status;
6690 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006691 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006692 };
6693 struct nfs41_test_stateid_res res;
6694 struct rpc_message msg = {
6695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6696 .rpc_argp = &args,
6697 .rpc_resp = &res,
6698 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006699
Chuck Lever38527b12012-07-11 16:30:23 -04006700 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006701 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006702 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006703 if (status != NFS_OK) {
6704 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006705 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006706 }
6707 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006708 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006709}
6710
Chuck Lever38527b12012-07-11 16:30:23 -04006711/**
6712 * nfs41_test_stateid - perform a TEST_STATEID operation
6713 *
6714 * @server: server / transport on which to perform the operation
6715 * @stateid: state ID to test
6716 *
6717 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6718 * Otherwise a negative NFS4ERR value is returned if the operation
6719 * failed or the state ID is not currently valid.
6720 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006721static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006722{
6723 struct nfs4_exception exception = { };
6724 int err;
6725 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006726 err = _nfs41_test_stateid(server, stateid);
6727 if (err != -NFS4ERR_DELAY)
6728 break;
6729 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006730 } while (exception.retry);
6731 return err;
6732}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006733
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006734static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006735{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006736 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006737 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006738 };
6739 struct nfs41_free_stateid_res res;
6740 struct rpc_message msg = {
6741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6742 .rpc_argp = &args,
6743 .rpc_resp = &res,
6744 };
Chuck Lever38527b12012-07-11 16:30:23 -04006745 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006746
Chuck Lever38527b12012-07-11 16:30:23 -04006747 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006748 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006749 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006750 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006751 dprintk("NFS reply free_stateid: %d\n", status);
6752 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006753}
6754
Chuck Lever38527b12012-07-11 16:30:23 -04006755/**
6756 * nfs41_free_stateid - perform a FREE_STATEID operation
6757 *
6758 * @server: server / transport on which to perform the operation
6759 * @stateid: state ID to release
6760 *
6761 * Returns NFS_OK if the server freed "stateid". Otherwise a
6762 * negative NFS4ERR value is returned.
6763 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006764static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006765{
6766 struct nfs4_exception exception = { };
6767 int err;
6768 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006769 err = _nfs4_free_stateid(server, stateid);
6770 if (err != -NFS4ERR_DELAY)
6771 break;
6772 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006773 } while (exception.retry);
6774 return err;
6775}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006776
6777static bool nfs41_match_stateid(const nfs4_stateid *s1,
6778 const nfs4_stateid *s2)
6779{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006780 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006781 return false;
6782
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006783 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006784 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006785 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006786 return true;
6787
6788 return false;
6789}
6790
Andy Adamson557134a2009-04-01 09:21:53 -04006791#endif /* CONFIG_NFS_V4_1 */
6792
Trond Myklebust36281ca2012-03-04 18:13:56 -05006793static bool nfs4_match_stateid(const nfs4_stateid *s1,
6794 const nfs4_stateid *s2)
6795{
Trond Myklebustf597c532012-03-04 18:13:56 -05006796 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006797}
6798
6799
Trond Myklebust17280172012-03-11 13:11:00 -04006800static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006801 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006802 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 .recover_open = nfs4_open_reclaim,
6804 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006805 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006806 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807};
6808
Andy Adamson591d71c2009-04-01 09:22:47 -04006809#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006810static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006811 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6812 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6813 .recover_open = nfs4_open_reclaim,
6814 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006815 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006816 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006817 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006818};
6819#endif /* CONFIG_NFS_V4_1 */
6820
Trond Myklebust17280172012-03-11 13:11:00 -04006821static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006822 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006823 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 .recover_open = nfs4_open_expired,
6825 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006826 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006827 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828};
6829
Andy Adamson591d71c2009-04-01 09:22:47 -04006830#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006831static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006832 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6833 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006834 .recover_open = nfs41_open_expired,
6835 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006836 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006837 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006838};
6839#endif /* CONFIG_NFS_V4_1 */
6840
Trond Myklebust17280172012-03-11 13:11:00 -04006841static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006842 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006843 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006844 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006845};
6846
6847#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006848static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006849 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006850 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006851 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006852};
6853#endif
6854
Trond Myklebust97dc1352010-06-16 09:52:26 -04006855static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6856 .minor_version = 0,
6857 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006858 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006859 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006860 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6861 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6862 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006863};
6864
6865#if defined(CONFIG_NFS_V4_1)
6866static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6867 .minor_version = 1,
6868 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006869 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006870 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006871 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6872 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6873 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006874};
6875#endif
6876
Trond Myklebust97dc1352010-06-16 09:52:26 -04006877const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6878 [0] = &nfs_v4_0_minor_ops,
6879#if defined(CONFIG_NFS_V4_1)
6880 [1] = &nfs_v4_1_minor_ops,
6881#endif
6882};
6883
Bryan Schumaker73a79702012-07-16 16:39:12 -04006884const struct inode_operations nfs4_dir_inode_operations = {
6885 .create = nfs_create,
6886 .lookup = nfs_lookup,
6887 .atomic_open = nfs_atomic_open,
6888 .link = nfs_link,
6889 .unlink = nfs_unlink,
6890 .symlink = nfs_symlink,
6891 .mkdir = nfs_mkdir,
6892 .rmdir = nfs_rmdir,
6893 .mknod = nfs_mknod,
6894 .rename = nfs_rename,
6895 .permission = nfs_permission,
6896 .getattr = nfs_getattr,
6897 .setattr = nfs_setattr,
6898 .getxattr = generic_getxattr,
6899 .setxattr = generic_setxattr,
6900 .listxattr = generic_listxattr,
6901 .removexattr = generic_removexattr,
6902};
6903
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006904static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006905 .permission = nfs_permission,
6906 .getattr = nfs_getattr,
6907 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006908 .getxattr = generic_getxattr,
6909 .setxattr = generic_setxattr,
6910 .listxattr = generic_listxattr,
6911 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006912};
6913
David Howells509de812006-08-22 20:06:11 -04006914const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 .version = 4, /* protocol version */
6916 .dentry_ops = &nfs4_dentry_operations,
6917 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006918 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006919 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006921 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006922 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 .getattr = nfs4_proc_getattr,
6924 .setattr = nfs4_proc_setattr,
6925 .lookup = nfs4_proc_lookup,
6926 .access = nfs4_proc_access,
6927 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 .create = nfs4_proc_create,
6929 .remove = nfs4_proc_remove,
6930 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006931 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 .unlink_done = nfs4_proc_unlink_done,
6933 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006934 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006935 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006936 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 .link = nfs4_proc_link,
6938 .symlink = nfs4_proc_symlink,
6939 .mkdir = nfs4_proc_mkdir,
6940 .rmdir = nfs4_proc_remove,
6941 .readdir = nfs4_proc_readdir,
6942 .mknod = nfs4_proc_mknod,
6943 .statfs = nfs4_proc_statfs,
6944 .fsinfo = nfs4_proc_fsinfo,
6945 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006946 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 .decode_dirent = nfs4_decode_dirent,
6948 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006949 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006950 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006951 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006953 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006954 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006955 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006957 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006958 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006960 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006961 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006962 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006963 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006964 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006965 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006966 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006967 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006968 .create_server = nfs4_create_server,
6969 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970};
6971
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006972static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6973 .prefix = XATTR_NAME_NFSV4_ACL,
6974 .list = nfs4_xattr_list_nfs4_acl,
6975 .get = nfs4_xattr_get_nfs4_acl,
6976 .set = nfs4_xattr_set_nfs4_acl,
6977};
6978
6979const struct xattr_handler *nfs4_xattr_handlers[] = {
6980 &nfs4_xattr_nfs4_acl_handler,
6981 NULL
6982};
6983
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984/*
6985 * Local variables:
6986 * c-basic-offset: 8
6987 * End:
6988 */