blob: 6b94f2d52532479f66acad01772433a33905e10d [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) {
2016 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2017 current->files, current->tgid);
2018 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2019 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002020 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002021 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002022 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Bryan Schumaker7c513052011-03-24 17:12:24 +00002024 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002025 if (status == 0 && state != NULL)
2026 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002027 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002030static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2031 struct nfs_fattr *fattr, struct iattr *sattr,
2032 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002034 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002035 struct nfs4_exception exception = {
2036 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002037 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 int err;
2040 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002041 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2042 switch (err) {
2043 case -NFS4ERR_OPENMODE:
2044 if (state && !(state->state & FMODE_WRITE)) {
2045 err = -EBADF;
2046 if (sattr->ia_valid & ATTR_OPEN)
2047 err = -EACCES;
2048 goto out;
2049 }
2050 }
2051 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002053out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return err;
2055}
2056
2057struct nfs4_closedata {
2058 struct inode *inode;
2059 struct nfs4_state *state;
2060 struct nfs_closeargs arg;
2061 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002062 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002063 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002064 bool roc;
2065 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066};
2067
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002068static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002069{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002070 struct nfs4_closedata *calldata = data;
2071 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002072 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002073
Fred Isamanf7e89172011-01-06 11:36:32 +00002074 if (calldata->roc)
2075 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002076 nfs4_put_open_state(calldata->state);
2077 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002078 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002079 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002080 kfree(calldata);
2081}
2082
Trond Myklebust88069f72009-12-08 08:33:16 -05002083static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2084 fmode_t fmode)
2085{
2086 spin_lock(&state->owner->so_lock);
2087 if (!(fmode & FMODE_READ))
2088 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2089 if (!(fmode & FMODE_WRITE))
2090 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2091 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2092 spin_unlock(&state->owner->so_lock);
2093}
2094
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002095static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002097 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 struct nfs_server *server = NFS_SERVER(calldata->inode);
2100
Chuck Levera3ca5652012-03-01 17:00:40 -05002101 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002102 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2103 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /* hmm. we are done with the inode, and in the process of freeing
2105 * the state_owner. we keep this around to process errors
2106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 switch (task->tk_status) {
2108 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002109 if (calldata->roc)
2110 pnfs_roc_set_barrier(state->inode,
2111 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002112 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002113 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002114 nfs4_close_clear_stateid_flags(state,
2115 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 break;
2117 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002118 case -NFS4ERR_OLD_STATEID:
2119 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002121 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002122 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002124 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2125 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002127 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002128 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002129 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002132static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002134 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002135 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002136 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002137
Chuck Levera3ca5652012-03-01 17:00:40 -05002138 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002139 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002140 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002141
Trond Myklebust88069f72009-12-08 08:33:16 -05002142 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2143 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002144 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002145 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002146 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002147 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002148 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2149 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2150 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002151 }
2152 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002153 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2154 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2155 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002156 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002157 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002158 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002159
2160 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002161 /* Note: exit _without_ calling nfs4_close_done */
2162 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002163 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002164 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002165
Fred Isamanf7e89172011-01-06 11:36:32 +00002166 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002167 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002168 if (calldata->roc &&
2169 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2170 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2171 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002172 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002173 }
2174 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002175
Trond Myklebust516a6af2005-10-27 22:12:41 -04002176 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002177 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002178 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002179 &calldata->arg.seq_args,
2180 &calldata->res.seq_res,
2181 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002182 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002183 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002184out:
2185 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002188static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002189 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190 .rpc_call_done = nfs4_close_done,
2191 .rpc_release = nfs4_free_closedata,
2192};
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194/*
2195 * It is possible for data to be read/written from a mem-mapped file
2196 * after the sys_close call (which hits the vfs layer as a flush).
2197 * This means that we can't safely call nfsv4 close on a file until
2198 * the inode is cleared. This in turn means that we are not good
2199 * NFSv4 citizens - we do not indicate to the server to update the file's
2200 * share state even when we are done with one of the three share
2201 * stateid's in the inode.
2202 *
2203 * NOTE: Caller must be holding the sp->so_owner semaphore!
2204 */
Al Viro643168c2011-06-22 18:20:23 -04002205int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002207 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002209 struct nfs4_state_owner *sp = state->owner;
2210 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002211 struct rpc_message msg = {
2212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2213 .rpc_cred = state->owner->so_cred,
2214 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002215 struct rpc_task_setup task_setup_data = {
2216 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002217 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002218 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002219 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 .flags = RPC_TASK_ASYNC,
2221 };
Trond Myklebust95121352005-10-18 14:20:12 -07002222 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002224 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002226 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002227 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002228 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002230 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002231 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002233 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002234 if (calldata->arg.seqid == NULL)
2235 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002236 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002237 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002238 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002239 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002240 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002241 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002242 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002243
Trond Myklebust1174dd12010-12-21 10:52:24 -05002244 msg.rpc_argp = &calldata->arg;
2245 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002246 task_setup_data.callback_data = calldata;
2247 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002248 if (IS_ERR(task))
2249 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002250 status = 0;
2251 if (wait)
2252 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002253 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002254 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002255out_free_calldata:
2256 kfree(calldata);
2257out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002258 if (roc)
2259 pnfs_roc_release(state->inode);
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);
2402 if (!auth) {
2403 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 Myklebustb291f1b2012-08-14 18:30:41 -04003742 if (pages && buflen <= PAGE_SIZE) {
3743 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;
Chuck Lever56d08fe2012-07-11 16:30:32 -04003787 size_t acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003788
Andy Adamsonbf118a32011-12-07 11:55:27 -05003789 /* As long as we're doing a round trip to the server anyway,
3790 * let's be prepared for a page of acl data. */
3791 if (npages == 0)
3792 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003793 if (npages > ARRAY_SIZE(pages))
3794 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003795
Andy Adamsonbf118a32011-12-07 11:55:27 -05003796 for (i = 0; i < npages; i++) {
3797 pages[i] = alloc_page(GFP_KERNEL);
3798 if (!pages[i])
3799 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003800 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003801
3802 /* for decoding across pages */
3803 res.acl_scratch = alloc_page(GFP_KERNEL);
3804 if (!res.acl_scratch)
3805 goto out_free;
3806
Andy Adamsonbf118a32011-12-07 11:55:27 -05003807 args.acl_len = npages * PAGE_SIZE;
3808 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003809
Andy Adamsonbf118a32011-12-07 11:55:27 -05003810 /* Let decode_getfacl know not to fail if the ACL data is larger than
3811 * the page we send as a guess */
3812 if (buf == NULL)
3813 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003814
Peng Taode040be2012-01-10 22:42:47 +08003815 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003816 __func__, buf, buflen, npages, args.acl_len);
3817 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3818 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003819 if (ret)
3820 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003821
Trond Myklebust519d3952012-08-14 17:30:10 -04003822 acl_len = res.acl_len;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003823 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003824 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003825 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003826 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003827 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828 if (buf) {
3829 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003830 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003832 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003833 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003834 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003835 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003836out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003837 for (i = 0; i < npages; i++)
3838 if (pages[i])
3839 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003840 if (res.acl_scratch)
3841 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003842 return ret;
3843}
3844
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003845static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3846{
3847 struct nfs4_exception exception = { };
3848 ssize_t ret;
3849 do {
3850 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3851 if (ret >= 0)
3852 break;
3853 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3854 } while (exception.retry);
3855 return ret;
3856}
3857
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003858static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3859{
3860 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003861 int ret;
3862
3863 if (!nfs4_server_supports_acls(server))
3864 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003865 ret = nfs_revalidate_inode(server, inode);
3866 if (ret < 0)
3867 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003868 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3869 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003870 ret = nfs4_read_cached_acl(inode, buf, buflen);
3871 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003872 /* -ENOENT is returned if there is no ACL or if there is an ACL
3873 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003874 return ret;
3875 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003876}
3877
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003878static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003879{
3880 struct nfs_server *server = NFS_SERVER(inode);
3881 struct page *pages[NFS4ACL_MAXPAGES];
3882 struct nfs_setaclargs arg = {
3883 .fh = NFS_FH(inode),
3884 .acl_pages = pages,
3885 .acl_len = buflen,
3886 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003887 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003888 struct rpc_message msg = {
3889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3890 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003891 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003892 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003893 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003894 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003895
3896 if (!nfs4_server_supports_acls(server))
3897 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003898 if (npages > ARRAY_SIZE(pages))
3899 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003900 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3901 if (i < 0)
3902 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003903 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003904 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003905
3906 /*
3907 * Free each page after tx, so the only ref left is
3908 * held by the network stack
3909 */
3910 for (; i > 0; i--)
3911 put_page(pages[i-1]);
3912
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003913 /*
3914 * Acl update can result in inode attribute update.
3915 * so mark the attribute cache invalid.
3916 */
3917 spin_lock(&inode->i_lock);
3918 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3919 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003920 nfs_access_zap_cache(inode);
3921 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003922 return ret;
3923}
3924
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003925static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3926{
3927 struct nfs4_exception exception = { };
3928 int err;
3929 do {
3930 err = nfs4_handle_exception(NFS_SERVER(inode),
3931 __nfs4_proc_set_acl(inode, buf, buflen),
3932 &exception);
3933 } while (exception.retry);
3934 return err;
3935}
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003938nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003940 struct nfs_client *clp = server->nfs_client;
3941
3942 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 return 0;
3944 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003945 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003946 case -NFS4ERR_ADMIN_REVOKED:
3947 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003948 if (state == NULL)
3949 break;
3950 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003951 case -NFS4ERR_OPENMODE:
3952 if (state == NULL)
3953 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003954 nfs4_schedule_stateid_recovery(server, state);
3955 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003956 case -NFS4ERR_EXPIRED:
3957 if (state != NULL)
3958 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003960 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003961 nfs4_schedule_lease_recovery(clp);
3962 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003963#if defined(CONFIG_NFS_V4_1)
3964 case -NFS4ERR_BADSESSION:
3965 case -NFS4ERR_BADSLOT:
3966 case -NFS4ERR_BAD_HIGH_SLOT:
3967 case -NFS4ERR_DEADSESSION:
3968 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3969 case -NFS4ERR_SEQ_FALSE_RETRY:
3970 case -NFS4ERR_SEQ_MISORDERED:
3971 dprintk("%s ERROR %d, Reset session\n", __func__,
3972 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003973 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003974 task->tk_status = 0;
3975 return -EAGAIN;
3976#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003978 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003979 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003980 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3982 task->tk_status = 0;
3983 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003984 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 case -NFS4ERR_OLD_STATEID:
3986 task->tk_status = 0;
3987 return -EAGAIN;
3988 }
3989 task->tk_status = nfs4_map_errors(task->tk_status);
3990 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003991wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003992 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003993 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3994 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3995 task->tk_status = 0;
3996 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997}
3998
Chuck Leverf0920752012-05-21 22:45:41 -04003999static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4000 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004001{
4002 __be32 verf[2];
4003
Chuck Lever2c820d92012-05-21 22:45:33 -04004004 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4005 /* An impossible timestamp guarantees this value
4006 * will never match a generated boot time. */
4007 verf[0] = 0;
4008 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4009 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004010 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4011 verf[0] = (__be32)nn->boot_time.tv_sec;
4012 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004013 }
Chuck Levercd937102012-03-02 17:14:31 -05004014 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4015}
4016
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004017/**
4018 * nfs4_proc_setclientid - Negotiate client ID
4019 * @clp: state data structure
4020 * @program: RPC program for NFSv4 callback service
4021 * @port: IP port number for NFS4 callback service
4022 * @cred: RPC credential to use for this call
4023 * @res: where to place the result
4024 *
4025 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4026 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004027int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4028 unsigned short port, struct rpc_cred *cred,
4029 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
4031 nfs4_verifier sc_verifier;
4032 struct nfs4_setclientid setclientid = {
4033 .sc_verifier = &sc_verifier,
4034 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004035 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 };
4037 struct rpc_message msg = {
4038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4039 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004040 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004041 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004043 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Chuck Leverde734832012-07-11 16:30:50 -04004045 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004046 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Leverde734832012-07-11 16:30:50 -04004047 rcu_read_lock();
4048 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
4049 sizeof(setclientid.sc_name), "%s/%s %s",
4050 clp->cl_ipaddr,
4051 rpc_peeraddr2str(clp->cl_rpcclient,
4052 RPC_DISPLAY_ADDR),
4053 rpc_peeraddr2str(clp->cl_rpcclient,
4054 RPC_DISPLAY_PROTO));
4055 /* cb_client4 */
4056 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004057 sizeof(setclientid.sc_netid),
4058 rpc_peeraddr2str(clp->cl_rpcclient,
4059 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004060 rcu_read_unlock();
4061 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004062 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 clp->cl_ipaddr, port >> 8, port & 255);
4064
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004065 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4066 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4067 setclientid.sc_name_len, setclientid.sc_name);
4068 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4069 dprintk("NFS reply setclientid: %d\n", status);
4070 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071}
4072
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004073/**
4074 * nfs4_proc_setclientid_confirm - Confirm client ID
4075 * @clp: state data structure
4076 * @res: result of a previous SETCLIENTID
4077 * @cred: RPC credential to use for this call
4078 *
4079 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4080 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004081int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004082 struct nfs4_setclientid_res *arg,
4083 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
4085 struct nfs_fsinfo fsinfo;
4086 struct rpc_message msg = {
4087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004088 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004090 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 };
4092 unsigned long now;
4093 int status;
4094
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004095 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4096 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4097 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004099 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (status == 0) {
4101 spin_lock(&clp->cl_lock);
4102 clp->cl_lease_time = fsinfo.lease_time * HZ;
4103 clp->cl_last_renewal = now;
4104 spin_unlock(&clp->cl_lock);
4105 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004106 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 return status;
4108}
4109
Trond Myklebustfe650402006-01-03 09:55:18 +01004110struct nfs4_delegreturndata {
4111 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004112 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004113 struct nfs_fh fh;
4114 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004115 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004116 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004117 int rpc_status;
4118};
4119
Trond Myklebustfe650402006-01-03 09:55:18 +01004120static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4121{
4122 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004123
Trond Myklebust14516c32010-07-31 14:29:06 -04004124 if (!nfs4_sequence_done(task, &data->res.seq_res))
4125 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004126
Ricardo Labiaga79708862009-12-07 09:23:21 -05004127 switch (task->tk_status) {
4128 case -NFS4ERR_STALE_STATEID:
4129 case -NFS4ERR_EXPIRED:
4130 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004131 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004132 break;
4133 default:
4134 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4135 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004136 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004137 return;
4138 }
4139 }
4140 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004141}
4142
4143static void nfs4_delegreturn_release(void *calldata)
4144{
Trond Myklebustfe650402006-01-03 09:55:18 +01004145 kfree(calldata);
4146}
4147
Andy Adamson938e1012009-04-01 09:22:28 -04004148#if defined(CONFIG_NFS_V4_1)
4149static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4150{
4151 struct nfs4_delegreturndata *d_data;
4152
4153 d_data = (struct nfs4_delegreturndata *)data;
4154
Trond Myklebust035168a2010-06-16 09:52:26 -04004155 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004156 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004157 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004158 return;
4159 rpc_call_start(task);
4160}
4161#endif /* CONFIG_NFS_V4_1 */
4162
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004163static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004164#if defined(CONFIG_NFS_V4_1)
4165 .rpc_call_prepare = nfs4_delegreturn_prepare,
4166#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004167 .rpc_call_done = nfs4_delegreturn_done,
4168 .rpc_release = nfs4_delegreturn_release,
4169};
4170
Trond Myklebuste6f81072008-01-24 18:14:34 -05004171static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004172{
4173 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004174 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004175 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004176 struct rpc_message msg = {
4177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4178 .rpc_cred = cred,
4179 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004180 struct rpc_task_setup task_setup_data = {
4181 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004182 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004183 .callback_ops = &nfs4_delegreturn_ops,
4184 .flags = RPC_TASK_ASYNC,
4185 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004186 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004187
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004188 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004189 if (data == NULL)
4190 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004191 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004192 data->args.fhandle = &data->fh;
4193 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004194 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004195 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004196 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004197 data->res.fattr = &data->fattr;
4198 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004199 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004200 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004201 data->rpc_status = 0;
4202
Trond Myklebustc970aa82007-07-14 15:39:59 -04004203 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004204 msg.rpc_argp = &data->args;
4205 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004206 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004207 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004208 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004209 if (!issync)
4210 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004211 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004212 if (status != 0)
4213 goto out;
4214 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004215 if (status == 0)
4216 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4217 else
4218 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004219out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004220 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004221 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222}
4223
Trond Myklebuste6f81072008-01-24 18:14:34 -05004224int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
4226 struct nfs_server *server = NFS_SERVER(inode);
4227 struct nfs4_exception exception = { };
4228 int err;
4229 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004230 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 switch (err) {
4232 case -NFS4ERR_STALE_STATEID:
4233 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 case 0:
4235 return 0;
4236 }
4237 err = nfs4_handle_exception(server, err, &exception);
4238 } while (exception.retry);
4239 return err;
4240}
4241
4242#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4243#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4244
4245/*
4246 * sleep, with exponential backoff, and retry the LOCK operation.
4247 */
4248static unsigned long
4249nfs4_set_lock_task_retry(unsigned long timeout)
4250{
Jeff Laytond3103102011-12-01 22:44:39 +01004251 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 timeout <<= 1;
4253 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4254 return NFS4_LOCK_MAXTIMEOUT;
4255 return timeout;
4256}
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4259{
4260 struct inode *inode = state->inode;
4261 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004262 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004263 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004265 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004267 struct nfs_lockt_res res = {
4268 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 };
4270 struct rpc_message msg = {
4271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4272 .rpc_argp = &arg,
4273 .rpc_resp = &res,
4274 .rpc_cred = state->owner->so_cred,
4275 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 struct nfs4_lock_state *lsp;
4277 int status;
4278
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004279 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004280 status = nfs4_set_lock_state(state, request);
4281 if (status != 0)
4282 goto out;
4283 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004284 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004285 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004286 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004287 switch (status) {
4288 case 0:
4289 request->fl_type = F_UNLCK;
4290 break;
4291 case -NFS4ERR_DENIED:
4292 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004294 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004295out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return status;
4297}
4298
4299static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4300{
4301 struct nfs4_exception exception = { };
4302 int err;
4303
4304 do {
4305 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4306 _nfs4_proc_getlk(state, cmd, request),
4307 &exception);
4308 } while (exception.retry);
4309 return err;
4310}
4311
4312static int do_vfs_lock(struct file *file, struct file_lock *fl)
4313{
4314 int res = 0;
4315 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4316 case FL_POSIX:
4317 res = posix_lock_file_wait(file, fl);
4318 break;
4319 case FL_FLOCK:
4320 res = flock_lock_file_wait(file, fl);
4321 break;
4322 default:
4323 BUG();
4324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return res;
4326}
4327
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004328struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004329 struct nfs_locku_args arg;
4330 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004331 struct nfs4_lock_state *lsp;
4332 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004333 struct file_lock fl;
4334 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004335 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004336};
4337
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004338static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4339 struct nfs_open_context *ctx,
4340 struct nfs4_lock_state *lsp,
4341 struct nfs_seqid *seqid)
4342{
4343 struct nfs4_unlockdata *p;
4344 struct inode *inode = lsp->ls_state->inode;
4345
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004346 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004347 if (p == NULL)
4348 return NULL;
4349 p->arg.fh = NFS_FH(inode);
4350 p->arg.fl = &p->fl;
4351 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004352 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004353 p->arg.stateid = &lsp->ls_stateid;
4354 p->lsp = lsp;
4355 atomic_inc(&lsp->ls_count);
4356 /* Ensure we don't close file until we're done freeing locks! */
4357 p->ctx = get_nfs_open_context(ctx);
4358 memcpy(&p->fl, fl, sizeof(p->fl));
4359 p->server = NFS_SERVER(inode);
4360 return p;
4361}
4362
Trond Myklebust06f814a2006-01-03 09:55:07 +01004363static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004364{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004365 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004366 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004367 nfs4_put_lock_state(calldata->lsp);
4368 put_nfs_open_context(calldata->ctx);
4369 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004370}
4371
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004372static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004373{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004374 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004375
Trond Myklebust14516c32010-07-31 14:29:06 -04004376 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4377 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004378 switch (task->tk_status) {
4379 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004380 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4381 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004382 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004383 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004384 case -NFS4ERR_BAD_STATEID:
4385 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004386 case -NFS4ERR_STALE_STATEID:
4387 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004388 break;
4389 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004390 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004391 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004392 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393}
4394
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004395static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004396{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004397 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004399 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004400 return;
4401 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004402 /* Note: exit _without_ running nfs4_locku_done */
4403 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004404 return;
4405 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004406 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004407 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004408 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004409 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004410 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004411 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412}
4413
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004414static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004415 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004416 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004417 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004418};
4419
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004420static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4421 struct nfs_open_context *ctx,
4422 struct nfs4_lock_state *lsp,
4423 struct nfs_seqid *seqid)
4424{
4425 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004426 struct rpc_message msg = {
4427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4428 .rpc_cred = ctx->cred,
4429 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004430 struct rpc_task_setup task_setup_data = {
4431 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004432 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004433 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004434 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004435 .flags = RPC_TASK_ASYNC,
4436 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004437
Frank Filz137d6ac2007-07-09 15:32:29 -07004438 /* Ensure this is an unlock - when canceling a lock, the
4439 * canceled lock is passed in, and it won't be an unlock.
4440 */
4441 fl->fl_type = F_UNLCK;
4442
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4444 if (data == NULL) {
4445 nfs_free_seqid(seqid);
4446 return ERR_PTR(-ENOMEM);
4447 }
4448
Trond Myklebust9d12b212012-01-17 22:04:25 -05004449 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004450 msg.rpc_argp = &data->arg;
4451 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004452 task_setup_data.callback_data = data;
4453 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004454}
4455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4457{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004458 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004459 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004460 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004461 struct rpc_task *task;
4462 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004463 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Trond Myklebust9b073572006-06-29 16:38:34 -04004465 status = nfs4_set_lock_state(state, request);
4466 /* Unlock _before_ we do the RPC call */
4467 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004468 down_read(&nfsi->rwsem);
4469 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4470 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004471 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004472 }
4473 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004474 if (status != 0)
4475 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004476 /* Is this a delegated lock? */
4477 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004478 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004479 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004480 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004481 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004482 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004483 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004484 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485 status = PTR_ERR(task);
4486 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004487 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004488 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004489 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004490out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004491 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004492 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493}
4494
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004495struct nfs4_lockdata {
4496 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004497 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004498 struct nfs4_lock_state *lsp;
4499 struct nfs_open_context *ctx;
4500 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004501 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004502 int rpc_status;
4503 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004504 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004505};
4506
4507static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004508 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4509 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510{
4511 struct nfs4_lockdata *p;
4512 struct inode *inode = lsp->ls_state->inode;
4513 struct nfs_server *server = NFS_SERVER(inode);
4514
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004515 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004516 if (p == NULL)
4517 return NULL;
4518
4519 p->arg.fh = NFS_FH(inode);
4520 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004521 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004522 if (p->arg.open_seqid == NULL)
4523 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004524 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004526 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004528 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004529 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004530 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004531 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004533 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004534 atomic_inc(&lsp->ls_count);
4535 p->ctx = get_nfs_open_context(ctx);
4536 memcpy(&p->fl, fl, sizeof(p->fl));
4537 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004538out_free_seqid:
4539 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540out_free:
4541 kfree(p);
4542 return NULL;
4543}
4544
4545static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4546{
4547 struct nfs4_lockdata *data = calldata;
4548 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Harvey Harrison3110ff82008-05-02 13:42:44 -07004550 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004551 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4552 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004553 /* Do we need to do an open_to_lock_owner? */
4554 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004555 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4556 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557 data->arg.open_stateid = &state->stateid;
4558 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004559 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004560 } else
4561 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004562 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004563 if (nfs4_setup_sequence(data->server,
4564 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004565 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004566 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004567 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004568 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004569}
4570
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004571static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4572{
4573 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4574 nfs4_lock_prepare(task, calldata);
4575}
4576
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4578{
4579 struct nfs4_lockdata *data = calldata;
4580
Harvey Harrison3110ff82008-05-02 13:42:44 -07004581 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582
Trond Myklebust14516c32010-07-31 14:29:06 -04004583 if (!nfs4_sequence_done(task, &data->res.seq_res))
4584 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004585
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004586 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004587 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588 if (data->rpc_status == 0)
4589 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4590 else
4591 goto out;
4592 }
4593 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004594 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004596 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004599 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004600}
4601
4602static void nfs4_lock_release(void *calldata)
4603{
4604 struct nfs4_lockdata *data = calldata;
4605
Harvey Harrison3110ff82008-05-02 13:42:44 -07004606 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004607 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004608 if (data->cancelled != 0) {
4609 struct rpc_task *task;
4610 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4611 data->arg.lock_seqid);
4612 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004613 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004614 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004615 } else
4616 nfs_free_seqid(data->arg.lock_seqid);
4617 nfs4_put_lock_state(data->lsp);
4618 put_nfs_open_context(data->ctx);
4619 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004620 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621}
4622
4623static const struct rpc_call_ops nfs4_lock_ops = {
4624 .rpc_call_prepare = nfs4_lock_prepare,
4625 .rpc_call_done = nfs4_lock_done,
4626 .rpc_release = nfs4_lock_release,
4627};
4628
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004629static const struct rpc_call_ops nfs4_recover_lock_ops = {
4630 .rpc_call_prepare = nfs4_recover_lock_prepare,
4631 .rpc_call_done = nfs4_lock_done,
4632 .rpc_release = nfs4_lock_release,
4633};
4634
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004635static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4636{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004637 switch (error) {
4638 case -NFS4ERR_ADMIN_REVOKED:
4639 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004640 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004641 if (new_lock_owner != 0 ||
4642 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004643 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004644 break;
4645 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004646 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004647 case -NFS4ERR_EXPIRED:
4648 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004649 };
4650}
4651
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004652static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004653{
4654 struct nfs4_lockdata *data;
4655 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004656 struct rpc_message msg = {
4657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4658 .rpc_cred = state->owner->so_cred,
4659 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004660 struct rpc_task_setup task_setup_data = {
4661 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004662 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004663 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004664 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004665 .flags = RPC_TASK_ASYNC,
4666 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004667 int ret;
4668
Harvey Harrison3110ff82008-05-02 13:42:44 -07004669 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004670 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004671 fl->fl_u.nfs4_fl.owner,
4672 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673 if (data == NULL)
4674 return -ENOMEM;
4675 if (IS_SETLKW(cmd))
4676 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004677 if (recovery_type > NFS_LOCK_NEW) {
4678 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004679 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004680 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4681 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004682 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004683 msg.rpc_argp = &data->arg;
4684 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004685 task_setup_data.callback_data = data;
4686 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004687 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004689 ret = nfs4_wait_for_completion_rpc_task(task);
4690 if (ret == 0) {
4691 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004692 if (ret)
4693 nfs4_handle_setlk_error(data->server, data->lsp,
4694 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695 } else
4696 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004697 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004698 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004699 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
4702static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4703{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004704 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004705 struct nfs4_exception exception = {
4706 .inode = state->inode,
4707 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004708 int err;
4709
4710 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004711 /* Cache the lock if possible... */
4712 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4713 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004714 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004715 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004716 break;
4717 nfs4_handle_exception(server, err, &exception);
4718 } while (exception.retry);
4719 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
4722static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4723{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004724 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004725 struct nfs4_exception exception = {
4726 .inode = state->inode,
4727 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004728 int err;
4729
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004730 err = nfs4_set_lock_state(state, request);
4731 if (err != 0)
4732 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004733 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004734 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4735 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004736 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004737 switch (err) {
4738 default:
4739 goto out;
4740 case -NFS4ERR_GRACE:
4741 case -NFS4ERR_DELAY:
4742 nfs4_handle_exception(server, err, &exception);
4743 err = 0;
4744 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004745 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004746out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
4749
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004750#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004751/**
4752 * nfs41_check_expired_locks - possibly free a lock stateid
4753 *
4754 * @state: NFSv4 state for an inode
4755 *
4756 * Returns NFS_OK if recovery for this stateid is now finished.
4757 * Otherwise a negative NFS4ERR value is returned.
4758 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004759static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004760{
Chuck Levereb64cf92012-07-11 16:30:05 -04004761 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004762 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004763 struct nfs_server *server = NFS_SERVER(state->inode);
4764
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004765 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4766 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4767 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4768 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004769 /* Free the stateid unless the server
4770 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004771 if (status != -NFS4ERR_BAD_STATEID)
4772 nfs41_free_stateid(server,
4773 &lsp->ls_stateid);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004774 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4775 ret = status;
4776 }
4777 }
4778 };
4779
4780 return ret;
4781}
4782
4783static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4784{
4785 int status = NFS_OK;
4786
4787 if (test_bit(LK_STATE_IN_USE, &state->flags))
4788 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004789 if (status != NFS_OK)
4790 status = nfs4_lock_expired(state, request);
4791 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004792}
4793#endif
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4796{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004797 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004798 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004799 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004801 if ((fl_flags & FL_POSIX) &&
4802 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4803 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004804 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004805 status = nfs4_set_lock_state(state, request);
4806 if (status != 0)
4807 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004808 request->fl_flags |= FL_ACCESS;
4809 status = do_vfs_lock(request->fl_file, request);
4810 if (status < 0)
4811 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004812 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004813 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004814 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004815 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004816 request->fl_flags = fl_flags & ~FL_SLEEP;
4817 status = do_vfs_lock(request->fl_file, request);
4818 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004819 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004820 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004821 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004822 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004823 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004824 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004825 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004826 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4827 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004828out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004829 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004830out:
4831 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return status;
4833}
4834
4835static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4836{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004837 struct nfs4_exception exception = {
4838 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004839 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004840 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 int err;
4842
4843 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004844 err = _nfs4_proc_setlk(state, cmd, request);
4845 if (err == -NFS4ERR_DENIED)
4846 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004848 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 } while (exception.retry);
4850 return err;
4851}
4852
4853static int
4854nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4855{
4856 struct nfs_open_context *ctx;
4857 struct nfs4_state *state;
4858 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4859 int status;
4860
4861 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004862 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 state = ctx->state;
4864
4865 if (request->fl_start < 0 || request->fl_end < 0)
4866 return -EINVAL;
4867
Trond Myklebustd9531262009-07-21 19:22:38 -04004868 if (IS_GETLK(cmd)) {
4869 if (state != NULL)
4870 return nfs4_proc_getlk(state, F_GETLK, request);
4871 return 0;
4872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4875 return -EINVAL;
4876
Trond Myklebustd9531262009-07-21 19:22:38 -04004877 if (request->fl_type == F_UNLCK) {
4878 if (state != NULL)
4879 return nfs4_proc_unlck(state, cmd, request);
4880 return 0;
4881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Trond Myklebustd9531262009-07-21 19:22:38 -04004883 if (state == NULL)
4884 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004885 /*
4886 * Don't rely on the VFS having checked the file open mode,
4887 * since it won't do this for flock() locks.
4888 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004889 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004890 case F_RDLCK:
4891 if (!(filp->f_mode & FMODE_READ))
4892 return -EBADF;
4893 break;
4894 case F_WRLCK:
4895 if (!(filp->f_mode & FMODE_WRITE))
4896 return -EBADF;
4897 }
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 do {
4900 status = nfs4_proc_setlk(state, cmd, request);
4901 if ((status != -EAGAIN) || IS_SETLK(cmd))
4902 break;
4903 timeout = nfs4_set_lock_task_retry(timeout);
4904 status = -ERESTARTSYS;
4905 if (signalled())
4906 break;
4907 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 return status;
4909}
4910
Trond Myklebust888e6942005-11-04 15:38:11 -05004911int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4912{
4913 struct nfs_server *server = NFS_SERVER(state->inode);
4914 struct nfs4_exception exception = { };
4915 int err;
4916
4917 err = nfs4_set_lock_state(state, fl);
4918 if (err != 0)
4919 goto out;
4920 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004921 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004922 switch (err) {
4923 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004924 printk(KERN_ERR "NFS: %s: unhandled error "
4925 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004926 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004927 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004928 goto out;
4929 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004930 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004931 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004932 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004933 nfs4_schedule_lease_recovery(server->nfs_client);
4934 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004935 case -NFS4ERR_BADSESSION:
4936 case -NFS4ERR_BADSLOT:
4937 case -NFS4ERR_BAD_HIGH_SLOT:
4938 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4939 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004940 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004941 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004942 case -ERESTARTSYS:
4943 /*
4944 * The show must go on: exit, but mark the
4945 * stateid as needing recovery.
4946 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004947 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004948 case -NFS4ERR_ADMIN_REVOKED:
4949 case -NFS4ERR_BAD_STATEID:
4950 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004951 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004952 err = 0;
4953 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004954 case -EKEYEXPIRED:
4955 /*
4956 * User RPCSEC_GSS context has expired.
4957 * We cannot recover this stateid now, so
4958 * skip it and allow recovery thread to
4959 * proceed.
4960 */
4961 err = 0;
4962 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004963 case -ENOMEM:
4964 case -NFS4ERR_DENIED:
4965 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4966 err = 0;
4967 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004968 case -NFS4ERR_DELAY:
4969 break;
4970 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004971 err = nfs4_handle_exception(server, err, &exception);
4972 } while (exception.retry);
4973out:
4974 return err;
4975}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004976
Trond Myklebustcf470c32012-03-07 13:49:12 -05004977struct nfs_release_lockowner_data {
4978 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004979 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004980 struct nfs_release_lockowner_args args;
4981};
4982
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004983static void nfs4_release_lockowner_release(void *calldata)
4984{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004985 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004986 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004987 kfree(calldata);
4988}
4989
Trond Myklebust17280172012-03-11 13:11:00 -04004990static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004991 .rpc_release = nfs4_release_lockowner_release,
4992};
4993
Trond Myklebustcf470c32012-03-07 13:49:12 -05004994int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004995{
4996 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004997 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004998 struct rpc_message msg = {
4999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5000 };
5001
5002 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005003 return -EINVAL;
5004 data = kmalloc(sizeof(*data), GFP_NOFS);
5005 if (!data)
5006 return -ENOMEM;
5007 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005008 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005009 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5010 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5011 data->args.lock_owner.s_dev = server->s_dev;
5012 msg.rpc_argp = &data->args;
5013 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5014 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005015}
5016
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005017#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5018
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005019static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5020 const void *buf, size_t buflen,
5021 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005022{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005023 if (strcmp(key, "") != 0)
5024 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005025
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005026 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005027}
5028
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005029static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5030 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005031{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005032 if (strcmp(key, "") != 0)
5033 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005034
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005035 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005036}
5037
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005038static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5039 size_t list_len, const char *name,
5040 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005041{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005042 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005043
J. Bruce Fields096455a2006-03-20 23:23:42 -05005044 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5045 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005046
5047 if (list && len <= list_len)
5048 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005049 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005050}
5051
Andy Adamson533eb462011-06-13 18:25:56 -04005052/*
5053 * nfs_fhget will use either the mounted_on_fileid or the fileid
5054 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005055static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5056{
Andy Adamson533eb462011-06-13 18:25:56 -04005057 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5058 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5059 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005060 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005061 return;
5062
5063 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005064 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005065 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5066 fattr->nlink = 2;
5067}
5068
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005069static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5070 const struct qstr *name,
5071 struct nfs4_fs_locations *fs_locations,
5072 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005073{
5074 struct nfs_server *server = NFS_SERVER(dir);
5075 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005076 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005077 };
5078 struct nfs4_fs_locations_arg args = {
5079 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005080 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005081 .page = page,
5082 .bitmask = bitmask,
5083 };
Benny Halevy22958462009-04-01 09:22:02 -04005084 struct nfs4_fs_locations_res res = {
5085 .fs_locations = fs_locations,
5086 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005087 struct rpc_message msg = {
5088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5089 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005090 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005091 };
5092 int status;
5093
Harvey Harrison3110ff82008-05-02 13:42:44 -07005094 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005095
5096 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5097 * is not supported */
5098 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5099 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5100 else
5101 bitmask[0] |= FATTR4_WORD0_FILEID;
5102
Trond Myklebustc228fd32007-01-13 02:28:11 -05005103 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005104 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005105 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005106 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005107 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005108 return status;
5109}
5110
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005111int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5112 const struct qstr *name,
5113 struct nfs4_fs_locations *fs_locations,
5114 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005115{
5116 struct nfs4_exception exception = { };
5117 int err;
5118 do {
5119 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005120 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005121 &exception);
5122 } while (exception.retry);
5123 return err;
5124}
5125
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005126static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5127{
5128 int status;
5129 struct nfs4_secinfo_arg args = {
5130 .dir_fh = NFS_FH(dir),
5131 .name = name,
5132 };
5133 struct nfs4_secinfo_res res = {
5134 .flavors = flavors,
5135 };
5136 struct rpc_message msg = {
5137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5138 .rpc_argp = &args,
5139 .rpc_resp = &res,
5140 };
5141
5142 dprintk("NFS call secinfo %s\n", name->name);
5143 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5144 dprintk("NFS reply secinfo: %d\n", status);
5145 return status;
5146}
5147
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005148int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5149 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005150{
5151 struct nfs4_exception exception = { };
5152 int err;
5153 do {
5154 err = nfs4_handle_exception(NFS_SERVER(dir),
5155 _nfs4_proc_secinfo(dir, name, flavors),
5156 &exception);
5157 } while (exception.retry);
5158 return err;
5159}
5160
Andy Adamson557134a2009-04-01 09:21:53 -04005161#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005162/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005163 * Check the exchange flags returned by the server for invalid flags, having
5164 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5165 * DS flags set.
5166 */
5167static int nfs4_check_cl_exchange_flags(u32 flags)
5168{
5169 if (flags & ~EXCHGID4_FLAG_MASK_R)
5170 goto out_inval;
5171 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5172 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5173 goto out_inval;
5174 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5175 goto out_inval;
5176 return NFS_OK;
5177out_inval:
5178 return -NFS4ERR_INVAL;
5179}
5180
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005181static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005182nfs41_same_server_scope(struct nfs41_server_scope *a,
5183 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005184{
5185 if (a->server_scope_sz == b->server_scope_sz &&
5186 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5187 return true;
5188
5189 return false;
5190}
5191
Andy Adamson357f54d2010-12-14 10:11:57 -05005192/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005193 * nfs4_proc_bind_conn_to_session()
5194 *
5195 * The 4.1 client currently uses the same TCP connection for the
5196 * fore and backchannel.
5197 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005198int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005199{
5200 int status;
5201 struct nfs41_bind_conn_to_session_res res;
5202 struct rpc_message msg = {
5203 .rpc_proc =
5204 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5205 .rpc_argp = clp,
5206 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005207 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005208 };
5209
5210 dprintk("--> %s\n", __func__);
5211 BUG_ON(clp == NULL);
5212
5213 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5214 if (unlikely(res.session == NULL)) {
5215 status = -ENOMEM;
5216 goto out;
5217 }
5218
5219 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5220 if (status == 0) {
5221 if (memcmp(res.session->sess_id.data,
5222 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5223 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5224 status = -EIO;
5225 goto out_session;
5226 }
5227 if (res.dir != NFS4_CDFS4_BOTH) {
5228 dprintk("NFS: %s: Unexpected direction from server\n",
5229 __func__);
5230 status = -EIO;
5231 goto out_session;
5232 }
5233 if (res.use_conn_in_rdma_mode) {
5234 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5235 __func__);
5236 status = -EIO;
5237 goto out_session;
5238 }
5239 }
5240out_session:
5241 kfree(res.session);
5242out:
5243 dprintk("<-- %s status= %d\n", __func__, status);
5244 return status;
5245}
5246
5247/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005248 * nfs4_proc_exchange_id()
5249 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005250 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5251 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005252 * Since the clientid has expired, all compounds using sessions
5253 * associated with the stale clientid will be returning
5254 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5255 * be in some phase of session reset.
5256 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005257int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005258{
5259 nfs4_verifier verifier;
5260 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005261 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005262 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005263 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005264 };
5265 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005266 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005267 };
5268 int status;
5269 struct rpc_message msg = {
5270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5271 .rpc_argp = &args,
5272 .rpc_resp = &res,
5273 .rpc_cred = cred,
5274 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005275
Chuck Leverf0920752012-05-21 22:45:41 -04005276 nfs4_init_boot_verifier(clp, &verifier);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005277 args.id_len = scnprintf(args.id, sizeof(args.id),
Chuck Leverde734832012-07-11 16:30:50 -04005278 "%s/%s",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005279 clp->cl_ipaddr,
Chuck Leverde734832012-07-11 16:30:50 -04005280 clp->cl_rpcclient->cl_nodename);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005281 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5282 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5283 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005284
Chuck Leveracdeb692012-05-21 22:46:16 -04005285 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005286 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005287 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005288 status = -ENOMEM;
5289 goto out;
5290 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005291
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005292 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005293 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005294 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005295 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005296 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005297 }
5298
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005299 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005300 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005301 status = -ENOMEM;
5302 goto out_server_scope;
5303 }
5304
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005305 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005306 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005307 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005308
Chuck Lever177313f2012-05-21 22:44:58 -04005309 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005310 clp->cl_clientid = res.clientid;
5311 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5312 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5313 clp->cl_seqid = res.seqid;
5314
Chuck Leveracdeb692012-05-21 22:46:16 -04005315 kfree(clp->cl_serverowner);
5316 clp->cl_serverowner = res.server_owner;
5317 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005318
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005319 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005320 kfree(clp->cl_implid);
5321 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005322
Chuck Lever177313f2012-05-21 22:44:58 -04005323 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005324 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005325 res.server_scope)) {
5326 dprintk("%s: server_scope mismatch detected\n",
5327 __func__);
5328 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005329 kfree(clp->cl_serverscope);
5330 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005331 }
5332
Chuck Lever177313f2012-05-21 22:44:58 -04005333 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005334 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005335 goto out;
5336 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005337 } else
5338 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005339
Chuck Leveracdeb692012-05-21 22:46:16 -04005340out_server_owner:
5341 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005342out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005343 kfree(res.server_scope);
5344out:
Chuck Lever177313f2012-05-21 22:44:58 -04005345 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005346 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005347 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005348 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005349 clp->cl_implid->date.seconds,
5350 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005351 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005352 return status;
5353}
5354
Trond Myklebust66245532012-05-25 17:18:09 -04005355static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5356 struct rpc_cred *cred)
5357{
5358 struct rpc_message msg = {
5359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5360 .rpc_argp = clp,
5361 .rpc_cred = cred,
5362 };
5363 int status;
5364
5365 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5366 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005367 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005368 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5369 return status;
5370}
5371
5372static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5373 struct rpc_cred *cred)
5374{
5375 unsigned int loop;
5376 int ret;
5377
5378 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5379 ret = _nfs4_proc_destroy_clientid(clp, cred);
5380 switch (ret) {
5381 case -NFS4ERR_DELAY:
5382 case -NFS4ERR_CLIENTID_BUSY:
5383 ssleep(1);
5384 break;
5385 default:
5386 return ret;
5387 }
5388 }
5389 return 0;
5390}
5391
5392int nfs4_destroy_clientid(struct nfs_client *clp)
5393{
5394 struct rpc_cred *cred;
5395 int ret = 0;
5396
5397 if (clp->cl_mvops->minor_version < 1)
5398 goto out;
5399 if (clp->cl_exchange_flags == 0)
5400 goto out;
5401 cred = nfs4_get_exchange_id_cred(clp);
5402 ret = nfs4_proc_destroy_clientid(clp, cred);
5403 if (cred)
5404 put_rpccred(cred);
5405 switch (ret) {
5406 case 0:
5407 case -NFS4ERR_STALE_CLIENTID:
5408 clp->cl_exchange_flags = 0;
5409 }
5410out:
5411 return ret;
5412}
5413
Andy Adamson2050f0c2009-04-01 09:22:30 -04005414struct nfs4_get_lease_time_data {
5415 struct nfs4_get_lease_time_args *args;
5416 struct nfs4_get_lease_time_res *res;
5417 struct nfs_client *clp;
5418};
5419
5420static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5421 void *calldata)
5422{
5423 int ret;
5424 struct nfs4_get_lease_time_data *data =
5425 (struct nfs4_get_lease_time_data *)calldata;
5426
5427 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005428 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005429 /* just setup sequence, do not trigger session recovery
5430 since we're invoked within one */
5431 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005432 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005433 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005434
5435 BUG_ON(ret == -EAGAIN);
5436 rpc_call_start(task);
5437 dprintk("<-- %s\n", __func__);
5438}
5439
5440/*
5441 * Called from nfs4_state_manager thread for session setup, so don't recover
5442 * from sequence operation or clientid errors.
5443 */
5444static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5445{
5446 struct nfs4_get_lease_time_data *data =
5447 (struct nfs4_get_lease_time_data *)calldata;
5448
5449 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005450 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5451 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005452 switch (task->tk_status) {
5453 case -NFS4ERR_DELAY:
5454 case -NFS4ERR_GRACE:
5455 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5456 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5457 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005458 /* fall through */
5459 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005460 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005461 return;
5462 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005463 dprintk("<-- %s\n", __func__);
5464}
5465
Trond Myklebust17280172012-03-11 13:11:00 -04005466static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005467 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5468 .rpc_call_done = nfs4_get_lease_time_done,
5469};
5470
5471int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5472{
5473 struct rpc_task *task;
5474 struct nfs4_get_lease_time_args args;
5475 struct nfs4_get_lease_time_res res = {
5476 .lr_fsinfo = fsinfo,
5477 };
5478 struct nfs4_get_lease_time_data data = {
5479 .args = &args,
5480 .res = &res,
5481 .clp = clp,
5482 };
5483 struct rpc_message msg = {
5484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5485 .rpc_argp = &args,
5486 .rpc_resp = &res,
5487 };
5488 struct rpc_task_setup task_setup = {
5489 .rpc_client = clp->cl_rpcclient,
5490 .rpc_message = &msg,
5491 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005492 .callback_data = &data,
5493 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005494 };
5495 int status;
5496
Trond Myklebust9d12b212012-01-17 22:04:25 -05005497 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005498 dprintk("--> %s\n", __func__);
5499 task = rpc_run_task(&task_setup);
5500
5501 if (IS_ERR(task))
5502 status = PTR_ERR(task);
5503 else {
5504 status = task->tk_status;
5505 rpc_put_task(task);
5506 }
5507 dprintk("<-- %s return %d\n", __func__, status);
5508
5509 return status;
5510}
5511
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005512static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5513{
5514 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5515}
5516
5517static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5518 struct nfs4_slot *new,
5519 u32 max_slots,
5520 u32 ivalue)
5521{
5522 struct nfs4_slot *old = NULL;
5523 u32 i;
5524
5525 spin_lock(&tbl->slot_tbl_lock);
5526 if (new) {
5527 old = tbl->slots;
5528 tbl->slots = new;
5529 tbl->max_slots = max_slots;
5530 }
5531 tbl->highest_used_slotid = -1; /* no slot is currently used */
5532 for (i = 0; i < tbl->max_slots; i++)
5533 tbl->slots[i].seq_nr = ivalue;
5534 spin_unlock(&tbl->slot_tbl_lock);
5535 kfree(old);
5536}
5537
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005538/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005539 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005540 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005541static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5542 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005543{
Andy Adamson104aeba2010-01-14 17:45:10 -05005544 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005545 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005546
Andy Adamson104aeba2010-01-14 17:45:10 -05005547 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5548 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005549
Andy Adamson104aeba2010-01-14 17:45:10 -05005550 /* Does the newly negotiated max_reqs match the existing slot table? */
5551 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005552 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005553 if (!new)
5554 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005555 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005556 ret = 0;
5557
5558 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005559 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5560 tbl, tbl->slots, tbl->max_slots);
5561out:
5562 dprintk("<-- %s: return %d\n", __func__, ret);
5563 return ret;
5564}
5565
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005566/* Destroy the slot table */
5567static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5568{
5569 if (session->fc_slot_table.slots != NULL) {
5570 kfree(session->fc_slot_table.slots);
5571 session->fc_slot_table.slots = NULL;
5572 }
5573 if (session->bc_slot_table.slots != NULL) {
5574 kfree(session->bc_slot_table.slots);
5575 session->bc_slot_table.slots = NULL;
5576 }
5577 return;
5578}
5579
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005580/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005581 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005582 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005583static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005584{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005585 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005586 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005587
Andy Adamsonaacd5532011-11-09 13:58:21 -05005588 dprintk("--> %s\n", __func__);
5589 /* Fore channel */
5590 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005591 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5592 if (status) /* -ENOMEM */
5593 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005594 /* Back channel */
5595 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005596 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5597 if (status && tbl->slots == NULL)
5598 /* Fore and back channel share a connection so get
5599 * both slot tables or neither */
5600 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005601 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005602}
5603
5604struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5605{
5606 struct nfs4_session *session;
5607 struct nfs4_slot_table *tbl;
5608
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005609 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005610 if (!session)
5611 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005612
Andy Adamson557134a2009-04-01 09:21:53 -04005613 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005614 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005615 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005616 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005617 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005618
5619 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005620 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005621 spin_lock_init(&tbl->slot_tbl_lock);
5622 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005623 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005624
Trond Myklebust1055d762010-06-16 09:52:27 -04005625 session->session_state = 1<<NFS4_SESSION_INITING;
5626
Andy Adamson557134a2009-04-01 09:21:53 -04005627 session->clp = clp;
5628 return session;
5629}
5630
5631void nfs4_destroy_session(struct nfs4_session *session)
5632{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005633 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005634 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005635
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005636 cred = nfs4_get_exchange_id_cred(session->clp);
5637 nfs4_proc_destroy_session(session, cred);
5638 if (cred)
5639 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005640
5641 rcu_read_lock();
5642 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5643 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005644 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005645 __func__, xprt);
5646 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005647 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005648 kfree(session);
5649}
5650
Andy Adamsonfc931582009-04-01 09:22:31 -04005651/*
5652 * Initialize the values to be used by the client in CREATE_SESSION
5653 * If nfs4_init_session set the fore channel request and response sizes,
5654 * use them.
5655 *
5656 * Set the back channel max_resp_sz_cached to zero to force the client to
5657 * always set csa_cachethis to FALSE because the current implementation
5658 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5659 */
5660static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5661{
5662 struct nfs4_session *session = args->client->cl_session;
5663 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5664 mxresp_sz = session->fc_attrs.max_resp_sz;
5665
5666 if (mxrqst_sz == 0)
5667 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5668 if (mxresp_sz == 0)
5669 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5670 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005671 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5672 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005673 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005674 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005675
5676 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005677 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005678 __func__,
5679 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005680 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005681
5682 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005683 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5684 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5685 args->bc_attrs.max_resp_sz_cached = 0;
5686 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5687 args->bc_attrs.max_reqs = 1;
5688
5689 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5690 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5691 __func__,
5692 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5693 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5694 args->bc_attrs.max_reqs);
5695}
5696
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005697static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005698{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005699 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5700 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5701
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005702 if (rcvd->max_resp_sz > sent->max_resp_sz)
5703 return -EINVAL;
5704 /*
5705 * Our requested max_ops is the minimum we need; we're not
5706 * prepared to break up compounds into smaller pieces than that.
5707 * So, no point even trying to continue if the server won't
5708 * cooperate:
5709 */
5710 if (rcvd->max_ops < sent->max_ops)
5711 return -EINVAL;
5712 if (rcvd->max_reqs == 0)
5713 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005714 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5715 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005716 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005717}
5718
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005719static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5720{
5721 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5722 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005723
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005724 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5725 return -EINVAL;
5726 if (rcvd->max_resp_sz < sent->max_resp_sz)
5727 return -EINVAL;
5728 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5729 return -EINVAL;
5730 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005731 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005732 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005733 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005734 return -EINVAL;
5735 return 0;
5736}
Andy Adamson8d353012009-04-01 09:22:32 -04005737
Andy Adamson8d353012009-04-01 09:22:32 -04005738static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5739 struct nfs4_session *session)
5740{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005741 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005742
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005743 ret = nfs4_verify_fore_channel_attrs(args, session);
5744 if (ret)
5745 return ret;
5746 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005747}
5748
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005749static int _nfs4_proc_create_session(struct nfs_client *clp,
5750 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005751{
5752 struct nfs4_session *session = clp->cl_session;
5753 struct nfs41_create_session_args args = {
5754 .client = clp,
5755 .cb_program = NFS4_CALLBACK,
5756 };
5757 struct nfs41_create_session_res res = {
5758 .client = clp,
5759 };
5760 struct rpc_message msg = {
5761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5762 .rpc_argp = &args,
5763 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005764 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005765 };
5766 int status;
5767
5768 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005769 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005770
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005771 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005772
Andy Adamson8d353012009-04-01 09:22:32 -04005773 if (!status)
5774 /* Verify the session's negotiated channel_attrs values */
5775 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005776 if (!status) {
5777 /* Increment the clientid slot sequence id */
5778 clp->cl_seqid++;
5779 }
5780
5781 return status;
5782}
5783
5784/*
5785 * Issues a CREATE_SESSION operation to the server.
5786 * It is the responsibility of the caller to verify the session is
5787 * expired before calling this routine.
5788 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005789int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005790{
5791 int status;
5792 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005793 struct nfs4_session *session = clp->cl_session;
5794
5795 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5796
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005797 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005798 if (status)
5799 goto out;
5800
Andy Adamsonaacd5532011-11-09 13:58:21 -05005801 /* Init or reset the session slot tables */
5802 status = nfs4_setup_session_slot_tables(session);
5803 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005804 if (status)
5805 goto out;
5806
5807 ptr = (unsigned *)&session->sess_id.data[0];
5808 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5809 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005810out:
5811 dprintk("<-- %s\n", __func__);
5812 return status;
5813}
5814
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005815/*
5816 * Issue the over-the-wire RPC DESTROY_SESSION.
5817 * The caller must serialize access to this routine.
5818 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005819int nfs4_proc_destroy_session(struct nfs4_session *session,
5820 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005821{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005822 struct rpc_message msg = {
5823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5824 .rpc_argp = session,
5825 .rpc_cred = cred,
5826 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005827 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005828
5829 dprintk("--> nfs4_proc_destroy_session\n");
5830
5831 /* session is still being setup */
5832 if (session->clp->cl_cons_state != NFS_CS_READY)
5833 return status;
5834
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005835 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005836
5837 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005838 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005839 "Session has been destroyed regardless...\n", status);
5840
5841 dprintk("<-- nfs4_proc_destroy_session\n");
5842 return status;
5843}
5844
Trond Myklebust7b38c362012-05-23 13:23:31 -04005845/*
5846 * With sessions, the client is not marked ready until after a
5847 * successful EXCHANGE_ID and CREATE_SESSION.
5848 *
5849 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5850 * other versions of NFS can be tried.
5851 */
5852static int nfs41_check_session_ready(struct nfs_client *clp)
5853{
5854 int ret;
5855
5856 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5857 ret = nfs4_client_recover_expired_lease(clp);
5858 if (ret)
5859 return ret;
5860 }
5861 if (clp->cl_cons_state < NFS_CS_READY)
5862 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005863 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005864 return 0;
5865}
5866
Trond Myklebustfccba802009-07-21 16:48:07 -04005867int nfs4_init_session(struct nfs_server *server)
5868{
5869 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005870 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005871 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005872
5873 if (!nfs4_has_session(clp))
5874 return 0;
5875
Trond Myklebust1055d762010-06-16 09:52:27 -04005876 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005877 spin_lock(&clp->cl_lock);
5878 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005879
Trond Myklebust7b38c362012-05-23 13:23:31 -04005880 rsize = server->rsize;
5881 if (rsize == 0)
5882 rsize = NFS_MAX_FILE_IO_SIZE;
5883 wsize = server->wsize;
5884 if (wsize == 0)
5885 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005886
Trond Myklebust7b38c362012-05-23 13:23:31 -04005887 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5888 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5889 }
5890 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005891
Trond Myklebust7b38c362012-05-23 13:23:31 -04005892 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005893}
5894
Trond Myklebust7b38c362012-05-23 13:23:31 -04005895int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005896{
5897 struct nfs4_session *session = clp->cl_session;
5898 int ret;
5899
Trond Myklebust7b38c362012-05-23 13:23:31 -04005900 spin_lock(&clp->cl_lock);
5901 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5902 /*
5903 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5904 * DS lease to be equal to the MDS lease.
5905 */
5906 clp->cl_lease_time = lease_time;
5907 clp->cl_last_renewal = jiffies;
5908 }
5909 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005910
Trond Myklebust7b38c362012-05-23 13:23:31 -04005911 ret = nfs41_check_session_ready(clp);
5912 if (ret)
5913 return ret;
5914 /* Test for the DS role */
5915 if (!is_ds_client(clp))
5916 return -ENODEV;
5917 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005918}
5919EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5920
5921
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005922/*
5923 * Renew the cl_session lease.
5924 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005925struct nfs4_sequence_data {
5926 struct nfs_client *clp;
5927 struct nfs4_sequence_args args;
5928 struct nfs4_sequence_res res;
5929};
5930
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005931static void nfs41_sequence_release(void *data)
5932{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005933 struct nfs4_sequence_data *calldata = data;
5934 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005935
Alexandros Batsakis71358402010-02-05 03:45:05 -08005936 if (atomic_read(&clp->cl_count) > 1)
5937 nfs4_schedule_state_renewal(clp);
5938 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005939 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005940}
5941
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005942static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5943{
5944 switch(task->tk_status) {
5945 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005946 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5947 return -EAGAIN;
5948 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005949 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005950 }
5951 return 0;
5952}
5953
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005954static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005955{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005956 struct nfs4_sequence_data *calldata = data;
5957 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005958
Trond Myklebust14516c32010-07-31 14:29:06 -04005959 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5960 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005961
5962 if (task->tk_status < 0) {
5963 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005964 if (atomic_read(&clp->cl_count) == 1)
5965 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005966
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005967 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5968 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005969 return;
5970 }
5971 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005972 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005973out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005974 dprintk("<-- %s\n", __func__);
5975}
5976
5977static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5978{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005979 struct nfs4_sequence_data *calldata = data;
5980 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005981 struct nfs4_sequence_args *args;
5982 struct nfs4_sequence_res *res;
5983
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005984 args = task->tk_msg.rpc_argp;
5985 res = task->tk_msg.rpc_resp;
5986
Trond Myklebust9d12b212012-01-17 22:04:25 -05005987 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005988 return;
5989 rpc_call_start(task);
5990}
5991
5992static const struct rpc_call_ops nfs41_sequence_ops = {
5993 .rpc_call_done = nfs41_sequence_call_done,
5994 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005995 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005996};
5997
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005998static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005999{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006000 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006001 struct rpc_message msg = {
6002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6003 .rpc_cred = cred,
6004 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006005 struct rpc_task_setup task_setup_data = {
6006 .rpc_client = clp->cl_rpcclient,
6007 .rpc_message = &msg,
6008 .callback_ops = &nfs41_sequence_ops,
6009 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6010 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006011
Alexandros Batsakis71358402010-02-05 03:45:05 -08006012 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006013 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006014 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006015 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006016 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006017 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006018 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006019 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006020 msg.rpc_argp = &calldata->args;
6021 msg.rpc_resp = &calldata->res;
6022 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006023 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006024
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006025 return rpc_run_task(&task_setup_data);
6026}
6027
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006028static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006029{
6030 struct rpc_task *task;
6031 int ret = 0;
6032
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006033 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6034 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006035 task = _nfs41_proc_sequence(clp, cred);
6036 if (IS_ERR(task))
6037 ret = PTR_ERR(task);
6038 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006039 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006040 dprintk("<-- %s status=%d\n", __func__, ret);
6041 return ret;
6042}
6043
6044static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6045{
6046 struct rpc_task *task;
6047 int ret;
6048
6049 task = _nfs41_proc_sequence(clp, cred);
6050 if (IS_ERR(task)) {
6051 ret = PTR_ERR(task);
6052 goto out;
6053 }
6054 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006055 if (!ret) {
6056 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6057
6058 if (task->tk_status == 0)
6059 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006060 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006061 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006062 rpc_put_task(task);
6063out:
6064 dprintk("<-- %s status=%d\n", __func__, ret);
6065 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006066}
6067
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006068struct nfs4_reclaim_complete_data {
6069 struct nfs_client *clp;
6070 struct nfs41_reclaim_complete_args arg;
6071 struct nfs41_reclaim_complete_res res;
6072};
6073
6074static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6075{
6076 struct nfs4_reclaim_complete_data *calldata = data;
6077
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006078 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006079 if (nfs41_setup_sequence(calldata->clp->cl_session,
6080 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006081 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006082 return;
6083
6084 rpc_call_start(task);
6085}
6086
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006087static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6088{
6089 switch(task->tk_status) {
6090 case 0:
6091 case -NFS4ERR_COMPLETE_ALREADY:
6092 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6093 break;
6094 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006095 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006096 /* fall through */
6097 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006098 return -EAGAIN;
6099 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006100 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006101 }
6102 return 0;
6103}
6104
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006105static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6106{
6107 struct nfs4_reclaim_complete_data *calldata = data;
6108 struct nfs_client *clp = calldata->clp;
6109 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6110
6111 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006112 if (!nfs41_sequence_done(task, res))
6113 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006114
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006115 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6116 rpc_restart_call_prepare(task);
6117 return;
6118 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006119 dprintk("<-- %s\n", __func__);
6120}
6121
6122static void nfs4_free_reclaim_complete_data(void *data)
6123{
6124 struct nfs4_reclaim_complete_data *calldata = data;
6125
6126 kfree(calldata);
6127}
6128
6129static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6130 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6131 .rpc_call_done = nfs4_reclaim_complete_done,
6132 .rpc_release = nfs4_free_reclaim_complete_data,
6133};
6134
6135/*
6136 * Issue a global reclaim complete.
6137 */
6138static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6139{
6140 struct nfs4_reclaim_complete_data *calldata;
6141 struct rpc_task *task;
6142 struct rpc_message msg = {
6143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6144 };
6145 struct rpc_task_setup task_setup_data = {
6146 .rpc_client = clp->cl_rpcclient,
6147 .rpc_message = &msg,
6148 .callback_ops = &nfs4_reclaim_complete_call_ops,
6149 .flags = RPC_TASK_ASYNC,
6150 };
6151 int status = -ENOMEM;
6152
6153 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006154 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006155 if (calldata == NULL)
6156 goto out;
6157 calldata->clp = clp;
6158 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006159
Trond Myklebust9d12b212012-01-17 22:04:25 -05006160 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006161 msg.rpc_argp = &calldata->arg;
6162 msg.rpc_resp = &calldata->res;
6163 task_setup_data.callback_data = calldata;
6164 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006165 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006166 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006167 goto out;
6168 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006169 status = nfs4_wait_for_completion_rpc_task(task);
6170 if (status == 0)
6171 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006172 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006173 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006174out:
6175 dprintk("<-- %s status=%d\n", __func__, status);
6176 return status;
6177}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006178
6179static void
6180nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6181{
6182 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006183 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006184
6185 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006186 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6187 * right now covering the LAYOUTGET we are about to send.
6188 * However, that is not so catastrophic, and there seems
6189 * to be no way to prevent it completely.
6190 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006191 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006192 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006193 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006194 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6195 NFS_I(lgp->args.inode)->layout,
6196 lgp->args.ctx->state)) {
6197 rpc_exit(task, NFS4_OK);
6198 return;
6199 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006200 rpc_call_start(task);
6201}
6202
6203static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6204{
6205 struct nfs4_layoutget *lgp = calldata;
6206 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6207
6208 dprintk("--> %s\n", __func__);
6209
6210 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6211 return;
6212
6213 switch (task->tk_status) {
6214 case 0:
6215 break;
6216 case -NFS4ERR_LAYOUTTRYLATER:
6217 case -NFS4ERR_RECALLCONFLICT:
6218 task->tk_status = -NFS4ERR_DELAY;
6219 /* Fall through */
6220 default:
6221 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6222 rpc_restart_call_prepare(task);
6223 return;
6224 }
6225 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006226 dprintk("<-- %s\n", __func__);
6227}
6228
Idan Kedar85541162012-08-02 11:47:10 +03006229static size_t max_response_pages(struct nfs_server *server)
6230{
6231 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6232 return nfs_page_array_len(0, max_resp_sz);
6233}
6234
6235static void nfs4_free_pages(struct page **pages, size_t size)
6236{
6237 int i;
6238
6239 if (!pages)
6240 return;
6241
6242 for (i = 0; i < size; i++) {
6243 if (!pages[i])
6244 break;
6245 __free_page(pages[i]);
6246 }
6247 kfree(pages);
6248}
6249
6250static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6251{
6252 struct page **pages;
6253 int i;
6254
6255 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6256 if (!pages) {
6257 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6258 return NULL;
6259 }
6260
6261 for (i = 0; i < size; i++) {
6262 pages[i] = alloc_page(gfp_flags);
6263 if (!pages[i]) {
6264 dprintk("%s: failed to allocate page\n", __func__);
6265 nfs4_free_pages(pages, size);
6266 return NULL;
6267 }
6268 }
6269
6270 return pages;
6271}
6272
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006273static void nfs4_layoutget_release(void *calldata)
6274{
6275 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006276 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6277 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006278
6279 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006280 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006281 put_nfs_open_context(lgp->args.ctx);
6282 kfree(calldata);
6283 dprintk("<-- %s\n", __func__);
6284}
6285
6286static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6287 .rpc_call_prepare = nfs4_layoutget_prepare,
6288 .rpc_call_done = nfs4_layoutget_done,
6289 .rpc_release = nfs4_layoutget_release,
6290};
6291
Idan Kedar21d1f582012-08-02 11:47:11 +03006292void nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006293{
6294 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006295 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006296 struct rpc_task *task;
6297 struct rpc_message msg = {
6298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6299 .rpc_argp = &lgp->args,
6300 .rpc_resp = &lgp->res,
6301 };
6302 struct rpc_task_setup task_setup_data = {
6303 .rpc_client = server->client,
6304 .rpc_message = &msg,
6305 .callback_ops = &nfs4_layoutget_call_ops,
6306 .callback_data = lgp,
6307 .flags = RPC_TASK_ASYNC,
6308 };
6309 int status = 0;
6310
6311 dprintk("--> %s\n", __func__);
6312
Idan Kedar85541162012-08-02 11:47:10 +03006313 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6314 if (!lgp->args.layout.pages) {
6315 nfs4_layoutget_release(lgp);
Idan Kedar21d1f582012-08-02 11:47:11 +03006316 return;
Idan Kedar85541162012-08-02 11:47:10 +03006317 }
6318 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6319
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006320 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006321 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006322 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006323 task = rpc_run_task(&task_setup_data);
6324 if (IS_ERR(task))
Idan Kedar21d1f582012-08-02 11:47:11 +03006325 return;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006326 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006327 if (status == 0)
6328 status = task->tk_status;
6329 if (status == 0)
6330 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006331 rpc_put_task(task);
6332 dprintk("<-- %s status=%d\n", __func__, status);
Idan Kedar21d1f582012-08-02 11:47:11 +03006333 return;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006334}
6335
Benny Halevycbe82602011-05-22 19:52:37 +03006336static void
6337nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6338{
6339 struct nfs4_layoutreturn *lrp = calldata;
6340
6341 dprintk("--> %s\n", __func__);
6342 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006343 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006344 return;
6345 rpc_call_start(task);
6346}
6347
6348static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6349{
6350 struct nfs4_layoutreturn *lrp = calldata;
6351 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006352 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006353
6354 dprintk("--> %s\n", __func__);
6355
6356 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6357 return;
6358
6359 server = NFS_SERVER(lrp->args.inode);
6360 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006361 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006362 return;
6363 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006364 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust47fbf7972012-08-08 16:03:13 -04006365 if (task->tk_status == 0 && lrp->res.lrs_present)
6366 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006367 lo->plh_block_lgets--;
6368 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006369 dprintk("<-- %s\n", __func__);
6370}
6371
6372static void nfs4_layoutreturn_release(void *calldata)
6373{
6374 struct nfs4_layoutreturn *lrp = calldata;
6375
6376 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006377 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006378 kfree(calldata);
6379 dprintk("<-- %s\n", __func__);
6380}
6381
6382static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6383 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6384 .rpc_call_done = nfs4_layoutreturn_done,
6385 .rpc_release = nfs4_layoutreturn_release,
6386};
6387
6388int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6389{
6390 struct rpc_task *task;
6391 struct rpc_message msg = {
6392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6393 .rpc_argp = &lrp->args,
6394 .rpc_resp = &lrp->res,
6395 };
6396 struct rpc_task_setup task_setup_data = {
6397 .rpc_client = lrp->clp->cl_rpcclient,
6398 .rpc_message = &msg,
6399 .callback_ops = &nfs4_layoutreturn_call_ops,
6400 .callback_data = lrp,
6401 };
6402 int status;
6403
6404 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006405 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006406 task = rpc_run_task(&task_setup_data);
6407 if (IS_ERR(task))
6408 return PTR_ERR(task);
6409 status = task->tk_status;
6410 dprintk("<-- %s status=%d\n", __func__, status);
6411 rpc_put_task(task);
6412 return status;
6413}
6414
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006415/*
6416 * Retrieve the list of Data Server devices from the MDS.
6417 */
6418static int _nfs4_getdevicelist(struct nfs_server *server,
6419 const struct nfs_fh *fh,
6420 struct pnfs_devicelist *devlist)
6421{
6422 struct nfs4_getdevicelist_args args = {
6423 .fh = fh,
6424 .layoutclass = server->pnfs_curr_ld->id,
6425 };
6426 struct nfs4_getdevicelist_res res = {
6427 .devlist = devlist,
6428 };
6429 struct rpc_message msg = {
6430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6431 .rpc_argp = &args,
6432 .rpc_resp = &res,
6433 };
6434 int status;
6435
6436 dprintk("--> %s\n", __func__);
6437 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6438 &res.seq_res, 0);
6439 dprintk("<-- %s status=%d\n", __func__, status);
6440 return status;
6441}
6442
6443int nfs4_proc_getdevicelist(struct nfs_server *server,
6444 const struct nfs_fh *fh,
6445 struct pnfs_devicelist *devlist)
6446{
6447 struct nfs4_exception exception = { };
6448 int err;
6449
6450 do {
6451 err = nfs4_handle_exception(server,
6452 _nfs4_getdevicelist(server, fh, devlist),
6453 &exception);
6454 } while (exception.retry);
6455
6456 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6457 err, devlist->num_devs);
6458
6459 return err;
6460}
6461EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6462
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006463static int
6464_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6465{
6466 struct nfs4_getdeviceinfo_args args = {
6467 .pdev = pdev,
6468 };
6469 struct nfs4_getdeviceinfo_res res = {
6470 .pdev = pdev,
6471 };
6472 struct rpc_message msg = {
6473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6474 .rpc_argp = &args,
6475 .rpc_resp = &res,
6476 };
6477 int status;
6478
6479 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006480 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006481 dprintk("<-- %s status=%d\n", __func__, status);
6482
6483 return status;
6484}
6485
6486int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6487{
6488 struct nfs4_exception exception = { };
6489 int err;
6490
6491 do {
6492 err = nfs4_handle_exception(server,
6493 _nfs4_proc_getdeviceinfo(server, pdev),
6494 &exception);
6495 } while (exception.retry);
6496 return err;
6497}
6498EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6499
Andy Adamson863a3c62011-03-23 13:27:54 +00006500static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6501{
6502 struct nfs4_layoutcommit_data *data = calldata;
6503 struct nfs_server *server = NFS_SERVER(data->args.inode);
6504
6505 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006506 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006507 return;
6508 rpc_call_start(task);
6509}
6510
6511static void
6512nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6513{
6514 struct nfs4_layoutcommit_data *data = calldata;
6515 struct nfs_server *server = NFS_SERVER(data->args.inode);
6516
6517 if (!nfs4_sequence_done(task, &data->res.seq_res))
6518 return;
6519
6520 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006521 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6522 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6523 case -NFS4ERR_BADLAYOUT: /* no layout */
6524 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006525 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006526 break;
6527 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006528 nfs_post_op_update_inode_force_wcc(data->args.inode,
6529 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006530 break;
6531 default:
6532 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6533 rpc_restart_call_prepare(task);
6534 return;
6535 }
6536 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006537}
6538
6539static void nfs4_layoutcommit_release(void *calldata)
6540{
6541 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006542 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006543 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006544
Andy Adamsondb29c082011-07-30 20:52:38 -04006545 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006546 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006547 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6548 list_del_init(&lseg->pls_lc_list);
6549 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6550 &lseg->pls_flags))
6551 put_lseg(lseg);
6552 }
Peng Tao92407e72011-10-23 20:21:17 -07006553
6554 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6555 smp_mb__after_clear_bit();
6556 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6557
Andy Adamson863a3c62011-03-23 13:27:54 +00006558 put_rpccred(data->cred);
6559 kfree(data);
6560}
6561
6562static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6563 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6564 .rpc_call_done = nfs4_layoutcommit_done,
6565 .rpc_release = nfs4_layoutcommit_release,
6566};
6567
6568int
Andy Adamsonef311532011-03-12 02:58:10 -05006569nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006570{
6571 struct rpc_message msg = {
6572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6573 .rpc_argp = &data->args,
6574 .rpc_resp = &data->res,
6575 .rpc_cred = data->cred,
6576 };
6577 struct rpc_task_setup task_setup_data = {
6578 .task = &data->task,
6579 .rpc_client = NFS_CLIENT(data->args.inode),
6580 .rpc_message = &msg,
6581 .callback_ops = &nfs4_layoutcommit_ops,
6582 .callback_data = data,
6583 .flags = RPC_TASK_ASYNC,
6584 };
6585 struct rpc_task *task;
6586 int status = 0;
6587
6588 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6589 "lbw: %llu inode %lu\n",
6590 data->task.tk_pid, sync,
6591 data->args.lastbytewritten,
6592 data->args.inode->i_ino);
6593
Trond Myklebust9d12b212012-01-17 22:04:25 -05006594 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006595 task = rpc_run_task(&task_setup_data);
6596 if (IS_ERR(task))
6597 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006598 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006599 goto out;
6600 status = nfs4_wait_for_completion_rpc_task(task);
6601 if (status != 0)
6602 goto out;
6603 status = task->tk_status;
6604out:
6605 dprintk("%s: status %d\n", __func__, status);
6606 rpc_put_task(task);
6607 return status;
6608}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006609
6610static int
6611_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6612 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6613{
6614 struct nfs41_secinfo_no_name_args args = {
6615 .style = SECINFO_STYLE_CURRENT_FH,
6616 };
6617 struct nfs4_secinfo_res res = {
6618 .flavors = flavors,
6619 };
6620 struct rpc_message msg = {
6621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6622 .rpc_argp = &args,
6623 .rpc_resp = &res,
6624 };
6625 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6626}
6627
6628static int
6629nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6630 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6631{
6632 struct nfs4_exception exception = { };
6633 int err;
6634 do {
6635 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6636 switch (err) {
6637 case 0:
6638 case -NFS4ERR_WRONGSEC:
6639 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006640 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006641 default:
6642 err = nfs4_handle_exception(server, err, &exception);
6643 }
6644 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006645out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006646 return err;
6647}
6648
6649static int
6650nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6651 struct nfs_fsinfo *info)
6652{
6653 int err;
6654 struct page *page;
6655 rpc_authflavor_t flavor;
6656 struct nfs4_secinfo_flavors *flavors;
6657
6658 page = alloc_page(GFP_KERNEL);
6659 if (!page) {
6660 err = -ENOMEM;
6661 goto out;
6662 }
6663
6664 flavors = page_address(page);
6665 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6666
6667 /*
6668 * Fall back on "guess and check" method if
6669 * the server doesn't support SECINFO_NO_NAME
6670 */
6671 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6672 err = nfs4_find_root_sec(server, fhandle, info);
6673 goto out_freepage;
6674 }
6675 if (err)
6676 goto out_freepage;
6677
6678 flavor = nfs_find_best_sec(flavors);
6679 if (err == 0)
6680 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6681
6682out_freepage:
6683 put_page(page);
6684 if (err == -EACCES)
6685 return -EPERM;
6686out:
6687 return err;
6688}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006689
6690static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006691{
6692 int status;
6693 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006694 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006695 };
6696 struct nfs41_test_stateid_res res;
6697 struct rpc_message msg = {
6698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6699 .rpc_argp = &args,
6700 .rpc_resp = &res,
6701 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006702
Chuck Lever38527b12012-07-11 16:30:23 -04006703 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006704 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006705 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006706 if (status != NFS_OK) {
6707 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006708 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006709 }
6710 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006711 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006712}
6713
Chuck Lever38527b12012-07-11 16:30:23 -04006714/**
6715 * nfs41_test_stateid - perform a TEST_STATEID operation
6716 *
6717 * @server: server / transport on which to perform the operation
6718 * @stateid: state ID to test
6719 *
6720 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6721 * Otherwise a negative NFS4ERR value is returned if the operation
6722 * failed or the state ID is not currently valid.
6723 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006724static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006725{
6726 struct nfs4_exception exception = { };
6727 int err;
6728 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006729 err = _nfs41_test_stateid(server, stateid);
6730 if (err != -NFS4ERR_DELAY)
6731 break;
6732 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006733 } while (exception.retry);
6734 return err;
6735}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006736
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006737static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006738{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006739 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006740 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006741 };
6742 struct nfs41_free_stateid_res res;
6743 struct rpc_message msg = {
6744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6745 .rpc_argp = &args,
6746 .rpc_resp = &res,
6747 };
Chuck Lever38527b12012-07-11 16:30:23 -04006748 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006749
Chuck Lever38527b12012-07-11 16:30:23 -04006750 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006751 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006752 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006753 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006754 dprintk("NFS reply free_stateid: %d\n", status);
6755 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006756}
6757
Chuck Lever38527b12012-07-11 16:30:23 -04006758/**
6759 * nfs41_free_stateid - perform a FREE_STATEID operation
6760 *
6761 * @server: server / transport on which to perform the operation
6762 * @stateid: state ID to release
6763 *
6764 * Returns NFS_OK if the server freed "stateid". Otherwise a
6765 * negative NFS4ERR value is returned.
6766 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006767static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006768{
6769 struct nfs4_exception exception = { };
6770 int err;
6771 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006772 err = _nfs4_free_stateid(server, stateid);
6773 if (err != -NFS4ERR_DELAY)
6774 break;
6775 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006776 } while (exception.retry);
6777 return err;
6778}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006779
6780static bool nfs41_match_stateid(const nfs4_stateid *s1,
6781 const nfs4_stateid *s2)
6782{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006783 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006784 return false;
6785
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006786 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006787 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006788 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006789 return true;
6790
6791 return false;
6792}
6793
Andy Adamson557134a2009-04-01 09:21:53 -04006794#endif /* CONFIG_NFS_V4_1 */
6795
Trond Myklebust36281ca2012-03-04 18:13:56 -05006796static bool nfs4_match_stateid(const nfs4_stateid *s1,
6797 const nfs4_stateid *s2)
6798{
Trond Myklebustf597c532012-03-04 18:13:56 -05006799 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006800}
6801
6802
Trond Myklebust17280172012-03-11 13:11:00 -04006803static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006804 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006805 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 .recover_open = nfs4_open_reclaim,
6807 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006808 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006809 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810};
6811
Andy Adamson591d71c2009-04-01 09:22:47 -04006812#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006813static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006814 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6815 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6816 .recover_open = nfs4_open_reclaim,
6817 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006818 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006819 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006820 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006821};
6822#endif /* CONFIG_NFS_V4_1 */
6823
Trond Myklebust17280172012-03-11 13:11:00 -04006824static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006825 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006826 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 .recover_open = nfs4_open_expired,
6828 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006829 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006830 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831};
6832
Andy Adamson591d71c2009-04-01 09:22:47 -04006833#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006834static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006835 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6836 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006837 .recover_open = nfs41_open_expired,
6838 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006839 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006840 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006841};
6842#endif /* CONFIG_NFS_V4_1 */
6843
Trond Myklebust17280172012-03-11 13:11:00 -04006844static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006845 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006846 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006847 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006848};
6849
6850#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006851static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006852 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006853 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006854 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006855};
6856#endif
6857
Trond Myklebust97dc1352010-06-16 09:52:26 -04006858static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6859 .minor_version = 0,
6860 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006861 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006862 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006863 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6864 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6865 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006866};
6867
6868#if defined(CONFIG_NFS_V4_1)
6869static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6870 .minor_version = 1,
6871 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006872 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006873 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006874 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6875 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6876 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006877};
6878#endif
6879
Trond Myklebust97dc1352010-06-16 09:52:26 -04006880const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6881 [0] = &nfs_v4_0_minor_ops,
6882#if defined(CONFIG_NFS_V4_1)
6883 [1] = &nfs_v4_1_minor_ops,
6884#endif
6885};
6886
Bryan Schumaker73a79702012-07-16 16:39:12 -04006887const struct inode_operations nfs4_dir_inode_operations = {
6888 .create = nfs_create,
6889 .lookup = nfs_lookup,
6890 .atomic_open = nfs_atomic_open,
6891 .link = nfs_link,
6892 .unlink = nfs_unlink,
6893 .symlink = nfs_symlink,
6894 .mkdir = nfs_mkdir,
6895 .rmdir = nfs_rmdir,
6896 .mknod = nfs_mknod,
6897 .rename = nfs_rename,
6898 .permission = nfs_permission,
6899 .getattr = nfs_getattr,
6900 .setattr = nfs_setattr,
6901 .getxattr = generic_getxattr,
6902 .setxattr = generic_setxattr,
6903 .listxattr = generic_listxattr,
6904 .removexattr = generic_removexattr,
6905};
6906
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006907static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006908 .permission = nfs_permission,
6909 .getattr = nfs_getattr,
6910 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006911 .getxattr = generic_getxattr,
6912 .setxattr = generic_setxattr,
6913 .listxattr = generic_listxattr,
6914 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006915};
6916
David Howells509de812006-08-22 20:06:11 -04006917const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 .version = 4, /* protocol version */
6919 .dentry_ops = &nfs4_dentry_operations,
6920 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006921 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006922 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006924 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006925 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 .getattr = nfs4_proc_getattr,
6927 .setattr = nfs4_proc_setattr,
6928 .lookup = nfs4_proc_lookup,
6929 .access = nfs4_proc_access,
6930 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 .create = nfs4_proc_create,
6932 .remove = nfs4_proc_remove,
6933 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006934 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 .unlink_done = nfs4_proc_unlink_done,
6936 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006937 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006938 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006939 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 .link = nfs4_proc_link,
6941 .symlink = nfs4_proc_symlink,
6942 .mkdir = nfs4_proc_mkdir,
6943 .rmdir = nfs4_proc_remove,
6944 .readdir = nfs4_proc_readdir,
6945 .mknod = nfs4_proc_mknod,
6946 .statfs = nfs4_proc_statfs,
6947 .fsinfo = nfs4_proc_fsinfo,
6948 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006949 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 .decode_dirent = nfs4_decode_dirent,
6951 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006952 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006953 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006954 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006956 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006957 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006958 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006960 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006961 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006963 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006964 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006965 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006966 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006967 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006968 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006969 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006970 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006971 .create_server = nfs4_create_server,
6972 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973};
6974
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006975static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6976 .prefix = XATTR_NAME_NFSV4_ACL,
6977 .list = nfs4_xattr_list_nfs4_acl,
6978 .get = nfs4_xattr_get_nfs4_acl,
6979 .set = nfs4_xattr_set_nfs4_acl,
6980};
6981
6982const struct xattr_handler *nfs4_xattr_handlers[] = {
6983 &nfs4_xattr_nfs4_acl_handler,
6984 NULL
6985};
6986
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987/*
6988 * Local variables:
6989 * c-basic-offset: 8
6990 * End:
6991 */