blob: 6d5750cabd8dc9b7472e27e6364ccbf664ac1b88 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Trond Myklebust52567b02009-10-23 14:46:42 -0400107 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700109 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * This is our standard bitmap for GETATTR requests.
117 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400118const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 FATTR4_WORD0_TYPE
120 | FATTR4_WORD0_CHANGE
121 | FATTR4_WORD0_SIZE
122 | FATTR4_WORD0_FSID
123 | FATTR4_WORD0_FILEID,
124 FATTR4_WORD1_MODE
125 | FATTR4_WORD1_NUMLINKS
126 | FATTR4_WORD1_OWNER
127 | FATTR4_WORD1_OWNER_GROUP
128 | FATTR4_WORD1_RAWDEV
129 | FATTR4_WORD1_SPACE_USED
130 | FATTR4_WORD1_TIME_ACCESS
131 | FATTR4_WORD1_TIME_METADATA
132 | FATTR4_WORD1_TIME_MODIFY
133};
134
Trond Myklebust1549210f2012-06-05 09:16:47 -0400135static const u32 nfs4_pnfs_open_bitmap[3] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID,
141 FATTR4_WORD1_MODE
142 | FATTR4_WORD1_NUMLINKS
143 | FATTR4_WORD1_OWNER
144 | FATTR4_WORD1_OWNER_GROUP
145 | FATTR4_WORD1_RAWDEV
146 | FATTR4_WORD1_SPACE_USED
147 | FATTR4_WORD1_TIME_ACCESS
148 | FATTR4_WORD1_TIME_METADATA
149 | FATTR4_WORD1_TIME_MODIFY,
150 FATTR4_WORD2_MDSTHRESHOLD
151};
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153const u32 nfs4_statfs_bitmap[2] = {
154 FATTR4_WORD0_FILES_AVAIL
155 | FATTR4_WORD0_FILES_FREE
156 | FATTR4_WORD0_FILES_TOTAL,
157 FATTR4_WORD1_SPACE_AVAIL
158 | FATTR4_WORD1_SPACE_FREE
159 | FATTR4_WORD1_SPACE_TOTAL
160};
161
Trond Myklebust4ce79712005-06-22 17:16:21 +0000162const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 FATTR4_WORD0_MAXLINK
164 | FATTR4_WORD0_MAXNAME,
165 0
166};
167
Fred Isamandae100c2011-07-30 20:52:37 -0400168const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 | FATTR4_WORD0_MAXREAD
170 | FATTR4_WORD0_MAXWRITE
171 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700172 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400173 | FATTR4_WORD1_FS_LAYOUT_TYPES,
174 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Manoj Naik830b8e32006-06-09 09:34:25 -0400177const u32 nfs4_fs_locations_bitmap[2] = {
178 FATTR4_WORD0_TYPE
179 | FATTR4_WORD0_CHANGE
180 | FATTR4_WORD0_SIZE
181 | FATTR4_WORD0_FSID
182 | FATTR4_WORD0_FILEID
183 | FATTR4_WORD0_FS_LOCATIONS,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
192 | FATTR4_WORD1_TIME_MODIFY
193 | FATTR4_WORD1_MOUNTED_ON_FILEID
194};
195
Al Virobc4785c2006-10-19 23:28:51 -0700196static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct nfs4_readdir_arg *readdir)
198{
Al Viro0dbb4c62006-10-19 23:28:49 -0700199 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 BUG_ON(readdir->count < 80);
202 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000203 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
205 return;
206 }
207
208 readdir->cookie = 0;
209 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
210 if (cookie == 2)
211 return;
212
213 /*
214 * NFSv4 servers do not return entries for '.' and '..'
215 * Therefore, we fake these entries here. We let '.'
216 * have cookie 0 and '..' have cookie 1. Note that
217 * when talking to the server, we always send cookie 0
218 * instead of 1 or 2.
219 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800220 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (cookie == 0) {
223 *p++ = xdr_one; /* next */
224 *p++ = xdr_zero; /* cookie, first word */
225 *p++ = xdr_one; /* cookie, second word */
226 *p++ = xdr_one; /* entry len */
227 memcpy(p, ".\0\0\0", 4); /* entry */
228 p++;
229 *p++ = xdr_one; /* bitmap length */
230 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
231 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400232 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 *p++ = xdr_one; /* next */
236 *p++ = xdr_zero; /* cookie, first word */
237 *p++ = xdr_two; /* cookie, second word */
238 *p++ = xdr_two; /* entry len */
239 memcpy(p, "..\0\0", 4); /* entry */
240 p++;
241 *p++ = xdr_one; /* bitmap length */
242 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
243 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400244 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 readdir->pgbase = (char *)p - (char *)start;
247 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800248 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Trond Myklebust65de8722008-12-23 15:21:44 -0500251static int nfs4_wait_clnt_recover(struct nfs_client *clp)
252{
253 int res;
254
255 might_sleep();
256
Trond Myklebuste005e802008-12-23 15:21:48 -0500257 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400258 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400259 if (res)
260 return res;
261
262 if (clp->cl_cons_state < 0)
263 return clp->cl_cons_state;
264 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500265}
266
267static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
268{
269 int res = 0;
270
271 might_sleep();
272
273 if (*timeout <= 0)
274 *timeout = NFS4_POLL_RETRY_MIN;
275 if (*timeout > NFS4_POLL_RETRY_MAX)
276 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100277 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 if (fatal_signal_pending(current))
279 res = -ERESTARTSYS;
280 *timeout <<= 1;
281 return res;
282}
283
284/* This is the error handling routine for processes that are allowed
285 * to sleep.
286 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800287static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500288{
289 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500290 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500292 int ret = errorcode;
293
294 exception->retry = 0;
295 switch(errorcode) {
296 case 0:
297 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500298 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400299 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400300 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500301 exception->retry = 1;
302 return 0;
303 }
304 if (state == NULL)
305 break;
306 nfs4_schedule_stateid_recovery(server, state);
307 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500308 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500309 case -NFS4ERR_ADMIN_REVOKED:
310 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500311 if (state == NULL)
312 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400313 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500314 nfs4_schedule_stateid_recovery(server, state);
315 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400316 case -NFS4ERR_EXPIRED:
317 if (state != NULL)
318 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500319 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500320 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500321 nfs4_schedule_lease_recovery(clp);
322 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500323#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400324 case -NFS4ERR_BADSESSION:
325 case -NFS4ERR_BADSLOT:
326 case -NFS4ERR_BAD_HIGH_SLOT:
327 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
328 case -NFS4ERR_DEADSESSION:
329 case -NFS4ERR_SEQ_FALSE_RETRY:
330 case -NFS4ERR_SEQ_MISORDERED:
331 dprintk("%s ERROR: %d Reset session\n", __func__,
332 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400333 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400334 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500335 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500336#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500338 if (exception->timeout > HZ) {
339 /* We have retried a decent amount, time to
340 * fail
341 */
342 ret = -EBUSY;
343 break;
344 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 case -NFS4ERR_GRACE:
346 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500347 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500348 ret = nfs4_delay(server->client, &exception->timeout);
349 if (ret != 0)
350 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400351 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 case -NFS4ERR_OLD_STATEID:
353 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800354 break;
355 case -NFS4ERR_BADOWNER:
356 /* The following works around a Linux server bug! */
357 case -NFS4ERR_BADNAME:
358 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
359 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
360 exception->retry = 1;
361 printk(KERN_WARNING "NFS: v4 server %s "
362 "does not accept raw "
363 "uid/gids. "
364 "Reenabling the idmapper.\n",
365 server->nfs_client->cl_hostname);
366 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500367 }
368 /* We failed to handle the error */
369 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500370wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500371 ret = nfs4_wait_clnt_recover(clp);
372 if (ret == 0)
373 exception->retry = 1;
374 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500375}
376
377
Trond Myklebust452e9352010-07-31 14:29:06 -0400378static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_lock(&clp->cl_lock);
381 if (time_before(clp->cl_last_renewal,timestamp))
382 clp->cl_last_renewal = timestamp;
383 spin_unlock(&clp->cl_lock);
384}
385
Trond Myklebust452e9352010-07-31 14:29:06 -0400386static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
387{
388 do_renew_lease(server->nfs_client, timestamp);
389}
390
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400391#if defined(CONFIG_NFS_V4_1)
392
Benny Halevy510b8172009-04-01 09:22:14 -0400393/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400394 * nfs4_free_slot - free a slot and efficiently update slot table.
395 *
396 * freeing a slot is trivially done by clearing its respective bit
397 * in the bitmap.
398 * If the freed slotid equals highest_used_slotid we want to update it
399 * so that the server would be able to size down the slot table if needed,
400 * otherwise we know that the highest_used_slotid is still in use.
401 * When updating highest_used_slotid there may be "holes" in the bitmap
402 * so we need to scan down from highest_used_slotid to 0 looking for the now
403 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500404 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500405 *
406 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400407 */
408static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500409nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400410{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500411 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400412 /* clear used bit in bitmap */
413 __clear_bit(slotid, tbl->used_slots);
414
415 /* update highest_used_slotid when it is freed */
416 if (slotid == tbl->highest_used_slotid) {
417 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500418 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400419 tbl->highest_used_slotid = slotid;
420 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500421 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400422 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500423 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
424 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400425}
426
Trond Myklebust961a8282012-01-17 22:57:37 -0500427bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
428{
429 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
430 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400431}
432
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800433/*
Andy Adamson42acd022011-01-06 02:04:34 +0000434 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435 */
Andy Adamson42acd022011-01-06 02:04:34 +0000436static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400438 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500439 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
440 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800441 return;
442 }
443
Trond Myklebust45d43c22012-02-06 19:38:51 -0500444 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800445 return;
446
Andy Adamson42acd022011-01-06 02:04:34 +0000447 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
448 complete(&ses->fc_slot_table.complete);
449}
450
451/*
452 * Signal state manager thread if session back channel is drained
453 */
454void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
455{
456 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500457 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000458 return;
459 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
460 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800461}
462
Trond Myklebustd185a332010-06-16 09:52:25 -0400463static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400464{
465 struct nfs4_slot_table *tbl;
466
Trond Myklebustd185a332010-06-16 09:52:25 -0400467 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400468 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400469 /* just wake up the next guy waiting since
470 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500471 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500472 return;
Andy Adamson13615872009-04-01 09:22:17 -0400473 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500474
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500475 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500476 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000477 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500478 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400479 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400480}
481
Trond Myklebust14516c32010-07-31 14:29:06 -0400482static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483{
484 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400486
487 /*
488 * sr_status remains 1 if an RPC level error occurred. The server
489 * may or may not have processed the sequence operation..
490 * Proceed as if the server received and processed the sequence
491 * operation.
492 */
493 if (res->sr_status == 1)
494 res->sr_status = NFS_OK;
495
Bryan Schumaker468f8612011-04-18 15:57:32 -0400496 /* don't increment the sequence number if the task wasn't sent */
497 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400498 goto out;
499
Andy Adamson691daf32009-12-04 15:55:39 -0500500 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400501 switch (res->sr_status) {
502 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400503 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400504 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400505 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400506 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400507 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500508 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500509 if (res->sr_status_flags != 0)
510 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400511 break;
512 case -NFS4ERR_DELAY:
513 /* The server detected a resend of the RPC call and
514 * returned NFS4ERR_DELAY as per Section 2.10.6.2
515 * of RFC5661.
516 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200517 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400518 __func__,
519 res->sr_slot - res->sr_session->fc_slot_table.slots,
520 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400521 goto out_retry;
522 default:
523 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400524 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400525 }
526out:
527 /* The session may be reset by one of the error handlers. */
528 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400529 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400530 return 1;
531out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400532 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 goto out;
534 rpc_delay(task, NFS4_POLL_RETRY_MAX);
535 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400536}
537
Trond Myklebust14516c32010-07-31 14:29:06 -0400538static int nfs4_sequence_done(struct rpc_task *task,
539 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400540{
Trond Myklebust14516c32010-07-31 14:29:06 -0400541 if (res->sr_session == NULL)
542 return 1;
543 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400544}
545
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400546/*
Benny Halevy510b8172009-04-01 09:22:14 -0400547 * nfs4_find_slot - efficiently look for a free slot
548 *
549 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
550 * If found, we mark the slot as used, update the highest_used_slotid,
551 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500552 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400553 *
554 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400555 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500556static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800557nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400558{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500559 u32 slotid;
560 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400561
Trond Myklebust45d43c22012-02-06 19:38:51 -0500562 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400563 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
564 tbl->max_slots);
565 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
566 if (slotid >= tbl->max_slots)
567 goto out;
568 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500569 if (slotid > tbl->highest_used_slotid ||
570 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400571 tbl->highest_used_slotid = slotid;
572 ret_id = slotid;
573out:
574 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
575 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400576 return ret_id;
577}
578
Trond Myklebust9d12b212012-01-17 22:04:25 -0500579static void nfs41_init_sequence(struct nfs4_sequence_args *args,
580 struct nfs4_sequence_res *res, int cache_reply)
581{
582 args->sa_session = NULL;
583 args->sa_cache_this = 0;
584 if (cache_reply)
585 args->sa_cache_this = 1;
586 res->sr_session = NULL;
587 res->sr_slot = NULL;
588}
589
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000590int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 struct nfs4_sequence_args *args,
592 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct rpc_task *task)
594{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595 struct nfs4_slot *slot;
596 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500597 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400598
599 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400601 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 return 0;
603
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400604 tbl = &session->fc_slot_table;
605
606 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400607 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800608 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500609 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500610 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400611 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500612 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500613 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400614 }
615
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800616 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
617 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
618 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
619 spin_unlock(&tbl->slot_tbl_lock);
620 dprintk("%s enforce FIFO order\n", __func__);
621 return -EAGAIN;
622 }
623
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800624 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500625 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400626 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
627 spin_unlock(&tbl->slot_tbl_lock);
628 dprintk("<-- %s: no free slots\n", __func__);
629 return -EAGAIN;
630 }
631 spin_unlock(&tbl->slot_tbl_lock);
632
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800633 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400634 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400635 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400636 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400637
638 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
639
Benny Halevy99fe60d2009-04-01 09:22:29 -0400640 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400641 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400643 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644 /*
645 * sr_status is only set in decode_sequence, and so will remain
646 * set to 1 if an rpc level failure occurs.
647 */
648 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649 return 0;
650}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000651EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652
Trond Myklebust035168a2010-06-16 09:52:26 -0400653int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 struct nfs4_sequence_args *args,
655 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 struct rpc_task *task)
657{
Trond Myklebust035168a2010-06-16 09:52:26 -0400658 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659 int ret = 0;
660
Trond Myklebust9d12b212012-01-17 22:04:25 -0500661 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400663
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200664 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400665 __func__, session->clp, session, res->sr_slot ?
666 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400667
Trond Myklebust9d12b212012-01-17 22:04:25 -0500668 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669out:
670 dprintk("<-- %s status=%d\n", __func__, ret);
671 return ret;
672}
673
674struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400675 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676 struct nfs4_sequence_args *seq_args;
677 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678};
679
680static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
681{
682 struct nfs41_call_sync_data *data = calldata;
683
Trond Myklebust035168a2010-06-16 09:52:26 -0400684 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
685
686 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500687 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 return;
689 rpc_call_start(task);
690}
691
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800692static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
693{
694 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
695 nfs41_call_sync_prepare(task, calldata);
696}
697
Andy Adamson69ab40c2009-04-01 09:22:19 -0400698static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
699{
700 struct nfs41_call_sync_data *data = calldata;
701
Trond Myklebust14516c32010-07-31 14:29:06 -0400702 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400703}
704
Trond Myklebust17280172012-03-11 13:11:00 -0400705static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400707 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708};
709
Trond Myklebust17280172012-03-11 13:11:00 -0400710static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800711 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
712 .rpc_call_done = nfs41_call_sync_done,
713};
714
Bryan Schumaker7c513052011-03-24 17:12:24 +0000715static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
716 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717 struct rpc_message *msg,
718 struct nfs4_sequence_args *args,
719 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800720 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721{
722 int ret;
723 struct rpc_task *task;
724 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400725 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726 .seq_args = args,
727 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 };
729 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000730 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731 .rpc_message = msg,
732 .callback_ops = &nfs41_call_sync_ops,
733 .callback_data = &data
734 };
735
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800736 if (privileged)
737 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400738 task = rpc_run_task(&task_setup);
739 if (IS_ERR(task))
740 ret = PTR_ERR(task);
741 else {
742 ret = task->tk_status;
743 rpc_put_task(task);
744 }
745 return ret;
746}
747
Bryan Schumaker7c513052011-03-24 17:12:24 +0000748int _nfs4_call_sync_session(struct rpc_clnt *clnt,
749 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400750 struct rpc_message *msg,
751 struct nfs4_sequence_args *args,
752 struct nfs4_sequence_res *res,
753 int cache_reply)
754{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500755 nfs41_init_sequence(args, res, cache_reply);
756 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400757}
758
Trond Myklebustdf896452010-06-16 09:52:26 -0400759#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500760static inline
761void nfs41_init_sequence(struct nfs4_sequence_args *args,
762 struct nfs4_sequence_res *res, int cache_reply)
763{
764}
765
Trond Myklebust14516c32010-07-31 14:29:06 -0400766static int nfs4_sequence_done(struct rpc_task *task,
767 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400768{
Trond Myklebust14516c32010-07-31 14:29:06 -0400769 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400770}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400771#endif /* CONFIG_NFS_V4_1 */
772
Bryan Schumaker7c513052011-03-24 17:12:24 +0000773int _nfs4_call_sync(struct rpc_clnt *clnt,
774 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400775 struct rpc_message *msg,
776 struct nfs4_sequence_args *args,
777 struct nfs4_sequence_res *res,
778 int cache_reply)
779{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500780 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400782}
783
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000784static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000785int nfs4_call_sync(struct rpc_clnt *clnt,
786 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000787 struct rpc_message *msg,
788 struct nfs4_sequence_args *args,
789 struct nfs4_sequence_res *res,
790 int cache_reply)
791{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000792 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
793 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000794}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400795
Trond Myklebust38478b22006-05-25 01:40:57 -0400796static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Trond Myklebust38478b22006-05-25 01:40:57 -0400798 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Trond Myklebust38478b22006-05-25 01:40:57 -0400800 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400801 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700802 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400803 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700804 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400809 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810 struct nfs_openargs o_arg;
811 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100812 struct nfs_open_confirmargs c_arg;
813 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 struct nfs4_string owner_name;
815 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400820 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100822 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400823 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100824 int rpc_status;
825 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826};
827
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400828
829static void nfs4_init_opendata_res(struct nfs4_opendata *p)
830{
831 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400832 p->o_res.seqid = p->o_arg.seqid;
833 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400834 p->o_res.server = p->o_arg.server;
835 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500836 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400837}
838
Al Viro82a2c1b2011-06-22 18:30:55 -0400839static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400841 const struct iattr *attrs,
842 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843{
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct inode *dir = parent->d_inode;
846 struct nfs_server *server = NFS_SERVER(dir);
847 struct nfs4_opendata *p;
848
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400849 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 if (p == NULL)
851 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400852 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 if (p->o_arg.seqid == NULL)
854 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400855 nfs_sb_active(dentry->d_sb);
856 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 p->dir = parent;
858 p->owner = sp;
859 atomic_inc(&sp->so_count);
860 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861 p->o_arg.open_flags = flags;
862 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400863 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400864 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
865 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400866 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 p->o_arg.server = server;
868 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400869 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500871 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500872 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400873
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100874 p->o_arg.u.attrs = &p->attrs;
875 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500876
877 verf[0] = jiffies;
878 verf[1] = current->pid;
879 memcpy(p->o_arg.u.verifier.data, verf,
880 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100882 p->c_arg.fh = &p->o_res.fh;
883 p->c_arg.stateid = &p->o_res.stateid;
884 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400885 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400886 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100887 return p;
888err_free:
889 kfree(p);
890err:
891 dput(parent);
892 return NULL;
893}
894
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400895static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100896{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400897 struct nfs4_opendata *p = container_of(kref,
898 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400899 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400900
901 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 if (p->state != NULL)
903 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400904 nfs4_put_state_owner(p->owner);
905 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400906 dput(p->dentry);
907 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500908 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400909 kfree(p);
910}
911
912static void nfs4_opendata_put(struct nfs4_opendata *p)
913{
914 if (p != NULL)
915 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100916}
917
Trond Myklebust06f814a2006-01-03 09:55:07 +0100918static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
919{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920 int ret;
921
Trond Myklebust06f814a2006-01-03 09:55:07 +0100922 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100923 return ret;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400927{
928 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929
Trond Myklebust536e43d2012-01-17 22:04:26 -0500930 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 goto out;
932 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400933 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500934 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
935 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400936 break;
937 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500938 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
939 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 break;
941 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500942 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
943 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400946 return ret;
947}
948
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500951 if (delegation == NULL)
952 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500955 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400956 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500957 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 return 1;
959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100962{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100964 case FMODE_WRITE:
965 state->n_wronly++;
966 break;
967 case FMODE_READ:
968 state->n_rdonly++;
969 break;
970 case FMODE_READ|FMODE_WRITE:
971 state->n_rdwr++;
972 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100974}
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400977{
978 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500979 nfs4_stateid_copy(&state->stateid, stateid);
980 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400982 case FMODE_READ:
983 set_bit(NFS_O_RDONLY_STATE, &state->flags);
984 break;
985 case FMODE_WRITE:
986 set_bit(NFS_O_WRONLY_STATE, &state->flags);
987 break;
988 case FMODE_READ|FMODE_WRITE:
989 set_bit(NFS_O_RDWR_STATE, &state->flags);
990 }
991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400994{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400995 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400997 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400998}
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001002 /*
1003 * Protect the call to nfs4_state_set_mode_locked and
1004 * serialise the stateid update
1005 */
1006 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001007 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001008 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009 set_bit(NFS_DELEGATED_STATE, &state->flags);
1010 }
1011 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 write_sequnlock(&state->seqlock);
1014 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001016 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001020{
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *deleg_cur;
1023 int ret = 0;
1024
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001026
1027 rcu_read_lock();
1028 deleg_cur = rcu_dereference(nfsi->delegation);
1029 if (deleg_cur == NULL)
1030 goto no_delegation;
1031
1032 spin_lock(&deleg_cur->lock);
1033 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001035 goto no_delegation_unlock;
1036
1037 if (delegation == NULL)
1038 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001039 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001040 goto no_delegation_unlock;
1041
Trond Myklebustb7391f42008-12-23 15:21:52 -05001042 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001044 ret = 1;
1045no_delegation_unlock:
1046 spin_unlock(&deleg_cur->lock);
1047no_delegation:
1048 rcu_read_unlock();
1049
1050 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001052 ret = 1;
1053 }
1054
1055 return ret;
1056}
1057
1058
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060{
1061 struct nfs_delegation *delegation;
1062
1063 rcu_read_lock();
1064 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066 rcu_read_unlock();
1067 return;
1068 }
1069 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001070 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071}
1072
Trond Myklebust6ee41262007-07-08 14:11:36 -04001073static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074{
1075 struct nfs4_state *state = opendata->state;
1076 struct nfs_inode *nfsi = NFS_I(state->inode);
1077 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001078 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001079 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 nfs4_stateid stateid;
1081 int ret = -EAGAIN;
1082
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (can_open_cached(state, fmode, open_mode)) {
1087 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001089 goto out_return_state;
1090 }
1091 spin_unlock(&state->owner->so_lock);
1092 }
Trond Myklebust34310432008-12-23 15:21:38 -05001093 rcu_read_lock();
1094 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001095 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001096 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001098 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001100 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001102 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 if (ret != 0)
1104 goto out;
1105 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001106
1107 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001109 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111out:
1112 return ERR_PTR(ret);
1113out_return_state:
1114 atomic_inc(&state->count);
1115 return state;
1116}
1117
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001118static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1119{
1120 struct inode *inode;
1121 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001122 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001123 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124
Trond Myklebustaac00a82007-07-05 19:02:21 -04001125 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001126 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 goto out;
1128 }
1129
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001133 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001135 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001136 goto err;
1137 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138 state = nfs4_get_open_state(inode, data->owner);
1139 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001140 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001141 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001142 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001143 int delegation_flags = 0;
1144
Trond Myklebust003707c2007-07-05 18:07:55 -04001145 rcu_read_lock();
1146 delegation = rcu_dereference(NFS_I(inode)->delegation);
1147 if (delegation)
1148 delegation_flags = delegation->flags;
1149 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001150 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1151 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1152 "returning a delegation for "
1153 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001154 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001155 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001156 nfs_inode_set_delegation(state->inode,
1157 data->owner->so_cred,
1158 &data->o_res);
1159 else
1160 nfs_inode_reclaim_delegation(state->inode,
1161 data->owner->so_cred,
1162 &data->o_res);
1163 }
Trond Myklebust34310432008-12-23 15:21:38 -05001164
1165 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001166 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001167 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001168out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001169 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001170err_put_inode:
1171 iput(inode);
1172err:
1173 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001174}
1175
Trond Myklebust864472e2006-01-03 09:55:15 +01001176static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1177{
1178 struct nfs_inode *nfsi = NFS_I(state->inode);
1179 struct nfs_open_context *ctx;
1180
1181 spin_lock(&state->inode->i_lock);
1182 list_for_each_entry(ctx, &nfsi->open_files, list) {
1183 if (ctx->state != state)
1184 continue;
1185 get_nfs_open_context(ctx);
1186 spin_unlock(&state->inode->i_lock);
1187 return ctx;
1188 }
1189 spin_unlock(&state->inode->i_lock);
1190 return ERR_PTR(-ENOENT);
1191}
1192
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001193static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1194{
1195 struct nfs4_opendata *opendata;
1196
Al Viro3d4ff432011-06-22 18:40:12 -04001197 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001198 if (opendata == NULL)
1199 return ERR_PTR(-ENOMEM);
1200 opendata->state = state;
1201 atomic_inc(&state->count);
1202 return opendata;
1203}
1204
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001205static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001206{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 int ret;
1209
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 opendata->o_arg.open_flags = 0;
1211 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1213 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1214 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001215 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 if (ret != 0)
1217 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219 if (IS_ERR(newstate))
1220 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001221 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 return 0;
1224}
1225
1226static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1227{
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 int ret;
1230
1231 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 smp_rmb();
1234 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001235 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 if (ret != 0)
1238 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001239 if (newstate != state)
1240 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 }
1242 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001243 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 if (ret != 0)
1246 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 if (newstate != state)
1248 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 }
1250 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001251 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 if (ret != 0)
1254 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 if (newstate != state)
1256 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001258 /*
1259 * We may have performed cached opens for all three recoveries.
1260 * Check if we need to update the current stateid.
1261 */
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001263 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001264 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001266 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001267 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 return 0;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
1273 * OPEN_RECLAIM:
1274 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001278 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001280 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int status;
1282
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001283 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1284 if (IS_ERR(opendata))
1285 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1287 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001290 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001291 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001292 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 opendata->o_arg.u.delegation_type = delegation_type;
1294 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return status;
1297}
1298
Trond Myklebust539cd032007-06-05 11:46:42 -04001299static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs_server *server = NFS_SERVER(state->inode);
1302 struct nfs4_exception exception = { };
1303 int err;
1304 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001305 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001306 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001307 break;
1308 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 } while (exception.retry);
1310 return err;
1311}
1312
Trond Myklebust864472e2006-01-03 09:55:15 +01001313static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1314{
1315 struct nfs_open_context *ctx;
1316 int ret;
1317
1318 ctx = nfs4_state_find_open_context(state);
1319 if (IS_ERR(ctx))
1320 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 put_nfs_open_context(ctx);
1323 return ret;
1324}
1325
Trond Myklebust13437e12007-07-06 15:10:43 -04001326static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001328 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001331 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1332 if (IS_ERR(opendata))
1333 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001334 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001336 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001337 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Trond Myklebust13437e12007-07-06 15:10:43 -04001341int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001344 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int err;
1346 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001347 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 switch (err) {
1349 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350 case -ENOENT:
1351 case -ESTALE:
1352 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001353 case -NFS4ERR_BADSESSION:
1354 case -NFS4ERR_BADSLOT:
1355 case -NFS4ERR_BAD_HIGH_SLOT:
1356 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1357 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001358 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 case -NFS4ERR_STALE_CLIENTID:
1361 case -NFS4ERR_STALE_STATEID:
1362 case -NFS4ERR_EXPIRED:
1363 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001364 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001365 goto out;
1366 case -ERESTARTSYS:
1367 /*
1368 * The show must go on: exit, but mark the
1369 * stateid as needing recovery.
1370 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001371 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001372 case -NFS4ERR_ADMIN_REVOKED:
1373 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001374 nfs_inode_find_state_and_recover(state->inode,
1375 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001376 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001377 case -EKEYEXPIRED:
1378 /*
1379 * User RPCSEC_GSS context has expired.
1380 * We cannot recover this stateid now, so
1381 * skip it and allow recovery thread to
1382 * proceed.
1383 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001384 case -ENOMEM:
1385 err = 0;
1386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 err = nfs4_handle_exception(server, err, &exception);
1389 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001390out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return err;
1392}
1393
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1395{
1396 struct nfs4_opendata *data = calldata;
1397
1398 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001399 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001400 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001401 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001402 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001403 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001404 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405}
1406
1407static void nfs4_open_confirm_release(void *calldata)
1408{
1409 struct nfs4_opendata *data = calldata;
1410 struct nfs4_state *state = NULL;
1411
1412 /* If this request hasn't been cancelled, do nothing */
1413 if (data->cancelled == 0)
1414 goto out_free;
1415 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001416 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001419 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001420 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001421out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001422 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423}
1424
1425static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426 .rpc_call_done = nfs4_open_confirm_done,
1427 .rpc_release = nfs4_open_confirm_release,
1428};
1429
1430/*
1431 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1432 */
1433static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1434{
1435 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1436 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001437 struct rpc_message msg = {
1438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1439 .rpc_argp = &data->c_arg,
1440 .rpc_resp = &data->c_res,
1441 .rpc_cred = data->owner->so_cred,
1442 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001443 struct rpc_task_setup task_setup_data = {
1444 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001445 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001446 .callback_ops = &nfs4_open_confirm_ops,
1447 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001448 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001449 .flags = RPC_TASK_ASYNC,
1450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int status;
1452
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001453 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001454 data->rpc_done = 0;
1455 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001457 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001458 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001459 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001460 status = nfs4_wait_for_completion_rpc_task(task);
1461 if (status != 0) {
1462 data->cancelled = 1;
1463 smp_wmb();
1464 } else
1465 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001466 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return status;
1468}
1469
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 struct nfs4_opendata *data = calldata;
1473 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001474
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1476 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001477 /*
1478 * Check if we still need to send an OPEN call, or if we can use
1479 * a delegation instead.
1480 */
1481 if (data->state != NULL) {
1482 struct nfs_delegation *delegation;
1483
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001484 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001485 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001486 rcu_read_lock();
1487 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001488 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1489 can_open_delegated(delegation, data->o_arg.fmode))
1490 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001491 rcu_read_unlock();
1492 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001493 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001494 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001495 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001496 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001497 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1498 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001499 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001500 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001501 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001502 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001503 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001504 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001505 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001506unlock_no_action:
1507 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001508out_no_action:
1509 task->tk_action = NULL;
1510
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001513static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1514{
1515 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1516 nfs4_open_prepare(task, calldata);
1517}
1518
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519static void nfs4_open_done(struct rpc_task *task, void *calldata)
1520{
1521 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001524
Trond Myklebust14516c32010-07-31 14:29:06 -04001525 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1526 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001527
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 if (task->tk_status == 0) {
1529 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 case S_IFREG:
1531 break;
1532 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534 break;
1535 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001537 break;
1538 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001540 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001541 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001542 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1543 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001544 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001545 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001547
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548static void nfs4_open_release(void *calldata)
1549{
1550 struct nfs4_opendata *data = calldata;
1551 struct nfs4_state *state = NULL;
1552
1553 /* If this request hasn't been cancelled, do nothing */
1554 if (data->cancelled == 0)
1555 goto out_free;
1556 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001557 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 goto out_free;
1559 /* In case we need an open_confirm, no cleanup! */
1560 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1561 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001563 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001564 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001565out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001566 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567}
1568
1569static const struct rpc_call_ops nfs4_open_ops = {
1570 .rpc_call_prepare = nfs4_open_prepare,
1571 .rpc_call_done = nfs4_open_done,
1572 .rpc_release = nfs4_open_release,
1573};
1574
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001575static const struct rpc_call_ops nfs4_recover_open_ops = {
1576 .rpc_call_prepare = nfs4_recover_open_prepare,
1577 .rpc_call_done = nfs4_open_done,
1578 .rpc_release = nfs4_open_release,
1579};
1580
1581static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001582{
1583 struct inode *dir = data->dir->d_inode;
1584 struct nfs_server *server = NFS_SERVER(dir);
1585 struct nfs_openargs *o_arg = &data->o_arg;
1586 struct nfs_openres *o_res = &data->o_res;
1587 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001588 struct rpc_message msg = {
1589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1590 .rpc_argp = o_arg,
1591 .rpc_resp = o_res,
1592 .rpc_cred = data->owner->so_cred,
1593 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001594 struct rpc_task_setup task_setup_data = {
1595 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001596 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001597 .callback_ops = &nfs4_open_ops,
1598 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001599 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001600 .flags = RPC_TASK_ASYNC,
1601 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001602 int status;
1603
Trond Myklebust9d12b212012-01-17 22:04:25 -05001604 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001605 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001606 data->rpc_done = 0;
1607 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001608 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001609 if (isrecover)
1610 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001611 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001612 if (IS_ERR(task))
1613 return PTR_ERR(task);
1614 status = nfs4_wait_for_completion_rpc_task(task);
1615 if (status != 0) {
1616 data->cancelled = 1;
1617 smp_wmb();
1618 } else
1619 status = data->rpc_status;
1620 rpc_put_task(task);
1621
1622 return status;
1623}
1624
1625static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1626{
1627 struct inode *dir = data->dir->d_inode;
1628 struct nfs_openres *o_res = &data->o_res;
1629 int status;
1630
1631 status = nfs4_run_open_task(data, 1);
1632 if (status != 0 || !data->rpc_done)
1633 return status;
1634
Trond Myklebust6926afd2012-01-07 13:22:46 -05001635 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1636
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001637 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1638 status = _nfs4_proc_open_confirm(data);
1639 if (status != 0)
1640 return status;
1641 }
1642
1643 return status;
1644}
1645
1646/*
1647 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1648 */
1649static int _nfs4_proc_open(struct nfs4_opendata *data)
1650{
1651 struct inode *dir = data->dir->d_inode;
1652 struct nfs_server *server = NFS_SERVER(dir);
1653 struct nfs_openargs *o_arg = &data->o_arg;
1654 struct nfs_openres *o_res = &data->o_res;
1655 int status;
1656
1657 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001658 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001660 if (status != 0) {
1661 if (status == -NFS4ERR_BADNAME &&
1662 !(o_arg->open_flags & O_CREAT))
1663 return -ENOENT;
1664 return status;
1665 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666
Trond Myklebust6926afd2012-01-07 13:22:46 -05001667 nfs_fattr_map_and_free_names(server, &data->f_attr);
1668
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001669 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001670 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001671 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1672 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001674 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001679 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Andy Adamsond83217c2011-03-01 01:34:17 +00001683static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001684{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001685 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001686 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001687
Trond Myklebusta78cb572009-08-09 15:06:19 -04001688 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001689 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001690 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001691 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001692 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1693 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001694 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001695 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001696 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001697 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001698 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001699}
1700
Andy Adamsond83217c2011-03-01 01:34:17 +00001701static int nfs4_recover_expired_lease(struct nfs_server *server)
1702{
1703 return nfs4_client_recover_expired_lease(server->nfs_client);
1704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * OPEN_EXPIRED:
1708 * reclaim state on the server after a network partition.
1709 * Assumes caller holds the appropriate lock
1710 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001711static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001713 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001716 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1717 if (IS_ERR(opendata))
1718 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001720 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001721 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001722 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001726static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727{
Trond Myklebust539cd032007-06-05 11:46:42 -04001728 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729 struct nfs4_exception exception = { };
1730 int err;
1731
1732 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001733 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001734 switch (err) {
1735 default:
1736 goto out;
1737 case -NFS4ERR_GRACE:
1738 case -NFS4ERR_DELAY:
1739 nfs4_handle_exception(server, err, &exception);
1740 err = 0;
1741 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001742 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001743out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001744 return err;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001750 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 ctx = nfs4_state_find_open_context(state);
1753 if (IS_ERR(ctx))
1754 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001755 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001756 put_nfs_open_context(ctx);
1757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001761static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001764 nfs4_stateid *stateid = &state->stateid;
1765 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001766
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001767 /* If a state reset has been done, test_stateid is unneeded */
1768 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1769 return;
1770
1771 status = nfs41_test_stateid(server, stateid);
1772 if (status != NFS_OK) {
1773 /* Free the stateid unless the server explicitly
1774 * informs us the stateid is unrecognized. */
1775 if (status != -NFS4ERR_BAD_STATEID)
1776 nfs41_free_stateid(server, stateid);
1777
1778 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1779 }
1780}
1781
1782/**
1783 * nfs41_check_open_stateid - possibly free an open stateid
1784 *
1785 * @state: NFSv4 state for an inode
1786 *
1787 * Returns NFS_OK if recovery for this stateid is now finished.
1788 * Otherwise a negative NFS4ERR value is returned.
1789 */
1790static int nfs41_check_open_stateid(struct nfs4_state *state)
1791{
1792 struct nfs_server *server = NFS_SERVER(state->inode);
1793 nfs4_stateid *stateid = &state->stateid;
1794 int status;
1795
1796 /* If a state reset has been done, test_stateid is unneeded */
1797 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1798 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1799 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1800 return -NFS4ERR_BAD_STATEID;
1801
1802 status = nfs41_test_stateid(server, stateid);
1803 if (status != NFS_OK) {
1804 /* Free the stateid unless the server explicitly
1805 * informs us the stateid is unrecognized. */
1806 if (status != -NFS4ERR_BAD_STATEID)
1807 nfs41_free_stateid(server, stateid);
1808
1809 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1810 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1811 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001812 }
1813 return status;
1814}
1815
1816static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1817{
Chuck Levereb64cf92012-07-11 16:30:05 -04001818 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001819
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001820 nfs41_clear_delegation_stateid(state);
1821 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001822 if (status != NFS_OK)
1823 status = nfs4_open_expired(sp, state);
1824 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001825}
1826#endif
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001829 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1830 * fields corresponding to attributes that were used to store the verifier.
1831 * Make sure we clobber those fields in the later setattr call
1832 */
1833static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1834{
1835 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1836 !(sattr->ia_valid & ATTR_ATIME_SET))
1837 sattr->ia_valid |= ATTR_ATIME;
1838
1839 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1840 !(sattr->ia_valid & ATTR_MTIME_SET))
1841 sattr->ia_valid |= ATTR_MTIME;
1842}
1843
1844/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
Andy Adamson82be4172012-05-23 05:02:35 -04001847static int _nfs4_do_open(struct inode *dir,
1848 struct dentry *dentry,
1849 fmode_t fmode,
1850 int flags,
1851 struct iattr *sattr,
1852 struct rpc_cred *cred,
1853 struct nfs4_state **res,
1854 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 struct nfs4_state_owner *sp;
1857 struct nfs4_state *state = NULL;
1858 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001859 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001860 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001864 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1865 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1867 goto out_err;
1868 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001869 status = nfs4_recover_expired_lease(server);
1870 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001871 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001872 if (dentry->d_inode != NULL)
1873 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001874 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001875 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001876 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001877 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Andy Adamson82be4172012-05-23 05:02:35 -04001879 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1880 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1881 if (!opendata->f_attr.mdsthreshold)
1882 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001883 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001884 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001885 if (dentry->d_inode != NULL)
1886 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001887
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001888 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001890 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001892 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001893 status = PTR_ERR(state);
1894 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001895 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001896 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001897 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001898
1899 if (opendata->o_arg.open_flags & O_EXCL) {
1900 nfs4_exclusive_attrset(opendata, sattr);
1901
1902 nfs_fattr_init(opendata->o_res.f_attr);
1903 status = nfs4_do_setattr(state->inode, cred,
1904 opendata->o_res.f_attr, sattr,
1905 state);
1906 if (status == 0)
1907 nfs_setattr_update_inode(state->inode, sattr);
1908 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1909 }
Andy Adamson82be4172012-05-23 05:02:35 -04001910
1911 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1912 *ctx_th = opendata->f_attr.mdsthreshold;
1913 else
1914 kfree(opendata->f_attr.mdsthreshold);
1915 opendata->f_attr.mdsthreshold = NULL;
1916
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001917 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 *res = state;
1920 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001921err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001922 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001923 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001924err_put_state_owner:
1925 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *res = NULL;
1928 return status;
1929}
1930
1931
Andy Adamson82be4172012-05-23 05:02:35 -04001932static struct nfs4_state *nfs4_do_open(struct inode *dir,
1933 struct dentry *dentry,
1934 fmode_t fmode,
1935 int flags,
1936 struct iattr *sattr,
1937 struct rpc_cred *cred,
1938 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct nfs4_exception exception = { };
1941 struct nfs4_state *res;
1942 int status;
1943
Trond Myklebust2d0dbc62012-06-08 10:58:09 -04001944 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001946 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1947 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (status == 0)
1949 break;
1950 /* NOTE: BAD_SEQID means the server and client disagree about the
1951 * book-keeping w.r.t. state-changing operations
1952 * (OPEN/CLOSE/LOCK/LOCKU...)
1953 * It is actually a sign of a bug on the client or on the server.
1954 *
1955 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001956 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * have unhashed the old state_owner for us, and that we can
1958 * therefore safely retry using a new one. We should still warn
1959 * the user though...
1960 */
1961 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001962 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001963 " returned a bad sequence-id error!\n",
1964 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 exception.retry = 1;
1966 continue;
1967 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001968 /*
1969 * BAD_STATEID on OPEN means that the server cancelled our
1970 * state before it received the OPEN_CONFIRM.
1971 * Recover by retrying the request as per the discussion
1972 * on Page 181 of RFC3530.
1973 */
1974 if (status == -NFS4ERR_BAD_STATEID) {
1975 exception.retry = 1;
1976 continue;
1977 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001978 if (status == -EAGAIN) {
1979 /* We must have found a delegation */
1980 exception.retry = 1;
1981 continue;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1984 status, &exception));
1985 } while (exception.retry);
1986 return res;
1987}
1988
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001989static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1990 struct nfs_fattr *fattr, struct iattr *sattr,
1991 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001993 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001995 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 .iap = sattr,
1997 .server = server,
1998 .bitmask = server->attr_bitmask,
1999 };
2000 struct nfs_setattrres res = {
2001 .fattr = fattr,
2002 .server = server,
2003 };
2004 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2006 .rpc_argp = &arg,
2007 .rpc_resp = &res,
2008 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002010 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002011 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Trond Myklebust0e574af2005-10-27 22:12:38 -04002013 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Trond Myklebust4fc87962012-03-08 17:42:01 -05002015 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002016 struct nfs_lockowner lockowner = {
2017 .l_owner = current->files,
2018 .l_pid = current->tgid,
2019 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002020 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002021 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002022 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2023 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002024 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002025 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002026 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Bryan Schumaker7c513052011-03-24 17:12:24 +00002028 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002029 if (status == 0 && state != NULL)
2030 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002031 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002034static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2035 struct nfs_fattr *fattr, struct iattr *sattr,
2036 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002038 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002039 struct nfs4_exception exception = {
2040 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002041 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002042 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 int err;
2044 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002045 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2046 switch (err) {
2047 case -NFS4ERR_OPENMODE:
2048 if (state && !(state->state & FMODE_WRITE)) {
2049 err = -EBADF;
2050 if (sattr->ia_valid & ATTR_OPEN)
2051 err = -EACCES;
2052 goto out;
2053 }
2054 }
2055 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002057out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return err;
2059}
2060
2061struct nfs4_closedata {
2062 struct inode *inode;
2063 struct nfs4_state *state;
2064 struct nfs_closeargs arg;
2065 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002066 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002067 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002068 bool roc;
2069 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070};
2071
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002072static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002073{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002074 struct nfs4_closedata *calldata = data;
2075 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002076 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002077
Fred Isamanf7e89172011-01-06 11:36:32 +00002078 if (calldata->roc)
2079 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002080 nfs4_put_open_state(calldata->state);
2081 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002082 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002083 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002084 kfree(calldata);
2085}
2086
Trond Myklebust88069f72009-12-08 08:33:16 -05002087static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2088 fmode_t fmode)
2089{
2090 spin_lock(&state->owner->so_lock);
2091 if (!(fmode & FMODE_READ))
2092 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2093 if (!(fmode & FMODE_WRITE))
2094 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2095 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2096 spin_unlock(&state->owner->so_lock);
2097}
2098
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002099static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002101 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct nfs_server *server = NFS_SERVER(calldata->inode);
2104
Chuck Levera3ca5652012-03-01 17:00:40 -05002105 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002106 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2107 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 /* hmm. we are done with the inode, and in the process of freeing
2109 * the state_owner. we keep this around to process errors
2110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 switch (task->tk_status) {
2112 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002113 if (calldata->roc)
2114 pnfs_roc_set_barrier(state->inode,
2115 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002116 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002117 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002118 nfs4_close_clear_stateid_flags(state,
2119 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 break;
2121 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002122 case -NFS4ERR_OLD_STATEID:
2123 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002125 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002128 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2129 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002131 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002132 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002133 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002136static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002138 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002139 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002140 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002141 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002142
Chuck Levera3ca5652012-03-01 17:00:40 -05002143 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002144 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002145 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002146
Trond Myklebust88069f72009-12-08 08:33:16 -05002147 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2148 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002149 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002150 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002151 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002152 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002153 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2154 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2155 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002156 }
2157 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002158 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2159 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2160 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002161 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002162 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002163 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002164
2165 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002166 /* Note: exit _without_ calling nfs4_close_done */
2167 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002168 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002169 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002170
Fred Isamanf7e89172011-01-06 11:36:32 +00002171 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002172 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002173 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002174 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002175 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002176 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002177
Trond Myklebust516a6af2005-10-27 22:12:41 -04002178 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002179 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002180 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002181 &calldata->arg.seq_args,
2182 &calldata->res.seq_res,
2183 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002184 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002186out:
2187 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002191 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002192 .rpc_call_done = nfs4_close_done,
2193 .rpc_release = nfs4_free_closedata,
2194};
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196/*
2197 * It is possible for data to be read/written from a mem-mapped file
2198 * after the sys_close call (which hits the vfs layer as a flush).
2199 * This means that we can't safely call nfsv4 close on a file until
2200 * the inode is cleared. This in turn means that we are not good
2201 * NFSv4 citizens - we do not indicate to the server to update the file's
2202 * share state even when we are done with one of the three share
2203 * stateid's in the inode.
2204 *
2205 * NOTE: Caller must be holding the sp->so_owner semaphore!
2206 */
Al Viro643168c2011-06-22 18:20:23 -04002207int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002209 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002211 struct nfs4_state_owner *sp = state->owner;
2212 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002213 struct rpc_message msg = {
2214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2215 .rpc_cred = state->owner->so_cred,
2216 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002217 struct rpc_task_setup task_setup_data = {
2218 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002219 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002221 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002222 .flags = RPC_TASK_ASYNC,
2223 };
Trond Myklebust95121352005-10-18 14:20:12 -07002224 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002226 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002228 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002229 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002230 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002232 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002233 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002235 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002236 if (calldata->arg.seqid == NULL)
2237 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002238 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002239 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002240 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002241 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002242 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002243 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002244 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002245
Trond Myklebust1174dd12010-12-21 10:52:24 -05002246 msg.rpc_argp = &calldata->arg;
2247 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002248 task_setup_data.callback_data = calldata;
2249 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002250 if (IS_ERR(task))
2251 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002252 status = 0;
2253 if (wait)
2254 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002255 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002256 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002257out_free_calldata:
2258 kfree(calldata);
2259out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002260 if (roc)
2261 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002262 nfs4_put_open_state(state);
2263 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002264 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Trond Myklebust2b484292010-09-17 10:56:51 -04002267static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002268nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct nfs4_state *state;
2271
Trond Myklebust565277f2007-10-15 18:17:53 -04002272 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002273 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2274 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002275 if (IS_ERR(state))
2276 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002277 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002278 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Trond Myklebust1185a552009-12-03 15:54:02 -05002281static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002282{
2283 if (ctx->state == NULL)
2284 return;
2285 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002286 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002287 else
Al Viro643168c2011-06-22 18:20:23 -04002288 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2292{
Benny Halevy43652ad2009-04-01 09:21:54 -04002293 struct nfs4_server_caps_arg args = {
2294 .fhandle = fhandle,
2295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 struct nfs4_server_caps_res res = {};
2297 struct rpc_message msg = {
2298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002299 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 .rpc_resp = &res,
2301 };
2302 int status;
2303
Bryan Schumaker7c513052011-03-24 17:12:24 +00002304 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (status == 0) {
2306 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002307 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2308 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2309 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2310 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2311 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2313 server->caps |= NFS_CAP_ACLS;
2314 if (res.has_links != 0)
2315 server->caps |= NFS_CAP_HARDLINKS;
2316 if (res.has_symlinks != 0)
2317 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002318 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2319 server->caps |= NFS_CAP_FILEID;
2320 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2321 server->caps |= NFS_CAP_MODE;
2322 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2323 server->caps |= NFS_CAP_NLINK;
2324 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2325 server->caps |= NFS_CAP_OWNER;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2327 server->caps |= NFS_CAP_OWNER_GROUP;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2329 server->caps |= NFS_CAP_ATIME;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2331 server->caps |= NFS_CAP_CTIME;
2332 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2333 server->caps |= NFS_CAP_MTIME;
2334
Trond Myklebusta65318b2009-03-11 14:10:28 -04002335 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2336 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2337 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002339 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return status;
2343}
2344
Trond Myklebust55a97592006-06-09 09:34:19 -04002345int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 struct nfs4_exception exception = { };
2348 int err;
2349 do {
2350 err = nfs4_handle_exception(server,
2351 _nfs4_server_capabilities(server, fhandle),
2352 &exception);
2353 } while (exception.retry);
2354 return err;
2355}
2356
2357static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2358 struct nfs_fsinfo *info)
2359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct nfs4_lookup_root_arg args = {
2361 .bitmask = nfs4_fattr_bitmap,
2362 };
2363 struct nfs4_lookup_res res = {
2364 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002365 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 .fh = fhandle,
2367 };
2368 struct rpc_message msg = {
2369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2370 .rpc_argp = &args,
2371 .rpc_resp = &res,
2372 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002373
Trond Myklebust0e574af2005-10-27 22:12:38 -04002374 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002375 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
2378static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2379 struct nfs_fsinfo *info)
2380{
2381 struct nfs4_exception exception = { };
2382 int err;
2383 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002384 err = _nfs4_lookup_root(server, fhandle, info);
2385 switch (err) {
2386 case 0:
2387 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002388 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002389 default:
2390 err = nfs4_handle_exception(server, err, &exception);
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002393out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return err;
2395}
2396
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002397static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2398 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2399{
2400 struct rpc_auth *auth;
2401 int ret;
2402
2403 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002404 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002405 ret = -EIO;
2406 goto out;
2407 }
2408 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002409out:
2410 return ret;
2411}
2412
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002413static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002414 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002416 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002417 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002419 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2420 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002421
2422 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002423 /* AUTH_UNIX is the default flavor if none was specified,
2424 * thus has already been tried. */
2425 if (flav_array[i] == RPC_AUTH_UNIX)
2426 continue;
2427
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002428 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002429 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002430 continue;
2431 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002432 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002433 /*
2434 * -EACCESS could mean that the user doesn't have correct permissions
2435 * to access the mount. It could also mean that we tried to mount
2436 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2437 * existing mount programs don't handle -EACCES very well so it should
2438 * be mapped to -EPERM instead.
2439 */
2440 if (status == -EACCES)
2441 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002442 return status;
2443}
2444
2445/*
2446 * get the file handle for the "/" directory on the server
2447 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002448int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2449 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002450{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002451 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002452 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002453 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2454 /*
2455 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2456 * by nfs4_map_errors() as this function exits.
2457 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002458 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (status == 0)
2460 status = nfs4_server_capabilities(server, fhandle);
2461 if (status == 0)
2462 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002463 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Bryan Schumakerbae36242012-05-10 15:07:31 -04002466static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2467 struct nfs_fsinfo *info)
2468{
2469 int error;
2470 struct nfs_fattr *fattr = info->fattr;
2471
2472 error = nfs4_server_capabilities(server, mntfh);
2473 if (error < 0) {
2474 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2475 return error;
2476 }
2477
2478 error = nfs4_proc_getattr(server, mntfh, fattr);
2479 if (error < 0) {
2480 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2481 return error;
2482 }
2483
2484 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2485 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2486 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2487
2488 return error;
2489}
2490
Manoj Naik6b97fd32006-06-09 09:34:29 -04002491/*
2492 * Get locations and (maybe) other attributes of a referral.
2493 * Note that we'll actually follow the referral later when
2494 * we detect fsid mismatch in inode revalidation
2495 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002496static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2497 const struct qstr *name, struct nfs_fattr *fattr,
2498 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002499{
2500 int status = -ENOMEM;
2501 struct page *page = NULL;
2502 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002503
2504 page = alloc_page(GFP_KERNEL);
2505 if (page == NULL)
2506 goto out;
2507 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2508 if (locations == NULL)
2509 goto out;
2510
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002511 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002512 if (status != 0)
2513 goto out;
2514 /* Make sure server returned a different fsid for the referral */
2515 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002516 dprintk("%s: server did not return a different fsid for"
2517 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002518 status = -EIO;
2519 goto out;
2520 }
Andy Adamson533eb462011-06-13 18:25:56 -04002521 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2522 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002523
Andy Adamson533eb462011-06-13 18:25:56 -04002524 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002525 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002526 memset(fhandle, 0, sizeof(struct nfs_fh));
2527out:
2528 if (page)
2529 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002530 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002531 return status;
2532}
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2535{
2536 struct nfs4_getattr_arg args = {
2537 .fh = fhandle,
2538 .bitmask = server->attr_bitmask,
2539 };
2540 struct nfs4_getattr_res res = {
2541 .fattr = fattr,
2542 .server = server,
2543 };
2544 struct rpc_message msg = {
2545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2546 .rpc_argp = &args,
2547 .rpc_resp = &res,
2548 };
2549
Trond Myklebust0e574af2005-10-27 22:12:38 -04002550 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002551 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2555{
2556 struct nfs4_exception exception = { };
2557 int err;
2558 do {
2559 err = nfs4_handle_exception(server,
2560 _nfs4_proc_getattr(server, fhandle, fattr),
2561 &exception);
2562 } while (exception.retry);
2563 return err;
2564}
2565
2566/*
2567 * The file is not closed if it is opened due to the a request to change
2568 * the size of the file. The open call will not be needed once the
2569 * VFS layer lookup-intents are implemented.
2570 *
2571 * Close is called when the inode is destroyed.
2572 * If we haven't opened the file for O_WRONLY, we
2573 * need to in the size_change case to obtain a stateid.
2574 *
2575 * Got race?
2576 * Because OPEN is always done by name in nfsv4, it is
2577 * possible that we opened a different file by the same
2578 * name. We can recognize this race condition, but we
2579 * can't do anything about it besides returning an error.
2580 *
2581 * This will be fixed with VFS changes (lookup-intent).
2582 */
2583static int
2584nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2585 struct iattr *sattr)
2586{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002587 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002588 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002589 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 int status;
2591
Benny Halevy8a1636c2010-07-14 15:43:57 -04002592 if (pnfs_ld_layoutret_on_setattr(inode))
2593 pnfs_return_layout(inode);
2594
Trond Myklebust0e574af2005-10-27 22:12:38 -04002595 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Andy Adamson26699402012-05-30 16:12:24 -04002597 /* Deal with open(O_TRUNC) */
2598 if (sattr->ia_valid & ATTR_OPEN)
2599 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2600
2601 /* Optimization: if the end result is no change, don't RPC */
2602 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2603 return 0;
2604
Trond Myklebustd5308382005-11-04 15:33:38 -05002605 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002606 if (sattr->ia_valid & ATTR_FILE) {
2607 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002608
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002609 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002610 if (ctx) {
2611 cred = ctx->cred;
2612 state = ctx->state;
2613 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002614 }
2615
2616 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002617 if (status == 0)
2618 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return status;
2620}
2621
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002622static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2623 const struct qstr *name, struct nfs_fh *fhandle,
2624 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002625{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002626 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002627 int status;
2628 struct nfs4_lookup_arg args = {
2629 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002630 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002631 .name = name,
2632 };
2633 struct nfs4_lookup_res res = {
2634 .server = server,
2635 .fattr = fattr,
2636 .fh = fhandle,
2637 };
2638 struct rpc_message msg = {
2639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2640 .rpc_argp = &args,
2641 .rpc_resp = &res,
2642 };
2643
2644 nfs_fattr_init(fattr);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002647 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 dprintk("NFS reply lookup: %d\n", status);
2649 return status;
2650}
2651
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002652static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002653{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002654 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002655 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002656 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2657 fattr->nlink = 2;
2658}
2659
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002660static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2661 struct qstr *name, struct nfs_fh *fhandle,
2662 struct nfs_fattr *fattr)
2663{
2664 struct nfs4_exception exception = { };
2665 struct rpc_clnt *client = *clnt;
2666 int err;
2667 do {
2668 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2669 switch (err) {
2670 case -NFS4ERR_BADNAME:
2671 err = -ENOENT;
2672 goto out;
2673 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002674 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002675 goto out;
2676 case -NFS4ERR_WRONGSEC:
2677 err = -EPERM;
2678 if (client != *clnt)
2679 goto out;
2680
2681 client = nfs4_create_sec_client(client, dir, name);
2682 if (IS_ERR(client))
2683 return PTR_ERR(client);
2684
2685 exception.retry = 1;
2686 break;
2687 default:
2688 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2689 }
2690 } while (exception.retry);
2691
2692out:
2693 if (err == 0)
2694 *clnt = client;
2695 else if (client != *clnt)
2696 rpc_shutdown_client(client);
2697
2698 return err;
2699}
2700
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002701static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002702 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002704 int status;
2705 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002706
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002707 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2708 if (client != NFS_CLIENT(dir)) {
2709 rpc_shutdown_client(client);
2710 nfs_fixup_secinfo_attributes(fattr);
2711 }
2712 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002715struct rpc_clnt *
2716nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2717 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2718{
2719 int status;
2720 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2721
2722 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2723 if (status < 0) {
2724 rpc_shutdown_client(client);
2725 return ERR_PTR(status);
2726 }
2727 return client;
2728}
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2731{
Trond Myklebust76b32992007-08-10 17:45:11 -04002732 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 struct nfs4_accessargs args = {
2734 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002735 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002737 struct nfs4_accessres res = {
2738 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002739 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct rpc_message msg = {
2741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2742 .rpc_argp = &args,
2743 .rpc_resp = &res,
2744 .rpc_cred = entry->cred,
2745 };
2746 int mode = entry->mask;
2747 int status;
2748
2749 /*
2750 * Determine which access bits we want to ask for...
2751 */
2752 if (mode & MAY_READ)
2753 args.access |= NFS4_ACCESS_READ;
2754 if (S_ISDIR(inode->i_mode)) {
2755 if (mode & MAY_WRITE)
2756 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2757 if (mode & MAY_EXEC)
2758 args.access |= NFS4_ACCESS_LOOKUP;
2759 } else {
2760 if (mode & MAY_WRITE)
2761 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2762 if (mode & MAY_EXEC)
2763 args.access |= NFS4_ACCESS_EXECUTE;
2764 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002765
2766 res.fattr = nfs_alloc_fattr();
2767 if (res.fattr == NULL)
2768 return -ENOMEM;
2769
Bryan Schumaker7c513052011-03-24 17:12:24 +00002770 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (!status) {
2772 entry->mask = 0;
2773 if (res.access & NFS4_ACCESS_READ)
2774 entry->mask |= MAY_READ;
2775 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2776 entry->mask |= MAY_WRITE;
2777 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2778 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002779 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002781 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return status;
2783}
2784
2785static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2786{
2787 struct nfs4_exception exception = { };
2788 int err;
2789 do {
2790 err = nfs4_handle_exception(NFS_SERVER(inode),
2791 _nfs4_proc_access(inode, entry),
2792 &exception);
2793 } while (exception.retry);
2794 return err;
2795}
2796
2797/*
2798 * TODO: For the time being, we don't try to get any attributes
2799 * along with any of the zero-copy operations READ, READDIR,
2800 * READLINK, WRITE.
2801 *
2802 * In the case of the first three, we want to put the GETATTR
2803 * after the read-type operation -- this is because it is hard
2804 * to predict the length of a GETATTR response in v4, and thus
2805 * align the READ data correctly. This means that the GETATTR
2806 * may end up partially falling into the page cache, and we should
2807 * shift it into the 'tail' of the xdr_buf before processing.
2808 * To do this efficiently, we need to know the total length
2809 * of data received, which doesn't seem to be available outside
2810 * of the RPC layer.
2811 *
2812 * In the case of WRITE, we also want to put the GETATTR after
2813 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002814 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 *
2816 * Both of these changes to the XDR layer would in fact be quite
2817 * minor, but I decided to leave them for a subsequent patch.
2818 */
2819static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2820 unsigned int pgbase, unsigned int pglen)
2821{
2822 struct nfs4_readlink args = {
2823 .fh = NFS_FH(inode),
2824 .pgbase = pgbase,
2825 .pglen = pglen,
2826 .pages = &page,
2827 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002828 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2831 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002832 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 };
2834
Bryan Schumaker7c513052011-03-24 17:12:24 +00002835 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 -07002836}
2837
2838static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2839 unsigned int pgbase, unsigned int pglen)
2840{
2841 struct nfs4_exception exception = { };
2842 int err;
2843 do {
2844 err = nfs4_handle_exception(NFS_SERVER(inode),
2845 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2846 &exception);
2847 } while (exception.retry);
2848 return err;
2849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002852 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854static int
2855nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002858 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 int status = 0;
2861
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002862 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2863 if (IS_ERR(ctx))
2864 return PTR_ERR(ctx);
2865
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002866 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002867 state = nfs4_do_open(dir, dentry, ctx->mode,
2868 flags, sattr, ctx->cred,
2869 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002870 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (IS_ERR(state)) {
2872 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002875 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002876 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002877 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002879 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return status;
2881}
2882
2883static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2884{
Trond Myklebust16e42952005-10-27 22:12:44 -04002885 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002886 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002888 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002890 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002891 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2895 .rpc_argp = &args,
2896 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002898 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002899
Bryan Schumaker7c513052011-03-24 17:12:24 +00002900 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002901 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002902 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return status;
2904}
2905
2906static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2907{
2908 struct nfs4_exception exception = { };
2909 int err;
2910 do {
2911 err = nfs4_handle_exception(NFS_SERVER(dir),
2912 _nfs4_proc_remove(dir, name),
2913 &exception);
2914 } while (exception.retry);
2915 return err;
2916}
2917
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002918static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002920 struct nfs_server *server = NFS_SERVER(dir);
2921 struct nfs_removeargs *args = msg->rpc_argp;
2922 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002924 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002926 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002929static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2930{
2931 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2932 &data->args.seq_args,
2933 &data->res.seq_res,
2934 task))
2935 return;
2936 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002939static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002941 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2942
Trond Myklebust14516c32010-07-31 14:29:06 -04002943 if (!nfs4_sequence_done(task, &res->seq_res))
2944 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002945 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002946 return 0;
2947 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002948 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Jeff Laytond3d41522010-09-17 17:31:57 -04002951static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2952{
2953 struct nfs_server *server = NFS_SERVER(dir);
2954 struct nfs_renameargs *arg = msg->rpc_argp;
2955 struct nfs_renameres *res = msg->rpc_resp;
2956
2957 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002958 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002959 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002960}
2961
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002962static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2963{
2964 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2965 &data->args.seq_args,
2966 &data->res.seq_res,
2967 task))
2968 return;
2969 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002970}
2971
2972static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2973 struct inode *new_dir)
2974{
2975 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2976
2977 if (!nfs4_sequence_done(task, &res->seq_res))
2978 return 0;
2979 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2980 return 0;
2981
2982 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002983 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002984 return 1;
2985}
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2988 struct inode *new_dir, struct qstr *new_name)
2989{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002990 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002991 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .old_dir = NFS_FH(old_dir),
2993 .new_dir = NFS_FH(new_dir),
2994 .old_name = old_name,
2995 .new_name = new_name,
2996 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002997 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002998 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3002 .rpc_argp = &arg,
3003 .rpc_resp = &res,
3004 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003005 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Bryan Schumaker7c513052011-03-24 17:12:24 +00003007 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (!status) {
3009 update_changeattr(old_dir, &res.old_cinfo);
3010 update_changeattr(new_dir, &res.new_cinfo);
3011 }
3012 return status;
3013}
3014
3015static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3016 struct inode *new_dir, struct qstr *new_name)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020 do {
3021 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3022 _nfs4_proc_rename(old_dir, old_name,
3023 new_dir, new_name),
3024 &exception);
3025 } while (exception.retry);
3026 return err;
3027}
3028
3029static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3030{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003031 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct nfs4_link_arg arg = {
3033 .fh = NFS_FH(inode),
3034 .dir_fh = NFS_FH(dir),
3035 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003036 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003038 struct nfs4_link_res res = {
3039 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 struct rpc_message msg = {
3042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3043 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003044 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003046 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Trond Myklebust136f2622010-04-16 16:22:49 -04003048 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003049 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003050 goto out;
3051
Bryan Schumaker7c513052011-03-24 17:12:24 +00003052 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003053 if (!status) {
3054 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003055 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003056 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003057out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003058 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return status;
3060}
3061
3062static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3063{
3064 struct nfs4_exception exception = { };
3065 int err;
3066 do {
3067 err = nfs4_handle_exception(NFS_SERVER(inode),
3068 _nfs4_proc_link(inode, dir, name),
3069 &exception);
3070 } while (exception.retry);
3071 return err;
3072}
3073
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003074struct nfs4_createdata {
3075 struct rpc_message msg;
3076 struct nfs4_create_arg arg;
3077 struct nfs4_create_res res;
3078 struct nfs_fh fh;
3079 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003080};
3081
3082static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3083 struct qstr *name, struct iattr *sattr, u32 ftype)
3084{
3085 struct nfs4_createdata *data;
3086
3087 data = kzalloc(sizeof(*data), GFP_KERNEL);
3088 if (data != NULL) {
3089 struct nfs_server *server = NFS_SERVER(dir);
3090
3091 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3092 data->msg.rpc_argp = &data->arg;
3093 data->msg.rpc_resp = &data->res;
3094 data->arg.dir_fh = NFS_FH(dir);
3095 data->arg.server = server;
3096 data->arg.name = name;
3097 data->arg.attrs = sattr;
3098 data->arg.ftype = ftype;
3099 data->arg.bitmask = server->attr_bitmask;
3100 data->res.server = server;
3101 data->res.fh = &data->fh;
3102 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003104 }
3105 return data;
3106}
3107
3108static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3109{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003110 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003111 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 if (status == 0) {
3113 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003114 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3115 }
3116 return status;
3117}
3118
3119static void nfs4_free_createdata(struct nfs4_createdata *data)
3120{
3121 kfree(data);
3122}
3123
Chuck Lever4f390c12006-08-22 20:06:22 -04003124static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003125 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 struct nfs4_createdata *data;
3128 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Chuck Lever94a6d752006-08-22 20:06:23 -04003130 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003132
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003133 status = -ENOMEM;
3134 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3135 if (data == NULL)
3136 goto out;
3137
3138 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3139 data->arg.u.symlink.pages = &page;
3140 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003142 status = nfs4_do_create(dir, dentry, data);
3143
3144 nfs4_free_createdata(data);
3145out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return status;
3147}
3148
Chuck Lever4f390c12006-08-22 20:06:22 -04003149static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003150 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 struct nfs4_exception exception = { };
3153 int err;
3154 do {
3155 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003156 _nfs4_proc_symlink(dir, dentry, page,
3157 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3164 struct iattr *sattr)
3165{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003166 struct nfs4_createdata *data;
3167 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3170 if (data == NULL)
3171 goto out;
3172
3173 status = nfs4_do_create(dir, dentry, data);
3174
3175 nfs4_free_createdata(data);
3176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return status;
3178}
3179
3180static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3181 struct iattr *sattr)
3182{
3183 struct nfs4_exception exception = { };
3184 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003185
3186 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 do {
3188 err = nfs4_handle_exception(NFS_SERVER(dir),
3189 _nfs4_proc_mkdir(dir, dentry, sattr),
3190 &exception);
3191 } while (exception.retry);
3192 return err;
3193}
3194
3195static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003196 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct inode *dir = dentry->d_inode;
3199 struct nfs4_readdir_arg args = {
3200 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003201 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 .pgbase = 0,
3203 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003204 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003205 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 };
3207 struct nfs4_readdir_res res;
3208 struct rpc_message msg = {
3209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3210 .rpc_argp = &args,
3211 .rpc_resp = &res,
3212 .rpc_cred = cred,
3213 };
3214 int status;
3215
Harvey Harrison3110ff82008-05-02 13:42:44 -07003216 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003217 dentry->d_parent->d_name.name,
3218 dentry->d_name.name,
3219 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003220 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003222 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 -05003223 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003224 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003225 status += args.pgbase;
3226 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003227
3228 nfs_invalidate_atime(dir);
3229
Harvey Harrison3110ff82008-05-02 13:42:44 -07003230 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return status;
3232}
3233
3234static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003235 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 struct nfs4_exception exception = { };
3238 int err;
3239 do {
3240 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3241 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003242 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 &exception);
3244 } while (exception.retry);
3245 return err;
3246}
3247
3248static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3249 struct iattr *sattr, dev_t rdev)
3250{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251 struct nfs4_createdata *data;
3252 int mode = sattr->ia_mode;
3253 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3256 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003257
3258 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3259 if (data == NULL)
3260 goto out;
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003263 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003265 data->arg.ftype = NF4BLK;
3266 data->arg.u.device.specdata1 = MAJOR(rdev);
3267 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
3269 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003270 data->arg.ftype = NF4CHR;
3271 data->arg.u.device.specdata1 = MAJOR(rdev);
3272 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003275 status = nfs4_do_create(dir, dentry, data);
3276
3277 nfs4_free_createdata(data);
3278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return status;
3280}
3281
3282static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3283 struct iattr *sattr, dev_t rdev)
3284{
3285 struct nfs4_exception exception = { };
3286 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003287
3288 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 do {
3290 err = nfs4_handle_exception(NFS_SERVER(dir),
3291 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3292 &exception);
3293 } while (exception.retry);
3294 return err;
3295}
3296
3297static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3298 struct nfs_fsstat *fsstat)
3299{
3300 struct nfs4_statfs_arg args = {
3301 .fh = fhandle,
3302 .bitmask = server->attr_bitmask,
3303 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003304 struct nfs4_statfs_res res = {
3305 .fsstat = fsstat,
3306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3309 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003310 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 };
3312
Trond Myklebust0e574af2005-10-27 22:12:38 -04003313 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003314 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3318{
3319 struct nfs4_exception exception = { };
3320 int err;
3321 do {
3322 err = nfs4_handle_exception(server,
3323 _nfs4_proc_statfs(server, fhandle, fsstat),
3324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
3329static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3330 struct nfs_fsinfo *fsinfo)
3331{
3332 struct nfs4_fsinfo_arg args = {
3333 .fh = fhandle,
3334 .bitmask = server->attr_bitmask,
3335 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003336 struct nfs4_fsinfo_res res = {
3337 .fsinfo = fsinfo,
3338 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct rpc_message msg = {
3340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3341 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003342 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 };
3344
Bryan Schumaker7c513052011-03-24 17:12:24 +00003345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
3348static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3349{
3350 struct nfs4_exception exception = { };
3351 int err;
3352
3353 do {
3354 err = nfs4_handle_exception(server,
3355 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3356 &exception);
3357 } while (exception.retry);
3358 return err;
3359}
3360
3361static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3362{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003363 int error;
3364
Trond Myklebust0e574af2005-10-27 22:12:38 -04003365 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003366 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3367 if (error == 0)
3368 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3369
3370 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
3373static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3374 struct nfs_pathconf *pathconf)
3375{
3376 struct nfs4_pathconf_arg args = {
3377 .fh = fhandle,
3378 .bitmask = server->attr_bitmask,
3379 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003380 struct nfs4_pathconf_res res = {
3381 .pathconf = pathconf,
3382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3385 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003386 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 };
3388
3389 /* None of the pathconf attributes are mandatory to implement */
3390 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3391 memset(pathconf, 0, sizeof(*pathconf));
3392 return 0;
3393 }
3394
Trond Myklebust0e574af2005-10-27 22:12:38 -04003395 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003396 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3400 struct nfs_pathconf *pathconf)
3401{
3402 struct nfs4_exception exception = { };
3403 int err;
3404
3405 do {
3406 err = nfs4_handle_exception(server,
3407 _nfs4_proc_pathconf(server, fhandle, pathconf),
3408 &exception);
3409 } while (exception.retry);
3410 return err;
3411}
3412
Benny Halevyd20581a2011-05-22 19:52:03 +03003413void __nfs4_read_done_cb(struct nfs_read_data *data)
3414{
Fred Isamancd841602012-04-20 14:47:44 -04003415 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003416}
3417
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003418static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Fred Isamancd841602012-04-20 14:47:44 -04003420 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003422 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003423 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003424 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003426
Benny Halevyd20581a2011-05-22 19:52:03 +03003427 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003429 renew_lease(server, data->timestamp);
3430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003433static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3434{
3435
3436 dprintk("--> %s\n", __func__);
3437
3438 if (!nfs4_sequence_done(task, &data->res.seq_res))
3439 return -EAGAIN;
3440
Benny Halevyd20581a2011-05-22 19:52:03 +03003441 return data->read_done_cb ? data->read_done_cb(task, data) :
3442 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003443}
3444
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003445static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003448 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003449 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003450 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003453static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3454{
Fred Isamancd841602012-04-20 14:47:44 -04003455 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003456 &data->args.seq_args,
3457 &data->res.seq_res,
3458 task))
3459 return;
3460 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
Fred Isamanb029bc92011-03-03 15:13:42 +00003463static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
Fred Isamancd841602012-04-20 14:47:44 -04003465 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003467 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003468 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003469 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003471 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003473 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003474 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Fred Isamanb029bc92011-03-03 15:13:42 +00003478static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3479{
3480 if (!nfs4_sequence_done(task, &data->res.seq_res))
3481 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003482 return data->write_done_cb ? data->write_done_cb(task, data) :
3483 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003484}
3485
Trond Myklebust5a37f852012-04-28 14:55:16 -04003486static
3487bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003488{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003489 const struct nfs_pgio_header *hdr = data->header;
3490
3491 /* Don't request attributes for pNFS or O_DIRECT writes */
3492 if (data->ds_clp != NULL || hdr->dreq != NULL)
3493 return false;
3494 /* Otherwise, request attributes if and only if we don't hold
3495 * a delegation
3496 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003497 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003498}
Fred Isamana69aef12011-03-03 15:13:47 +00003499
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003500static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
Fred Isamancd841602012-04-20 14:47:44 -04003502 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003503
Trond Myklebust5a37f852012-04-28 14:55:16 -04003504 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003505 data->args.bitmask = NULL;
3506 data->res.fattr = NULL;
3507 } else
3508 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003509
Fred Isamanb029bc92011-03-03 15:13:42 +00003510 if (!data->write_done_cb)
3511 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003512 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 data->timestamp = jiffies;
3514
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003515 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003516 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003519static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3520{
Fred Isamancd841602012-04-20 14:47:44 -04003521 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003522 &data->args.seq_args,
3523 &data->res.seq_res,
3524 task))
3525 return;
3526 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Fred Isaman0b7c0152012-04-20 14:47:39 -04003529static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3532 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003533 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return;
3536 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003537}
3538
Fred Isaman0b7c0152012-04-20 14:47:39 -04003539static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
3541 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003542
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003543 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003544 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003545 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
Fred Isaman0b7c0152012-04-20 14:47:39 -04003550static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003551{
3552 if (!nfs4_sequence_done(task, &data->res.seq_res))
3553 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003554 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003555}
3556
Fred Isaman0b7c0152012-04-20 14:47:39 -04003557static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003559 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003560
Fred Isaman0b7c0152012-04-20 14:47:39 -04003561 if (data->commit_done_cb == NULL)
3562 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003563 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003565 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003568struct nfs4_renewdata {
3569 struct nfs_client *client;
3570 unsigned long timestamp;
3571};
3572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573/*
3574 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3575 * standalone procedure for queueing an asynchronous RENEW.
3576 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003577static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003578{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003579 struct nfs4_renewdata *data = calldata;
3580 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003581
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003582 if (atomic_read(&clp->cl_count) > 1)
3583 nfs4_schedule_state_renewal(clp);
3584 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003585 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003586}
3587
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003588static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003590 struct nfs4_renewdata *data = calldata;
3591 struct nfs_client *clp = data->client;
3592 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
3594 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003595 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003596 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3597 return;
3598 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003599 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003600 return;
3601 }
3602 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003604 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003607static const struct rpc_call_ops nfs4_renew_ops = {
3608 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003609 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003610};
3611
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003612static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 struct rpc_message msg = {
3615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3616 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003617 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003619 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003621 if (renew_flags == 0)
3622 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003623 if (!atomic_inc_not_zero(&clp->cl_count))
3624 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003625 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003626 if (data == NULL)
3627 return -ENOMEM;
3628 data->client = clp;
3629 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003631 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003634static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 struct rpc_message msg = {
3637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3638 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003639 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 };
3641 unsigned long now = jiffies;
3642 int status;
3643
3644 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3645 if (status < 0)
3646 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003647 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return 0;
3649}
3650
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003651static inline int nfs4_server_supports_acls(struct nfs_server *server)
3652{
3653 return (server->caps & NFS_CAP_ACLS)
3654 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3655 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3656}
3657
Trond Myklebust21f498c2012-08-24 10:59:25 -04003658/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3659 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003660 * the stack.
3661 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003662#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003663
Neil Hormane9e3d722011-03-04 19:26:03 -05003664static int buf_to_pages_noslab(const void *buf, size_t buflen,
3665 struct page **pages, unsigned int *pgbase)
3666{
3667 struct page *newpage, **spages;
3668 int rc = 0;
3669 size_t len;
3670 spages = pages;
3671
3672 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003673 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003674 newpage = alloc_page(GFP_KERNEL);
3675
3676 if (newpage == NULL)
3677 goto unwind;
3678 memcpy(page_address(newpage), buf, len);
3679 buf += len;
3680 buflen -= len;
3681 *pages++ = newpage;
3682 rc++;
3683 } while (buflen != 0);
3684
3685 return rc;
3686
3687unwind:
3688 for(; rc > 0; rc--)
3689 __free_page(spages[rc-1]);
3690 return -ENOMEM;
3691}
3692
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003693struct nfs4_cached_acl {
3694 int cached;
3695 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003696 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003697};
3698
3699static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003700{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003701 struct nfs_inode *nfsi = NFS_I(inode);
3702
3703 spin_lock(&inode->i_lock);
3704 kfree(nfsi->nfs4_acl);
3705 nfsi->nfs4_acl = acl;
3706 spin_unlock(&inode->i_lock);
3707}
3708
3709static void nfs4_zap_acl_attr(struct inode *inode)
3710{
3711 nfs4_set_cached_acl(inode, NULL);
3712}
3713
3714static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3715{
3716 struct nfs_inode *nfsi = NFS_I(inode);
3717 struct nfs4_cached_acl *acl;
3718 int ret = -ENOENT;
3719
3720 spin_lock(&inode->i_lock);
3721 acl = nfsi->nfs4_acl;
3722 if (acl == NULL)
3723 goto out;
3724 if (buf == NULL) /* user is just asking for length */
3725 goto out_len;
3726 if (acl->cached == 0)
3727 goto out;
3728 ret = -ERANGE; /* see getxattr(2) man page */
3729 if (acl->len > buflen)
3730 goto out;
3731 memcpy(buf, acl->data, acl->len);
3732out_len:
3733 ret = acl->len;
3734out:
3735 spin_unlock(&inode->i_lock);
3736 return ret;
3737}
3738
Sachin Prabhu5794d212012-04-17 14:36:40 +01003739static 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 +00003740{
3741 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003742 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003743
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003744 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003745 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003746 if (acl == NULL)
3747 goto out;
3748 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003749 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003750 } else {
3751 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3752 if (acl == NULL)
3753 goto out;
3754 acl->cached = 0;
3755 }
3756 acl->len = acl_len;
3757out:
3758 nfs4_set_cached_acl(inode, acl);
3759}
3760
Andy Adamsonbf118a32011-12-07 11:55:27 -05003761/*
3762 * The getxattr API returns the required buffer length when called with a
3763 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3764 * the required buf. On a NULL buf, we send a page of data to the server
3765 * guessing that the ACL request can be serviced by a page. If so, we cache
3766 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3767 * the cache. If not so, we throw away the page, and cache the required
3768 * length. The next getxattr call will then produce another round trip to
3769 * the server, this time with the input buf of the required size.
3770 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003771static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003772{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003773 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003774 struct nfs_getaclargs args = {
3775 .fh = NFS_FH(inode),
3776 .acl_pages = pages,
3777 .acl_len = buflen,
3778 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003779 struct nfs_getaclres res = {
3780 .acl_len = buflen,
3781 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003782 struct rpc_message msg = {
3783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3784 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003785 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003786 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003787 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3788 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003789
Andy Adamsonbf118a32011-12-07 11:55:27 -05003790 /* As long as we're doing a round trip to the server anyway,
3791 * let's be prepared for a page of acl data. */
3792 if (npages == 0)
3793 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003794 if (npages > ARRAY_SIZE(pages))
3795 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003796
Andy Adamsonbf118a32011-12-07 11:55:27 -05003797 for (i = 0; i < npages; i++) {
3798 pages[i] = alloc_page(GFP_KERNEL);
3799 if (!pages[i])
3800 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003801 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003802
3803 /* for decoding across pages */
3804 res.acl_scratch = alloc_page(GFP_KERNEL);
3805 if (!res.acl_scratch)
3806 goto out_free;
3807
Andy Adamsonbf118a32011-12-07 11:55:27 -05003808 args.acl_len = npages * PAGE_SIZE;
3809 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003810
Peng Taode040be2012-01-10 22:42:47 +08003811 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003812 __func__, buf, buflen, npages, args.acl_len);
3813 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3814 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 if (ret)
3816 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003817
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003818 /* Handle the case where the passed-in buffer is too short */
3819 if (res.acl_flags & NFS4_ACL_TRUNC) {
3820 /* Did the user only issue a request for the acl length? */
3821 if (buf == NULL)
3822 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003823 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003824 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003825 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003826 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3827 if (buf)
3828 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3829out_ok:
3830 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003832 for (i = 0; i < npages; i++)
3833 if (pages[i])
3834 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003835 if (res.acl_scratch)
3836 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003837 return ret;
3838}
3839
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003840static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3841{
3842 struct nfs4_exception exception = { };
3843 ssize_t ret;
3844 do {
3845 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3846 if (ret >= 0)
3847 break;
3848 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3849 } while (exception.retry);
3850 return ret;
3851}
3852
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003853static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3854{
3855 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003856 int ret;
3857
3858 if (!nfs4_server_supports_acls(server))
3859 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860 ret = nfs_revalidate_inode(server, inode);
3861 if (ret < 0)
3862 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003863 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3864 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003865 ret = nfs4_read_cached_acl(inode, buf, buflen);
3866 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003867 /* -ENOENT is returned if there is no ACL or if there is an ACL
3868 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003869 return ret;
3870 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003871}
3872
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003873static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003874{
3875 struct nfs_server *server = NFS_SERVER(inode);
3876 struct page *pages[NFS4ACL_MAXPAGES];
3877 struct nfs_setaclargs arg = {
3878 .fh = NFS_FH(inode),
3879 .acl_pages = pages,
3880 .acl_len = buflen,
3881 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003882 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003883 struct rpc_message msg = {
3884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3885 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003886 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003887 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003888 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003889 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003890
3891 if (!nfs4_server_supports_acls(server))
3892 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003893 if (npages > ARRAY_SIZE(pages))
3894 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003895 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3896 if (i < 0)
3897 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003898 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003899 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003900
3901 /*
3902 * Free each page after tx, so the only ref left is
3903 * held by the network stack
3904 */
3905 for (; i > 0; i--)
3906 put_page(pages[i-1]);
3907
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003908 /*
3909 * Acl update can result in inode attribute update.
3910 * so mark the attribute cache invalid.
3911 */
3912 spin_lock(&inode->i_lock);
3913 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3914 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003915 nfs_access_zap_cache(inode);
3916 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003917 return ret;
3918}
3919
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003920static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3921{
3922 struct nfs4_exception exception = { };
3923 int err;
3924 do {
3925 err = nfs4_handle_exception(NFS_SERVER(inode),
3926 __nfs4_proc_set_acl(inode, buf, buflen),
3927 &exception);
3928 } while (exception.retry);
3929 return err;
3930}
3931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003933nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003935 struct nfs_client *clp = server->nfs_client;
3936
3937 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 return 0;
3939 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003940 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003941 case -NFS4ERR_ADMIN_REVOKED:
3942 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003943 if (state == NULL)
3944 break;
3945 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003946 case -NFS4ERR_OPENMODE:
3947 if (state == NULL)
3948 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003949 nfs4_schedule_stateid_recovery(server, state);
3950 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003951 case -NFS4ERR_EXPIRED:
3952 if (state != NULL)
3953 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003955 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003956 nfs4_schedule_lease_recovery(clp);
3957 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003958#if defined(CONFIG_NFS_V4_1)
3959 case -NFS4ERR_BADSESSION:
3960 case -NFS4ERR_BADSLOT:
3961 case -NFS4ERR_BAD_HIGH_SLOT:
3962 case -NFS4ERR_DEADSESSION:
3963 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3964 case -NFS4ERR_SEQ_FALSE_RETRY:
3965 case -NFS4ERR_SEQ_MISORDERED:
3966 dprintk("%s ERROR %d, Reset session\n", __func__,
3967 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003968 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003969 task->tk_status = 0;
3970 return -EAGAIN;
3971#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003973 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003974 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003975 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3977 task->tk_status = 0;
3978 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003979 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 case -NFS4ERR_OLD_STATEID:
3981 task->tk_status = 0;
3982 return -EAGAIN;
3983 }
3984 task->tk_status = nfs4_map_errors(task->tk_status);
3985 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003986wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003987 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003988 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3989 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3990 task->tk_status = 0;
3991 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
Chuck Leverf0920752012-05-21 22:45:41 -04003994static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3995 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003996{
3997 __be32 verf[2];
3998
Chuck Lever2c820d92012-05-21 22:45:33 -04003999 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4000 /* An impossible timestamp guarantees this value
4001 * will never match a generated boot time. */
4002 verf[0] = 0;
4003 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4004 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004005 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4006 verf[0] = (__be32)nn->boot_time.tv_sec;
4007 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004008 }
Chuck Levercd937102012-03-02 17:14:31 -05004009 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4010}
4011
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004012/**
4013 * nfs4_proc_setclientid - Negotiate client ID
4014 * @clp: state data structure
4015 * @program: RPC program for NFSv4 callback service
4016 * @port: IP port number for NFS4 callback service
4017 * @cred: RPC credential to use for this call
4018 * @res: where to place the result
4019 *
4020 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4021 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004022int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4023 unsigned short port, struct rpc_cred *cred,
4024 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
4026 nfs4_verifier sc_verifier;
4027 struct nfs4_setclientid setclientid = {
4028 .sc_verifier = &sc_verifier,
4029 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004030 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 };
4032 struct rpc_message msg = {
4033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4034 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004035 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004036 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004038 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Chuck Leverde734832012-07-11 16:30:50 -04004040 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004041 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Leverde734832012-07-11 16:30:50 -04004042 rcu_read_lock();
4043 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
4044 sizeof(setclientid.sc_name), "%s/%s %s",
4045 clp->cl_ipaddr,
4046 rpc_peeraddr2str(clp->cl_rpcclient,
4047 RPC_DISPLAY_ADDR),
4048 rpc_peeraddr2str(clp->cl_rpcclient,
4049 RPC_DISPLAY_PROTO));
4050 /* cb_client4 */
4051 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004052 sizeof(setclientid.sc_netid),
4053 rpc_peeraddr2str(clp->cl_rpcclient,
4054 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004055 rcu_read_unlock();
4056 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004057 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 clp->cl_ipaddr, port >> 8, port & 255);
4059
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004060 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4061 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4062 setclientid.sc_name_len, setclientid.sc_name);
4063 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4064 dprintk("NFS reply setclientid: %d\n", status);
4065 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004068/**
4069 * nfs4_proc_setclientid_confirm - Confirm client ID
4070 * @clp: state data structure
4071 * @res: result of a previous SETCLIENTID
4072 * @cred: RPC credential to use for this call
4073 *
4074 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4075 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004076int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004077 struct nfs4_setclientid_res *arg,
4078 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
4080 struct nfs_fsinfo fsinfo;
4081 struct rpc_message msg = {
4082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004083 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004085 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 };
4087 unsigned long now;
4088 int status;
4089
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004090 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4091 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4092 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004094 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (status == 0) {
4096 spin_lock(&clp->cl_lock);
4097 clp->cl_lease_time = fsinfo.lease_time * HZ;
4098 clp->cl_last_renewal = now;
4099 spin_unlock(&clp->cl_lock);
4100 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004101 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 return status;
4103}
4104
Trond Myklebustfe650402006-01-03 09:55:18 +01004105struct nfs4_delegreturndata {
4106 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004107 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004108 struct nfs_fh fh;
4109 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004110 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004111 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004112 int rpc_status;
4113};
4114
Trond Myklebustfe650402006-01-03 09:55:18 +01004115static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4116{
4117 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004118
Trond Myklebust14516c32010-07-31 14:29:06 -04004119 if (!nfs4_sequence_done(task, &data->res.seq_res))
4120 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004121
Ricardo Labiaga79708862009-12-07 09:23:21 -05004122 switch (task->tk_status) {
4123 case -NFS4ERR_STALE_STATEID:
4124 case -NFS4ERR_EXPIRED:
4125 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004126 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004127 break;
4128 default:
4129 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4130 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004131 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004132 return;
4133 }
4134 }
4135 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004136}
4137
4138static void nfs4_delegreturn_release(void *calldata)
4139{
Trond Myklebustfe650402006-01-03 09:55:18 +01004140 kfree(calldata);
4141}
4142
Andy Adamson938e1012009-04-01 09:22:28 -04004143#if defined(CONFIG_NFS_V4_1)
4144static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4145{
4146 struct nfs4_delegreturndata *d_data;
4147
4148 d_data = (struct nfs4_delegreturndata *)data;
4149
Trond Myklebust035168a2010-06-16 09:52:26 -04004150 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004151 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004152 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004153 return;
4154 rpc_call_start(task);
4155}
4156#endif /* CONFIG_NFS_V4_1 */
4157
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004158static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004159#if defined(CONFIG_NFS_V4_1)
4160 .rpc_call_prepare = nfs4_delegreturn_prepare,
4161#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004162 .rpc_call_done = nfs4_delegreturn_done,
4163 .rpc_release = nfs4_delegreturn_release,
4164};
4165
Trond Myklebuste6f81072008-01-24 18:14:34 -05004166static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004167{
4168 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004169 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004170 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004171 struct rpc_message msg = {
4172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4173 .rpc_cred = cred,
4174 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004175 struct rpc_task_setup task_setup_data = {
4176 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004177 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004178 .callback_ops = &nfs4_delegreturn_ops,
4179 .flags = RPC_TASK_ASYNC,
4180 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004181 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004182
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004183 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004184 if (data == NULL)
4185 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004186 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004187 data->args.fhandle = &data->fh;
4188 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004189 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004190 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004191 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004192 data->res.fattr = &data->fattr;
4193 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004194 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004195 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004196 data->rpc_status = 0;
4197
Trond Myklebustc970aa82007-07-14 15:39:59 -04004198 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004199 msg.rpc_argp = &data->args;
4200 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004201 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004202 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004203 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004204 if (!issync)
4205 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004206 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004207 if (status != 0)
4208 goto out;
4209 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004210 if (status == 0)
4211 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4212 else
4213 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004214out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004215 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004216 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Trond Myklebuste6f81072008-01-24 18:14:34 -05004219int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
4221 struct nfs_server *server = NFS_SERVER(inode);
4222 struct nfs4_exception exception = { };
4223 int err;
4224 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004225 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 switch (err) {
4227 case -NFS4ERR_STALE_STATEID:
4228 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 case 0:
4230 return 0;
4231 }
4232 err = nfs4_handle_exception(server, err, &exception);
4233 } while (exception.retry);
4234 return err;
4235}
4236
4237#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4238#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4239
4240/*
4241 * sleep, with exponential backoff, and retry the LOCK operation.
4242 */
4243static unsigned long
4244nfs4_set_lock_task_retry(unsigned long timeout)
4245{
Jeff Laytond3103102011-12-01 22:44:39 +01004246 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 timeout <<= 1;
4248 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4249 return NFS4_LOCK_MAXTIMEOUT;
4250 return timeout;
4251}
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4254{
4255 struct inode *inode = state->inode;
4256 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004257 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004258 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004260 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004262 struct nfs_lockt_res res = {
4263 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 };
4265 struct rpc_message msg = {
4266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4267 .rpc_argp = &arg,
4268 .rpc_resp = &res,
4269 .rpc_cred = state->owner->so_cred,
4270 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 struct nfs4_lock_state *lsp;
4272 int status;
4273
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004274 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004275 status = nfs4_set_lock_state(state, request);
4276 if (status != 0)
4277 goto out;
4278 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004279 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004280 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004281 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004282 switch (status) {
4283 case 0:
4284 request->fl_type = F_UNLCK;
4285 break;
4286 case -NFS4ERR_DENIED:
4287 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004289 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004290out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 return status;
4292}
4293
4294static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4295{
4296 struct nfs4_exception exception = { };
4297 int err;
4298
4299 do {
4300 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4301 _nfs4_proc_getlk(state, cmd, request),
4302 &exception);
4303 } while (exception.retry);
4304 return err;
4305}
4306
4307static int do_vfs_lock(struct file *file, struct file_lock *fl)
4308{
4309 int res = 0;
4310 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4311 case FL_POSIX:
4312 res = posix_lock_file_wait(file, fl);
4313 break;
4314 case FL_FLOCK:
4315 res = flock_lock_file_wait(file, fl);
4316 break;
4317 default:
4318 BUG();
4319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return res;
4321}
4322
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004323struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004324 struct nfs_locku_args arg;
4325 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004326 struct nfs4_lock_state *lsp;
4327 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004328 struct file_lock fl;
4329 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004330 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004331};
4332
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004333static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4334 struct nfs_open_context *ctx,
4335 struct nfs4_lock_state *lsp,
4336 struct nfs_seqid *seqid)
4337{
4338 struct nfs4_unlockdata *p;
4339 struct inode *inode = lsp->ls_state->inode;
4340
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004341 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004342 if (p == NULL)
4343 return NULL;
4344 p->arg.fh = NFS_FH(inode);
4345 p->arg.fl = &p->fl;
4346 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004347 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004348 p->arg.stateid = &lsp->ls_stateid;
4349 p->lsp = lsp;
4350 atomic_inc(&lsp->ls_count);
4351 /* Ensure we don't close file until we're done freeing locks! */
4352 p->ctx = get_nfs_open_context(ctx);
4353 memcpy(&p->fl, fl, sizeof(p->fl));
4354 p->server = NFS_SERVER(inode);
4355 return p;
4356}
4357
Trond Myklebust06f814a2006-01-03 09:55:07 +01004358static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004359{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004360 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004361 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004362 nfs4_put_lock_state(calldata->lsp);
4363 put_nfs_open_context(calldata->ctx);
4364 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004365}
4366
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004367static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004368{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004369 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004370
Trond Myklebust14516c32010-07-31 14:29:06 -04004371 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4372 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004373 switch (task->tk_status) {
4374 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004375 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4376 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004377 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004378 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004379 case -NFS4ERR_BAD_STATEID:
4380 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004381 case -NFS4ERR_STALE_STATEID:
4382 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004383 break;
4384 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004385 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004386 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004387 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004388}
4389
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004390static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004392 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004394 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004395 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004396 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004397 /* Note: exit _without_ running nfs4_locku_done */
4398 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004399 return;
4400 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004401 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004402 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004403 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004404 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004405 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004406 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004409static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004410 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004411 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004412 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004413};
4414
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004415static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4416 struct nfs_open_context *ctx,
4417 struct nfs4_lock_state *lsp,
4418 struct nfs_seqid *seqid)
4419{
4420 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004421 struct rpc_message msg = {
4422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4423 .rpc_cred = ctx->cred,
4424 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004425 struct rpc_task_setup task_setup_data = {
4426 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004427 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004428 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004429 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004430 .flags = RPC_TASK_ASYNC,
4431 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004432
Frank Filz137d6ac2007-07-09 15:32:29 -07004433 /* Ensure this is an unlock - when canceling a lock, the
4434 * canceled lock is passed in, and it won't be an unlock.
4435 */
4436 fl->fl_type = F_UNLCK;
4437
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004438 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4439 if (data == NULL) {
4440 nfs_free_seqid(seqid);
4441 return ERR_PTR(-ENOMEM);
4442 }
4443
Trond Myklebust9d12b212012-01-17 22:04:25 -05004444 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004445 msg.rpc_argp = &data->arg;
4446 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004447 task_setup_data.callback_data = data;
4448 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449}
4450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4452{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004453 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004454 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004455 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004456 struct rpc_task *task;
4457 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004458 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Trond Myklebust9b073572006-06-29 16:38:34 -04004460 status = nfs4_set_lock_state(state, request);
4461 /* Unlock _before_ we do the RPC call */
4462 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004463 down_read(&nfsi->rwsem);
4464 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4465 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004466 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004467 }
4468 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004469 if (status != 0)
4470 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004471 /* Is this a delegated lock? */
4472 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004473 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004474 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004475 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004476 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004477 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004478 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004479 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004480 status = PTR_ERR(task);
4481 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004482 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004483 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004484 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004485out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004486 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004487 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004490struct nfs4_lockdata {
4491 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004492 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004493 struct nfs4_lock_state *lsp;
4494 struct nfs_open_context *ctx;
4495 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004496 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004497 int rpc_status;
4498 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004499 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004500};
4501
4502static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004503 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4504 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004505{
4506 struct nfs4_lockdata *p;
4507 struct inode *inode = lsp->ls_state->inode;
4508 struct nfs_server *server = NFS_SERVER(inode);
4509
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004510 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004511 if (p == NULL)
4512 return NULL;
4513
4514 p->arg.fh = NFS_FH(inode);
4515 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004516 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004517 if (p->arg.open_seqid == NULL)
4518 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004519 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004521 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004523 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004524 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004525 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004526 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004528 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529 atomic_inc(&lsp->ls_count);
4530 p->ctx = get_nfs_open_context(ctx);
4531 memcpy(&p->fl, fl, sizeof(p->fl));
4532 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004533out_free_seqid:
4534 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535out_free:
4536 kfree(p);
4537 return NULL;
4538}
4539
4540static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4541{
4542 struct nfs4_lockdata *data = calldata;
4543 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Harvey Harrison3110ff82008-05-02 13:42:44 -07004545 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004546 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4547 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004548 /* Do we need to do an open_to_lock_owner? */
4549 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004550 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4551 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004552 data->arg.open_stateid = &state->stateid;
4553 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004554 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004555 } else
4556 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004557 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004558 if (nfs4_setup_sequence(data->server,
4559 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004560 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004561 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004562 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004563 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564}
4565
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004566static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4567{
4568 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4569 nfs4_lock_prepare(task, calldata);
4570}
4571
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004572static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4573{
4574 struct nfs4_lockdata *data = calldata;
4575
Harvey Harrison3110ff82008-05-02 13:42:44 -07004576 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577
Trond Myklebust14516c32010-07-31 14:29:06 -04004578 if (!nfs4_sequence_done(task, &data->res.seq_res))
4579 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004580
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004581 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004583 if (data->rpc_status == 0)
4584 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4585 else
4586 goto out;
4587 }
4588 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004589 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004590 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004591 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004592 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004594 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595}
4596
4597static void nfs4_lock_release(void *calldata)
4598{
4599 struct nfs4_lockdata *data = calldata;
4600
Harvey Harrison3110ff82008-05-02 13:42:44 -07004601 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004602 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004603 if (data->cancelled != 0) {
4604 struct rpc_task *task;
4605 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4606 data->arg.lock_seqid);
4607 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004608 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004609 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610 } else
4611 nfs_free_seqid(data->arg.lock_seqid);
4612 nfs4_put_lock_state(data->lsp);
4613 put_nfs_open_context(data->ctx);
4614 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004615 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616}
4617
4618static const struct rpc_call_ops nfs4_lock_ops = {
4619 .rpc_call_prepare = nfs4_lock_prepare,
4620 .rpc_call_done = nfs4_lock_done,
4621 .rpc_release = nfs4_lock_release,
4622};
4623
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004624static const struct rpc_call_ops nfs4_recover_lock_ops = {
4625 .rpc_call_prepare = nfs4_recover_lock_prepare,
4626 .rpc_call_done = nfs4_lock_done,
4627 .rpc_release = nfs4_lock_release,
4628};
4629
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004630static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4631{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004632 switch (error) {
4633 case -NFS4ERR_ADMIN_REVOKED:
4634 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004635 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004636 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004637 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004638 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004639 break;
4640 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004641 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004642 case -NFS4ERR_EXPIRED:
4643 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004644 };
4645}
4646
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004647static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648{
4649 struct nfs4_lockdata *data;
4650 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004651 struct rpc_message msg = {
4652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4653 .rpc_cred = state->owner->so_cred,
4654 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004655 struct rpc_task_setup task_setup_data = {
4656 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004657 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004658 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004659 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004660 .flags = RPC_TASK_ASYNC,
4661 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004662 int ret;
4663
Harvey Harrison3110ff82008-05-02 13:42:44 -07004664 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004665 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004666 fl->fl_u.nfs4_fl.owner,
4667 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668 if (data == NULL)
4669 return -ENOMEM;
4670 if (IS_SETLKW(cmd))
4671 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004672 if (recovery_type > NFS_LOCK_NEW) {
4673 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004674 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004675 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4676 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004677 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004678 msg.rpc_argp = &data->arg;
4679 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004680 task_setup_data.callback_data = data;
4681 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004682 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004684 ret = nfs4_wait_for_completion_rpc_task(task);
4685 if (ret == 0) {
4686 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004687 if (ret)
4688 nfs4_handle_setlk_error(data->server, data->lsp,
4689 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004690 } else
4691 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004692 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004693 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004694 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
4697static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4698{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004699 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004700 struct nfs4_exception exception = {
4701 .inode = state->inode,
4702 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004703 int err;
4704
4705 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004706 /* Cache the lock if possible... */
4707 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4708 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004709 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004710 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004711 break;
4712 nfs4_handle_exception(server, err, &exception);
4713 } while (exception.retry);
4714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
4717static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4718{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004719 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004720 struct nfs4_exception exception = {
4721 .inode = state->inode,
4722 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004723 int err;
4724
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004725 err = nfs4_set_lock_state(state, request);
4726 if (err != 0)
4727 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004728 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004729 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4730 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004731 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004732 switch (err) {
4733 default:
4734 goto out;
4735 case -NFS4ERR_GRACE:
4736 case -NFS4ERR_DELAY:
4737 nfs4_handle_exception(server, err, &exception);
4738 err = 0;
4739 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004740 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004741out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004742 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004745#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004746/**
4747 * nfs41_check_expired_locks - possibly free a lock stateid
4748 *
4749 * @state: NFSv4 state for an inode
4750 *
4751 * Returns NFS_OK if recovery for this stateid is now finished.
4752 * Otherwise a negative NFS4ERR value is returned.
4753 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004754static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004755{
Chuck Levereb64cf92012-07-11 16:30:05 -04004756 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004757 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004758 struct nfs_server *server = NFS_SERVER(state->inode);
4759
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004760 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004761 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004762 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4763 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004764 /* Free the stateid unless the server
4765 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004766 if (status != -NFS4ERR_BAD_STATEID)
4767 nfs41_free_stateid(server,
4768 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004769 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004770 ret = status;
4771 }
4772 }
4773 };
4774
4775 return ret;
4776}
4777
4778static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4779{
4780 int status = NFS_OK;
4781
4782 if (test_bit(LK_STATE_IN_USE, &state->flags))
4783 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004784 if (status != NFS_OK)
4785 status = nfs4_lock_expired(state, request);
4786 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004787}
4788#endif
4789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4791{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004792 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004793 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004794 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004796 if ((fl_flags & FL_POSIX) &&
4797 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4798 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004799 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004800 status = nfs4_set_lock_state(state, request);
4801 if (status != 0)
4802 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004803 request->fl_flags |= FL_ACCESS;
4804 status = do_vfs_lock(request->fl_file, request);
4805 if (status < 0)
4806 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004807 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004808 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004809 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004810 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004811 request->fl_flags = fl_flags & ~FL_SLEEP;
4812 status = do_vfs_lock(request->fl_file, request);
4813 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004814 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004815 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004816 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004817 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004818 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004819 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004820 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004821 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4822 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004823out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004824 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004825out:
4826 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 return status;
4828}
4829
4830static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4831{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004832 struct nfs4_exception exception = {
4833 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004834 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004835 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 int err;
4837
4838 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004839 err = _nfs4_proc_setlk(state, cmd, request);
4840 if (err == -NFS4ERR_DENIED)
4841 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004843 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 } while (exception.retry);
4845 return err;
4846}
4847
4848static int
4849nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4850{
4851 struct nfs_open_context *ctx;
4852 struct nfs4_state *state;
4853 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4854 int status;
4855
4856 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004857 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 state = ctx->state;
4859
4860 if (request->fl_start < 0 || request->fl_end < 0)
4861 return -EINVAL;
4862
Trond Myklebustd9531262009-07-21 19:22:38 -04004863 if (IS_GETLK(cmd)) {
4864 if (state != NULL)
4865 return nfs4_proc_getlk(state, F_GETLK, request);
4866 return 0;
4867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4870 return -EINVAL;
4871
Trond Myklebustd9531262009-07-21 19:22:38 -04004872 if (request->fl_type == F_UNLCK) {
4873 if (state != NULL)
4874 return nfs4_proc_unlck(state, cmd, request);
4875 return 0;
4876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Trond Myklebustd9531262009-07-21 19:22:38 -04004878 if (state == NULL)
4879 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004880 /*
4881 * Don't rely on the VFS having checked the file open mode,
4882 * since it won't do this for flock() locks.
4883 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004884 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004885 case F_RDLCK:
4886 if (!(filp->f_mode & FMODE_READ))
4887 return -EBADF;
4888 break;
4889 case F_WRLCK:
4890 if (!(filp->f_mode & FMODE_WRITE))
4891 return -EBADF;
4892 }
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 do {
4895 status = nfs4_proc_setlk(state, cmd, request);
4896 if ((status != -EAGAIN) || IS_SETLK(cmd))
4897 break;
4898 timeout = nfs4_set_lock_task_retry(timeout);
4899 status = -ERESTARTSYS;
4900 if (signalled())
4901 break;
4902 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return status;
4904}
4905
Trond Myklebust888e6942005-11-04 15:38:11 -05004906int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4907{
4908 struct nfs_server *server = NFS_SERVER(state->inode);
4909 struct nfs4_exception exception = { };
4910 int err;
4911
4912 err = nfs4_set_lock_state(state, fl);
4913 if (err != 0)
4914 goto out;
4915 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004916 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004917 switch (err) {
4918 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004919 printk(KERN_ERR "NFS: %s: unhandled error "
4920 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004921 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004922 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004923 goto out;
4924 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004925 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004926 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004927 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004928 nfs4_schedule_lease_recovery(server->nfs_client);
4929 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004930 case -NFS4ERR_BADSESSION:
4931 case -NFS4ERR_BADSLOT:
4932 case -NFS4ERR_BAD_HIGH_SLOT:
4933 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4934 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004935 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004936 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004937 case -ERESTARTSYS:
4938 /*
4939 * The show must go on: exit, but mark the
4940 * stateid as needing recovery.
4941 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004942 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004943 case -NFS4ERR_ADMIN_REVOKED:
4944 case -NFS4ERR_BAD_STATEID:
4945 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004946 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004947 err = 0;
4948 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004949 case -EKEYEXPIRED:
4950 /*
4951 * User RPCSEC_GSS context has expired.
4952 * We cannot recover this stateid now, so
4953 * skip it and allow recovery thread to
4954 * proceed.
4955 */
4956 err = 0;
4957 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004958 case -ENOMEM:
4959 case -NFS4ERR_DENIED:
4960 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4961 err = 0;
4962 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004963 case -NFS4ERR_DELAY:
4964 break;
4965 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004966 err = nfs4_handle_exception(server, err, &exception);
4967 } while (exception.retry);
4968out:
4969 return err;
4970}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004971
Trond Myklebustcf470c32012-03-07 13:49:12 -05004972struct nfs_release_lockowner_data {
4973 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004974 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004975 struct nfs_release_lockowner_args args;
4976};
4977
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004978static void nfs4_release_lockowner_release(void *calldata)
4979{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004980 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004981 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004982 kfree(calldata);
4983}
4984
Trond Myklebust17280172012-03-11 13:11:00 -04004985static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004986 .rpc_release = nfs4_release_lockowner_release,
4987};
4988
Trond Myklebustcf470c32012-03-07 13:49:12 -05004989int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004990{
4991 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004992 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004993 struct rpc_message msg = {
4994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4995 };
4996
4997 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004998 return -EINVAL;
4999 data = kmalloc(sizeof(*data), GFP_NOFS);
5000 if (!data)
5001 return -ENOMEM;
5002 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005003 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005004 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5005 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5006 data->args.lock_owner.s_dev = server->s_dev;
5007 msg.rpc_argp = &data->args;
5008 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5009 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005010}
5011
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005012#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5013
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005014static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5015 const void *buf, size_t buflen,
5016 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005017{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005018 if (strcmp(key, "") != 0)
5019 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005020
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005021 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005022}
5023
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005024static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5025 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005026{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005027 if (strcmp(key, "") != 0)
5028 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005029
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005030 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005031}
5032
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005033static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5034 size_t list_len, const char *name,
5035 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005036{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005037 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005038
J. Bruce Fields096455a2006-03-20 23:23:42 -05005039 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5040 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005041
5042 if (list && len <= list_len)
5043 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005044 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005045}
5046
Andy Adamson533eb462011-06-13 18:25:56 -04005047/*
5048 * nfs_fhget will use either the mounted_on_fileid or the fileid
5049 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005050static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5051{
Andy Adamson533eb462011-06-13 18:25:56 -04005052 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5053 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5054 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005055 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005056 return;
5057
5058 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005059 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005060 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5061 fattr->nlink = 2;
5062}
5063
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005064static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5065 const struct qstr *name,
5066 struct nfs4_fs_locations *fs_locations,
5067 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005068{
5069 struct nfs_server *server = NFS_SERVER(dir);
5070 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005071 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005072 };
5073 struct nfs4_fs_locations_arg args = {
5074 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005075 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005076 .page = page,
5077 .bitmask = bitmask,
5078 };
Benny Halevy22958462009-04-01 09:22:02 -04005079 struct nfs4_fs_locations_res res = {
5080 .fs_locations = fs_locations,
5081 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005082 struct rpc_message msg = {
5083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5084 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005085 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005086 };
5087 int status;
5088
Harvey Harrison3110ff82008-05-02 13:42:44 -07005089 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005090
5091 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5092 * is not supported */
5093 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5094 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5095 else
5096 bitmask[0] |= FATTR4_WORD0_FILEID;
5097
Trond Myklebustc228fd32007-01-13 02:28:11 -05005098 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005099 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005100 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005101 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005102 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005103 return status;
5104}
5105
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005106int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5107 const struct qstr *name,
5108 struct nfs4_fs_locations *fs_locations,
5109 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005110{
5111 struct nfs4_exception exception = { };
5112 int err;
5113 do {
5114 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005115 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005116 &exception);
5117 } while (exception.retry);
5118 return err;
5119}
5120
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005121static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5122{
5123 int status;
5124 struct nfs4_secinfo_arg args = {
5125 .dir_fh = NFS_FH(dir),
5126 .name = name,
5127 };
5128 struct nfs4_secinfo_res res = {
5129 .flavors = flavors,
5130 };
5131 struct rpc_message msg = {
5132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5133 .rpc_argp = &args,
5134 .rpc_resp = &res,
5135 };
5136
5137 dprintk("NFS call secinfo %s\n", name->name);
5138 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5139 dprintk("NFS reply secinfo: %d\n", status);
5140 return status;
5141}
5142
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005143int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5144 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005145{
5146 struct nfs4_exception exception = { };
5147 int err;
5148 do {
5149 err = nfs4_handle_exception(NFS_SERVER(dir),
5150 _nfs4_proc_secinfo(dir, name, flavors),
5151 &exception);
5152 } while (exception.retry);
5153 return err;
5154}
5155
Andy Adamson557134a2009-04-01 09:21:53 -04005156#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005157/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005158 * Check the exchange flags returned by the server for invalid flags, having
5159 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5160 * DS flags set.
5161 */
5162static int nfs4_check_cl_exchange_flags(u32 flags)
5163{
5164 if (flags & ~EXCHGID4_FLAG_MASK_R)
5165 goto out_inval;
5166 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5167 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5168 goto out_inval;
5169 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5170 goto out_inval;
5171 return NFS_OK;
5172out_inval:
5173 return -NFS4ERR_INVAL;
5174}
5175
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005176static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005177nfs41_same_server_scope(struct nfs41_server_scope *a,
5178 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005179{
5180 if (a->server_scope_sz == b->server_scope_sz &&
5181 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5182 return true;
5183
5184 return false;
5185}
5186
Andy Adamson357f54d2010-12-14 10:11:57 -05005187/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005188 * nfs4_proc_bind_conn_to_session()
5189 *
5190 * The 4.1 client currently uses the same TCP connection for the
5191 * fore and backchannel.
5192 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005193int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005194{
5195 int status;
5196 struct nfs41_bind_conn_to_session_res res;
5197 struct rpc_message msg = {
5198 .rpc_proc =
5199 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5200 .rpc_argp = clp,
5201 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005202 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005203 };
5204
5205 dprintk("--> %s\n", __func__);
5206 BUG_ON(clp == NULL);
5207
5208 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5209 if (unlikely(res.session == NULL)) {
5210 status = -ENOMEM;
5211 goto out;
5212 }
5213
5214 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5215 if (status == 0) {
5216 if (memcmp(res.session->sess_id.data,
5217 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5218 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5219 status = -EIO;
5220 goto out_session;
5221 }
5222 if (res.dir != NFS4_CDFS4_BOTH) {
5223 dprintk("NFS: %s: Unexpected direction from server\n",
5224 __func__);
5225 status = -EIO;
5226 goto out_session;
5227 }
5228 if (res.use_conn_in_rdma_mode) {
5229 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5230 __func__);
5231 status = -EIO;
5232 goto out_session;
5233 }
5234 }
5235out_session:
5236 kfree(res.session);
5237out:
5238 dprintk("<-- %s status= %d\n", __func__, status);
5239 return status;
5240}
5241
5242/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005243 * nfs4_proc_exchange_id()
5244 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005245 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5246 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005247 * Since the clientid has expired, all compounds using sessions
5248 * associated with the stale clientid will be returning
5249 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5250 * be in some phase of session reset.
5251 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005252int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005253{
5254 nfs4_verifier verifier;
5255 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005256 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005257 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005258 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005259 };
5260 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005261 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005262 };
5263 int status;
5264 struct rpc_message msg = {
5265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5266 .rpc_argp = &args,
5267 .rpc_resp = &res,
5268 .rpc_cred = cred,
5269 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005270
Chuck Leverf0920752012-05-21 22:45:41 -04005271 nfs4_init_boot_verifier(clp, &verifier);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005272 args.id_len = scnprintf(args.id, sizeof(args.id),
Chuck Leverde734832012-07-11 16:30:50 -04005273 "%s/%s",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005274 clp->cl_ipaddr,
Chuck Leverde734832012-07-11 16:30:50 -04005275 clp->cl_rpcclient->cl_nodename);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005276 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5277 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5278 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005279
Chuck Leveracdeb692012-05-21 22:46:16 -04005280 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005281 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005282 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005283 status = -ENOMEM;
5284 goto out;
5285 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005286
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005287 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005288 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005289 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005290 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005291 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005292 }
5293
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005294 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005295 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005296 status = -ENOMEM;
5297 goto out_server_scope;
5298 }
5299
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005300 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005301 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005302 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005303
Chuck Lever177313f2012-05-21 22:44:58 -04005304 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005305 clp->cl_clientid = res.clientid;
5306 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5307 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5308 clp->cl_seqid = res.seqid;
5309
Chuck Leveracdeb692012-05-21 22:46:16 -04005310 kfree(clp->cl_serverowner);
5311 clp->cl_serverowner = res.server_owner;
5312 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005313
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005314 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005315 kfree(clp->cl_implid);
5316 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005317
Chuck Lever177313f2012-05-21 22:44:58 -04005318 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005319 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005320 res.server_scope)) {
5321 dprintk("%s: server_scope mismatch detected\n",
5322 __func__);
5323 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005324 kfree(clp->cl_serverscope);
5325 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005326 }
5327
Chuck Lever177313f2012-05-21 22:44:58 -04005328 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005329 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005330 goto out;
5331 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005332 } else
5333 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005334
Chuck Leveracdeb692012-05-21 22:46:16 -04005335out_server_owner:
5336 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005337out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005338 kfree(res.server_scope);
5339out:
Chuck Lever177313f2012-05-21 22:44:58 -04005340 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005341 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005342 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005343 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005344 clp->cl_implid->date.seconds,
5345 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005346 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005347 return status;
5348}
5349
Trond Myklebust66245532012-05-25 17:18:09 -04005350static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5351 struct rpc_cred *cred)
5352{
5353 struct rpc_message msg = {
5354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5355 .rpc_argp = clp,
5356 .rpc_cred = cred,
5357 };
5358 int status;
5359
5360 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5361 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005362 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005363 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5364 return status;
5365}
5366
5367static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5368 struct rpc_cred *cred)
5369{
5370 unsigned int loop;
5371 int ret;
5372
5373 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5374 ret = _nfs4_proc_destroy_clientid(clp, cred);
5375 switch (ret) {
5376 case -NFS4ERR_DELAY:
5377 case -NFS4ERR_CLIENTID_BUSY:
5378 ssleep(1);
5379 break;
5380 default:
5381 return ret;
5382 }
5383 }
5384 return 0;
5385}
5386
5387int nfs4_destroy_clientid(struct nfs_client *clp)
5388{
5389 struct rpc_cred *cred;
5390 int ret = 0;
5391
5392 if (clp->cl_mvops->minor_version < 1)
5393 goto out;
5394 if (clp->cl_exchange_flags == 0)
5395 goto out;
5396 cred = nfs4_get_exchange_id_cred(clp);
5397 ret = nfs4_proc_destroy_clientid(clp, cred);
5398 if (cred)
5399 put_rpccred(cred);
5400 switch (ret) {
5401 case 0:
5402 case -NFS4ERR_STALE_CLIENTID:
5403 clp->cl_exchange_flags = 0;
5404 }
5405out:
5406 return ret;
5407}
5408
Andy Adamson2050f0c2009-04-01 09:22:30 -04005409struct nfs4_get_lease_time_data {
5410 struct nfs4_get_lease_time_args *args;
5411 struct nfs4_get_lease_time_res *res;
5412 struct nfs_client *clp;
5413};
5414
5415static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5416 void *calldata)
5417{
5418 int ret;
5419 struct nfs4_get_lease_time_data *data =
5420 (struct nfs4_get_lease_time_data *)calldata;
5421
5422 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005423 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005424 /* just setup sequence, do not trigger session recovery
5425 since we're invoked within one */
5426 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005427 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005428 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005429
5430 BUG_ON(ret == -EAGAIN);
5431 rpc_call_start(task);
5432 dprintk("<-- %s\n", __func__);
5433}
5434
5435/*
5436 * Called from nfs4_state_manager thread for session setup, so don't recover
5437 * from sequence operation or clientid errors.
5438 */
5439static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5440{
5441 struct nfs4_get_lease_time_data *data =
5442 (struct nfs4_get_lease_time_data *)calldata;
5443
5444 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005445 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5446 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005447 switch (task->tk_status) {
5448 case -NFS4ERR_DELAY:
5449 case -NFS4ERR_GRACE:
5450 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5451 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5452 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005453 /* fall through */
5454 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005455 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005456 return;
5457 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005458 dprintk("<-- %s\n", __func__);
5459}
5460
Trond Myklebust17280172012-03-11 13:11:00 -04005461static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005462 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5463 .rpc_call_done = nfs4_get_lease_time_done,
5464};
5465
5466int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5467{
5468 struct rpc_task *task;
5469 struct nfs4_get_lease_time_args args;
5470 struct nfs4_get_lease_time_res res = {
5471 .lr_fsinfo = fsinfo,
5472 };
5473 struct nfs4_get_lease_time_data data = {
5474 .args = &args,
5475 .res = &res,
5476 .clp = clp,
5477 };
5478 struct rpc_message msg = {
5479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5480 .rpc_argp = &args,
5481 .rpc_resp = &res,
5482 };
5483 struct rpc_task_setup task_setup = {
5484 .rpc_client = clp->cl_rpcclient,
5485 .rpc_message = &msg,
5486 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005487 .callback_data = &data,
5488 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005489 };
5490 int status;
5491
Trond Myklebust9d12b212012-01-17 22:04:25 -05005492 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005493 dprintk("--> %s\n", __func__);
5494 task = rpc_run_task(&task_setup);
5495
5496 if (IS_ERR(task))
5497 status = PTR_ERR(task);
5498 else {
5499 status = task->tk_status;
5500 rpc_put_task(task);
5501 }
5502 dprintk("<-- %s return %d\n", __func__, status);
5503
5504 return status;
5505}
5506
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005507static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5508{
5509 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5510}
5511
5512static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5513 struct nfs4_slot *new,
5514 u32 max_slots,
5515 u32 ivalue)
5516{
5517 struct nfs4_slot *old = NULL;
5518 u32 i;
5519
5520 spin_lock(&tbl->slot_tbl_lock);
5521 if (new) {
5522 old = tbl->slots;
5523 tbl->slots = new;
5524 tbl->max_slots = max_slots;
5525 }
5526 tbl->highest_used_slotid = -1; /* no slot is currently used */
5527 for (i = 0; i < tbl->max_slots; i++)
5528 tbl->slots[i].seq_nr = ivalue;
5529 spin_unlock(&tbl->slot_tbl_lock);
5530 kfree(old);
5531}
5532
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005533/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005534 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005535 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005536static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5537 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005538{
Andy Adamson104aeba2010-01-14 17:45:10 -05005539 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005540 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005541
Andy Adamson104aeba2010-01-14 17:45:10 -05005542 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5543 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005544
Andy Adamson104aeba2010-01-14 17:45:10 -05005545 /* Does the newly negotiated max_reqs match the existing slot table? */
5546 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005547 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005548 if (!new)
5549 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005550 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005551 ret = 0;
5552
5553 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005554 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5555 tbl, tbl->slots, tbl->max_slots);
5556out:
5557 dprintk("<-- %s: return %d\n", __func__, ret);
5558 return ret;
5559}
5560
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005561/* Destroy the slot table */
5562static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5563{
5564 if (session->fc_slot_table.slots != NULL) {
5565 kfree(session->fc_slot_table.slots);
5566 session->fc_slot_table.slots = NULL;
5567 }
5568 if (session->bc_slot_table.slots != NULL) {
5569 kfree(session->bc_slot_table.slots);
5570 session->bc_slot_table.slots = NULL;
5571 }
5572 return;
5573}
5574
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005575/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005576 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005577 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005578static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005579{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005580 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005581 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005582
Andy Adamsonaacd5532011-11-09 13:58:21 -05005583 dprintk("--> %s\n", __func__);
5584 /* Fore channel */
5585 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005586 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5587 if (status) /* -ENOMEM */
5588 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005589 /* Back channel */
5590 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005591 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5592 if (status && tbl->slots == NULL)
5593 /* Fore and back channel share a connection so get
5594 * both slot tables or neither */
5595 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005596 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005597}
5598
5599struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5600{
5601 struct nfs4_session *session;
5602 struct nfs4_slot_table *tbl;
5603
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005604 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005605 if (!session)
5606 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005607
Andy Adamson557134a2009-04-01 09:21:53 -04005608 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005609 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005610 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005611 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005612 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005613
5614 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005615 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005616 spin_lock_init(&tbl->slot_tbl_lock);
5617 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005618 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005619
Trond Myklebust1055d762010-06-16 09:52:27 -04005620 session->session_state = 1<<NFS4_SESSION_INITING;
5621
Andy Adamson557134a2009-04-01 09:21:53 -04005622 session->clp = clp;
5623 return session;
5624}
5625
5626void nfs4_destroy_session(struct nfs4_session *session)
5627{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005628 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005629 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005630
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005631 cred = nfs4_get_exchange_id_cred(session->clp);
5632 nfs4_proc_destroy_session(session, cred);
5633 if (cred)
5634 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005635
5636 rcu_read_lock();
5637 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5638 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005639 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005640 __func__, xprt);
5641 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005642 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005643 kfree(session);
5644}
5645
Andy Adamsonfc931582009-04-01 09:22:31 -04005646/*
5647 * Initialize the values to be used by the client in CREATE_SESSION
5648 * If nfs4_init_session set the fore channel request and response sizes,
5649 * use them.
5650 *
5651 * Set the back channel max_resp_sz_cached to zero to force the client to
5652 * always set csa_cachethis to FALSE because the current implementation
5653 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5654 */
5655static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5656{
5657 struct nfs4_session *session = args->client->cl_session;
5658 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5659 mxresp_sz = session->fc_attrs.max_resp_sz;
5660
5661 if (mxrqst_sz == 0)
5662 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5663 if (mxresp_sz == 0)
5664 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5665 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005666 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5667 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005668 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005669 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005670
5671 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005672 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005673 __func__,
5674 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005675 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005676
5677 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005678 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5679 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5680 args->bc_attrs.max_resp_sz_cached = 0;
5681 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5682 args->bc_attrs.max_reqs = 1;
5683
5684 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5685 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5686 __func__,
5687 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5688 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5689 args->bc_attrs.max_reqs);
5690}
5691
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005692static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005693{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005694 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5695 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5696
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005697 if (rcvd->max_resp_sz > sent->max_resp_sz)
5698 return -EINVAL;
5699 /*
5700 * Our requested max_ops is the minimum we need; we're not
5701 * prepared to break up compounds into smaller pieces than that.
5702 * So, no point even trying to continue if the server won't
5703 * cooperate:
5704 */
5705 if (rcvd->max_ops < sent->max_ops)
5706 return -EINVAL;
5707 if (rcvd->max_reqs == 0)
5708 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005709 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5710 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005711 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005712}
5713
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005714static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5715{
5716 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5717 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005718
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005719 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5720 return -EINVAL;
5721 if (rcvd->max_resp_sz < sent->max_resp_sz)
5722 return -EINVAL;
5723 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5724 return -EINVAL;
5725 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005726 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005727 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005728 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005729 return -EINVAL;
5730 return 0;
5731}
Andy Adamson8d353012009-04-01 09:22:32 -04005732
Andy Adamson8d353012009-04-01 09:22:32 -04005733static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5734 struct nfs4_session *session)
5735{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005736 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005737
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005738 ret = nfs4_verify_fore_channel_attrs(args, session);
5739 if (ret)
5740 return ret;
5741 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005742}
5743
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005744static int _nfs4_proc_create_session(struct nfs_client *clp,
5745 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005746{
5747 struct nfs4_session *session = clp->cl_session;
5748 struct nfs41_create_session_args args = {
5749 .client = clp,
5750 .cb_program = NFS4_CALLBACK,
5751 };
5752 struct nfs41_create_session_res res = {
5753 .client = clp,
5754 };
5755 struct rpc_message msg = {
5756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5757 .rpc_argp = &args,
5758 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005759 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005760 };
5761 int status;
5762
5763 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005764 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005765
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005766 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005767
Andy Adamson8d353012009-04-01 09:22:32 -04005768 if (!status)
5769 /* Verify the session's negotiated channel_attrs values */
5770 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005771 if (!status) {
5772 /* Increment the clientid slot sequence id */
5773 clp->cl_seqid++;
5774 }
5775
5776 return status;
5777}
5778
5779/*
5780 * Issues a CREATE_SESSION operation to the server.
5781 * It is the responsibility of the caller to verify the session is
5782 * expired before calling this routine.
5783 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005784int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005785{
5786 int status;
5787 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005788 struct nfs4_session *session = clp->cl_session;
5789
5790 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5791
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005792 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005793 if (status)
5794 goto out;
5795
Andy Adamsonaacd5532011-11-09 13:58:21 -05005796 /* Init or reset the session slot tables */
5797 status = nfs4_setup_session_slot_tables(session);
5798 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005799 if (status)
5800 goto out;
5801
5802 ptr = (unsigned *)&session->sess_id.data[0];
5803 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5804 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005805out:
5806 dprintk("<-- %s\n", __func__);
5807 return status;
5808}
5809
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005810/*
5811 * Issue the over-the-wire RPC DESTROY_SESSION.
5812 * The caller must serialize access to this routine.
5813 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005814int nfs4_proc_destroy_session(struct nfs4_session *session,
5815 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005816{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005817 struct rpc_message msg = {
5818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5819 .rpc_argp = session,
5820 .rpc_cred = cred,
5821 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005822 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005823
5824 dprintk("--> nfs4_proc_destroy_session\n");
5825
5826 /* session is still being setup */
5827 if (session->clp->cl_cons_state != NFS_CS_READY)
5828 return status;
5829
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005830 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005831
5832 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005833 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005834 "Session has been destroyed regardless...\n", status);
5835
5836 dprintk("<-- nfs4_proc_destroy_session\n");
5837 return status;
5838}
5839
Trond Myklebust7b38c362012-05-23 13:23:31 -04005840/*
5841 * With sessions, the client is not marked ready until after a
5842 * successful EXCHANGE_ID and CREATE_SESSION.
5843 *
5844 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5845 * other versions of NFS can be tried.
5846 */
5847static int nfs41_check_session_ready(struct nfs_client *clp)
5848{
5849 int ret;
5850
5851 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5852 ret = nfs4_client_recover_expired_lease(clp);
5853 if (ret)
5854 return ret;
5855 }
5856 if (clp->cl_cons_state < NFS_CS_READY)
5857 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005858 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005859 return 0;
5860}
5861
Trond Myklebustfccba802009-07-21 16:48:07 -04005862int nfs4_init_session(struct nfs_server *server)
5863{
5864 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005865 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005866 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005867
5868 if (!nfs4_has_session(clp))
5869 return 0;
5870
Trond Myklebust1055d762010-06-16 09:52:27 -04005871 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005872 spin_lock(&clp->cl_lock);
5873 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005874
Trond Myklebust7b38c362012-05-23 13:23:31 -04005875 rsize = server->rsize;
5876 if (rsize == 0)
5877 rsize = NFS_MAX_FILE_IO_SIZE;
5878 wsize = server->wsize;
5879 if (wsize == 0)
5880 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005881
Trond Myklebust7b38c362012-05-23 13:23:31 -04005882 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5883 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5884 }
5885 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005886
Trond Myklebust7b38c362012-05-23 13:23:31 -04005887 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005888}
5889
Trond Myklebust7b38c362012-05-23 13:23:31 -04005890int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005891{
5892 struct nfs4_session *session = clp->cl_session;
5893 int ret;
5894
Trond Myklebust7b38c362012-05-23 13:23:31 -04005895 spin_lock(&clp->cl_lock);
5896 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5897 /*
5898 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5899 * DS lease to be equal to the MDS lease.
5900 */
5901 clp->cl_lease_time = lease_time;
5902 clp->cl_last_renewal = jiffies;
5903 }
5904 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005905
Trond Myklebust7b38c362012-05-23 13:23:31 -04005906 ret = nfs41_check_session_ready(clp);
5907 if (ret)
5908 return ret;
5909 /* Test for the DS role */
5910 if (!is_ds_client(clp))
5911 return -ENODEV;
5912 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005913}
5914EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5915
5916
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005917/*
5918 * Renew the cl_session lease.
5919 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005920struct nfs4_sequence_data {
5921 struct nfs_client *clp;
5922 struct nfs4_sequence_args args;
5923 struct nfs4_sequence_res res;
5924};
5925
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005926static void nfs41_sequence_release(void *data)
5927{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005928 struct nfs4_sequence_data *calldata = data;
5929 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005930
Alexandros Batsakis71358402010-02-05 03:45:05 -08005931 if (atomic_read(&clp->cl_count) > 1)
5932 nfs4_schedule_state_renewal(clp);
5933 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005934 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005935}
5936
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005937static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5938{
5939 switch(task->tk_status) {
5940 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005941 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5942 return -EAGAIN;
5943 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005944 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005945 }
5946 return 0;
5947}
5948
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005949static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005950{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005951 struct nfs4_sequence_data *calldata = data;
5952 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005953
Trond Myklebust14516c32010-07-31 14:29:06 -04005954 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5955 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005956
5957 if (task->tk_status < 0) {
5958 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005959 if (atomic_read(&clp->cl_count) == 1)
5960 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005961
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005962 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5963 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005964 return;
5965 }
5966 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005967 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005968out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005969 dprintk("<-- %s\n", __func__);
5970}
5971
5972static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5973{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005974 struct nfs4_sequence_data *calldata = data;
5975 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005976 struct nfs4_sequence_args *args;
5977 struct nfs4_sequence_res *res;
5978
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005979 args = task->tk_msg.rpc_argp;
5980 res = task->tk_msg.rpc_resp;
5981
Trond Myklebust9d12b212012-01-17 22:04:25 -05005982 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005983 return;
5984 rpc_call_start(task);
5985}
5986
5987static const struct rpc_call_ops nfs41_sequence_ops = {
5988 .rpc_call_done = nfs41_sequence_call_done,
5989 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005990 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005991};
5992
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005993static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005994{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005995 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005996 struct rpc_message msg = {
5997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5998 .rpc_cred = cred,
5999 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006000 struct rpc_task_setup task_setup_data = {
6001 .rpc_client = clp->cl_rpcclient,
6002 .rpc_message = &msg,
6003 .callback_ops = &nfs41_sequence_ops,
6004 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6005 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006006
Alexandros Batsakis71358402010-02-05 03:45:05 -08006007 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006008 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006009 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006010 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006011 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006012 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006013 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006014 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006015 msg.rpc_argp = &calldata->args;
6016 msg.rpc_resp = &calldata->res;
6017 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006018 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006019
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006020 return rpc_run_task(&task_setup_data);
6021}
6022
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006023static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006024{
6025 struct rpc_task *task;
6026 int ret = 0;
6027
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006028 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6029 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006030 task = _nfs41_proc_sequence(clp, cred);
6031 if (IS_ERR(task))
6032 ret = PTR_ERR(task);
6033 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006034 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006035 dprintk("<-- %s status=%d\n", __func__, ret);
6036 return ret;
6037}
6038
6039static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6040{
6041 struct rpc_task *task;
6042 int ret;
6043
6044 task = _nfs41_proc_sequence(clp, cred);
6045 if (IS_ERR(task)) {
6046 ret = PTR_ERR(task);
6047 goto out;
6048 }
6049 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006050 if (!ret) {
6051 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6052
6053 if (task->tk_status == 0)
6054 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006055 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006056 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006057 rpc_put_task(task);
6058out:
6059 dprintk("<-- %s status=%d\n", __func__, ret);
6060 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006061}
6062
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006063struct nfs4_reclaim_complete_data {
6064 struct nfs_client *clp;
6065 struct nfs41_reclaim_complete_args arg;
6066 struct nfs41_reclaim_complete_res res;
6067};
6068
6069static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6070{
6071 struct nfs4_reclaim_complete_data *calldata = data;
6072
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006073 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006074 if (nfs41_setup_sequence(calldata->clp->cl_session,
6075 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006076 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006077 return;
6078
6079 rpc_call_start(task);
6080}
6081
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006082static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6083{
6084 switch(task->tk_status) {
6085 case 0:
6086 case -NFS4ERR_COMPLETE_ALREADY:
6087 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6088 break;
6089 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006090 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006091 /* fall through */
6092 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006093 return -EAGAIN;
6094 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006095 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006096 }
6097 return 0;
6098}
6099
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006100static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6101{
6102 struct nfs4_reclaim_complete_data *calldata = data;
6103 struct nfs_client *clp = calldata->clp;
6104 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6105
6106 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006107 if (!nfs41_sequence_done(task, res))
6108 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006109
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006110 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6111 rpc_restart_call_prepare(task);
6112 return;
6113 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006114 dprintk("<-- %s\n", __func__);
6115}
6116
6117static void nfs4_free_reclaim_complete_data(void *data)
6118{
6119 struct nfs4_reclaim_complete_data *calldata = data;
6120
6121 kfree(calldata);
6122}
6123
6124static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6125 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6126 .rpc_call_done = nfs4_reclaim_complete_done,
6127 .rpc_release = nfs4_free_reclaim_complete_data,
6128};
6129
6130/*
6131 * Issue a global reclaim complete.
6132 */
6133static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6134{
6135 struct nfs4_reclaim_complete_data *calldata;
6136 struct rpc_task *task;
6137 struct rpc_message msg = {
6138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6139 };
6140 struct rpc_task_setup task_setup_data = {
6141 .rpc_client = clp->cl_rpcclient,
6142 .rpc_message = &msg,
6143 .callback_ops = &nfs4_reclaim_complete_call_ops,
6144 .flags = RPC_TASK_ASYNC,
6145 };
6146 int status = -ENOMEM;
6147
6148 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006149 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006150 if (calldata == NULL)
6151 goto out;
6152 calldata->clp = clp;
6153 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006154
Trond Myklebust9d12b212012-01-17 22:04:25 -05006155 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006156 msg.rpc_argp = &calldata->arg;
6157 msg.rpc_resp = &calldata->res;
6158 task_setup_data.callback_data = calldata;
6159 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006160 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006161 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006162 goto out;
6163 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006164 status = nfs4_wait_for_completion_rpc_task(task);
6165 if (status == 0)
6166 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006167 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006168 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006169out:
6170 dprintk("<-- %s status=%d\n", __func__, status);
6171 return status;
6172}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006173
6174static void
6175nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6176{
6177 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006178 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006179
6180 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006181 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6182 * right now covering the LAYOUTGET we are about to send.
6183 * However, that is not so catastrophic, and there seems
6184 * to be no way to prevent it completely.
6185 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006186 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006187 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006188 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006189 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6190 NFS_I(lgp->args.inode)->layout,
6191 lgp->args.ctx->state)) {
6192 rpc_exit(task, NFS4_OK);
6193 return;
6194 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006195 rpc_call_start(task);
6196}
6197
6198static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6199{
6200 struct nfs4_layoutget *lgp = calldata;
6201 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6202
6203 dprintk("--> %s\n", __func__);
6204
6205 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6206 return;
6207
6208 switch (task->tk_status) {
6209 case 0:
6210 break;
6211 case -NFS4ERR_LAYOUTTRYLATER:
6212 case -NFS4ERR_RECALLCONFLICT:
6213 task->tk_status = -NFS4ERR_DELAY;
6214 /* Fall through */
6215 default:
6216 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6217 rpc_restart_call_prepare(task);
6218 return;
6219 }
6220 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006221 dprintk("<-- %s\n", __func__);
6222}
6223
Idan Kedar85541162012-08-02 11:47:10 +03006224static size_t max_response_pages(struct nfs_server *server)
6225{
6226 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6227 return nfs_page_array_len(0, max_resp_sz);
6228}
6229
6230static void nfs4_free_pages(struct page **pages, size_t size)
6231{
6232 int i;
6233
6234 if (!pages)
6235 return;
6236
6237 for (i = 0; i < size; i++) {
6238 if (!pages[i])
6239 break;
6240 __free_page(pages[i]);
6241 }
6242 kfree(pages);
6243}
6244
6245static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6246{
6247 struct page **pages;
6248 int i;
6249
6250 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6251 if (!pages) {
6252 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6253 return NULL;
6254 }
6255
6256 for (i = 0; i < size; i++) {
6257 pages[i] = alloc_page(gfp_flags);
6258 if (!pages[i]) {
6259 dprintk("%s: failed to allocate page\n", __func__);
6260 nfs4_free_pages(pages, size);
6261 return NULL;
6262 }
6263 }
6264
6265 return pages;
6266}
6267
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006268static void nfs4_layoutget_release(void *calldata)
6269{
6270 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006271 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6272 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006273
6274 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006275 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006276 put_nfs_open_context(lgp->args.ctx);
6277 kfree(calldata);
6278 dprintk("<-- %s\n", __func__);
6279}
6280
6281static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6282 .rpc_call_prepare = nfs4_layoutget_prepare,
6283 .rpc_call_done = nfs4_layoutget_done,
6284 .rpc_release = nfs4_layoutget_release,
6285};
6286
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006287struct pnfs_layout_segment *
6288nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006289{
6290 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006291 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006292 struct rpc_task *task;
6293 struct rpc_message msg = {
6294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6295 .rpc_argp = &lgp->args,
6296 .rpc_resp = &lgp->res,
6297 };
6298 struct rpc_task_setup task_setup_data = {
6299 .rpc_client = server->client,
6300 .rpc_message = &msg,
6301 .callback_ops = &nfs4_layoutget_call_ops,
6302 .callback_data = lgp,
6303 .flags = RPC_TASK_ASYNC,
6304 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006305 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006306 int status = 0;
6307
6308 dprintk("--> %s\n", __func__);
6309
Idan Kedar85541162012-08-02 11:47:10 +03006310 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6311 if (!lgp->args.layout.pages) {
6312 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006313 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006314 }
6315 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6316
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006317 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006318 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006319 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006320 task = rpc_run_task(&task_setup_data);
6321 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006322 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006323 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006324 if (status == 0)
6325 status = task->tk_status;
6326 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006327 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006328 rpc_put_task(task);
6329 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006330 if (status)
6331 return ERR_PTR(status);
6332 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006333}
6334
Benny Halevycbe82602011-05-22 19:52:37 +03006335static void
6336nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6337{
6338 struct nfs4_layoutreturn *lrp = calldata;
6339
6340 dprintk("--> %s\n", __func__);
6341 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006342 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006343 return;
6344 rpc_call_start(task);
6345}
6346
6347static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6348{
6349 struct nfs4_layoutreturn *lrp = calldata;
6350 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006351 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006352
6353 dprintk("--> %s\n", __func__);
6354
6355 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6356 return;
6357
6358 server = NFS_SERVER(lrp->args.inode);
6359 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006360 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006361 return;
6362 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006363 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust47fbf7972012-08-08 16:03:13 -04006364 if (task->tk_status == 0 && lrp->res.lrs_present)
6365 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006366 lo->plh_block_lgets--;
6367 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006368 dprintk("<-- %s\n", __func__);
6369}
6370
6371static void nfs4_layoutreturn_release(void *calldata)
6372{
6373 struct nfs4_layoutreturn *lrp = calldata;
6374
6375 dprintk("--> %s\n", __func__);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006376 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006377 kfree(calldata);
6378 dprintk("<-- %s\n", __func__);
6379}
6380
6381static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6382 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6383 .rpc_call_done = nfs4_layoutreturn_done,
6384 .rpc_release = nfs4_layoutreturn_release,
6385};
6386
6387int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6388{
6389 struct rpc_task *task;
6390 struct rpc_message msg = {
6391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6392 .rpc_argp = &lrp->args,
6393 .rpc_resp = &lrp->res,
6394 };
6395 struct rpc_task_setup task_setup_data = {
6396 .rpc_client = lrp->clp->cl_rpcclient,
6397 .rpc_message = &msg,
6398 .callback_ops = &nfs4_layoutreturn_call_ops,
6399 .callback_data = lrp,
6400 };
6401 int status;
6402
6403 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006404 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006405 task = rpc_run_task(&task_setup_data);
6406 if (IS_ERR(task))
6407 return PTR_ERR(task);
6408 status = task->tk_status;
6409 dprintk("<-- %s status=%d\n", __func__, status);
6410 rpc_put_task(task);
6411 return status;
6412}
6413
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006414/*
6415 * Retrieve the list of Data Server devices from the MDS.
6416 */
6417static int _nfs4_getdevicelist(struct nfs_server *server,
6418 const struct nfs_fh *fh,
6419 struct pnfs_devicelist *devlist)
6420{
6421 struct nfs4_getdevicelist_args args = {
6422 .fh = fh,
6423 .layoutclass = server->pnfs_curr_ld->id,
6424 };
6425 struct nfs4_getdevicelist_res res = {
6426 .devlist = devlist,
6427 };
6428 struct rpc_message msg = {
6429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6430 .rpc_argp = &args,
6431 .rpc_resp = &res,
6432 };
6433 int status;
6434
6435 dprintk("--> %s\n", __func__);
6436 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6437 &res.seq_res, 0);
6438 dprintk("<-- %s status=%d\n", __func__, status);
6439 return status;
6440}
6441
6442int nfs4_proc_getdevicelist(struct nfs_server *server,
6443 const struct nfs_fh *fh,
6444 struct pnfs_devicelist *devlist)
6445{
6446 struct nfs4_exception exception = { };
6447 int err;
6448
6449 do {
6450 err = nfs4_handle_exception(server,
6451 _nfs4_getdevicelist(server, fh, devlist),
6452 &exception);
6453 } while (exception.retry);
6454
6455 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6456 err, devlist->num_devs);
6457
6458 return err;
6459}
6460EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6461
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006462static int
6463_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6464{
6465 struct nfs4_getdeviceinfo_args args = {
6466 .pdev = pdev,
6467 };
6468 struct nfs4_getdeviceinfo_res res = {
6469 .pdev = pdev,
6470 };
6471 struct rpc_message msg = {
6472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6473 .rpc_argp = &args,
6474 .rpc_resp = &res,
6475 };
6476 int status;
6477
6478 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006479 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006480 dprintk("<-- %s status=%d\n", __func__, status);
6481
6482 return status;
6483}
6484
6485int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6486{
6487 struct nfs4_exception exception = { };
6488 int err;
6489
6490 do {
6491 err = nfs4_handle_exception(server,
6492 _nfs4_proc_getdeviceinfo(server, pdev),
6493 &exception);
6494 } while (exception.retry);
6495 return err;
6496}
6497EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6498
Andy Adamson863a3c62011-03-23 13:27:54 +00006499static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6500{
6501 struct nfs4_layoutcommit_data *data = calldata;
6502 struct nfs_server *server = NFS_SERVER(data->args.inode);
6503
6504 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006505 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006506 return;
6507 rpc_call_start(task);
6508}
6509
6510static void
6511nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6512{
6513 struct nfs4_layoutcommit_data *data = calldata;
6514 struct nfs_server *server = NFS_SERVER(data->args.inode);
6515
6516 if (!nfs4_sequence_done(task, &data->res.seq_res))
6517 return;
6518
6519 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006520 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6521 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6522 case -NFS4ERR_BADLAYOUT: /* no layout */
6523 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006524 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006525 break;
6526 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006527 nfs_post_op_update_inode_force_wcc(data->args.inode,
6528 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006529 break;
6530 default:
6531 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6532 rpc_restart_call_prepare(task);
6533 return;
6534 }
6535 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006536}
6537
6538static void nfs4_layoutcommit_release(void *calldata)
6539{
6540 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006541 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006542 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006543
Andy Adamsondb29c082011-07-30 20:52:38 -04006544 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006545 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006546 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6547 list_del_init(&lseg->pls_lc_list);
6548 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6549 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006550 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006551 }
Peng Tao92407e72011-10-23 20:21:17 -07006552
6553 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6554 smp_mb__after_clear_bit();
6555 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6556
Andy Adamson863a3c62011-03-23 13:27:54 +00006557 put_rpccred(data->cred);
6558 kfree(data);
6559}
6560
6561static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6562 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6563 .rpc_call_done = nfs4_layoutcommit_done,
6564 .rpc_release = nfs4_layoutcommit_release,
6565};
6566
6567int
Andy Adamsonef311532011-03-12 02:58:10 -05006568nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006569{
6570 struct rpc_message msg = {
6571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6572 .rpc_argp = &data->args,
6573 .rpc_resp = &data->res,
6574 .rpc_cred = data->cred,
6575 };
6576 struct rpc_task_setup task_setup_data = {
6577 .task = &data->task,
6578 .rpc_client = NFS_CLIENT(data->args.inode),
6579 .rpc_message = &msg,
6580 .callback_ops = &nfs4_layoutcommit_ops,
6581 .callback_data = data,
6582 .flags = RPC_TASK_ASYNC,
6583 };
6584 struct rpc_task *task;
6585 int status = 0;
6586
6587 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6588 "lbw: %llu inode %lu\n",
6589 data->task.tk_pid, sync,
6590 data->args.lastbytewritten,
6591 data->args.inode->i_ino);
6592
Trond Myklebust9d12b212012-01-17 22:04:25 -05006593 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006594 task = rpc_run_task(&task_setup_data);
6595 if (IS_ERR(task))
6596 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006597 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006598 goto out;
6599 status = nfs4_wait_for_completion_rpc_task(task);
6600 if (status != 0)
6601 goto out;
6602 status = task->tk_status;
6603out:
6604 dprintk("%s: status %d\n", __func__, status);
6605 rpc_put_task(task);
6606 return status;
6607}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006608
6609static int
6610_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6611 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6612{
6613 struct nfs41_secinfo_no_name_args args = {
6614 .style = SECINFO_STYLE_CURRENT_FH,
6615 };
6616 struct nfs4_secinfo_res res = {
6617 .flavors = flavors,
6618 };
6619 struct rpc_message msg = {
6620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6621 .rpc_argp = &args,
6622 .rpc_resp = &res,
6623 };
6624 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6625}
6626
6627static int
6628nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6629 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6630{
6631 struct nfs4_exception exception = { };
6632 int err;
6633 do {
6634 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6635 switch (err) {
6636 case 0:
6637 case -NFS4ERR_WRONGSEC:
6638 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006639 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006640 default:
6641 err = nfs4_handle_exception(server, err, &exception);
6642 }
6643 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006644out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006645 return err;
6646}
6647
6648static int
6649nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6650 struct nfs_fsinfo *info)
6651{
6652 int err;
6653 struct page *page;
6654 rpc_authflavor_t flavor;
6655 struct nfs4_secinfo_flavors *flavors;
6656
6657 page = alloc_page(GFP_KERNEL);
6658 if (!page) {
6659 err = -ENOMEM;
6660 goto out;
6661 }
6662
6663 flavors = page_address(page);
6664 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6665
6666 /*
6667 * Fall back on "guess and check" method if
6668 * the server doesn't support SECINFO_NO_NAME
6669 */
6670 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6671 err = nfs4_find_root_sec(server, fhandle, info);
6672 goto out_freepage;
6673 }
6674 if (err)
6675 goto out_freepage;
6676
6677 flavor = nfs_find_best_sec(flavors);
6678 if (err == 0)
6679 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6680
6681out_freepage:
6682 put_page(page);
6683 if (err == -EACCES)
6684 return -EPERM;
6685out:
6686 return err;
6687}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006688
6689static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006690{
6691 int status;
6692 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006693 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006694 };
6695 struct nfs41_test_stateid_res res;
6696 struct rpc_message msg = {
6697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6698 .rpc_argp = &args,
6699 .rpc_resp = &res,
6700 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006701
Chuck Lever38527b12012-07-11 16:30:23 -04006702 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006703 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006704 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006705 if (status != NFS_OK) {
6706 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006707 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006708 }
6709 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006710 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006711}
6712
Chuck Lever38527b12012-07-11 16:30:23 -04006713/**
6714 * nfs41_test_stateid - perform a TEST_STATEID operation
6715 *
6716 * @server: server / transport on which to perform the operation
6717 * @stateid: state ID to test
6718 *
6719 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6720 * Otherwise a negative NFS4ERR value is returned if the operation
6721 * failed or the state ID is not currently valid.
6722 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006723static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006724{
6725 struct nfs4_exception exception = { };
6726 int err;
6727 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006728 err = _nfs41_test_stateid(server, stateid);
6729 if (err != -NFS4ERR_DELAY)
6730 break;
6731 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006732 } while (exception.retry);
6733 return err;
6734}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006735
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006736static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006737{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006738 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006739 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006740 };
6741 struct nfs41_free_stateid_res res;
6742 struct rpc_message msg = {
6743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6744 .rpc_argp = &args,
6745 .rpc_resp = &res,
6746 };
Chuck Lever38527b12012-07-11 16:30:23 -04006747 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006748
Chuck Lever38527b12012-07-11 16:30:23 -04006749 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006750 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006751 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006752 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006753 dprintk("NFS reply free_stateid: %d\n", status);
6754 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006755}
6756
Chuck Lever38527b12012-07-11 16:30:23 -04006757/**
6758 * nfs41_free_stateid - perform a FREE_STATEID operation
6759 *
6760 * @server: server / transport on which to perform the operation
6761 * @stateid: state ID to release
6762 *
6763 * Returns NFS_OK if the server freed "stateid". Otherwise a
6764 * negative NFS4ERR value is returned.
6765 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006766static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006767{
6768 struct nfs4_exception exception = { };
6769 int err;
6770 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006771 err = _nfs4_free_stateid(server, stateid);
6772 if (err != -NFS4ERR_DELAY)
6773 break;
6774 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006775 } while (exception.retry);
6776 return err;
6777}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006778
6779static bool nfs41_match_stateid(const nfs4_stateid *s1,
6780 const nfs4_stateid *s2)
6781{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006782 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006783 return false;
6784
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006785 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006786 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006787 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006788 return true;
6789
6790 return false;
6791}
6792
Andy Adamson557134a2009-04-01 09:21:53 -04006793#endif /* CONFIG_NFS_V4_1 */
6794
Trond Myklebust36281ca2012-03-04 18:13:56 -05006795static bool nfs4_match_stateid(const nfs4_stateid *s1,
6796 const nfs4_stateid *s2)
6797{
Trond Myklebustf597c532012-03-04 18:13:56 -05006798 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006799}
6800
6801
Trond Myklebust17280172012-03-11 13:11:00 -04006802static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006803 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006804 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 .recover_open = nfs4_open_reclaim,
6806 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006807 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006808 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809};
6810
Andy Adamson591d71c2009-04-01 09:22:47 -04006811#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006812static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006813 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6814 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6815 .recover_open = nfs4_open_reclaim,
6816 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006817 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006818 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006819 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006820};
6821#endif /* CONFIG_NFS_V4_1 */
6822
Trond Myklebust17280172012-03-11 13:11:00 -04006823static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006824 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006825 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 .recover_open = nfs4_open_expired,
6827 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006828 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006829 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830};
6831
Andy Adamson591d71c2009-04-01 09:22:47 -04006832#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006833static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006834 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6835 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006836 .recover_open = nfs41_open_expired,
6837 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006838 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006839 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006840};
6841#endif /* CONFIG_NFS_V4_1 */
6842
Trond Myklebust17280172012-03-11 13:11:00 -04006843static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006844 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006845 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006846 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006847};
6848
6849#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006850static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006851 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006852 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006853 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006854};
6855#endif
6856
Trond Myklebust97dc1352010-06-16 09:52:26 -04006857static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6858 .minor_version = 0,
6859 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006860 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006861 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006862 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6863 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6864 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006865};
6866
6867#if defined(CONFIG_NFS_V4_1)
6868static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6869 .minor_version = 1,
6870 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006871 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006872 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006873 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6874 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6875 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006876};
6877#endif
6878
Trond Myklebust97dc1352010-06-16 09:52:26 -04006879const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6880 [0] = &nfs_v4_0_minor_ops,
6881#if defined(CONFIG_NFS_V4_1)
6882 [1] = &nfs_v4_1_minor_ops,
6883#endif
6884};
6885
Bryan Schumaker73a79702012-07-16 16:39:12 -04006886const struct inode_operations nfs4_dir_inode_operations = {
6887 .create = nfs_create,
6888 .lookup = nfs_lookup,
6889 .atomic_open = nfs_atomic_open,
6890 .link = nfs_link,
6891 .unlink = nfs_unlink,
6892 .symlink = nfs_symlink,
6893 .mkdir = nfs_mkdir,
6894 .rmdir = nfs_rmdir,
6895 .mknod = nfs_mknod,
6896 .rename = nfs_rename,
6897 .permission = nfs_permission,
6898 .getattr = nfs_getattr,
6899 .setattr = nfs_setattr,
6900 .getxattr = generic_getxattr,
6901 .setxattr = generic_setxattr,
6902 .listxattr = generic_listxattr,
6903 .removexattr = generic_removexattr,
6904};
6905
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006906static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006907 .permission = nfs_permission,
6908 .getattr = nfs_getattr,
6909 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006910 .getxattr = generic_getxattr,
6911 .setxattr = generic_setxattr,
6912 .listxattr = generic_listxattr,
6913 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006914};
6915
David Howells509de812006-08-22 20:06:11 -04006916const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 .version = 4, /* protocol version */
6918 .dentry_ops = &nfs4_dentry_operations,
6919 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006920 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006921 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006923 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006924 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 .getattr = nfs4_proc_getattr,
6926 .setattr = nfs4_proc_setattr,
6927 .lookup = nfs4_proc_lookup,
6928 .access = nfs4_proc_access,
6929 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 .create = nfs4_proc_create,
6931 .remove = nfs4_proc_remove,
6932 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006933 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 .unlink_done = nfs4_proc_unlink_done,
6935 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006936 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006937 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006938 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 .link = nfs4_proc_link,
6940 .symlink = nfs4_proc_symlink,
6941 .mkdir = nfs4_proc_mkdir,
6942 .rmdir = nfs4_proc_remove,
6943 .readdir = nfs4_proc_readdir,
6944 .mknod = nfs4_proc_mknod,
6945 .statfs = nfs4_proc_statfs,
6946 .fsinfo = nfs4_proc_fsinfo,
6947 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006948 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 .decode_dirent = nfs4_decode_dirent,
6950 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006951 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006952 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006953 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006955 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006956 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006957 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006959 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006960 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006962 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006963 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006964 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006965 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006966 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006967 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006968 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006969 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006970 .create_server = nfs4_create_server,
6971 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972};
6973
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006974static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6975 .prefix = XATTR_NAME_NFSV4_ACL,
6976 .list = nfs4_xattr_list_nfs4_acl,
6977 .get = nfs4_xattr_get_nfs4_acl,
6978 .set = nfs4_xattr_set_nfs4_acl,
6979};
6980
6981const struct xattr_handler *nfs4_xattr_handlers[] = {
6982 &nfs4_xattr_nfs4_acl_handler,
6983 NULL
6984};
6985
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986/*
6987 * Local variables:
6988 * c-basic-offset: 8
6989 * End:
6990 */