blob: 635274140b180287668dbaa7540bd84852051181 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Trond Myklebust52567b02009-10-23 14:46:42 -0400107 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700109 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * This is our standard bitmap for GETATTR requests.
117 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400118const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 FATTR4_WORD0_TYPE
120 | FATTR4_WORD0_CHANGE
121 | FATTR4_WORD0_SIZE
122 | FATTR4_WORD0_FSID
123 | FATTR4_WORD0_FILEID,
124 FATTR4_WORD1_MODE
125 | FATTR4_WORD1_NUMLINKS
126 | FATTR4_WORD1_OWNER
127 | FATTR4_WORD1_OWNER_GROUP
128 | FATTR4_WORD1_RAWDEV
129 | FATTR4_WORD1_SPACE_USED
130 | FATTR4_WORD1_TIME_ACCESS
131 | FATTR4_WORD1_TIME_METADATA
132 | FATTR4_WORD1_TIME_MODIFY
133};
134
Trond Myklebust1549210f2012-06-05 09:16:47 -0400135static const u32 nfs4_pnfs_open_bitmap[3] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID,
141 FATTR4_WORD1_MODE
142 | FATTR4_WORD1_NUMLINKS
143 | FATTR4_WORD1_OWNER
144 | FATTR4_WORD1_OWNER_GROUP
145 | FATTR4_WORD1_RAWDEV
146 | FATTR4_WORD1_SPACE_USED
147 | FATTR4_WORD1_TIME_ACCESS
148 | FATTR4_WORD1_TIME_METADATA
149 | FATTR4_WORD1_TIME_MODIFY,
150 FATTR4_WORD2_MDSTHRESHOLD
151};
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153const u32 nfs4_statfs_bitmap[2] = {
154 FATTR4_WORD0_FILES_AVAIL
155 | FATTR4_WORD0_FILES_FREE
156 | FATTR4_WORD0_FILES_TOTAL,
157 FATTR4_WORD1_SPACE_AVAIL
158 | FATTR4_WORD1_SPACE_FREE
159 | FATTR4_WORD1_SPACE_TOTAL
160};
161
Trond Myklebust4ce79712005-06-22 17:16:21 +0000162const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 FATTR4_WORD0_MAXLINK
164 | FATTR4_WORD0_MAXNAME,
165 0
166};
167
Fred Isamandae100c2011-07-30 20:52:37 -0400168const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 | FATTR4_WORD0_MAXREAD
170 | FATTR4_WORD0_MAXWRITE
171 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700172 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400173 | FATTR4_WORD1_FS_LAYOUT_TYPES,
174 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Manoj Naik830b8e32006-06-09 09:34:25 -0400177const u32 nfs4_fs_locations_bitmap[2] = {
178 FATTR4_WORD0_TYPE
179 | FATTR4_WORD0_CHANGE
180 | FATTR4_WORD0_SIZE
181 | FATTR4_WORD0_FSID
182 | FATTR4_WORD0_FILEID
183 | FATTR4_WORD0_FS_LOCATIONS,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
192 | FATTR4_WORD1_TIME_MODIFY
193 | FATTR4_WORD1_MOUNTED_ON_FILEID
194};
195
Al Virobc4785c2006-10-19 23:28:51 -0700196static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct nfs4_readdir_arg *readdir)
198{
Al Viro0dbb4c62006-10-19 23:28:49 -0700199 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 BUG_ON(readdir->count < 80);
202 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000203 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
205 return;
206 }
207
208 readdir->cookie = 0;
209 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
210 if (cookie == 2)
211 return;
212
213 /*
214 * NFSv4 servers do not return entries for '.' and '..'
215 * Therefore, we fake these entries here. We let '.'
216 * have cookie 0 and '..' have cookie 1. Note that
217 * when talking to the server, we always send cookie 0
218 * instead of 1 or 2.
219 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800220 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (cookie == 0) {
223 *p++ = xdr_one; /* next */
224 *p++ = xdr_zero; /* cookie, first word */
225 *p++ = xdr_one; /* cookie, second word */
226 *p++ = xdr_one; /* entry len */
227 memcpy(p, ".\0\0\0", 4); /* entry */
228 p++;
229 *p++ = xdr_one; /* bitmap length */
230 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
231 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400232 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 *p++ = xdr_one; /* next */
236 *p++ = xdr_zero; /* cookie, first word */
237 *p++ = xdr_two; /* cookie, second word */
238 *p++ = xdr_two; /* entry len */
239 memcpy(p, "..\0\0", 4); /* entry */
240 p++;
241 *p++ = xdr_one; /* bitmap length */
242 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
243 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400244 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 readdir->pgbase = (char *)p - (char *)start;
247 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800248 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Trond Myklebust65de8722008-12-23 15:21:44 -0500251static int nfs4_wait_clnt_recover(struct nfs_client *clp)
252{
253 int res;
254
255 might_sleep();
256
Trond Myklebuste005e802008-12-23 15:21:48 -0500257 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400258 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400259 if (res)
260 return res;
261
262 if (clp->cl_cons_state < 0)
263 return clp->cl_cons_state;
264 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500265}
266
267static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
268{
269 int res = 0;
270
271 might_sleep();
272
273 if (*timeout <= 0)
274 *timeout = NFS4_POLL_RETRY_MIN;
275 if (*timeout > NFS4_POLL_RETRY_MAX)
276 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100277 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 if (fatal_signal_pending(current))
279 res = -ERESTARTSYS;
280 *timeout <<= 1;
281 return res;
282}
283
284/* This is the error handling routine for processes that are allowed
285 * to sleep.
286 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800287static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500288{
289 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500290 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500292 int ret = errorcode;
293
294 exception->retry = 0;
295 switch(errorcode) {
296 case 0:
297 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500298 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400299 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400300 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500301 exception->retry = 1;
302 return 0;
303 }
304 if (state == NULL)
305 break;
306 nfs4_schedule_stateid_recovery(server, state);
307 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500308 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500309 case -NFS4ERR_ADMIN_REVOKED:
310 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500311 if (state == NULL)
312 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400313 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500314 nfs4_schedule_stateid_recovery(server, state);
315 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400316 case -NFS4ERR_EXPIRED:
317 if (state != NULL)
318 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500319 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500320 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500321 nfs4_schedule_lease_recovery(clp);
322 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500323#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400324 case -NFS4ERR_BADSESSION:
325 case -NFS4ERR_BADSLOT:
326 case -NFS4ERR_BAD_HIGH_SLOT:
327 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
328 case -NFS4ERR_DEADSESSION:
329 case -NFS4ERR_SEQ_FALSE_RETRY:
330 case -NFS4ERR_SEQ_MISORDERED:
331 dprintk("%s ERROR: %d Reset session\n", __func__,
332 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400333 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400334 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500335 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500336#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500338 if (exception->timeout > HZ) {
339 /* We have retried a decent amount, time to
340 * fail
341 */
342 ret = -EBUSY;
343 break;
344 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 case -NFS4ERR_GRACE:
346 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500347 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500348 ret = nfs4_delay(server->client, &exception->timeout);
349 if (ret != 0)
350 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400351 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 case -NFS4ERR_OLD_STATEID:
353 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800354 break;
355 case -NFS4ERR_BADOWNER:
356 /* The following works around a Linux server bug! */
357 case -NFS4ERR_BADNAME:
358 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
359 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
360 exception->retry = 1;
361 printk(KERN_WARNING "NFS: v4 server %s "
362 "does not accept raw "
363 "uid/gids. "
364 "Reenabling the idmapper.\n",
365 server->nfs_client->cl_hostname);
366 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500367 }
368 /* We failed to handle the error */
369 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500370wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500371 ret = nfs4_wait_clnt_recover(clp);
372 if (ret == 0)
373 exception->retry = 1;
374 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500375}
376
377
Trond Myklebust452e9352010-07-31 14:29:06 -0400378static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_lock(&clp->cl_lock);
381 if (time_before(clp->cl_last_renewal,timestamp))
382 clp->cl_last_renewal = timestamp;
383 spin_unlock(&clp->cl_lock);
384}
385
Trond Myklebust452e9352010-07-31 14:29:06 -0400386static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
387{
388 do_renew_lease(server->nfs_client, timestamp);
389}
390
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400391#if defined(CONFIG_NFS_V4_1)
392
Benny Halevy510b8172009-04-01 09:22:14 -0400393/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400394 * nfs4_free_slot - free a slot and efficiently update slot table.
395 *
396 * freeing a slot is trivially done by clearing its respective bit
397 * in the bitmap.
398 * If the freed slotid equals highest_used_slotid we want to update it
399 * so that the server would be able to size down the slot table if needed,
400 * otherwise we know that the highest_used_slotid is still in use.
401 * When updating highest_used_slotid there may be "holes" in the bitmap
402 * so we need to scan down from highest_used_slotid to 0 looking for the now
403 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500404 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500405 *
406 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400407 */
408static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500409nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400410{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500411 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400412 /* clear used bit in bitmap */
413 __clear_bit(slotid, tbl->used_slots);
414
415 /* update highest_used_slotid when it is freed */
416 if (slotid == tbl->highest_used_slotid) {
417 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500418 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400419 tbl->highest_used_slotid = slotid;
420 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500421 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400422 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500423 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
424 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400425}
426
Trond Myklebust961a8282012-01-17 22:57:37 -0500427bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
428{
429 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
430 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400431}
432
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800433/*
Andy Adamson42acd022011-01-06 02:04:34 +0000434 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435 */
Andy Adamson42acd022011-01-06 02:04:34 +0000436static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400438 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500439 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
440 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800441 return;
442 }
443
Trond Myklebust45d43c22012-02-06 19:38:51 -0500444 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800445 return;
446
Andy Adamson42acd022011-01-06 02:04:34 +0000447 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
448 complete(&ses->fc_slot_table.complete);
449}
450
451/*
452 * Signal state manager thread if session back channel is drained
453 */
454void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
455{
456 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500457 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000458 return;
459 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
460 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800461}
462
Trond Myklebustd185a332010-06-16 09:52:25 -0400463static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400464{
465 struct nfs4_slot_table *tbl;
466
Trond Myklebustd185a332010-06-16 09:52:25 -0400467 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400468 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400469 /* just wake up the next guy waiting since
470 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500471 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500472 return;
Andy Adamson13615872009-04-01 09:22:17 -0400473 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500474
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500475 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500476 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000477 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500478 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400479 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400480}
481
Trond Myklebust14516c32010-07-31 14:29:06 -0400482static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483{
484 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400486
487 /*
488 * sr_status remains 1 if an RPC level error occurred. The server
489 * may or may not have processed the sequence operation..
490 * Proceed as if the server received and processed the sequence
491 * operation.
492 */
493 if (res->sr_status == 1)
494 res->sr_status = NFS_OK;
495
Bryan Schumaker468f8612011-04-18 15:57:32 -0400496 /* don't increment the sequence number if the task wasn't sent */
497 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400498 goto out;
499
Andy Adamson691daf32009-12-04 15:55:39 -0500500 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400501 switch (res->sr_status) {
502 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400503 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400504 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400505 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400506 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400507 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500508 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500509 if (res->sr_status_flags != 0)
510 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400511 break;
512 case -NFS4ERR_DELAY:
513 /* The server detected a resend of the RPC call and
514 * returned NFS4ERR_DELAY as per Section 2.10.6.2
515 * of RFC5661.
516 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200517 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400518 __func__,
519 res->sr_slot - res->sr_session->fc_slot_table.slots,
520 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400521 goto out_retry;
522 default:
523 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400524 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400525 }
526out:
527 /* The session may be reset by one of the error handlers. */
528 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400529 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400530 return 1;
531out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400532 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 goto out;
534 rpc_delay(task, NFS4_POLL_RETRY_MAX);
535 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400536}
537
Trond Myklebust14516c32010-07-31 14:29:06 -0400538static int nfs4_sequence_done(struct rpc_task *task,
539 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400540{
Trond Myklebust14516c32010-07-31 14:29:06 -0400541 if (res->sr_session == NULL)
542 return 1;
543 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400544}
545
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400546/*
Benny Halevy510b8172009-04-01 09:22:14 -0400547 * nfs4_find_slot - efficiently look for a free slot
548 *
549 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
550 * If found, we mark the slot as used, update the highest_used_slotid,
551 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500552 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400553 *
554 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400555 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500556static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800557nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400558{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500559 u32 slotid;
560 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400561
Trond Myklebust45d43c22012-02-06 19:38:51 -0500562 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400563 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
564 tbl->max_slots);
565 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
566 if (slotid >= tbl->max_slots)
567 goto out;
568 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500569 if (slotid > tbl->highest_used_slotid ||
570 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400571 tbl->highest_used_slotid = slotid;
572 ret_id = slotid;
573out:
574 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
575 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400576 return ret_id;
577}
578
Trond Myklebust9d12b212012-01-17 22:04:25 -0500579static void nfs41_init_sequence(struct nfs4_sequence_args *args,
580 struct nfs4_sequence_res *res, int cache_reply)
581{
582 args->sa_session = NULL;
583 args->sa_cache_this = 0;
584 if (cache_reply)
585 args->sa_cache_this = 1;
586 res->sr_session = NULL;
587 res->sr_slot = NULL;
588}
589
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000590int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 struct nfs4_sequence_args *args,
592 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct rpc_task *task)
594{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595 struct nfs4_slot *slot;
596 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500597 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400598
599 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400601 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 return 0;
603
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400604 tbl = &session->fc_slot_table;
605
606 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400607 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800608 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500609 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500610 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400611 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500612 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500613 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400614 }
615
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800616 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
617 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
618 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
619 spin_unlock(&tbl->slot_tbl_lock);
620 dprintk("%s enforce FIFO order\n", __func__);
621 return -EAGAIN;
622 }
623
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800624 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500625 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400626 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
627 spin_unlock(&tbl->slot_tbl_lock);
628 dprintk("<-- %s: no free slots\n", __func__);
629 return -EAGAIN;
630 }
631 spin_unlock(&tbl->slot_tbl_lock);
632
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800633 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400634 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400635 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400636 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400637
638 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
639
Benny Halevy99fe60d2009-04-01 09:22:29 -0400640 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400641 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400643 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644 /*
645 * sr_status is only set in decode_sequence, and so will remain
646 * set to 1 if an rpc level failure occurs.
647 */
648 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649 return 0;
650}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000651EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652
Trond Myklebust035168a2010-06-16 09:52:26 -0400653int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 struct nfs4_sequence_args *args,
655 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 struct rpc_task *task)
657{
Trond Myklebust035168a2010-06-16 09:52:26 -0400658 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659 int ret = 0;
660
Trond Myklebust9d12b212012-01-17 22:04:25 -0500661 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400663
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200664 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400665 __func__, session->clp, session, res->sr_slot ?
666 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400667
Trond Myklebust9d12b212012-01-17 22:04:25 -0500668 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669out:
670 dprintk("<-- %s status=%d\n", __func__, ret);
671 return ret;
672}
673
674struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400675 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676 struct nfs4_sequence_args *seq_args;
677 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678};
679
680static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
681{
682 struct nfs41_call_sync_data *data = calldata;
683
Trond Myklebust035168a2010-06-16 09:52:26 -0400684 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
685
686 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500687 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 return;
689 rpc_call_start(task);
690}
691
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800692static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
693{
694 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
695 nfs41_call_sync_prepare(task, calldata);
696}
697
Andy Adamson69ab40c2009-04-01 09:22:19 -0400698static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
699{
700 struct nfs41_call_sync_data *data = calldata;
701
Trond Myklebust14516c32010-07-31 14:29:06 -0400702 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400703}
704
Trond Myklebust17280172012-03-11 13:11:00 -0400705static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400707 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708};
709
Trond Myklebust17280172012-03-11 13:11:00 -0400710static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800711 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
712 .rpc_call_done = nfs41_call_sync_done,
713};
714
Bryan Schumaker7c513052011-03-24 17:12:24 +0000715static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
716 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717 struct rpc_message *msg,
718 struct nfs4_sequence_args *args,
719 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800720 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721{
722 int ret;
723 struct rpc_task *task;
724 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400725 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726 .seq_args = args,
727 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 };
729 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000730 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731 .rpc_message = msg,
732 .callback_ops = &nfs41_call_sync_ops,
733 .callback_data = &data
734 };
735
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800736 if (privileged)
737 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400738 task = rpc_run_task(&task_setup);
739 if (IS_ERR(task))
740 ret = PTR_ERR(task);
741 else {
742 ret = task->tk_status;
743 rpc_put_task(task);
744 }
745 return ret;
746}
747
Bryan Schumaker7c513052011-03-24 17:12:24 +0000748int _nfs4_call_sync_session(struct rpc_clnt *clnt,
749 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400750 struct rpc_message *msg,
751 struct nfs4_sequence_args *args,
752 struct nfs4_sequence_res *res,
753 int cache_reply)
754{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500755 nfs41_init_sequence(args, res, cache_reply);
756 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400757}
758
Trond Myklebustdf896452010-06-16 09:52:26 -0400759#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500760static inline
761void nfs41_init_sequence(struct nfs4_sequence_args *args,
762 struct nfs4_sequence_res *res, int cache_reply)
763{
764}
765
Trond Myklebust14516c32010-07-31 14:29:06 -0400766static int nfs4_sequence_done(struct rpc_task *task,
767 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400768{
Trond Myklebust14516c32010-07-31 14:29:06 -0400769 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400770}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400771#endif /* CONFIG_NFS_V4_1 */
772
Bryan Schumaker7c513052011-03-24 17:12:24 +0000773int _nfs4_call_sync(struct rpc_clnt *clnt,
774 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400775 struct rpc_message *msg,
776 struct nfs4_sequence_args *args,
777 struct nfs4_sequence_res *res,
778 int cache_reply)
779{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500780 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400782}
783
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000784static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000785int nfs4_call_sync(struct rpc_clnt *clnt,
786 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000787 struct rpc_message *msg,
788 struct nfs4_sequence_args *args,
789 struct nfs4_sequence_res *res,
790 int cache_reply)
791{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000792 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
793 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000794}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400795
Trond Myklebust38478b22006-05-25 01:40:57 -0400796static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Trond Myklebust38478b22006-05-25 01:40:57 -0400798 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Trond Myklebust38478b22006-05-25 01:40:57 -0400800 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400801 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700802 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400803 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700804 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400809 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810 struct nfs_openargs o_arg;
811 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100812 struct nfs_open_confirmargs c_arg;
813 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 struct nfs4_string owner_name;
815 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400820 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100822 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400823 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100824 int rpc_status;
825 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826};
827
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400828
829static void nfs4_init_opendata_res(struct nfs4_opendata *p)
830{
831 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400832 p->o_res.seqid = p->o_arg.seqid;
833 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400834 p->o_res.server = p->o_arg.server;
835 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500836 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400837}
838
Al Viro82a2c1b2011-06-22 18:30:55 -0400839static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400841 const struct iattr *attrs,
842 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843{
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct inode *dir = parent->d_inode;
846 struct nfs_server *server = NFS_SERVER(dir);
847 struct nfs4_opendata *p;
848
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400849 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 if (p == NULL)
851 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400852 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 if (p->o_arg.seqid == NULL)
854 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400855 nfs_sb_active(dentry->d_sb);
856 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 p->dir = parent;
858 p->owner = sp;
859 atomic_inc(&sp->so_count);
860 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861 p->o_arg.open_flags = flags;
862 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400863 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400864 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
865 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400866 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 p->o_arg.server = server;
868 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400869 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500871 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500872 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400873
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100874 p->o_arg.u.attrs = &p->attrs;
875 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500876
877 verf[0] = jiffies;
878 verf[1] = current->pid;
879 memcpy(p->o_arg.u.verifier.data, verf,
880 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100882 p->c_arg.fh = &p->o_res.fh;
883 p->c_arg.stateid = &p->o_res.stateid;
884 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400885 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400886 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100887 return p;
888err_free:
889 kfree(p);
890err:
891 dput(parent);
892 return NULL;
893}
894
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400895static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100896{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400897 struct nfs4_opendata *p = container_of(kref,
898 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400899 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400900
901 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 if (p->state != NULL)
903 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400904 nfs4_put_state_owner(p->owner);
905 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400906 dput(p->dentry);
907 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500908 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400909 kfree(p);
910}
911
912static void nfs4_opendata_put(struct nfs4_opendata *p)
913{
914 if (p != NULL)
915 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100916}
917
Trond Myklebust06f814a2006-01-03 09:55:07 +0100918static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
919{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920 int ret;
921
Trond Myklebust06f814a2006-01-03 09:55:07 +0100922 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100923 return ret;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400927{
928 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929
Trond Myklebust536e43d2012-01-17 22:04:26 -0500930 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 goto out;
932 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400933 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500934 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
935 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400936 break;
937 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500938 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
939 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 break;
941 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500942 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
943 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400946 return ret;
947}
948
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500951 if (delegation == NULL)
952 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500955 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400956 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500957 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 return 1;
959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100962{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100964 case FMODE_WRITE:
965 state->n_wronly++;
966 break;
967 case FMODE_READ:
968 state->n_rdonly++;
969 break;
970 case FMODE_READ|FMODE_WRITE:
971 state->n_rdwr++;
972 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100974}
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400977{
978 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500979 nfs4_stateid_copy(&state->stateid, stateid);
980 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400982 case FMODE_READ:
983 set_bit(NFS_O_RDONLY_STATE, &state->flags);
984 break;
985 case FMODE_WRITE:
986 set_bit(NFS_O_WRONLY_STATE, &state->flags);
987 break;
988 case FMODE_READ|FMODE_WRITE:
989 set_bit(NFS_O_RDWR_STATE, &state->flags);
990 }
991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400994{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400995 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400997 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400998}
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001002 /*
1003 * Protect the call to nfs4_state_set_mode_locked and
1004 * serialise the stateid update
1005 */
1006 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001007 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001008 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009 set_bit(NFS_DELEGATED_STATE, &state->flags);
1010 }
1011 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 write_sequnlock(&state->seqlock);
1014 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001016 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001020{
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *deleg_cur;
1023 int ret = 0;
1024
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001026
1027 rcu_read_lock();
1028 deleg_cur = rcu_dereference(nfsi->delegation);
1029 if (deleg_cur == NULL)
1030 goto no_delegation;
1031
1032 spin_lock(&deleg_cur->lock);
1033 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001035 goto no_delegation_unlock;
1036
1037 if (delegation == NULL)
1038 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001039 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001040 goto no_delegation_unlock;
1041
Trond Myklebustb7391f42008-12-23 15:21:52 -05001042 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001044 ret = 1;
1045no_delegation_unlock:
1046 spin_unlock(&deleg_cur->lock);
1047no_delegation:
1048 rcu_read_unlock();
1049
1050 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001052 ret = 1;
1053 }
1054
1055 return ret;
1056}
1057
1058
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060{
1061 struct nfs_delegation *delegation;
1062
1063 rcu_read_lock();
1064 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066 rcu_read_unlock();
1067 return;
1068 }
1069 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001070 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071}
1072
Trond Myklebust6ee41262007-07-08 14:11:36 -04001073static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074{
1075 struct nfs4_state *state = opendata->state;
1076 struct nfs_inode *nfsi = NFS_I(state->inode);
1077 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001078 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001079 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 nfs4_stateid stateid;
1081 int ret = -EAGAIN;
1082
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (can_open_cached(state, fmode, open_mode)) {
1087 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001089 goto out_return_state;
1090 }
1091 spin_unlock(&state->owner->so_lock);
1092 }
Trond Myklebust34310432008-12-23 15:21:38 -05001093 rcu_read_lock();
1094 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001095 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001096 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001098 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001100 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001102 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 if (ret != 0)
1104 goto out;
1105 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001106
1107 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001109 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111out:
1112 return ERR_PTR(ret);
1113out_return_state:
1114 atomic_inc(&state->count);
1115 return state;
1116}
1117
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001118static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1119{
1120 struct inode *inode;
1121 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001122 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001123 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124
Trond Myklebustaac00a82007-07-05 19:02:21 -04001125 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001126 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 goto out;
1128 }
1129
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001133 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001135 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001136 goto err;
1137 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138 state = nfs4_get_open_state(inode, data->owner);
1139 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001140 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001141 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001142 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001143 int delegation_flags = 0;
1144
Trond Myklebust003707c2007-07-05 18:07:55 -04001145 rcu_read_lock();
1146 delegation = rcu_dereference(NFS_I(inode)->delegation);
1147 if (delegation)
1148 delegation_flags = delegation->flags;
1149 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001150 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1151 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1152 "returning a delegation for "
1153 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001154 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001155 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001156 nfs_inode_set_delegation(state->inode,
1157 data->owner->so_cred,
1158 &data->o_res);
1159 else
1160 nfs_inode_reclaim_delegation(state->inode,
1161 data->owner->so_cred,
1162 &data->o_res);
1163 }
Trond Myklebust34310432008-12-23 15:21:38 -05001164
1165 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001166 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001167 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001168out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001169 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001170err_put_inode:
1171 iput(inode);
1172err:
1173 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001174}
1175
Trond Myklebust864472e2006-01-03 09:55:15 +01001176static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1177{
1178 struct nfs_inode *nfsi = NFS_I(state->inode);
1179 struct nfs_open_context *ctx;
1180
1181 spin_lock(&state->inode->i_lock);
1182 list_for_each_entry(ctx, &nfsi->open_files, list) {
1183 if (ctx->state != state)
1184 continue;
1185 get_nfs_open_context(ctx);
1186 spin_unlock(&state->inode->i_lock);
1187 return ctx;
1188 }
1189 spin_unlock(&state->inode->i_lock);
1190 return ERR_PTR(-ENOENT);
1191}
1192
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001193static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1194{
1195 struct nfs4_opendata *opendata;
1196
Al Viro3d4ff432011-06-22 18:40:12 -04001197 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001198 if (opendata == NULL)
1199 return ERR_PTR(-ENOMEM);
1200 opendata->state = state;
1201 atomic_inc(&state->count);
1202 return opendata;
1203}
1204
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001205static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001206{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 int ret;
1209
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 opendata->o_arg.open_flags = 0;
1211 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1213 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1214 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001215 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 if (ret != 0)
1217 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219 if (IS_ERR(newstate))
1220 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001221 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 return 0;
1224}
1225
1226static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1227{
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 int ret;
1230
1231 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 smp_rmb();
1234 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001235 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 if (ret != 0)
1238 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001239 if (newstate != state)
1240 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 }
1242 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001243 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 if (ret != 0)
1246 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 if (newstate != state)
1248 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 }
1250 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001251 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 if (ret != 0)
1254 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 if (newstate != state)
1256 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001258 /*
1259 * We may have performed cached opens for all three recoveries.
1260 * Check if we need to update the current stateid.
1261 */
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001263 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001264 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001266 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001267 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 return 0;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
1273 * OPEN_RECLAIM:
1274 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001278 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001280 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int status;
1282
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001283 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1284 if (IS_ERR(opendata))
1285 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1287 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001290 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001291 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001292 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 opendata->o_arg.u.delegation_type = delegation_type;
1294 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return status;
1297}
1298
Trond Myklebust539cd032007-06-05 11:46:42 -04001299static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs_server *server = NFS_SERVER(state->inode);
1302 struct nfs4_exception exception = { };
1303 int err;
1304 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001305 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001306 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001307 break;
1308 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 } while (exception.retry);
1310 return err;
1311}
1312
Trond Myklebust864472e2006-01-03 09:55:15 +01001313static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1314{
1315 struct nfs_open_context *ctx;
1316 int ret;
1317
1318 ctx = nfs4_state_find_open_context(state);
1319 if (IS_ERR(ctx))
1320 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 put_nfs_open_context(ctx);
1323 return ret;
1324}
1325
Trond Myklebust13437e12007-07-06 15:10:43 -04001326static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001328 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001331 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1332 if (IS_ERR(opendata))
1333 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001334 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001336 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001337 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Trond Myklebust13437e12007-07-06 15:10:43 -04001341int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001344 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int err;
1346 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001347 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 switch (err) {
1349 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350 case -ENOENT:
1351 case -ESTALE:
1352 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001353 case -NFS4ERR_BADSESSION:
1354 case -NFS4ERR_BADSLOT:
1355 case -NFS4ERR_BAD_HIGH_SLOT:
1356 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1357 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001358 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 case -NFS4ERR_STALE_CLIENTID:
1361 case -NFS4ERR_STALE_STATEID:
1362 case -NFS4ERR_EXPIRED:
1363 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001364 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001365 goto out;
1366 case -ERESTARTSYS:
1367 /*
1368 * The show must go on: exit, but mark the
1369 * stateid as needing recovery.
1370 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001371 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001372 case -NFS4ERR_ADMIN_REVOKED:
1373 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001374 nfs_inode_find_state_and_recover(state->inode,
1375 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001376 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001377 case -EKEYEXPIRED:
1378 /*
1379 * User RPCSEC_GSS context has expired.
1380 * We cannot recover this stateid now, so
1381 * skip it and allow recovery thread to
1382 * proceed.
1383 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001384 case -ENOMEM:
1385 err = 0;
1386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 err = nfs4_handle_exception(server, err, &exception);
1389 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001390out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return err;
1392}
1393
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1395{
1396 struct nfs4_opendata *data = calldata;
1397
1398 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001399 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001400 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001401 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001402 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001403 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001404 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405}
1406
1407static void nfs4_open_confirm_release(void *calldata)
1408{
1409 struct nfs4_opendata *data = calldata;
1410 struct nfs4_state *state = NULL;
1411
1412 /* If this request hasn't been cancelled, do nothing */
1413 if (data->cancelled == 0)
1414 goto out_free;
1415 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001416 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001419 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001420 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001421out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001422 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423}
1424
1425static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426 .rpc_call_done = nfs4_open_confirm_done,
1427 .rpc_release = nfs4_open_confirm_release,
1428};
1429
1430/*
1431 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1432 */
1433static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1434{
1435 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1436 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001437 struct rpc_message msg = {
1438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1439 .rpc_argp = &data->c_arg,
1440 .rpc_resp = &data->c_res,
1441 .rpc_cred = data->owner->so_cred,
1442 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001443 struct rpc_task_setup task_setup_data = {
1444 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001445 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001446 .callback_ops = &nfs4_open_confirm_ops,
1447 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001448 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001449 .flags = RPC_TASK_ASYNC,
1450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int status;
1452
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001453 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001454 data->rpc_done = 0;
1455 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001457 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001458 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001459 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001460 status = nfs4_wait_for_completion_rpc_task(task);
1461 if (status != 0) {
1462 data->cancelled = 1;
1463 smp_wmb();
1464 } else
1465 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001466 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return status;
1468}
1469
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 struct nfs4_opendata *data = calldata;
1473 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001474
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1476 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001477 /*
1478 * Check if we still need to send an OPEN call, or if we can use
1479 * a delegation instead.
1480 */
1481 if (data->state != NULL) {
1482 struct nfs_delegation *delegation;
1483
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001484 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001485 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001486 rcu_read_lock();
1487 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001488 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1489 can_open_delegated(delegation, data->o_arg.fmode))
1490 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001491 rcu_read_unlock();
1492 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001493 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001494 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001495 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001496 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001497 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1498 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001499 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001500 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001501 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001502 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001503 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001504 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001505 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001506unlock_no_action:
1507 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001508out_no_action:
1509 task->tk_action = NULL;
1510
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001513static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1514{
1515 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1516 nfs4_open_prepare(task, calldata);
1517}
1518
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519static void nfs4_open_done(struct rpc_task *task, void *calldata)
1520{
1521 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001524
Trond Myklebust14516c32010-07-31 14:29:06 -04001525 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1526 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001527
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 if (task->tk_status == 0) {
1529 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 case S_IFREG:
1531 break;
1532 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534 break;
1535 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001537 break;
1538 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001540 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001541 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001542 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1543 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001544 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001545 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001547
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548static void nfs4_open_release(void *calldata)
1549{
1550 struct nfs4_opendata *data = calldata;
1551 struct nfs4_state *state = NULL;
1552
1553 /* If this request hasn't been cancelled, do nothing */
1554 if (data->cancelled == 0)
1555 goto out_free;
1556 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001557 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 goto out_free;
1559 /* In case we need an open_confirm, no cleanup! */
1560 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1561 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001563 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001564 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001565out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001566 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567}
1568
1569static const struct rpc_call_ops nfs4_open_ops = {
1570 .rpc_call_prepare = nfs4_open_prepare,
1571 .rpc_call_done = nfs4_open_done,
1572 .rpc_release = nfs4_open_release,
1573};
1574
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001575static const struct rpc_call_ops nfs4_recover_open_ops = {
1576 .rpc_call_prepare = nfs4_recover_open_prepare,
1577 .rpc_call_done = nfs4_open_done,
1578 .rpc_release = nfs4_open_release,
1579};
1580
1581static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001582{
1583 struct inode *dir = data->dir->d_inode;
1584 struct nfs_server *server = NFS_SERVER(dir);
1585 struct nfs_openargs *o_arg = &data->o_arg;
1586 struct nfs_openres *o_res = &data->o_res;
1587 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001588 struct rpc_message msg = {
1589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1590 .rpc_argp = o_arg,
1591 .rpc_resp = o_res,
1592 .rpc_cred = data->owner->so_cred,
1593 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001594 struct rpc_task_setup task_setup_data = {
1595 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001596 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001597 .callback_ops = &nfs4_open_ops,
1598 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001599 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001600 .flags = RPC_TASK_ASYNC,
1601 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001602 int status;
1603
Trond Myklebust9d12b212012-01-17 22:04:25 -05001604 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001605 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001606 data->rpc_done = 0;
1607 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001608 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001609 if (isrecover)
1610 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001611 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001612 if (IS_ERR(task))
1613 return PTR_ERR(task);
1614 status = nfs4_wait_for_completion_rpc_task(task);
1615 if (status != 0) {
1616 data->cancelled = 1;
1617 smp_wmb();
1618 } else
1619 status = data->rpc_status;
1620 rpc_put_task(task);
1621
1622 return status;
1623}
1624
1625static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1626{
1627 struct inode *dir = data->dir->d_inode;
1628 struct nfs_openres *o_res = &data->o_res;
1629 int status;
1630
1631 status = nfs4_run_open_task(data, 1);
1632 if (status != 0 || !data->rpc_done)
1633 return status;
1634
Trond Myklebust6926afd2012-01-07 13:22:46 -05001635 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1636
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001637 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1638 status = _nfs4_proc_open_confirm(data);
1639 if (status != 0)
1640 return status;
1641 }
1642
1643 return status;
1644}
1645
1646/*
1647 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1648 */
1649static int _nfs4_proc_open(struct nfs4_opendata *data)
1650{
1651 struct inode *dir = data->dir->d_inode;
1652 struct nfs_server *server = NFS_SERVER(dir);
1653 struct nfs_openargs *o_arg = &data->o_arg;
1654 struct nfs_openres *o_res = &data->o_res;
1655 int status;
1656
1657 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001658 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001660 if (status != 0) {
1661 if (status == -NFS4ERR_BADNAME &&
1662 !(o_arg->open_flags & O_CREAT))
1663 return -ENOENT;
1664 return status;
1665 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666
Trond Myklebust6926afd2012-01-07 13:22:46 -05001667 nfs_fattr_map_and_free_names(server, &data->f_attr);
1668
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001669 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001670 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001671 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1672 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001674 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001679 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Andy Adamsond83217c2011-03-01 01:34:17 +00001683static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001684{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001685 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001686 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001687
Trond Myklebusta78cb572009-08-09 15:06:19 -04001688 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001689 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001690 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001691 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001692 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1693 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001694 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001695 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001696 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001697 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001698 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001699}
1700
Andy Adamsond83217c2011-03-01 01:34:17 +00001701static int nfs4_recover_expired_lease(struct nfs_server *server)
1702{
1703 return nfs4_client_recover_expired_lease(server->nfs_client);
1704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * OPEN_EXPIRED:
1708 * reclaim state on the server after a network partition.
1709 * Assumes caller holds the appropriate lock
1710 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001711static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001713 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001716 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1717 if (IS_ERR(opendata))
1718 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001720 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001721 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001722 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001726static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727{
Trond Myklebust539cd032007-06-05 11:46:42 -04001728 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729 struct nfs4_exception exception = { };
1730 int err;
1731
1732 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001733 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001734 switch (err) {
1735 default:
1736 goto out;
1737 case -NFS4ERR_GRACE:
1738 case -NFS4ERR_DELAY:
1739 nfs4_handle_exception(server, err, &exception);
1740 err = 0;
1741 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001742 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001743out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001744 return err;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001750 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 ctx = nfs4_state_find_open_context(state);
1753 if (IS_ERR(ctx))
1754 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001755 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001756 put_nfs_open_context(ctx);
1757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001761static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001764 nfs4_stateid *stateid = &state->stateid;
1765 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001766
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001767 /* If a state reset has been done, test_stateid is unneeded */
1768 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1769 return;
1770
1771 status = nfs41_test_stateid(server, stateid);
1772 if (status != NFS_OK) {
1773 /* Free the stateid unless the server explicitly
1774 * informs us the stateid is unrecognized. */
1775 if (status != -NFS4ERR_BAD_STATEID)
1776 nfs41_free_stateid(server, stateid);
1777
1778 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1779 }
1780}
1781
1782/**
1783 * nfs41_check_open_stateid - possibly free an open stateid
1784 *
1785 * @state: NFSv4 state for an inode
1786 *
1787 * Returns NFS_OK if recovery for this stateid is now finished.
1788 * Otherwise a negative NFS4ERR value is returned.
1789 */
1790static int nfs41_check_open_stateid(struct nfs4_state *state)
1791{
1792 struct nfs_server *server = NFS_SERVER(state->inode);
1793 nfs4_stateid *stateid = &state->stateid;
1794 int status;
1795
1796 /* If a state reset has been done, test_stateid is unneeded */
1797 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1798 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1799 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1800 return -NFS4ERR_BAD_STATEID;
1801
1802 status = nfs41_test_stateid(server, stateid);
1803 if (status != NFS_OK) {
1804 /* Free the stateid unless the server explicitly
1805 * informs us the stateid is unrecognized. */
1806 if (status != -NFS4ERR_BAD_STATEID)
1807 nfs41_free_stateid(server, stateid);
1808
1809 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1810 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1811 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001812 }
1813 return status;
1814}
1815
1816static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1817{
Chuck Levereb64cf92012-07-11 16:30:05 -04001818 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001819
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001820 nfs41_clear_delegation_stateid(state);
1821 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001822 if (status != NFS_OK)
1823 status = nfs4_open_expired(sp, state);
1824 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001825}
1826#endif
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001829 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1830 * fields corresponding to attributes that were used to store the verifier.
1831 * Make sure we clobber those fields in the later setattr call
1832 */
1833static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1834{
1835 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1836 !(sattr->ia_valid & ATTR_ATIME_SET))
1837 sattr->ia_valid |= ATTR_ATIME;
1838
1839 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1840 !(sattr->ia_valid & ATTR_MTIME_SET))
1841 sattr->ia_valid |= ATTR_MTIME;
1842}
1843
1844/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
Andy Adamson82be4172012-05-23 05:02:35 -04001847static int _nfs4_do_open(struct inode *dir,
1848 struct dentry *dentry,
1849 fmode_t fmode,
1850 int flags,
1851 struct iattr *sattr,
1852 struct rpc_cred *cred,
1853 struct nfs4_state **res,
1854 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 struct nfs4_state_owner *sp;
1857 struct nfs4_state *state = NULL;
1858 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001859 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001860 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001864 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1865 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1867 goto out_err;
1868 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001869 status = nfs4_recover_expired_lease(server);
1870 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001871 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001872 if (dentry->d_inode != NULL)
1873 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001874 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001875 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001876 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001877 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Andy Adamson82be4172012-05-23 05:02:35 -04001879 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1880 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1881 if (!opendata->f_attr.mdsthreshold)
1882 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001883 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001884 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001885 if (dentry->d_inode != NULL)
1886 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001887
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001888 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001890 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001892 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001893 status = PTR_ERR(state);
1894 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001895 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001896 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001897 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001898
1899 if (opendata->o_arg.open_flags & O_EXCL) {
1900 nfs4_exclusive_attrset(opendata, sattr);
1901
1902 nfs_fattr_init(opendata->o_res.f_attr);
1903 status = nfs4_do_setattr(state->inode, cred,
1904 opendata->o_res.f_attr, sattr,
1905 state);
1906 if (status == 0)
1907 nfs_setattr_update_inode(state->inode, sattr);
1908 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1909 }
Andy Adamson82be4172012-05-23 05:02:35 -04001910
1911 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1912 *ctx_th = opendata->f_attr.mdsthreshold;
1913 else
1914 kfree(opendata->f_attr.mdsthreshold);
1915 opendata->f_attr.mdsthreshold = NULL;
1916
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001917 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 *res = state;
1920 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001921err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001922 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001923 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001924err_put_state_owner:
1925 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *res = NULL;
1928 return status;
1929}
1930
1931
Andy Adamson82be4172012-05-23 05:02:35 -04001932static struct nfs4_state *nfs4_do_open(struct inode *dir,
1933 struct dentry *dentry,
1934 fmode_t fmode,
1935 int flags,
1936 struct iattr *sattr,
1937 struct rpc_cred *cred,
1938 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct nfs4_exception exception = { };
1941 struct nfs4_state *res;
1942 int status;
1943
Trond Myklebust2d0dbc62012-06-08 10:58:09 -04001944 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001946 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1947 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (status == 0)
1949 break;
1950 /* NOTE: BAD_SEQID means the server and client disagree about the
1951 * book-keeping w.r.t. state-changing operations
1952 * (OPEN/CLOSE/LOCK/LOCKU...)
1953 * It is actually a sign of a bug on the client or on the server.
1954 *
1955 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001956 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * have unhashed the old state_owner for us, and that we can
1958 * therefore safely retry using a new one. We should still warn
1959 * the user though...
1960 */
1961 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001962 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001963 " returned a bad sequence-id error!\n",
1964 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 exception.retry = 1;
1966 continue;
1967 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001968 /*
1969 * BAD_STATEID on OPEN means that the server cancelled our
1970 * state before it received the OPEN_CONFIRM.
1971 * Recover by retrying the request as per the discussion
1972 * on Page 181 of RFC3530.
1973 */
1974 if (status == -NFS4ERR_BAD_STATEID) {
1975 exception.retry = 1;
1976 continue;
1977 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001978 if (status == -EAGAIN) {
1979 /* We must have found a delegation */
1980 exception.retry = 1;
1981 continue;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1984 status, &exception));
1985 } while (exception.retry);
1986 return res;
1987}
1988
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001989static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1990 struct nfs_fattr *fattr, struct iattr *sattr,
1991 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001993 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001995 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 .iap = sattr,
1997 .server = server,
1998 .bitmask = server->attr_bitmask,
1999 };
2000 struct nfs_setattrres res = {
2001 .fattr = fattr,
2002 .server = server,
2003 };
2004 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2006 .rpc_argp = &arg,
2007 .rpc_resp = &res,
2008 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002010 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002011 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Trond Myklebust0e574af2005-10-27 22:12:38 -04002013 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Trond Myklebust4fc87962012-03-08 17:42:01 -05002015 if (state != NULL) {
2016 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2017 current->files, current->tgid);
2018 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2019 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002020 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002021 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002022 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Bryan Schumaker7c513052011-03-24 17:12:24 +00002024 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002025 if (status == 0 && state != NULL)
2026 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002027 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002030static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2031 struct nfs_fattr *fattr, struct iattr *sattr,
2032 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002034 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002035 struct nfs4_exception exception = {
2036 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002037 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 int err;
2040 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002041 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2042 switch (err) {
2043 case -NFS4ERR_OPENMODE:
2044 if (state && !(state->state & FMODE_WRITE)) {
2045 err = -EBADF;
2046 if (sattr->ia_valid & ATTR_OPEN)
2047 err = -EACCES;
2048 goto out;
2049 }
2050 }
2051 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002053out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return err;
2055}
2056
2057struct nfs4_closedata {
2058 struct inode *inode;
2059 struct nfs4_state *state;
2060 struct nfs_closeargs arg;
2061 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002062 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002063 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002064 bool roc;
2065 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066};
2067
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002068static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002069{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002070 struct nfs4_closedata *calldata = data;
2071 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002072 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002073
Fred Isamanf7e89172011-01-06 11:36:32 +00002074 if (calldata->roc)
2075 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002076 nfs4_put_open_state(calldata->state);
2077 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002078 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002079 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002080 kfree(calldata);
2081}
2082
Trond Myklebust88069f72009-12-08 08:33:16 -05002083static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2084 fmode_t fmode)
2085{
2086 spin_lock(&state->owner->so_lock);
2087 if (!(fmode & FMODE_READ))
2088 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2089 if (!(fmode & FMODE_WRITE))
2090 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2091 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2092 spin_unlock(&state->owner->so_lock);
2093}
2094
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002095static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002097 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 struct nfs_server *server = NFS_SERVER(calldata->inode);
2100
Chuck Levera3ca5652012-03-01 17:00:40 -05002101 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002102 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2103 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /* hmm. we are done with the inode, and in the process of freeing
2105 * the state_owner. we keep this around to process errors
2106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 switch (task->tk_status) {
2108 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002109 if (calldata->roc)
2110 pnfs_roc_set_barrier(state->inode,
2111 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002112 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002113 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002114 nfs4_close_clear_stateid_flags(state,
2115 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 break;
2117 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002118 case -NFS4ERR_OLD_STATEID:
2119 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002121 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002122 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002124 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2125 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002127 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002128 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002129 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002132static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002134 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002135 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002136 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002137
Chuck Levera3ca5652012-03-01 17:00:40 -05002138 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002139 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002140 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002141
Trond Myklebust88069f72009-12-08 08:33:16 -05002142 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2143 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002144 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002145 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002146 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002147 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002148 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2149 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2150 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002151 }
2152 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002153 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2154 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2155 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002156 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002157 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002158 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002159
2160 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002161 /* Note: exit _without_ calling nfs4_close_done */
2162 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002163 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002164 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002165
Fred Isamanf7e89172011-01-06 11:36:32 +00002166 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002167 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002168 if (calldata->roc &&
2169 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2170 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2171 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002172 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002173 }
2174 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002175
Trond Myklebust516a6af2005-10-27 22:12:41 -04002176 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002177 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002178 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002179 &calldata->arg.seq_args,
2180 &calldata->res.seq_res,
2181 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002182 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002183 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002184out:
2185 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002188static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002189 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190 .rpc_call_done = nfs4_close_done,
2191 .rpc_release = nfs4_free_closedata,
2192};
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194/*
2195 * It is possible for data to be read/written from a mem-mapped file
2196 * after the sys_close call (which hits the vfs layer as a flush).
2197 * This means that we can't safely call nfsv4 close on a file until
2198 * the inode is cleared. This in turn means that we are not good
2199 * NFSv4 citizens - we do not indicate to the server to update the file's
2200 * share state even when we are done with one of the three share
2201 * stateid's in the inode.
2202 *
2203 * NOTE: Caller must be holding the sp->so_owner semaphore!
2204 */
Al Viro643168c2011-06-22 18:20:23 -04002205int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002207 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002209 struct nfs4_state_owner *sp = state->owner;
2210 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002211 struct rpc_message msg = {
2212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2213 .rpc_cred = state->owner->so_cred,
2214 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002215 struct rpc_task_setup task_setup_data = {
2216 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002217 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002218 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002219 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 .flags = RPC_TASK_ASYNC,
2221 };
Trond Myklebust95121352005-10-18 14:20:12 -07002222 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002224 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002226 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002227 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002228 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002230 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002231 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002233 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002234 if (calldata->arg.seqid == NULL)
2235 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002236 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002237 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002238 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002239 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002240 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002241 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002242 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002243
Trond Myklebust1174dd12010-12-21 10:52:24 -05002244 msg.rpc_argp = &calldata->arg;
2245 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002246 task_setup_data.callback_data = calldata;
2247 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002248 if (IS_ERR(task))
2249 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002250 status = 0;
2251 if (wait)
2252 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002253 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002254 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002255out_free_calldata:
2256 kfree(calldata);
2257out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002258 if (roc)
2259 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002260 nfs4_put_open_state(state);
2261 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002262 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Trond Myklebust2b484292010-09-17 10:56:51 -04002265static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002266nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 struct nfs4_state *state;
2269
Trond Myklebust565277f2007-10-15 18:17:53 -04002270 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002271 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2272 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002273 if (IS_ERR(state))
2274 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002275 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002276 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Trond Myklebust1185a552009-12-03 15:54:02 -05002279static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002280{
2281 if (ctx->state == NULL)
2282 return;
2283 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002284 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002285 else
Al Viro643168c2011-06-22 18:20:23 -04002286 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2290{
Benny Halevy43652ad2009-04-01 09:21:54 -04002291 struct nfs4_server_caps_arg args = {
2292 .fhandle = fhandle,
2293 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 struct nfs4_server_caps_res res = {};
2295 struct rpc_message msg = {
2296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002297 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 .rpc_resp = &res,
2299 };
2300 int status;
2301
Bryan Schumaker7c513052011-03-24 17:12:24 +00002302 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (status == 0) {
2304 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002305 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2306 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2307 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2308 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2309 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2311 server->caps |= NFS_CAP_ACLS;
2312 if (res.has_links != 0)
2313 server->caps |= NFS_CAP_HARDLINKS;
2314 if (res.has_symlinks != 0)
2315 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002316 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2317 server->caps |= NFS_CAP_FILEID;
2318 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2319 server->caps |= NFS_CAP_MODE;
2320 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2321 server->caps |= NFS_CAP_NLINK;
2322 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2323 server->caps |= NFS_CAP_OWNER;
2324 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2325 server->caps |= NFS_CAP_OWNER_GROUP;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2327 server->caps |= NFS_CAP_ATIME;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2329 server->caps |= NFS_CAP_CTIME;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2331 server->caps |= NFS_CAP_MTIME;
2332
Trond Myklebusta65318b2009-03-11 14:10:28 -04002333 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2334 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2335 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002337 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return status;
2341}
2342
Trond Myklebust55a97592006-06-09 09:34:19 -04002343int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 struct nfs4_exception exception = { };
2346 int err;
2347 do {
2348 err = nfs4_handle_exception(server,
2349 _nfs4_server_capabilities(server, fhandle),
2350 &exception);
2351 } while (exception.retry);
2352 return err;
2353}
2354
2355static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2356 struct nfs_fsinfo *info)
2357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 struct nfs4_lookup_root_arg args = {
2359 .bitmask = nfs4_fattr_bitmap,
2360 };
2361 struct nfs4_lookup_res res = {
2362 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002363 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 .fh = fhandle,
2365 };
2366 struct rpc_message msg = {
2367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2368 .rpc_argp = &args,
2369 .rpc_resp = &res,
2370 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002371
Trond Myklebust0e574af2005-10-27 22:12:38 -04002372 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002373 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
2376static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2377 struct nfs_fsinfo *info)
2378{
2379 struct nfs4_exception exception = { };
2380 int err;
2381 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002382 err = _nfs4_lookup_root(server, fhandle, info);
2383 switch (err) {
2384 case 0:
2385 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002386 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002387 default:
2388 err = nfs4_handle_exception(server, err, &exception);
2389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002391out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return err;
2393}
2394
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002395static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2396 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2397{
2398 struct rpc_auth *auth;
2399 int ret;
2400
2401 auth = rpcauth_create(flavor, server->client);
2402 if (!auth) {
2403 ret = -EIO;
2404 goto out;
2405 }
2406 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002407out:
2408 return ret;
2409}
2410
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002411static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002412 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002414 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002415 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002417 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2418 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002419
2420 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002421 /* AUTH_UNIX is the default flavor if none was specified,
2422 * thus has already been tried. */
2423 if (flav_array[i] == RPC_AUTH_UNIX)
2424 continue;
2425
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002426 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002427 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002428 continue;
2429 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002430 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002431 /*
2432 * -EACCESS could mean that the user doesn't have correct permissions
2433 * to access the mount. It could also mean that we tried to mount
2434 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2435 * existing mount programs don't handle -EACCES very well so it should
2436 * be mapped to -EPERM instead.
2437 */
2438 if (status == -EACCES)
2439 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002440 return status;
2441}
2442
2443/*
2444 * get the file handle for the "/" directory on the server
2445 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002446int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2447 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002448{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002449 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002450 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002451 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2452 /*
2453 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2454 * by nfs4_map_errors() as this function exits.
2455 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002456 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (status == 0)
2458 status = nfs4_server_capabilities(server, fhandle);
2459 if (status == 0)
2460 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002461 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Bryan Schumakerbae36242012-05-10 15:07:31 -04002464static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2465 struct nfs_fsinfo *info)
2466{
2467 int error;
2468 struct nfs_fattr *fattr = info->fattr;
2469
2470 error = nfs4_server_capabilities(server, mntfh);
2471 if (error < 0) {
2472 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2473 return error;
2474 }
2475
2476 error = nfs4_proc_getattr(server, mntfh, fattr);
2477 if (error < 0) {
2478 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2479 return error;
2480 }
2481
2482 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2483 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2484 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2485
2486 return error;
2487}
2488
Manoj Naik6b97fd32006-06-09 09:34:29 -04002489/*
2490 * Get locations and (maybe) other attributes of a referral.
2491 * Note that we'll actually follow the referral later when
2492 * we detect fsid mismatch in inode revalidation
2493 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002494static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2495 const struct qstr *name, struct nfs_fattr *fattr,
2496 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002497{
2498 int status = -ENOMEM;
2499 struct page *page = NULL;
2500 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002501
2502 page = alloc_page(GFP_KERNEL);
2503 if (page == NULL)
2504 goto out;
2505 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2506 if (locations == NULL)
2507 goto out;
2508
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002509 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002510 if (status != 0)
2511 goto out;
2512 /* Make sure server returned a different fsid for the referral */
2513 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002514 dprintk("%s: server did not return a different fsid for"
2515 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002516 status = -EIO;
2517 goto out;
2518 }
Andy Adamson533eb462011-06-13 18:25:56 -04002519 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2520 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002521
Andy Adamson533eb462011-06-13 18:25:56 -04002522 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002523 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002524 memset(fhandle, 0, sizeof(struct nfs_fh));
2525out:
2526 if (page)
2527 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002528 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002529 return status;
2530}
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2533{
2534 struct nfs4_getattr_arg args = {
2535 .fh = fhandle,
2536 .bitmask = server->attr_bitmask,
2537 };
2538 struct nfs4_getattr_res res = {
2539 .fattr = fattr,
2540 .server = server,
2541 };
2542 struct rpc_message msg = {
2543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2544 .rpc_argp = &args,
2545 .rpc_resp = &res,
2546 };
2547
Trond Myklebust0e574af2005-10-27 22:12:38 -04002548 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002549 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2553{
2554 struct nfs4_exception exception = { };
2555 int err;
2556 do {
2557 err = nfs4_handle_exception(server,
2558 _nfs4_proc_getattr(server, fhandle, fattr),
2559 &exception);
2560 } while (exception.retry);
2561 return err;
2562}
2563
2564/*
2565 * The file is not closed if it is opened due to the a request to change
2566 * the size of the file. The open call will not be needed once the
2567 * VFS layer lookup-intents are implemented.
2568 *
2569 * Close is called when the inode is destroyed.
2570 * If we haven't opened the file for O_WRONLY, we
2571 * need to in the size_change case to obtain a stateid.
2572 *
2573 * Got race?
2574 * Because OPEN is always done by name in nfsv4, it is
2575 * possible that we opened a different file by the same
2576 * name. We can recognize this race condition, but we
2577 * can't do anything about it besides returning an error.
2578 *
2579 * This will be fixed with VFS changes (lookup-intent).
2580 */
2581static int
2582nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2583 struct iattr *sattr)
2584{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002585 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002586 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002587 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 int status;
2589
Benny Halevy8a1636c2010-07-14 15:43:57 -04002590 if (pnfs_ld_layoutret_on_setattr(inode))
2591 pnfs_return_layout(inode);
2592
Trond Myklebust0e574af2005-10-27 22:12:38 -04002593 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Andy Adamson26699402012-05-30 16:12:24 -04002595 /* Deal with open(O_TRUNC) */
2596 if (sattr->ia_valid & ATTR_OPEN)
2597 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2598
2599 /* Optimization: if the end result is no change, don't RPC */
2600 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2601 return 0;
2602
Trond Myklebustd5308382005-11-04 15:33:38 -05002603 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002604 if (sattr->ia_valid & ATTR_FILE) {
2605 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002606
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002607 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002608 if (ctx) {
2609 cred = ctx->cred;
2610 state = ctx->state;
2611 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002612 }
2613
2614 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002615 if (status == 0)
2616 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return status;
2618}
2619
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002620static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2621 const struct qstr *name, struct nfs_fh *fhandle,
2622 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002623{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002624 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002625 int status;
2626 struct nfs4_lookup_arg args = {
2627 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002628 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002629 .name = name,
2630 };
2631 struct nfs4_lookup_res res = {
2632 .server = server,
2633 .fattr = fattr,
2634 .fh = fhandle,
2635 };
2636 struct rpc_message msg = {
2637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2638 .rpc_argp = &args,
2639 .rpc_resp = &res,
2640 };
2641
2642 nfs_fattr_init(fattr);
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002645 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 dprintk("NFS reply lookup: %d\n", status);
2647 return status;
2648}
2649
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002650static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002651{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002652 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002653 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002654 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2655 fattr->nlink = 2;
2656}
2657
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002658static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2659 struct qstr *name, struct nfs_fh *fhandle,
2660 struct nfs_fattr *fattr)
2661{
2662 struct nfs4_exception exception = { };
2663 struct rpc_clnt *client = *clnt;
2664 int err;
2665 do {
2666 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2667 switch (err) {
2668 case -NFS4ERR_BADNAME:
2669 err = -ENOENT;
2670 goto out;
2671 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002672 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002673 goto out;
2674 case -NFS4ERR_WRONGSEC:
2675 err = -EPERM;
2676 if (client != *clnt)
2677 goto out;
2678
2679 client = nfs4_create_sec_client(client, dir, name);
2680 if (IS_ERR(client))
2681 return PTR_ERR(client);
2682
2683 exception.retry = 1;
2684 break;
2685 default:
2686 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2687 }
2688 } while (exception.retry);
2689
2690out:
2691 if (err == 0)
2692 *clnt = client;
2693 else if (client != *clnt)
2694 rpc_shutdown_client(client);
2695
2696 return err;
2697}
2698
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002699static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002700 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002702 int status;
2703 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002704
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002705 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2706 if (client != NFS_CLIENT(dir)) {
2707 rpc_shutdown_client(client);
2708 nfs_fixup_secinfo_attributes(fattr);
2709 }
2710 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002713struct rpc_clnt *
2714nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2715 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2716{
2717 int status;
2718 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2719
2720 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2721 if (status < 0) {
2722 rpc_shutdown_client(client);
2723 return ERR_PTR(status);
2724 }
2725 return client;
2726}
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2729{
Trond Myklebust76b32992007-08-10 17:45:11 -04002730 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct nfs4_accessargs args = {
2732 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002733 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002735 struct nfs4_accessres res = {
2736 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002737 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 struct rpc_message msg = {
2739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2740 .rpc_argp = &args,
2741 .rpc_resp = &res,
2742 .rpc_cred = entry->cred,
2743 };
2744 int mode = entry->mask;
2745 int status;
2746
2747 /*
2748 * Determine which access bits we want to ask for...
2749 */
2750 if (mode & MAY_READ)
2751 args.access |= NFS4_ACCESS_READ;
2752 if (S_ISDIR(inode->i_mode)) {
2753 if (mode & MAY_WRITE)
2754 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2755 if (mode & MAY_EXEC)
2756 args.access |= NFS4_ACCESS_LOOKUP;
2757 } else {
2758 if (mode & MAY_WRITE)
2759 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2760 if (mode & MAY_EXEC)
2761 args.access |= NFS4_ACCESS_EXECUTE;
2762 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002763
2764 res.fattr = nfs_alloc_fattr();
2765 if (res.fattr == NULL)
2766 return -ENOMEM;
2767
Bryan Schumaker7c513052011-03-24 17:12:24 +00002768 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (!status) {
2770 entry->mask = 0;
2771 if (res.access & NFS4_ACCESS_READ)
2772 entry->mask |= MAY_READ;
2773 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2774 entry->mask |= MAY_WRITE;
2775 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2776 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002777 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002779 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return status;
2781}
2782
2783static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2784{
2785 struct nfs4_exception exception = { };
2786 int err;
2787 do {
2788 err = nfs4_handle_exception(NFS_SERVER(inode),
2789 _nfs4_proc_access(inode, entry),
2790 &exception);
2791 } while (exception.retry);
2792 return err;
2793}
2794
2795/*
2796 * TODO: For the time being, we don't try to get any attributes
2797 * along with any of the zero-copy operations READ, READDIR,
2798 * READLINK, WRITE.
2799 *
2800 * In the case of the first three, we want to put the GETATTR
2801 * after the read-type operation -- this is because it is hard
2802 * to predict the length of a GETATTR response in v4, and thus
2803 * align the READ data correctly. This means that the GETATTR
2804 * may end up partially falling into the page cache, and we should
2805 * shift it into the 'tail' of the xdr_buf before processing.
2806 * To do this efficiently, we need to know the total length
2807 * of data received, which doesn't seem to be available outside
2808 * of the RPC layer.
2809 *
2810 * In the case of WRITE, we also want to put the GETATTR after
2811 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002812 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 *
2814 * Both of these changes to the XDR layer would in fact be quite
2815 * minor, but I decided to leave them for a subsequent patch.
2816 */
2817static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2818 unsigned int pgbase, unsigned int pglen)
2819{
2820 struct nfs4_readlink args = {
2821 .fh = NFS_FH(inode),
2822 .pgbase = pgbase,
2823 .pglen = pglen,
2824 .pages = &page,
2825 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002826 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 struct rpc_message msg = {
2828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2829 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002830 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 };
2832
Bryan Schumaker7c513052011-03-24 17:12:24 +00002833 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2837 unsigned int pgbase, unsigned int pglen)
2838{
2839 struct nfs4_exception exception = { };
2840 int err;
2841 do {
2842 err = nfs4_handle_exception(NFS_SERVER(inode),
2843 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2844 &exception);
2845 } while (exception.retry);
2846 return err;
2847}
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002850 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852static int
2853nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002854 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 int status = 0;
2859
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002860 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2861 if (IS_ERR(ctx))
2862 return PTR_ERR(ctx);
2863
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002864 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002865 state = nfs4_do_open(dir, dentry, ctx->mode,
2866 flags, sattr, ctx->cred,
2867 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002868 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (IS_ERR(state)) {
2870 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002873 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002874 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002875 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002877 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return status;
2879}
2880
2881static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2882{
Trond Myklebust16e42952005-10-27 22:12:44 -04002883 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002884 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002886 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002888 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002889 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002890 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2893 .rpc_argp = &args,
2894 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002896 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002897
Bryan Schumaker7c513052011-03-24 17:12:24 +00002898 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002899 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002900 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902}
2903
2904static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(NFS_SERVER(dir),
2910 _nfs4_proc_remove(dir, name),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002916static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002918 struct nfs_server *server = NFS_SERVER(dir);
2919 struct nfs_removeargs *args = msg->rpc_argp;
2920 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002922 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002924 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002927static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2928{
2929 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2930 &data->args.seq_args,
2931 &data->res.seq_res,
2932 task))
2933 return;
2934 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002937static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002939 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2940
Trond Myklebust14516c32010-07-31 14:29:06 -04002941 if (!nfs4_sequence_done(task, &res->seq_res))
2942 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002943 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002944 return 0;
2945 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002946 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
Jeff Laytond3d41522010-09-17 17:31:57 -04002949static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2950{
2951 struct nfs_server *server = NFS_SERVER(dir);
2952 struct nfs_renameargs *arg = msg->rpc_argp;
2953 struct nfs_renameres *res = msg->rpc_resp;
2954
2955 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002956 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002957 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002958}
2959
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002960static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2961{
2962 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2963 &data->args.seq_args,
2964 &data->res.seq_res,
2965 task))
2966 return;
2967 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002968}
2969
2970static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2971 struct inode *new_dir)
2972{
2973 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2974
2975 if (!nfs4_sequence_done(task, &res->seq_res))
2976 return 0;
2977 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2978 return 0;
2979
2980 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002981 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002982 return 1;
2983}
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2986 struct inode *new_dir, struct qstr *new_name)
2987{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002988 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002989 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 .old_dir = NFS_FH(old_dir),
2991 .new_dir = NFS_FH(new_dir),
2992 .old_name = old_name,
2993 .new_name = new_name,
2994 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002995 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002996 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002997 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct rpc_message msg = {
2999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3000 .rpc_argp = &arg,
3001 .rpc_resp = &res,
3002 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003003 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Bryan Schumaker7c513052011-03-24 17:12:24 +00003005 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (!status) {
3007 update_changeattr(old_dir, &res.old_cinfo);
3008 update_changeattr(new_dir, &res.new_cinfo);
3009 }
3010 return status;
3011}
3012
3013static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3014 struct inode *new_dir, struct qstr *new_name)
3015{
3016 struct nfs4_exception exception = { };
3017 int err;
3018 do {
3019 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3020 _nfs4_proc_rename(old_dir, old_name,
3021 new_dir, new_name),
3022 &exception);
3023 } while (exception.retry);
3024 return err;
3025}
3026
3027static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3028{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003029 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct nfs4_link_arg arg = {
3031 .fh = NFS_FH(inode),
3032 .dir_fh = NFS_FH(dir),
3033 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003034 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003036 struct nfs4_link_res res = {
3037 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 struct rpc_message msg = {
3040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3041 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003042 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003044 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Trond Myklebust136f2622010-04-16 16:22:49 -04003046 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003047 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003048 goto out;
3049
Bryan Schumaker7c513052011-03-24 17:12:24 +00003050 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003051 if (!status) {
3052 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003053 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003054 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003055out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003056 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return status;
3058}
3059
3060static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3061{
3062 struct nfs4_exception exception = { };
3063 int err;
3064 do {
3065 err = nfs4_handle_exception(NFS_SERVER(inode),
3066 _nfs4_proc_link(inode, dir, name),
3067 &exception);
3068 } while (exception.retry);
3069 return err;
3070}
3071
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072struct nfs4_createdata {
3073 struct rpc_message msg;
3074 struct nfs4_create_arg arg;
3075 struct nfs4_create_res res;
3076 struct nfs_fh fh;
3077 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003078};
3079
3080static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3081 struct qstr *name, struct iattr *sattr, u32 ftype)
3082{
3083 struct nfs4_createdata *data;
3084
3085 data = kzalloc(sizeof(*data), GFP_KERNEL);
3086 if (data != NULL) {
3087 struct nfs_server *server = NFS_SERVER(dir);
3088
3089 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3090 data->msg.rpc_argp = &data->arg;
3091 data->msg.rpc_resp = &data->res;
3092 data->arg.dir_fh = NFS_FH(dir);
3093 data->arg.server = server;
3094 data->arg.name = name;
3095 data->arg.attrs = sattr;
3096 data->arg.ftype = ftype;
3097 data->arg.bitmask = server->attr_bitmask;
3098 data->res.server = server;
3099 data->res.fh = &data->fh;
3100 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003102 }
3103 return data;
3104}
3105
3106static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3107{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003108 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003109 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110 if (status == 0) {
3111 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3113 }
3114 return status;
3115}
3116
3117static void nfs4_free_createdata(struct nfs4_createdata *data)
3118{
3119 kfree(data);
3120}
3121
Chuck Lever4f390c12006-08-22 20:06:22 -04003122static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003123 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003125 struct nfs4_createdata *data;
3126 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Chuck Lever94a6d752006-08-22 20:06:23 -04003128 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003129 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003130
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 status = -ENOMEM;
3132 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3133 if (data == NULL)
3134 goto out;
3135
3136 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3137 data->arg.u.symlink.pages = &page;
3138 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003140 status = nfs4_do_create(dir, dentry, data);
3141
3142 nfs4_free_createdata(data);
3143out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return status;
3145}
3146
Chuck Lever4f390c12006-08-22 20:06:22 -04003147static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003148 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 struct nfs4_exception exception = { };
3151 int err;
3152 do {
3153 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003154 _nfs4_proc_symlink(dir, dentry, page,
3155 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 &exception);
3157 } while (exception.retry);
3158 return err;
3159}
3160
3161static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3162 struct iattr *sattr)
3163{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003164 struct nfs4_createdata *data;
3165 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003167 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3168 if (data == NULL)
3169 goto out;
3170
3171 status = nfs4_do_create(dir, dentry, data);
3172
3173 nfs4_free_createdata(data);
3174out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return status;
3176}
3177
3178static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3179 struct iattr *sattr)
3180{
3181 struct nfs4_exception exception = { };
3182 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003183
3184 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 do {
3186 err = nfs4_handle_exception(NFS_SERVER(dir),
3187 _nfs4_proc_mkdir(dir, dentry, sattr),
3188 &exception);
3189 } while (exception.retry);
3190 return err;
3191}
3192
3193static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003194 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
3196 struct inode *dir = dentry->d_inode;
3197 struct nfs4_readdir_arg args = {
3198 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003199 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 .pgbase = 0,
3201 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003202 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003203 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 };
3205 struct nfs4_readdir_res res;
3206 struct rpc_message msg = {
3207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3208 .rpc_argp = &args,
3209 .rpc_resp = &res,
3210 .rpc_cred = cred,
3211 };
3212 int status;
3213
Harvey Harrison3110ff82008-05-02 13:42:44 -07003214 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003215 dentry->d_parent->d_name.name,
3216 dentry->d_name.name,
3217 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3219 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003220 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003221 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003223 status += args.pgbase;
3224 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003225
3226 nfs_invalidate_atime(dir);
3227
Harvey Harrison3110ff82008-05-02 13:42:44 -07003228 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return status;
3230}
3231
3232static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003233 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 struct nfs4_exception exception = { };
3236 int err;
3237 do {
3238 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3239 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003240 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 &exception);
3242 } while (exception.retry);
3243 return err;
3244}
3245
3246static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3247 struct iattr *sattr, dev_t rdev)
3248{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003249 struct nfs4_createdata *data;
3250 int mode = sattr->ia_mode;
3251 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3254 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003255
3256 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3257 if (data == NULL)
3258 goto out;
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003261 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003263 data->arg.ftype = NF4BLK;
3264 data->arg.u.device.specdata1 = MAJOR(rdev);
3265 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
3267 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003268 data->arg.ftype = NF4CHR;
3269 data->arg.u.device.specdata1 = MAJOR(rdev);
3270 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003273 status = nfs4_do_create(dir, dentry, data);
3274
3275 nfs4_free_createdata(data);
3276out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return status;
3278}
3279
3280static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3281 struct iattr *sattr, dev_t rdev)
3282{
3283 struct nfs4_exception exception = { };
3284 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003285
3286 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 do {
3288 err = nfs4_handle_exception(NFS_SERVER(dir),
3289 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3290 &exception);
3291 } while (exception.retry);
3292 return err;
3293}
3294
3295static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3296 struct nfs_fsstat *fsstat)
3297{
3298 struct nfs4_statfs_arg args = {
3299 .fh = fhandle,
3300 .bitmask = server->attr_bitmask,
3301 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003302 struct nfs4_statfs_res res = {
3303 .fsstat = fsstat,
3304 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 struct rpc_message msg = {
3306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3307 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003308 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 };
3310
Trond Myklebust0e574af2005-10-27 22:12:38 -04003311 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003312 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
3315static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3316{
3317 struct nfs4_exception exception = { };
3318 int err;
3319 do {
3320 err = nfs4_handle_exception(server,
3321 _nfs4_proc_statfs(server, fhandle, fsstat),
3322 &exception);
3323 } while (exception.retry);
3324 return err;
3325}
3326
3327static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3328 struct nfs_fsinfo *fsinfo)
3329{
3330 struct nfs4_fsinfo_arg args = {
3331 .fh = fhandle,
3332 .bitmask = server->attr_bitmask,
3333 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003334 struct nfs4_fsinfo_res res = {
3335 .fsinfo = fsinfo,
3336 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct rpc_message msg = {
3338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3339 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003340 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 };
3342
Bryan Schumaker7c513052011-03-24 17:12:24 +00003343 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
3346static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3347{
3348 struct nfs4_exception exception = { };
3349 int err;
3350
3351 do {
3352 err = nfs4_handle_exception(server,
3353 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3354 &exception);
3355 } while (exception.retry);
3356 return err;
3357}
3358
3359static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3360{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003361 int error;
3362
Trond Myklebust0e574af2005-10-27 22:12:38 -04003363 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003364 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3365 if (error == 0)
3366 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3367
3368 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
3371static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3372 struct nfs_pathconf *pathconf)
3373{
3374 struct nfs4_pathconf_arg args = {
3375 .fh = fhandle,
3376 .bitmask = server->attr_bitmask,
3377 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003378 struct nfs4_pathconf_res res = {
3379 .pathconf = pathconf,
3380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 struct rpc_message msg = {
3382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3383 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003384 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 };
3386
3387 /* None of the pathconf attributes are mandatory to implement */
3388 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3389 memset(pathconf, 0, sizeof(*pathconf));
3390 return 0;
3391 }
3392
Trond Myklebust0e574af2005-10-27 22:12:38 -04003393 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003394 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3398 struct nfs_pathconf *pathconf)
3399{
3400 struct nfs4_exception exception = { };
3401 int err;
3402
3403 do {
3404 err = nfs4_handle_exception(server,
3405 _nfs4_proc_pathconf(server, fhandle, pathconf),
3406 &exception);
3407 } while (exception.retry);
3408 return err;
3409}
3410
Benny Halevyd20581a2011-05-22 19:52:03 +03003411void __nfs4_read_done_cb(struct nfs_read_data *data)
3412{
Fred Isamancd841602012-04-20 14:47:44 -04003413 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003414}
3415
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003416static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
Fred Isamancd841602012-04-20 14:47:44 -04003418 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003420 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003421 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003422 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003424
Benny Halevyd20581a2011-05-22 19:52:03 +03003425 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003427 renew_lease(server, data->timestamp);
3428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003431static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3432{
3433
3434 dprintk("--> %s\n", __func__);
3435
3436 if (!nfs4_sequence_done(task, &data->res.seq_res))
3437 return -EAGAIN;
3438
Benny Halevyd20581a2011-05-22 19:52:03 +03003439 return data->read_done_cb ? data->read_done_cb(task, data) :
3440 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003441}
3442
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003443static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003446 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003447 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003448 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003451static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3452{
Fred Isamancd841602012-04-20 14:47:44 -04003453 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003454 &data->args.seq_args,
3455 &data->res.seq_res,
3456 task))
3457 return;
3458 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
Fred Isamanb029bc92011-03-03 15:13:42 +00003461static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Fred Isamancd841602012-04-20 14:47:44 -04003463 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003465 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003466 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003467 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003469 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003471 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003472 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Fred Isamanb029bc92011-03-03 15:13:42 +00003476static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3477{
3478 if (!nfs4_sequence_done(task, &data->res.seq_res))
3479 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003480 return data->write_done_cb ? data->write_done_cb(task, data) :
3481 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003482}
3483
Trond Myklebust5a37f852012-04-28 14:55:16 -04003484static
3485bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003486{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003487 const struct nfs_pgio_header *hdr = data->header;
3488
3489 /* Don't request attributes for pNFS or O_DIRECT writes */
3490 if (data->ds_clp != NULL || hdr->dreq != NULL)
3491 return false;
3492 /* Otherwise, request attributes if and only if we don't hold
3493 * a delegation
3494 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003495 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003496}
Fred Isamana69aef12011-03-03 15:13:47 +00003497
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003498static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Fred Isamancd841602012-04-20 14:47:44 -04003500 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003501
Trond Myklebust5a37f852012-04-28 14:55:16 -04003502 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003503 data->args.bitmask = NULL;
3504 data->res.fattr = NULL;
3505 } else
3506 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003507
Fred Isamanb029bc92011-03-03 15:13:42 +00003508 if (!data->write_done_cb)
3509 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003510 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 data->timestamp = jiffies;
3512
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003514 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003517static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3518{
Fred Isamancd841602012-04-20 14:47:44 -04003519 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003520 &data->args.seq_args,
3521 &data->res.seq_res,
3522 task))
3523 return;
3524 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
Fred Isaman0b7c0152012-04-20 14:47:39 -04003527static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3530 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003531 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return;
3534 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003535}
3536
Fred Isaman0b7c0152012-04-20 14:47:39 -04003537static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
3539 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003540
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003541 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003542 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003543 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Fred Isaman0b7c0152012-04-20 14:47:39 -04003548static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003549{
3550 if (!nfs4_sequence_done(task, &data->res.seq_res))
3551 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003552 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003553}
3554
Fred Isaman0b7c0152012-04-20 14:47:39 -04003555static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003557 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003558
Fred Isaman0b7c0152012-04-20 14:47:39 -04003559 if (data->commit_done_cb == NULL)
3560 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003561 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003562 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003563 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003566struct nfs4_renewdata {
3567 struct nfs_client *client;
3568 unsigned long timestamp;
3569};
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571/*
3572 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3573 * standalone procedure for queueing an asynchronous RENEW.
3574 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003575static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003576{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003577 struct nfs4_renewdata *data = calldata;
3578 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003579
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003580 if (atomic_read(&clp->cl_count) > 1)
3581 nfs4_schedule_state_renewal(clp);
3582 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003583 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003584}
3585
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003586static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003588 struct nfs4_renewdata *data = calldata;
3589 struct nfs_client *clp = data->client;
3590 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003593 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003594 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3595 return;
3596 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003597 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003598 return;
3599 }
3600 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003602 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003605static const struct rpc_call_ops nfs4_renew_ops = {
3606 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003607 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003608};
3609
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003610static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3614 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003615 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003617 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003619 if (renew_flags == 0)
3620 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003621 if (!atomic_inc_not_zero(&clp->cl_count))
3622 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003623 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003624 if (data == NULL)
3625 return -ENOMEM;
3626 data->client = clp;
3627 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003629 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003632static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
3634 struct rpc_message msg = {
3635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3636 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003637 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 };
3639 unsigned long now = jiffies;
3640 int status;
3641
3642 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3643 if (status < 0)
3644 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003645 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return 0;
3647}
3648
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003649static inline int nfs4_server_supports_acls(struct nfs_server *server)
3650{
3651 return (server->caps & NFS_CAP_ACLS)
3652 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3653 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3654}
3655
3656/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3657 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3658 * the stack.
3659 */
3660#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3661
Neil Hormane9e3d722011-03-04 19:26:03 -05003662static int buf_to_pages_noslab(const void *buf, size_t buflen,
3663 struct page **pages, unsigned int *pgbase)
3664{
3665 struct page *newpage, **spages;
3666 int rc = 0;
3667 size_t len;
3668 spages = pages;
3669
3670 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003671 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003672 newpage = alloc_page(GFP_KERNEL);
3673
3674 if (newpage == NULL)
3675 goto unwind;
3676 memcpy(page_address(newpage), buf, len);
3677 buf += len;
3678 buflen -= len;
3679 *pages++ = newpage;
3680 rc++;
3681 } while (buflen != 0);
3682
3683 return rc;
3684
3685unwind:
3686 for(; rc > 0; rc--)
3687 __free_page(spages[rc-1]);
3688 return -ENOMEM;
3689}
3690
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003691struct nfs4_cached_acl {
3692 int cached;
3693 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003694 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003695};
3696
3697static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003698{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003699 struct nfs_inode *nfsi = NFS_I(inode);
3700
3701 spin_lock(&inode->i_lock);
3702 kfree(nfsi->nfs4_acl);
3703 nfsi->nfs4_acl = acl;
3704 spin_unlock(&inode->i_lock);
3705}
3706
3707static void nfs4_zap_acl_attr(struct inode *inode)
3708{
3709 nfs4_set_cached_acl(inode, NULL);
3710}
3711
3712static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3713{
3714 struct nfs_inode *nfsi = NFS_I(inode);
3715 struct nfs4_cached_acl *acl;
3716 int ret = -ENOENT;
3717
3718 spin_lock(&inode->i_lock);
3719 acl = nfsi->nfs4_acl;
3720 if (acl == NULL)
3721 goto out;
3722 if (buf == NULL) /* user is just asking for length */
3723 goto out_len;
3724 if (acl->cached == 0)
3725 goto out;
3726 ret = -ERANGE; /* see getxattr(2) man page */
3727 if (acl->len > buflen)
3728 goto out;
3729 memcpy(buf, acl->data, acl->len);
3730out_len:
3731 ret = acl->len;
3732out:
3733 spin_unlock(&inode->i_lock);
3734 return ret;
3735}
3736
Sachin Prabhu5794d212012-04-17 14:36:40 +01003737static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003738{
3739 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003740 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003741
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003742 if (pages && buflen <= PAGE_SIZE) {
3743 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744 if (acl == NULL)
3745 goto out;
3746 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003747 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003748 } else {
3749 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3750 if (acl == NULL)
3751 goto out;
3752 acl->cached = 0;
3753 }
3754 acl->len = acl_len;
3755out:
3756 nfs4_set_cached_acl(inode, acl);
3757}
3758
Andy Adamsonbf118a32011-12-07 11:55:27 -05003759/*
3760 * The getxattr API returns the required buffer length when called with a
3761 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3762 * the required buf. On a NULL buf, we send a page of data to the server
3763 * guessing that the ACL request can be serviced by a page. If so, we cache
3764 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3765 * the cache. If not so, we throw away the page, and cache the required
3766 * length. The next getxattr call will then produce another round trip to
3767 * the server, this time with the input buf of the required size.
3768 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003769static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003771 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003772 struct nfs_getaclargs args = {
3773 .fh = NFS_FH(inode),
3774 .acl_pages = pages,
3775 .acl_len = buflen,
3776 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003777 struct nfs_getaclres res = {
3778 .acl_len = buflen,
3779 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003780 struct rpc_message msg = {
3781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3782 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003783 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003784 };
Chuck Lever56d08fe2012-07-11 16:30:32 -04003785 int ret = -ENOMEM, npages, i;
3786 size_t acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003787
Andy Adamsonbf118a32011-12-07 11:55:27 -05003788 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3789 /* As long as we're doing a round trip to the server anyway,
3790 * let's be prepared for a page of acl data. */
3791 if (npages == 0)
3792 npages = 1;
3793
Sachin Prabhu5a006892012-04-17 14:35:39 +01003794 /* Add an extra page to handle the bitmap returned */
3795 npages++;
3796
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
Andy Adamsonbf118a32011-12-07 11:55:27 -05003811 /* Let decode_getfacl know not to fail if the ACL data is larger than
3812 * the page we send as a guess */
3813 if (buf == NULL)
3814 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003815
Peng Taode040be2012-01-10 22:42:47 +08003816 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003817 __func__, buf, buflen, npages, args.acl_len);
3818 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3819 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003820 if (ret)
3821 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003822
Trond Myklebust519d3952012-08-14 17:30:10 -04003823 acl_len = res.acl_len;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003824 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003825 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003826 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003827 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003828 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003829 if (buf) {
3830 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003831 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003832 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003833 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003834 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003835 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003836 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003837out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003838 for (i = 0; i < npages; i++)
3839 if (pages[i])
3840 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003841 if (res.acl_scratch)
3842 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003843 return ret;
3844}
3845
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003846static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3847{
3848 struct nfs4_exception exception = { };
3849 ssize_t ret;
3850 do {
3851 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3852 if (ret >= 0)
3853 break;
3854 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3855 } while (exception.retry);
3856 return ret;
3857}
3858
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003859static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3860{
3861 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003862 int ret;
3863
3864 if (!nfs4_server_supports_acls(server))
3865 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003866 ret = nfs_revalidate_inode(server, inode);
3867 if (ret < 0)
3868 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003869 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3870 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003871 ret = nfs4_read_cached_acl(inode, buf, buflen);
3872 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003873 /* -ENOENT is returned if there is no ACL or if there is an ACL
3874 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003875 return ret;
3876 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003877}
3878
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003879static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003880{
3881 struct nfs_server *server = NFS_SERVER(inode);
3882 struct page *pages[NFS4ACL_MAXPAGES];
3883 struct nfs_setaclargs arg = {
3884 .fh = NFS_FH(inode),
3885 .acl_pages = pages,
3886 .acl_len = buflen,
3887 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003888 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003889 struct rpc_message msg = {
3890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3891 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003892 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003893 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003894 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003895
3896 if (!nfs4_server_supports_acls(server))
3897 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003898 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3899 if (i < 0)
3900 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003901 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003902 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003903
3904 /*
3905 * Free each page after tx, so the only ref left is
3906 * held by the network stack
3907 */
3908 for (; i > 0; i--)
3909 put_page(pages[i-1]);
3910
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003911 /*
3912 * Acl update can result in inode attribute update.
3913 * so mark the attribute cache invalid.
3914 */
3915 spin_lock(&inode->i_lock);
3916 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3917 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003918 nfs_access_zap_cache(inode);
3919 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003920 return ret;
3921}
3922
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003923static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3924{
3925 struct nfs4_exception exception = { };
3926 int err;
3927 do {
3928 err = nfs4_handle_exception(NFS_SERVER(inode),
3929 __nfs4_proc_set_acl(inode, buf, buflen),
3930 &exception);
3931 } while (exception.retry);
3932 return err;
3933}
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003936nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003938 struct nfs_client *clp = server->nfs_client;
3939
3940 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 return 0;
3942 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003943 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003944 case -NFS4ERR_ADMIN_REVOKED:
3945 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003946 if (state == NULL)
3947 break;
3948 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003949 case -NFS4ERR_OPENMODE:
3950 if (state == NULL)
3951 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003952 nfs4_schedule_stateid_recovery(server, state);
3953 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003954 case -NFS4ERR_EXPIRED:
3955 if (state != NULL)
3956 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003958 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003959 nfs4_schedule_lease_recovery(clp);
3960 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003961#if defined(CONFIG_NFS_V4_1)
3962 case -NFS4ERR_BADSESSION:
3963 case -NFS4ERR_BADSLOT:
3964 case -NFS4ERR_BAD_HIGH_SLOT:
3965 case -NFS4ERR_DEADSESSION:
3966 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3967 case -NFS4ERR_SEQ_FALSE_RETRY:
3968 case -NFS4ERR_SEQ_MISORDERED:
3969 dprintk("%s ERROR %d, Reset session\n", __func__,
3970 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003971 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003972 task->tk_status = 0;
3973 return -EAGAIN;
3974#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003976 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003977 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003978 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3980 task->tk_status = 0;
3981 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003982 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 case -NFS4ERR_OLD_STATEID:
3984 task->tk_status = 0;
3985 return -EAGAIN;
3986 }
3987 task->tk_status = nfs4_map_errors(task->tk_status);
3988 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003989wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003990 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003991 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3992 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3993 task->tk_status = 0;
3994 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995}
3996
Chuck Leverf0920752012-05-21 22:45:41 -04003997static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3998 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003999{
4000 __be32 verf[2];
4001
Chuck Lever2c820d92012-05-21 22:45:33 -04004002 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4003 /* An impossible timestamp guarantees this value
4004 * will never match a generated boot time. */
4005 verf[0] = 0;
4006 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4007 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004008 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4009 verf[0] = (__be32)nn->boot_time.tv_sec;
4010 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004011 }
Chuck Levercd937102012-03-02 17:14:31 -05004012 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4013}
4014
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004015/**
4016 * nfs4_proc_setclientid - Negotiate client ID
4017 * @clp: state data structure
4018 * @program: RPC program for NFSv4 callback service
4019 * @port: IP port number for NFS4 callback service
4020 * @cred: RPC credential to use for this call
4021 * @res: where to place the result
4022 *
4023 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4024 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004025int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4026 unsigned short port, struct rpc_cred *cred,
4027 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
4029 nfs4_verifier sc_verifier;
4030 struct nfs4_setclientid setclientid = {
4031 .sc_verifier = &sc_verifier,
4032 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004033 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 };
4035 struct rpc_message msg = {
4036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4037 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004038 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004039 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004041 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Chuck Leverde734832012-07-11 16:30:50 -04004043 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004044 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Leverde734832012-07-11 16:30:50 -04004045 rcu_read_lock();
4046 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
4047 sizeof(setclientid.sc_name), "%s/%s %s",
4048 clp->cl_ipaddr,
4049 rpc_peeraddr2str(clp->cl_rpcclient,
4050 RPC_DISPLAY_ADDR),
4051 rpc_peeraddr2str(clp->cl_rpcclient,
4052 RPC_DISPLAY_PROTO));
4053 /* cb_client4 */
4054 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004055 sizeof(setclientid.sc_netid),
4056 rpc_peeraddr2str(clp->cl_rpcclient,
4057 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004058 rcu_read_unlock();
4059 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004060 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 clp->cl_ipaddr, port >> 8, port & 255);
4062
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004063 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4064 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4065 setclientid.sc_name_len, setclientid.sc_name);
4066 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4067 dprintk("NFS reply setclientid: %d\n", status);
4068 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004071/**
4072 * nfs4_proc_setclientid_confirm - Confirm client ID
4073 * @clp: state data structure
4074 * @res: result of a previous SETCLIENTID
4075 * @cred: RPC credential to use for this call
4076 *
4077 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4078 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004079int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004080 struct nfs4_setclientid_res *arg,
4081 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
4083 struct nfs_fsinfo fsinfo;
4084 struct rpc_message msg = {
4085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004086 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004088 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 };
4090 unsigned long now;
4091 int status;
4092
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004093 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4094 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4095 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004097 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 if (status == 0) {
4099 spin_lock(&clp->cl_lock);
4100 clp->cl_lease_time = fsinfo.lease_time * HZ;
4101 clp->cl_last_renewal = now;
4102 spin_unlock(&clp->cl_lock);
4103 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004104 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return status;
4106}
4107
Trond Myklebustfe650402006-01-03 09:55:18 +01004108struct nfs4_delegreturndata {
4109 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004110 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004111 struct nfs_fh fh;
4112 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004113 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004114 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004115 int rpc_status;
4116};
4117
Trond Myklebustfe650402006-01-03 09:55:18 +01004118static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4119{
4120 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004121
Trond Myklebust14516c32010-07-31 14:29:06 -04004122 if (!nfs4_sequence_done(task, &data->res.seq_res))
4123 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004124
Ricardo Labiaga79708862009-12-07 09:23:21 -05004125 switch (task->tk_status) {
4126 case -NFS4ERR_STALE_STATEID:
4127 case -NFS4ERR_EXPIRED:
4128 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004129 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004130 break;
4131 default:
4132 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4133 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004134 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004135 return;
4136 }
4137 }
4138 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004139}
4140
4141static void nfs4_delegreturn_release(void *calldata)
4142{
Trond Myklebustfe650402006-01-03 09:55:18 +01004143 kfree(calldata);
4144}
4145
Andy Adamson938e1012009-04-01 09:22:28 -04004146#if defined(CONFIG_NFS_V4_1)
4147static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4148{
4149 struct nfs4_delegreturndata *d_data;
4150
4151 d_data = (struct nfs4_delegreturndata *)data;
4152
Trond Myklebust035168a2010-06-16 09:52:26 -04004153 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004154 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004155 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004156 return;
4157 rpc_call_start(task);
4158}
4159#endif /* CONFIG_NFS_V4_1 */
4160
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004161static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004162#if defined(CONFIG_NFS_V4_1)
4163 .rpc_call_prepare = nfs4_delegreturn_prepare,
4164#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004165 .rpc_call_done = nfs4_delegreturn_done,
4166 .rpc_release = nfs4_delegreturn_release,
4167};
4168
Trond Myklebuste6f81072008-01-24 18:14:34 -05004169static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004170{
4171 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004172 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004173 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004174 struct rpc_message msg = {
4175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4176 .rpc_cred = cred,
4177 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004178 struct rpc_task_setup task_setup_data = {
4179 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004180 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004181 .callback_ops = &nfs4_delegreturn_ops,
4182 .flags = RPC_TASK_ASYNC,
4183 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004184 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004185
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004186 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004187 if (data == NULL)
4188 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004189 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004190 data->args.fhandle = &data->fh;
4191 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004192 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004193 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004194 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004195 data->res.fattr = &data->fattr;
4196 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004197 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004198 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004199 data->rpc_status = 0;
4200
Trond Myklebustc970aa82007-07-14 15:39:59 -04004201 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004202 msg.rpc_argp = &data->args;
4203 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004204 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004205 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004206 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004207 if (!issync)
4208 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004209 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004210 if (status != 0)
4211 goto out;
4212 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004213 if (status == 0)
4214 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4215 else
4216 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004217out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004218 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004219 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
Trond Myklebuste6f81072008-01-24 18:14:34 -05004222int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
4224 struct nfs_server *server = NFS_SERVER(inode);
4225 struct nfs4_exception exception = { };
4226 int err;
4227 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004228 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 switch (err) {
4230 case -NFS4ERR_STALE_STATEID:
4231 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 case 0:
4233 return 0;
4234 }
4235 err = nfs4_handle_exception(server, err, &exception);
4236 } while (exception.retry);
4237 return err;
4238}
4239
4240#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4241#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4242
4243/*
4244 * sleep, with exponential backoff, and retry the LOCK operation.
4245 */
4246static unsigned long
4247nfs4_set_lock_task_retry(unsigned long timeout)
4248{
Jeff Laytond3103102011-12-01 22:44:39 +01004249 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 timeout <<= 1;
4251 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4252 return NFS4_LOCK_MAXTIMEOUT;
4253 return timeout;
4254}
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4257{
4258 struct inode *inode = state->inode;
4259 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004260 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004261 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004263 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004265 struct nfs_lockt_res res = {
4266 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 };
4268 struct rpc_message msg = {
4269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4270 .rpc_argp = &arg,
4271 .rpc_resp = &res,
4272 .rpc_cred = state->owner->so_cred,
4273 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 struct nfs4_lock_state *lsp;
4275 int status;
4276
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004277 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004278 status = nfs4_set_lock_state(state, request);
4279 if (status != 0)
4280 goto out;
4281 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004282 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004283 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004284 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004285 switch (status) {
4286 case 0:
4287 request->fl_type = F_UNLCK;
4288 break;
4289 case -NFS4ERR_DENIED:
4290 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004292 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004293out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return status;
4295}
4296
4297static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4298{
4299 struct nfs4_exception exception = { };
4300 int err;
4301
4302 do {
4303 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4304 _nfs4_proc_getlk(state, cmd, request),
4305 &exception);
4306 } while (exception.retry);
4307 return err;
4308}
4309
4310static int do_vfs_lock(struct file *file, struct file_lock *fl)
4311{
4312 int res = 0;
4313 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4314 case FL_POSIX:
4315 res = posix_lock_file_wait(file, fl);
4316 break;
4317 case FL_FLOCK:
4318 res = flock_lock_file_wait(file, fl);
4319 break;
4320 default:
4321 BUG();
4322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return res;
4324}
4325
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004326struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004327 struct nfs_locku_args arg;
4328 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004329 struct nfs4_lock_state *lsp;
4330 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004331 struct file_lock fl;
4332 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004333 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004334};
4335
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004336static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4337 struct nfs_open_context *ctx,
4338 struct nfs4_lock_state *lsp,
4339 struct nfs_seqid *seqid)
4340{
4341 struct nfs4_unlockdata *p;
4342 struct inode *inode = lsp->ls_state->inode;
4343
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004344 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004345 if (p == NULL)
4346 return NULL;
4347 p->arg.fh = NFS_FH(inode);
4348 p->arg.fl = &p->fl;
4349 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004350 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004351 p->arg.stateid = &lsp->ls_stateid;
4352 p->lsp = lsp;
4353 atomic_inc(&lsp->ls_count);
4354 /* Ensure we don't close file until we're done freeing locks! */
4355 p->ctx = get_nfs_open_context(ctx);
4356 memcpy(&p->fl, fl, sizeof(p->fl));
4357 p->server = NFS_SERVER(inode);
4358 return p;
4359}
4360
Trond Myklebust06f814a2006-01-03 09:55:07 +01004361static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004362{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004363 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004364 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004365 nfs4_put_lock_state(calldata->lsp);
4366 put_nfs_open_context(calldata->ctx);
4367 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004368}
4369
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004370static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004371{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004372 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004373
Trond Myklebust14516c32010-07-31 14:29:06 -04004374 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4375 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004376 switch (task->tk_status) {
4377 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004378 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4379 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004380 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004381 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004382 case -NFS4ERR_BAD_STATEID:
4383 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384 case -NFS4ERR_STALE_STATEID:
4385 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004386 break;
4387 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004388 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004389 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004390 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391}
4392
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004393static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004394{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004395 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004397 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398 return;
4399 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004400 /* Note: exit _without_ running nfs4_locku_done */
4401 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004402 return;
4403 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004404 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004405 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004406 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004407 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004408 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004409 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
4411
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004412static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004413 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004414 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004415 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004416};
4417
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004418static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4419 struct nfs_open_context *ctx,
4420 struct nfs4_lock_state *lsp,
4421 struct nfs_seqid *seqid)
4422{
4423 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004424 struct rpc_message msg = {
4425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4426 .rpc_cred = ctx->cred,
4427 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004428 struct rpc_task_setup task_setup_data = {
4429 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004430 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004431 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004432 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004433 .flags = RPC_TASK_ASYNC,
4434 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004435
Frank Filz137d6ac2007-07-09 15:32:29 -07004436 /* Ensure this is an unlock - when canceling a lock, the
4437 * canceled lock is passed in, and it won't be an unlock.
4438 */
4439 fl->fl_type = F_UNLCK;
4440
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004441 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4442 if (data == NULL) {
4443 nfs_free_seqid(seqid);
4444 return ERR_PTR(-ENOMEM);
4445 }
4446
Trond Myklebust9d12b212012-01-17 22:04:25 -05004447 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004448 msg.rpc_argp = &data->arg;
4449 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004450 task_setup_data.callback_data = data;
4451 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004452}
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4455{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004456 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004457 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004458 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004459 struct rpc_task *task;
4460 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004461 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Trond Myklebust9b073572006-06-29 16:38:34 -04004463 status = nfs4_set_lock_state(state, request);
4464 /* Unlock _before_ we do the RPC call */
4465 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004466 down_read(&nfsi->rwsem);
4467 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4468 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004469 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004470 }
4471 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004472 if (status != 0)
4473 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004474 /* Is this a delegated lock? */
4475 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004476 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004477 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004478 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004479 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004480 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004481 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004482 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004483 status = PTR_ERR(task);
4484 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004485 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004486 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004487 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004488out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004489 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004490 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491}
4492
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004493struct nfs4_lockdata {
4494 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004495 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004496 struct nfs4_lock_state *lsp;
4497 struct nfs_open_context *ctx;
4498 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004499 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004500 int rpc_status;
4501 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004502 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503};
4504
4505static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004506 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4507 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508{
4509 struct nfs4_lockdata *p;
4510 struct inode *inode = lsp->ls_state->inode;
4511 struct nfs_server *server = NFS_SERVER(inode);
4512
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004513 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514 if (p == NULL)
4515 return NULL;
4516
4517 p->arg.fh = NFS_FH(inode);
4518 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004519 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004520 if (p->arg.open_seqid == NULL)
4521 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004522 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004524 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004526 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004527 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004528 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004529 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004530 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004531 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532 atomic_inc(&lsp->ls_count);
4533 p->ctx = get_nfs_open_context(ctx);
4534 memcpy(&p->fl, fl, sizeof(p->fl));
4535 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004536out_free_seqid:
4537 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538out_free:
4539 kfree(p);
4540 return NULL;
4541}
4542
4543static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4544{
4545 struct nfs4_lockdata *data = calldata;
4546 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Harvey Harrison3110ff82008-05-02 13:42:44 -07004548 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004549 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4550 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004551 /* Do we need to do an open_to_lock_owner? */
4552 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004553 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4554 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004555 data->arg.open_stateid = &state->stateid;
4556 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004557 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004558 } else
4559 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004560 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004561 if (nfs4_setup_sequence(data->server,
4562 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004563 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004564 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004565 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004566 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567}
4568
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004569static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4570{
4571 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4572 nfs4_lock_prepare(task, calldata);
4573}
4574
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4576{
4577 struct nfs4_lockdata *data = calldata;
4578
Harvey Harrison3110ff82008-05-02 13:42:44 -07004579 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580
Trond Myklebust14516c32010-07-31 14:29:06 -04004581 if (!nfs4_sequence_done(task, &data->res.seq_res))
4582 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004583
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004584 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004586 if (data->rpc_status == 0)
4587 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4588 else
4589 goto out;
4590 }
4591 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004592 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004594 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004596out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004597 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598}
4599
4600static void nfs4_lock_release(void *calldata)
4601{
4602 struct nfs4_lockdata *data = calldata;
4603
Harvey Harrison3110ff82008-05-02 13:42:44 -07004604 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004605 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606 if (data->cancelled != 0) {
4607 struct rpc_task *task;
4608 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4609 data->arg.lock_seqid);
4610 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004611 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004612 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004613 } else
4614 nfs_free_seqid(data->arg.lock_seqid);
4615 nfs4_put_lock_state(data->lsp);
4616 put_nfs_open_context(data->ctx);
4617 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004618 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619}
4620
4621static const struct rpc_call_ops nfs4_lock_ops = {
4622 .rpc_call_prepare = nfs4_lock_prepare,
4623 .rpc_call_done = nfs4_lock_done,
4624 .rpc_release = nfs4_lock_release,
4625};
4626
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004627static const struct rpc_call_ops nfs4_recover_lock_ops = {
4628 .rpc_call_prepare = nfs4_recover_lock_prepare,
4629 .rpc_call_done = nfs4_lock_done,
4630 .rpc_release = nfs4_lock_release,
4631};
4632
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004633static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4634{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004635 switch (error) {
4636 case -NFS4ERR_ADMIN_REVOKED:
4637 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004638 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004639 if (new_lock_owner != 0 ||
4640 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004641 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004642 break;
4643 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004644 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004645 case -NFS4ERR_EXPIRED:
4646 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004647 };
4648}
4649
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004650static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651{
4652 struct nfs4_lockdata *data;
4653 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004654 struct rpc_message msg = {
4655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4656 .rpc_cred = state->owner->so_cred,
4657 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004658 struct rpc_task_setup task_setup_data = {
4659 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004660 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004661 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004662 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004663 .flags = RPC_TASK_ASYNC,
4664 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004665 int ret;
4666
Harvey Harrison3110ff82008-05-02 13:42:44 -07004667 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004668 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004669 fl->fl_u.nfs4_fl.owner,
4670 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004671 if (data == NULL)
4672 return -ENOMEM;
4673 if (IS_SETLKW(cmd))
4674 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004675 if (recovery_type > NFS_LOCK_NEW) {
4676 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004677 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004678 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4679 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004680 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004681 msg.rpc_argp = &data->arg;
4682 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004683 task_setup_data.callback_data = data;
4684 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004685 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004686 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004687 ret = nfs4_wait_for_completion_rpc_task(task);
4688 if (ret == 0) {
4689 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004690 if (ret)
4691 nfs4_handle_setlk_error(data->server, data->lsp,
4692 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004693 } else
4694 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004695 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004696 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004697 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698}
4699
4700static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4701{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004702 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004703 struct nfs4_exception exception = {
4704 .inode = state->inode,
4705 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004706 int err;
4707
4708 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004709 /* Cache the lock if possible... */
4710 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4711 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004712 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004713 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004714 break;
4715 nfs4_handle_exception(server, err, &exception);
4716 } while (exception.retry);
4717 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718}
4719
4720static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4721{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004722 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004723 struct nfs4_exception exception = {
4724 .inode = state->inode,
4725 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004726 int err;
4727
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004728 err = nfs4_set_lock_state(state, request);
4729 if (err != 0)
4730 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004731 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004732 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4733 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004734 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004735 switch (err) {
4736 default:
4737 goto out;
4738 case -NFS4ERR_GRACE:
4739 case -NFS4ERR_DELAY:
4740 nfs4_handle_exception(server, err, &exception);
4741 err = 0;
4742 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004743 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004744out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004745 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004748#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004749/**
4750 * nfs41_check_expired_locks - possibly free a lock stateid
4751 *
4752 * @state: NFSv4 state for an inode
4753 *
4754 * Returns NFS_OK if recovery for this stateid is now finished.
4755 * Otherwise a negative NFS4ERR value is returned.
4756 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004757static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004758{
Chuck Levereb64cf92012-07-11 16:30:05 -04004759 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004760 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004761 struct nfs_server *server = NFS_SERVER(state->inode);
4762
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004763 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4764 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4765 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4766 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004767 /* Free the stateid unless the server
4768 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004769 if (status != -NFS4ERR_BAD_STATEID)
4770 nfs41_free_stateid(server,
4771 &lsp->ls_stateid);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004772 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4773 ret = status;
4774 }
4775 }
4776 };
4777
4778 return ret;
4779}
4780
4781static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4782{
4783 int status = NFS_OK;
4784
4785 if (test_bit(LK_STATE_IN_USE, &state->flags))
4786 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004787 if (status != NFS_OK)
4788 status = nfs4_lock_expired(state, request);
4789 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004790}
4791#endif
4792
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4794{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004795 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004796 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004797 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004799 if ((fl_flags & FL_POSIX) &&
4800 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4801 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004802 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004803 status = nfs4_set_lock_state(state, request);
4804 if (status != 0)
4805 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004806 request->fl_flags |= FL_ACCESS;
4807 status = do_vfs_lock(request->fl_file, request);
4808 if (status < 0)
4809 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004810 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004811 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004812 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004813 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004814 request->fl_flags = fl_flags & ~FL_SLEEP;
4815 status = do_vfs_lock(request->fl_file, request);
4816 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004817 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004818 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004819 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004820 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004821 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004822 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004823 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004824 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4825 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004826out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004827 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004828out:
4829 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 return status;
4831}
4832
4833static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4834{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004835 struct nfs4_exception exception = {
4836 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004837 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004838 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 int err;
4840
4841 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004842 err = _nfs4_proc_setlk(state, cmd, request);
4843 if (err == -NFS4ERR_DENIED)
4844 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004846 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 } while (exception.retry);
4848 return err;
4849}
4850
4851static int
4852nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4853{
4854 struct nfs_open_context *ctx;
4855 struct nfs4_state *state;
4856 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4857 int status;
4858
4859 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004860 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 state = ctx->state;
4862
4863 if (request->fl_start < 0 || request->fl_end < 0)
4864 return -EINVAL;
4865
Trond Myklebustd9531262009-07-21 19:22:38 -04004866 if (IS_GETLK(cmd)) {
4867 if (state != NULL)
4868 return nfs4_proc_getlk(state, F_GETLK, request);
4869 return 0;
4870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4873 return -EINVAL;
4874
Trond Myklebustd9531262009-07-21 19:22:38 -04004875 if (request->fl_type == F_UNLCK) {
4876 if (state != NULL)
4877 return nfs4_proc_unlck(state, cmd, request);
4878 return 0;
4879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Trond Myklebustd9531262009-07-21 19:22:38 -04004881 if (state == NULL)
4882 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004883 /*
4884 * Don't rely on the VFS having checked the file open mode,
4885 * since it won't do this for flock() locks.
4886 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004887 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004888 case F_RDLCK:
4889 if (!(filp->f_mode & FMODE_READ))
4890 return -EBADF;
4891 break;
4892 case F_WRLCK:
4893 if (!(filp->f_mode & FMODE_WRITE))
4894 return -EBADF;
4895 }
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 do {
4898 status = nfs4_proc_setlk(state, cmd, request);
4899 if ((status != -EAGAIN) || IS_SETLK(cmd))
4900 break;
4901 timeout = nfs4_set_lock_task_retry(timeout);
4902 status = -ERESTARTSYS;
4903 if (signalled())
4904 break;
4905 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 return status;
4907}
4908
Trond Myklebust888e6942005-11-04 15:38:11 -05004909int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4910{
4911 struct nfs_server *server = NFS_SERVER(state->inode);
4912 struct nfs4_exception exception = { };
4913 int err;
4914
4915 err = nfs4_set_lock_state(state, fl);
4916 if (err != 0)
4917 goto out;
4918 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004919 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004920 switch (err) {
4921 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004922 printk(KERN_ERR "NFS: %s: unhandled error "
4923 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004924 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004925 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004926 goto out;
4927 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004928 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004929 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004930 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004931 nfs4_schedule_lease_recovery(server->nfs_client);
4932 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004933 case -NFS4ERR_BADSESSION:
4934 case -NFS4ERR_BADSLOT:
4935 case -NFS4ERR_BAD_HIGH_SLOT:
4936 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4937 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004938 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004939 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004940 case -ERESTARTSYS:
4941 /*
4942 * The show must go on: exit, but mark the
4943 * stateid as needing recovery.
4944 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004945 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004946 case -NFS4ERR_ADMIN_REVOKED:
4947 case -NFS4ERR_BAD_STATEID:
4948 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004949 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004950 err = 0;
4951 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004952 case -EKEYEXPIRED:
4953 /*
4954 * User RPCSEC_GSS context has expired.
4955 * We cannot recover this stateid now, so
4956 * skip it and allow recovery thread to
4957 * proceed.
4958 */
4959 err = 0;
4960 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004961 case -ENOMEM:
4962 case -NFS4ERR_DENIED:
4963 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4964 err = 0;
4965 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004966 case -NFS4ERR_DELAY:
4967 break;
4968 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004969 err = nfs4_handle_exception(server, err, &exception);
4970 } while (exception.retry);
4971out:
4972 return err;
4973}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004974
Trond Myklebustcf470c32012-03-07 13:49:12 -05004975struct nfs_release_lockowner_data {
4976 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004977 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004978 struct nfs_release_lockowner_args args;
4979};
4980
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004981static void nfs4_release_lockowner_release(void *calldata)
4982{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004983 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004984 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004985 kfree(calldata);
4986}
4987
Trond Myklebust17280172012-03-11 13:11:00 -04004988static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004989 .rpc_release = nfs4_release_lockowner_release,
4990};
4991
Trond Myklebustcf470c32012-03-07 13:49:12 -05004992int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004993{
4994 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004995 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004996 struct rpc_message msg = {
4997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4998 };
4999
5000 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005001 return -EINVAL;
5002 data = kmalloc(sizeof(*data), GFP_NOFS);
5003 if (!data)
5004 return -ENOMEM;
5005 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005006 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005007 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5008 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5009 data->args.lock_owner.s_dev = server->s_dev;
5010 msg.rpc_argp = &data->args;
5011 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5012 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005013}
5014
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005015#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5016
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005017static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5018 const void *buf, size_t buflen,
5019 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005020{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005021 if (strcmp(key, "") != 0)
5022 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005023
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005024 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005025}
5026
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005027static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5028 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005029{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005030 if (strcmp(key, "") != 0)
5031 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005032
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005033 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005034}
5035
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005036static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5037 size_t list_len, const char *name,
5038 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005039{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005040 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005041
J. Bruce Fields096455a2006-03-20 23:23:42 -05005042 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5043 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005044
5045 if (list && len <= list_len)
5046 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005047 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005048}
5049
Andy Adamson533eb462011-06-13 18:25:56 -04005050/*
5051 * nfs_fhget will use either the mounted_on_fileid or the fileid
5052 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005053static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5054{
Andy Adamson533eb462011-06-13 18:25:56 -04005055 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5056 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5057 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005058 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005059 return;
5060
5061 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005062 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005063 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5064 fattr->nlink = 2;
5065}
5066
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005067static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5068 const struct qstr *name,
5069 struct nfs4_fs_locations *fs_locations,
5070 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005071{
5072 struct nfs_server *server = NFS_SERVER(dir);
5073 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005074 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005075 };
5076 struct nfs4_fs_locations_arg args = {
5077 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005078 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005079 .page = page,
5080 .bitmask = bitmask,
5081 };
Benny Halevy22958462009-04-01 09:22:02 -04005082 struct nfs4_fs_locations_res res = {
5083 .fs_locations = fs_locations,
5084 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005085 struct rpc_message msg = {
5086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5087 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005088 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005089 };
5090 int status;
5091
Harvey Harrison3110ff82008-05-02 13:42:44 -07005092 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005093
5094 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5095 * is not supported */
5096 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5097 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5098 else
5099 bitmask[0] |= FATTR4_WORD0_FILEID;
5100
Trond Myklebustc228fd32007-01-13 02:28:11 -05005101 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005102 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005103 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005104 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005105 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005106 return status;
5107}
5108
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005109int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5110 const struct qstr *name,
5111 struct nfs4_fs_locations *fs_locations,
5112 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005113{
5114 struct nfs4_exception exception = { };
5115 int err;
5116 do {
5117 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005118 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005119 &exception);
5120 } while (exception.retry);
5121 return err;
5122}
5123
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005124static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5125{
5126 int status;
5127 struct nfs4_secinfo_arg args = {
5128 .dir_fh = NFS_FH(dir),
5129 .name = name,
5130 };
5131 struct nfs4_secinfo_res res = {
5132 .flavors = flavors,
5133 };
5134 struct rpc_message msg = {
5135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5136 .rpc_argp = &args,
5137 .rpc_resp = &res,
5138 };
5139
5140 dprintk("NFS call secinfo %s\n", name->name);
5141 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5142 dprintk("NFS reply secinfo: %d\n", status);
5143 return status;
5144}
5145
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005146int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5147 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005148{
5149 struct nfs4_exception exception = { };
5150 int err;
5151 do {
5152 err = nfs4_handle_exception(NFS_SERVER(dir),
5153 _nfs4_proc_secinfo(dir, name, flavors),
5154 &exception);
5155 } while (exception.retry);
5156 return err;
5157}
5158
Andy Adamson557134a2009-04-01 09:21:53 -04005159#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005160/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005161 * Check the exchange flags returned by the server for invalid flags, having
5162 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5163 * DS flags set.
5164 */
5165static int nfs4_check_cl_exchange_flags(u32 flags)
5166{
5167 if (flags & ~EXCHGID4_FLAG_MASK_R)
5168 goto out_inval;
5169 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5170 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5171 goto out_inval;
5172 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5173 goto out_inval;
5174 return NFS_OK;
5175out_inval:
5176 return -NFS4ERR_INVAL;
5177}
5178
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005179static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005180nfs41_same_server_scope(struct nfs41_server_scope *a,
5181 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005182{
5183 if (a->server_scope_sz == b->server_scope_sz &&
5184 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5185 return true;
5186
5187 return false;
5188}
5189
Andy Adamson357f54d2010-12-14 10:11:57 -05005190/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005191 * nfs4_proc_bind_conn_to_session()
5192 *
5193 * The 4.1 client currently uses the same TCP connection for the
5194 * fore and backchannel.
5195 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005196int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005197{
5198 int status;
5199 struct nfs41_bind_conn_to_session_res res;
5200 struct rpc_message msg = {
5201 .rpc_proc =
5202 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5203 .rpc_argp = clp,
5204 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005205 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005206 };
5207
5208 dprintk("--> %s\n", __func__);
5209 BUG_ON(clp == NULL);
5210
5211 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5212 if (unlikely(res.session == NULL)) {
5213 status = -ENOMEM;
5214 goto out;
5215 }
5216
5217 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5218 if (status == 0) {
5219 if (memcmp(res.session->sess_id.data,
5220 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5221 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5222 status = -EIO;
5223 goto out_session;
5224 }
5225 if (res.dir != NFS4_CDFS4_BOTH) {
5226 dprintk("NFS: %s: Unexpected direction from server\n",
5227 __func__);
5228 status = -EIO;
5229 goto out_session;
5230 }
5231 if (res.use_conn_in_rdma_mode) {
5232 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5233 __func__);
5234 status = -EIO;
5235 goto out_session;
5236 }
5237 }
5238out_session:
5239 kfree(res.session);
5240out:
5241 dprintk("<-- %s status= %d\n", __func__, status);
5242 return status;
5243}
5244
5245/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005246 * nfs4_proc_exchange_id()
5247 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005248 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5249 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005250 * Since the clientid has expired, all compounds using sessions
5251 * associated with the stale clientid will be returning
5252 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5253 * be in some phase of session reset.
5254 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005255int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005256{
5257 nfs4_verifier verifier;
5258 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005259 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005260 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005261 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005262 };
5263 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005264 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005265 };
5266 int status;
5267 struct rpc_message msg = {
5268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5269 .rpc_argp = &args,
5270 .rpc_resp = &res,
5271 .rpc_cred = cred,
5272 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005273
Chuck Leverf0920752012-05-21 22:45:41 -04005274 nfs4_init_boot_verifier(clp, &verifier);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005275 args.id_len = scnprintf(args.id, sizeof(args.id),
Chuck Leverde734832012-07-11 16:30:50 -04005276 "%s/%s",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005277 clp->cl_ipaddr,
Chuck Leverde734832012-07-11 16:30:50 -04005278 clp->cl_rpcclient->cl_nodename);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005279 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5280 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5281 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005282
Chuck Leveracdeb692012-05-21 22:46:16 -04005283 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005284 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005285 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005286 status = -ENOMEM;
5287 goto out;
5288 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005289
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005290 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005291 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005292 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005293 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005294 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005295 }
5296
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005297 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005298 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005299 status = -ENOMEM;
5300 goto out_server_scope;
5301 }
5302
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005303 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005304 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005305 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005306
Chuck Lever177313f2012-05-21 22:44:58 -04005307 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005308 clp->cl_clientid = res.clientid;
5309 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5310 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5311 clp->cl_seqid = res.seqid;
5312
Chuck Leveracdeb692012-05-21 22:46:16 -04005313 kfree(clp->cl_serverowner);
5314 clp->cl_serverowner = res.server_owner;
5315 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005316
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005317 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005318 kfree(clp->cl_implid);
5319 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005320
Chuck Lever177313f2012-05-21 22:44:58 -04005321 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005322 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005323 res.server_scope)) {
5324 dprintk("%s: server_scope mismatch detected\n",
5325 __func__);
5326 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005327 kfree(clp->cl_serverscope);
5328 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005329 }
5330
Chuck Lever177313f2012-05-21 22:44:58 -04005331 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005332 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005333 goto out;
5334 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005335 } else
5336 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005337
Chuck Leveracdeb692012-05-21 22:46:16 -04005338out_server_owner:
5339 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005340out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005341 kfree(res.server_scope);
5342out:
Chuck Lever177313f2012-05-21 22:44:58 -04005343 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005344 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005345 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005346 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005347 clp->cl_implid->date.seconds,
5348 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005349 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005350 return status;
5351}
5352
Trond Myklebust66245532012-05-25 17:18:09 -04005353static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5354 struct rpc_cred *cred)
5355{
5356 struct rpc_message msg = {
5357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5358 .rpc_argp = clp,
5359 .rpc_cred = cred,
5360 };
5361 int status;
5362
5363 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5364 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005365 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005366 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5367 return status;
5368}
5369
5370static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5371 struct rpc_cred *cred)
5372{
5373 unsigned int loop;
5374 int ret;
5375
5376 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5377 ret = _nfs4_proc_destroy_clientid(clp, cred);
5378 switch (ret) {
5379 case -NFS4ERR_DELAY:
5380 case -NFS4ERR_CLIENTID_BUSY:
5381 ssleep(1);
5382 break;
5383 default:
5384 return ret;
5385 }
5386 }
5387 return 0;
5388}
5389
5390int nfs4_destroy_clientid(struct nfs_client *clp)
5391{
5392 struct rpc_cred *cred;
5393 int ret = 0;
5394
5395 if (clp->cl_mvops->minor_version < 1)
5396 goto out;
5397 if (clp->cl_exchange_flags == 0)
5398 goto out;
5399 cred = nfs4_get_exchange_id_cred(clp);
5400 ret = nfs4_proc_destroy_clientid(clp, cred);
5401 if (cred)
5402 put_rpccred(cred);
5403 switch (ret) {
5404 case 0:
5405 case -NFS4ERR_STALE_CLIENTID:
5406 clp->cl_exchange_flags = 0;
5407 }
5408out:
5409 return ret;
5410}
5411
Andy Adamson2050f0c2009-04-01 09:22:30 -04005412struct nfs4_get_lease_time_data {
5413 struct nfs4_get_lease_time_args *args;
5414 struct nfs4_get_lease_time_res *res;
5415 struct nfs_client *clp;
5416};
5417
5418static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5419 void *calldata)
5420{
5421 int ret;
5422 struct nfs4_get_lease_time_data *data =
5423 (struct nfs4_get_lease_time_data *)calldata;
5424
5425 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005426 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005427 /* just setup sequence, do not trigger session recovery
5428 since we're invoked within one */
5429 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005430 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005431 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005432
5433 BUG_ON(ret == -EAGAIN);
5434 rpc_call_start(task);
5435 dprintk("<-- %s\n", __func__);
5436}
5437
5438/*
5439 * Called from nfs4_state_manager thread for session setup, so don't recover
5440 * from sequence operation or clientid errors.
5441 */
5442static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5443{
5444 struct nfs4_get_lease_time_data *data =
5445 (struct nfs4_get_lease_time_data *)calldata;
5446
5447 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005448 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5449 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005450 switch (task->tk_status) {
5451 case -NFS4ERR_DELAY:
5452 case -NFS4ERR_GRACE:
5453 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5454 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5455 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005456 /* fall through */
5457 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005458 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005459 return;
5460 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005461 dprintk("<-- %s\n", __func__);
5462}
5463
Trond Myklebust17280172012-03-11 13:11:00 -04005464static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005465 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5466 .rpc_call_done = nfs4_get_lease_time_done,
5467};
5468
5469int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5470{
5471 struct rpc_task *task;
5472 struct nfs4_get_lease_time_args args;
5473 struct nfs4_get_lease_time_res res = {
5474 .lr_fsinfo = fsinfo,
5475 };
5476 struct nfs4_get_lease_time_data data = {
5477 .args = &args,
5478 .res = &res,
5479 .clp = clp,
5480 };
5481 struct rpc_message msg = {
5482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5483 .rpc_argp = &args,
5484 .rpc_resp = &res,
5485 };
5486 struct rpc_task_setup task_setup = {
5487 .rpc_client = clp->cl_rpcclient,
5488 .rpc_message = &msg,
5489 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005490 .callback_data = &data,
5491 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005492 };
5493 int status;
5494
Trond Myklebust9d12b212012-01-17 22:04:25 -05005495 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005496 dprintk("--> %s\n", __func__);
5497 task = rpc_run_task(&task_setup);
5498
5499 if (IS_ERR(task))
5500 status = PTR_ERR(task);
5501 else {
5502 status = task->tk_status;
5503 rpc_put_task(task);
5504 }
5505 dprintk("<-- %s return %d\n", __func__, status);
5506
5507 return status;
5508}
5509
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005510static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5511{
5512 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5513}
5514
5515static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5516 struct nfs4_slot *new,
5517 u32 max_slots,
5518 u32 ivalue)
5519{
5520 struct nfs4_slot *old = NULL;
5521 u32 i;
5522
5523 spin_lock(&tbl->slot_tbl_lock);
5524 if (new) {
5525 old = tbl->slots;
5526 tbl->slots = new;
5527 tbl->max_slots = max_slots;
5528 }
5529 tbl->highest_used_slotid = -1; /* no slot is currently used */
5530 for (i = 0; i < tbl->max_slots; i++)
5531 tbl->slots[i].seq_nr = ivalue;
5532 spin_unlock(&tbl->slot_tbl_lock);
5533 kfree(old);
5534}
5535
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005536/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005537 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005538 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005539static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5540 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005541{
Andy Adamson104aeba2010-01-14 17:45:10 -05005542 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005543 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005544
Andy Adamson104aeba2010-01-14 17:45:10 -05005545 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5546 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005547
Andy Adamson104aeba2010-01-14 17:45:10 -05005548 /* Does the newly negotiated max_reqs match the existing slot table? */
5549 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005550 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005551 if (!new)
5552 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005553 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005554 ret = 0;
5555
5556 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005557 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5558 tbl, tbl->slots, tbl->max_slots);
5559out:
5560 dprintk("<-- %s: return %d\n", __func__, ret);
5561 return ret;
5562}
5563
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005564/* Destroy the slot table */
5565static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5566{
5567 if (session->fc_slot_table.slots != NULL) {
5568 kfree(session->fc_slot_table.slots);
5569 session->fc_slot_table.slots = NULL;
5570 }
5571 if (session->bc_slot_table.slots != NULL) {
5572 kfree(session->bc_slot_table.slots);
5573 session->bc_slot_table.slots = NULL;
5574 }
5575 return;
5576}
5577
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005578/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005579 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005580 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005581static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005582{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005583 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005584 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005585
Andy Adamsonaacd5532011-11-09 13:58:21 -05005586 dprintk("--> %s\n", __func__);
5587 /* Fore channel */
5588 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005589 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5590 if (status) /* -ENOMEM */
5591 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005592 /* Back channel */
5593 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005594 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5595 if (status && tbl->slots == NULL)
5596 /* Fore and back channel share a connection so get
5597 * both slot tables or neither */
5598 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005599 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005600}
5601
5602struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5603{
5604 struct nfs4_session *session;
5605 struct nfs4_slot_table *tbl;
5606
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005607 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005608 if (!session)
5609 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005610
Andy Adamson557134a2009-04-01 09:21:53 -04005611 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005612 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005613 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005614 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005615 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005616
5617 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005618 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005619 spin_lock_init(&tbl->slot_tbl_lock);
5620 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005621 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005622
Trond Myklebust1055d762010-06-16 09:52:27 -04005623 session->session_state = 1<<NFS4_SESSION_INITING;
5624
Andy Adamson557134a2009-04-01 09:21:53 -04005625 session->clp = clp;
5626 return session;
5627}
5628
5629void nfs4_destroy_session(struct nfs4_session *session)
5630{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005631 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005632 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005633
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005634 cred = nfs4_get_exchange_id_cred(session->clp);
5635 nfs4_proc_destroy_session(session, cred);
5636 if (cred)
5637 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005638
5639 rcu_read_lock();
5640 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5641 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005642 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005643 __func__, xprt);
5644 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005645 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005646 kfree(session);
5647}
5648
Andy Adamsonfc931582009-04-01 09:22:31 -04005649/*
5650 * Initialize the values to be used by the client in CREATE_SESSION
5651 * If nfs4_init_session set the fore channel request and response sizes,
5652 * use them.
5653 *
5654 * Set the back channel max_resp_sz_cached to zero to force the client to
5655 * always set csa_cachethis to FALSE because the current implementation
5656 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5657 */
5658static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5659{
5660 struct nfs4_session *session = args->client->cl_session;
5661 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5662 mxresp_sz = session->fc_attrs.max_resp_sz;
5663
5664 if (mxrqst_sz == 0)
5665 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5666 if (mxresp_sz == 0)
5667 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5668 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005669 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5670 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005671 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005672 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005673
5674 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005675 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005676 __func__,
5677 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005678 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005679
5680 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005681 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5682 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5683 args->bc_attrs.max_resp_sz_cached = 0;
5684 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5685 args->bc_attrs.max_reqs = 1;
5686
5687 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5688 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5689 __func__,
5690 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5691 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5692 args->bc_attrs.max_reqs);
5693}
5694
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005695static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005696{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005697 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5698 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5699
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005700 if (rcvd->max_resp_sz > sent->max_resp_sz)
5701 return -EINVAL;
5702 /*
5703 * Our requested max_ops is the minimum we need; we're not
5704 * prepared to break up compounds into smaller pieces than that.
5705 * So, no point even trying to continue if the server won't
5706 * cooperate:
5707 */
5708 if (rcvd->max_ops < sent->max_ops)
5709 return -EINVAL;
5710 if (rcvd->max_reqs == 0)
5711 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005712 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5713 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005714 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005715}
5716
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005717static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5718{
5719 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5720 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005721
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005722 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5723 return -EINVAL;
5724 if (rcvd->max_resp_sz < sent->max_resp_sz)
5725 return -EINVAL;
5726 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5727 return -EINVAL;
5728 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005729 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005730 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005731 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005732 return -EINVAL;
5733 return 0;
5734}
Andy Adamson8d353012009-04-01 09:22:32 -04005735
Andy Adamson8d353012009-04-01 09:22:32 -04005736static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5737 struct nfs4_session *session)
5738{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005739 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005740
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005741 ret = nfs4_verify_fore_channel_attrs(args, session);
5742 if (ret)
5743 return ret;
5744 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005745}
5746
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005747static int _nfs4_proc_create_session(struct nfs_client *clp,
5748 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005749{
5750 struct nfs4_session *session = clp->cl_session;
5751 struct nfs41_create_session_args args = {
5752 .client = clp,
5753 .cb_program = NFS4_CALLBACK,
5754 };
5755 struct nfs41_create_session_res res = {
5756 .client = clp,
5757 };
5758 struct rpc_message msg = {
5759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5760 .rpc_argp = &args,
5761 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005762 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005763 };
5764 int status;
5765
5766 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005767 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005768
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005769 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005770
Andy Adamson8d353012009-04-01 09:22:32 -04005771 if (!status)
5772 /* Verify the session's negotiated channel_attrs values */
5773 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005774 if (!status) {
5775 /* Increment the clientid slot sequence id */
5776 clp->cl_seqid++;
5777 }
5778
5779 return status;
5780}
5781
5782/*
5783 * Issues a CREATE_SESSION operation to the server.
5784 * It is the responsibility of the caller to verify the session is
5785 * expired before calling this routine.
5786 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005787int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005788{
5789 int status;
5790 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005791 struct nfs4_session *session = clp->cl_session;
5792
5793 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5794
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005795 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005796 if (status)
5797 goto out;
5798
Andy Adamsonaacd5532011-11-09 13:58:21 -05005799 /* Init or reset the session slot tables */
5800 status = nfs4_setup_session_slot_tables(session);
5801 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005802 if (status)
5803 goto out;
5804
5805 ptr = (unsigned *)&session->sess_id.data[0];
5806 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5807 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005808out:
5809 dprintk("<-- %s\n", __func__);
5810 return status;
5811}
5812
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005813/*
5814 * Issue the over-the-wire RPC DESTROY_SESSION.
5815 * The caller must serialize access to this routine.
5816 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005817int nfs4_proc_destroy_session(struct nfs4_session *session,
5818 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005819{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005820 struct rpc_message msg = {
5821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5822 .rpc_argp = session,
5823 .rpc_cred = cred,
5824 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005825 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005826
5827 dprintk("--> nfs4_proc_destroy_session\n");
5828
5829 /* session is still being setup */
5830 if (session->clp->cl_cons_state != NFS_CS_READY)
5831 return status;
5832
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005833 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005834
5835 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005836 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005837 "Session has been destroyed regardless...\n", status);
5838
5839 dprintk("<-- nfs4_proc_destroy_session\n");
5840 return status;
5841}
5842
Trond Myklebust7b38c362012-05-23 13:23:31 -04005843/*
5844 * With sessions, the client is not marked ready until after a
5845 * successful EXCHANGE_ID and CREATE_SESSION.
5846 *
5847 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5848 * other versions of NFS can be tried.
5849 */
5850static int nfs41_check_session_ready(struct nfs_client *clp)
5851{
5852 int ret;
5853
5854 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5855 ret = nfs4_client_recover_expired_lease(clp);
5856 if (ret)
5857 return ret;
5858 }
5859 if (clp->cl_cons_state < NFS_CS_READY)
5860 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005861 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005862 return 0;
5863}
5864
Trond Myklebustfccba802009-07-21 16:48:07 -04005865int nfs4_init_session(struct nfs_server *server)
5866{
5867 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005868 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005869 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005870
5871 if (!nfs4_has_session(clp))
5872 return 0;
5873
Trond Myklebust1055d762010-06-16 09:52:27 -04005874 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005875 spin_lock(&clp->cl_lock);
5876 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005877
Trond Myklebust7b38c362012-05-23 13:23:31 -04005878 rsize = server->rsize;
5879 if (rsize == 0)
5880 rsize = NFS_MAX_FILE_IO_SIZE;
5881 wsize = server->wsize;
5882 if (wsize == 0)
5883 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005884
Trond Myklebust7b38c362012-05-23 13:23:31 -04005885 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5886 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5887 }
5888 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005889
Trond Myklebust7b38c362012-05-23 13:23:31 -04005890 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005891}
5892
Trond Myklebust7b38c362012-05-23 13:23:31 -04005893int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005894{
5895 struct nfs4_session *session = clp->cl_session;
5896 int ret;
5897
Trond Myklebust7b38c362012-05-23 13:23:31 -04005898 spin_lock(&clp->cl_lock);
5899 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5900 /*
5901 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5902 * DS lease to be equal to the MDS lease.
5903 */
5904 clp->cl_lease_time = lease_time;
5905 clp->cl_last_renewal = jiffies;
5906 }
5907 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005908
Trond Myklebust7b38c362012-05-23 13:23:31 -04005909 ret = nfs41_check_session_ready(clp);
5910 if (ret)
5911 return ret;
5912 /* Test for the DS role */
5913 if (!is_ds_client(clp))
5914 return -ENODEV;
5915 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005916}
5917EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5918
5919
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005920/*
5921 * Renew the cl_session lease.
5922 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005923struct nfs4_sequence_data {
5924 struct nfs_client *clp;
5925 struct nfs4_sequence_args args;
5926 struct nfs4_sequence_res res;
5927};
5928
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005929static void nfs41_sequence_release(void *data)
5930{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005931 struct nfs4_sequence_data *calldata = data;
5932 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005933
Alexandros Batsakis71358402010-02-05 03:45:05 -08005934 if (atomic_read(&clp->cl_count) > 1)
5935 nfs4_schedule_state_renewal(clp);
5936 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005937 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005938}
5939
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005940static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5941{
5942 switch(task->tk_status) {
5943 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005944 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5945 return -EAGAIN;
5946 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005947 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005948 }
5949 return 0;
5950}
5951
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005952static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005953{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005954 struct nfs4_sequence_data *calldata = data;
5955 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005956
Trond Myklebust14516c32010-07-31 14:29:06 -04005957 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5958 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005959
5960 if (task->tk_status < 0) {
5961 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005962 if (atomic_read(&clp->cl_count) == 1)
5963 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005964
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005965 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5966 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005967 return;
5968 }
5969 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005970 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005971out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005972 dprintk("<-- %s\n", __func__);
5973}
5974
5975static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5976{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005977 struct nfs4_sequence_data *calldata = data;
5978 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005979 struct nfs4_sequence_args *args;
5980 struct nfs4_sequence_res *res;
5981
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005982 args = task->tk_msg.rpc_argp;
5983 res = task->tk_msg.rpc_resp;
5984
Trond Myklebust9d12b212012-01-17 22:04:25 -05005985 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005986 return;
5987 rpc_call_start(task);
5988}
5989
5990static const struct rpc_call_ops nfs41_sequence_ops = {
5991 .rpc_call_done = nfs41_sequence_call_done,
5992 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005993 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005994};
5995
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005996static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005997{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005998 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005999 struct rpc_message msg = {
6000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6001 .rpc_cred = cred,
6002 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006003 struct rpc_task_setup task_setup_data = {
6004 .rpc_client = clp->cl_rpcclient,
6005 .rpc_message = &msg,
6006 .callback_ops = &nfs41_sequence_ops,
6007 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6008 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006009
Alexandros Batsakis71358402010-02-05 03:45:05 -08006010 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006011 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006012 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006013 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006014 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006015 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006016 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006017 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006018 msg.rpc_argp = &calldata->args;
6019 msg.rpc_resp = &calldata->res;
6020 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006021 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006022
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006023 return rpc_run_task(&task_setup_data);
6024}
6025
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006026static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006027{
6028 struct rpc_task *task;
6029 int ret = 0;
6030
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006031 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6032 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006033 task = _nfs41_proc_sequence(clp, cred);
6034 if (IS_ERR(task))
6035 ret = PTR_ERR(task);
6036 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006037 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006038 dprintk("<-- %s status=%d\n", __func__, ret);
6039 return ret;
6040}
6041
6042static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6043{
6044 struct rpc_task *task;
6045 int ret;
6046
6047 task = _nfs41_proc_sequence(clp, cred);
6048 if (IS_ERR(task)) {
6049 ret = PTR_ERR(task);
6050 goto out;
6051 }
6052 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006053 if (!ret) {
6054 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6055
6056 if (task->tk_status == 0)
6057 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006058 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006059 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006060 rpc_put_task(task);
6061out:
6062 dprintk("<-- %s status=%d\n", __func__, ret);
6063 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006064}
6065
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006066struct nfs4_reclaim_complete_data {
6067 struct nfs_client *clp;
6068 struct nfs41_reclaim_complete_args arg;
6069 struct nfs41_reclaim_complete_res res;
6070};
6071
6072static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6073{
6074 struct nfs4_reclaim_complete_data *calldata = data;
6075
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006076 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006077 if (nfs41_setup_sequence(calldata->clp->cl_session,
6078 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006079 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006080 return;
6081
6082 rpc_call_start(task);
6083}
6084
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006085static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6086{
6087 switch(task->tk_status) {
6088 case 0:
6089 case -NFS4ERR_COMPLETE_ALREADY:
6090 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6091 break;
6092 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006093 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006094 /* fall through */
6095 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006096 return -EAGAIN;
6097 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006098 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006099 }
6100 return 0;
6101}
6102
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006103static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6104{
6105 struct nfs4_reclaim_complete_data *calldata = data;
6106 struct nfs_client *clp = calldata->clp;
6107 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6108
6109 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006110 if (!nfs41_sequence_done(task, res))
6111 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006112
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006113 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6114 rpc_restart_call_prepare(task);
6115 return;
6116 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006117 dprintk("<-- %s\n", __func__);
6118}
6119
6120static void nfs4_free_reclaim_complete_data(void *data)
6121{
6122 struct nfs4_reclaim_complete_data *calldata = data;
6123
6124 kfree(calldata);
6125}
6126
6127static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6128 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6129 .rpc_call_done = nfs4_reclaim_complete_done,
6130 .rpc_release = nfs4_free_reclaim_complete_data,
6131};
6132
6133/*
6134 * Issue a global reclaim complete.
6135 */
6136static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6137{
6138 struct nfs4_reclaim_complete_data *calldata;
6139 struct rpc_task *task;
6140 struct rpc_message msg = {
6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6142 };
6143 struct rpc_task_setup task_setup_data = {
6144 .rpc_client = clp->cl_rpcclient,
6145 .rpc_message = &msg,
6146 .callback_ops = &nfs4_reclaim_complete_call_ops,
6147 .flags = RPC_TASK_ASYNC,
6148 };
6149 int status = -ENOMEM;
6150
6151 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006152 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006153 if (calldata == NULL)
6154 goto out;
6155 calldata->clp = clp;
6156 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006157
Trond Myklebust9d12b212012-01-17 22:04:25 -05006158 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006159 msg.rpc_argp = &calldata->arg;
6160 msg.rpc_resp = &calldata->res;
6161 task_setup_data.callback_data = calldata;
6162 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006163 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006164 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006165 goto out;
6166 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006167 status = nfs4_wait_for_completion_rpc_task(task);
6168 if (status == 0)
6169 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006170 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006171 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006172out:
6173 dprintk("<-- %s status=%d\n", __func__, status);
6174 return status;
6175}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006176
6177static void
6178nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6179{
6180 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006181 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006182
6183 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006184 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6185 * right now covering the LAYOUTGET we are about to send.
6186 * However, that is not so catastrophic, and there seems
6187 * to be no way to prevent it completely.
6188 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006189 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006190 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006191 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006192 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6193 NFS_I(lgp->args.inode)->layout,
6194 lgp->args.ctx->state)) {
6195 rpc_exit(task, NFS4_OK);
6196 return;
6197 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006198 rpc_call_start(task);
6199}
6200
6201static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6202{
6203 struct nfs4_layoutget *lgp = calldata;
6204 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6205
6206 dprintk("--> %s\n", __func__);
6207
6208 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6209 return;
6210
6211 switch (task->tk_status) {
6212 case 0:
6213 break;
6214 case -NFS4ERR_LAYOUTTRYLATER:
6215 case -NFS4ERR_RECALLCONFLICT:
6216 task->tk_status = -NFS4ERR_DELAY;
6217 /* Fall through */
6218 default:
6219 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6220 rpc_restart_call_prepare(task);
6221 return;
6222 }
6223 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006224 dprintk("<-- %s\n", __func__);
6225}
6226
Idan Kedar85541162012-08-02 11:47:10 +03006227static size_t max_response_pages(struct nfs_server *server)
6228{
6229 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6230 return nfs_page_array_len(0, max_resp_sz);
6231}
6232
6233static void nfs4_free_pages(struct page **pages, size_t size)
6234{
6235 int i;
6236
6237 if (!pages)
6238 return;
6239
6240 for (i = 0; i < size; i++) {
6241 if (!pages[i])
6242 break;
6243 __free_page(pages[i]);
6244 }
6245 kfree(pages);
6246}
6247
6248static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6249{
6250 struct page **pages;
6251 int i;
6252
6253 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6254 if (!pages) {
6255 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6256 return NULL;
6257 }
6258
6259 for (i = 0; i < size; i++) {
6260 pages[i] = alloc_page(gfp_flags);
6261 if (!pages[i]) {
6262 dprintk("%s: failed to allocate page\n", __func__);
6263 nfs4_free_pages(pages, size);
6264 return NULL;
6265 }
6266 }
6267
6268 return pages;
6269}
6270
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006271static void nfs4_layoutget_release(void *calldata)
6272{
6273 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006274 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6275 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006276
6277 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006278 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006279 put_nfs_open_context(lgp->args.ctx);
6280 kfree(calldata);
6281 dprintk("<-- %s\n", __func__);
6282}
6283
6284static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6285 .rpc_call_prepare = nfs4_layoutget_prepare,
6286 .rpc_call_done = nfs4_layoutget_done,
6287 .rpc_release = nfs4_layoutget_release,
6288};
6289
Idan Kedar21d1f582012-08-02 11:47:11 +03006290void nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006291{
6292 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006293 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006294 struct rpc_task *task;
6295 struct rpc_message msg = {
6296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6297 .rpc_argp = &lgp->args,
6298 .rpc_resp = &lgp->res,
6299 };
6300 struct rpc_task_setup task_setup_data = {
6301 .rpc_client = server->client,
6302 .rpc_message = &msg,
6303 .callback_ops = &nfs4_layoutget_call_ops,
6304 .callback_data = lgp,
6305 .flags = RPC_TASK_ASYNC,
6306 };
6307 int status = 0;
6308
6309 dprintk("--> %s\n", __func__);
6310
Idan Kedar85541162012-08-02 11:47:10 +03006311 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6312 if (!lgp->args.layout.pages) {
6313 nfs4_layoutget_release(lgp);
Idan Kedar21d1f582012-08-02 11:47:11 +03006314 return;
Idan Kedar85541162012-08-02 11:47:10 +03006315 }
6316 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6317
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006318 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006319 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006320 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006321 task = rpc_run_task(&task_setup_data);
6322 if (IS_ERR(task))
Idan Kedar21d1f582012-08-02 11:47:11 +03006323 return;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006324 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006325 if (status == 0)
6326 status = task->tk_status;
6327 if (status == 0)
6328 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006329 rpc_put_task(task);
6330 dprintk("<-- %s status=%d\n", __func__, status);
Idan Kedar21d1f582012-08-02 11:47:11 +03006331 return;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006332}
6333
Benny Halevycbe82602011-05-22 19:52:37 +03006334static void
6335nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6336{
6337 struct nfs4_layoutreturn *lrp = calldata;
6338
6339 dprintk("--> %s\n", __func__);
6340 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006341 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006342 return;
6343 rpc_call_start(task);
6344}
6345
6346static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6347{
6348 struct nfs4_layoutreturn *lrp = calldata;
6349 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006350 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006351
6352 dprintk("--> %s\n", __func__);
6353
6354 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6355 return;
6356
6357 server = NFS_SERVER(lrp->args.inode);
6358 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006359 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006360 return;
6361 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006362 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust47fbf7972012-08-08 16:03:13 -04006363 if (task->tk_status == 0 && lrp->res.lrs_present)
6364 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006365 lo->plh_block_lgets--;
6366 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006367 dprintk("<-- %s\n", __func__);
6368}
6369
6370static void nfs4_layoutreturn_release(void *calldata)
6371{
6372 struct nfs4_layoutreturn *lrp = calldata;
6373
6374 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006375 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006376 kfree(calldata);
6377 dprintk("<-- %s\n", __func__);
6378}
6379
6380static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6381 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6382 .rpc_call_done = nfs4_layoutreturn_done,
6383 .rpc_release = nfs4_layoutreturn_release,
6384};
6385
6386int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6387{
6388 struct rpc_task *task;
6389 struct rpc_message msg = {
6390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6391 .rpc_argp = &lrp->args,
6392 .rpc_resp = &lrp->res,
6393 };
6394 struct rpc_task_setup task_setup_data = {
6395 .rpc_client = lrp->clp->cl_rpcclient,
6396 .rpc_message = &msg,
6397 .callback_ops = &nfs4_layoutreturn_call_ops,
6398 .callback_data = lrp,
6399 };
6400 int status;
6401
6402 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006403 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006404 task = rpc_run_task(&task_setup_data);
6405 if (IS_ERR(task))
6406 return PTR_ERR(task);
6407 status = task->tk_status;
6408 dprintk("<-- %s status=%d\n", __func__, status);
6409 rpc_put_task(task);
6410 return status;
6411}
6412
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006413/*
6414 * Retrieve the list of Data Server devices from the MDS.
6415 */
6416static int _nfs4_getdevicelist(struct nfs_server *server,
6417 const struct nfs_fh *fh,
6418 struct pnfs_devicelist *devlist)
6419{
6420 struct nfs4_getdevicelist_args args = {
6421 .fh = fh,
6422 .layoutclass = server->pnfs_curr_ld->id,
6423 };
6424 struct nfs4_getdevicelist_res res = {
6425 .devlist = devlist,
6426 };
6427 struct rpc_message msg = {
6428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6429 .rpc_argp = &args,
6430 .rpc_resp = &res,
6431 };
6432 int status;
6433
6434 dprintk("--> %s\n", __func__);
6435 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6436 &res.seq_res, 0);
6437 dprintk("<-- %s status=%d\n", __func__, status);
6438 return status;
6439}
6440
6441int nfs4_proc_getdevicelist(struct nfs_server *server,
6442 const struct nfs_fh *fh,
6443 struct pnfs_devicelist *devlist)
6444{
6445 struct nfs4_exception exception = { };
6446 int err;
6447
6448 do {
6449 err = nfs4_handle_exception(server,
6450 _nfs4_getdevicelist(server, fh, devlist),
6451 &exception);
6452 } while (exception.retry);
6453
6454 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6455 err, devlist->num_devs);
6456
6457 return err;
6458}
6459EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6460
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006461static int
6462_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6463{
6464 struct nfs4_getdeviceinfo_args args = {
6465 .pdev = pdev,
6466 };
6467 struct nfs4_getdeviceinfo_res res = {
6468 .pdev = pdev,
6469 };
6470 struct rpc_message msg = {
6471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6472 .rpc_argp = &args,
6473 .rpc_resp = &res,
6474 };
6475 int status;
6476
6477 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006478 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006479 dprintk("<-- %s status=%d\n", __func__, status);
6480
6481 return status;
6482}
6483
6484int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6485{
6486 struct nfs4_exception exception = { };
6487 int err;
6488
6489 do {
6490 err = nfs4_handle_exception(server,
6491 _nfs4_proc_getdeviceinfo(server, pdev),
6492 &exception);
6493 } while (exception.retry);
6494 return err;
6495}
6496EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6497
Andy Adamson863a3c62011-03-23 13:27:54 +00006498static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6499{
6500 struct nfs4_layoutcommit_data *data = calldata;
6501 struct nfs_server *server = NFS_SERVER(data->args.inode);
6502
6503 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006504 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006505 return;
6506 rpc_call_start(task);
6507}
6508
6509static void
6510nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6511{
6512 struct nfs4_layoutcommit_data *data = calldata;
6513 struct nfs_server *server = NFS_SERVER(data->args.inode);
6514
6515 if (!nfs4_sequence_done(task, &data->res.seq_res))
6516 return;
6517
6518 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006519 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6520 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6521 case -NFS4ERR_BADLAYOUT: /* no layout */
6522 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006523 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006524 break;
6525 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006526 nfs_post_op_update_inode_force_wcc(data->args.inode,
6527 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006528 break;
6529 default:
6530 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6531 rpc_restart_call_prepare(task);
6532 return;
6533 }
6534 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006535}
6536
6537static void nfs4_layoutcommit_release(void *calldata)
6538{
6539 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006540 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006541 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006542
Andy Adamsondb29c082011-07-30 20:52:38 -04006543 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006544 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006545 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6546 list_del_init(&lseg->pls_lc_list);
6547 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6548 &lseg->pls_flags))
6549 put_lseg(lseg);
6550 }
Peng Tao92407e72011-10-23 20:21:17 -07006551
6552 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6553 smp_mb__after_clear_bit();
6554 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6555
Andy Adamson863a3c62011-03-23 13:27:54 +00006556 put_rpccred(data->cred);
6557 kfree(data);
6558}
6559
6560static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6561 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6562 .rpc_call_done = nfs4_layoutcommit_done,
6563 .rpc_release = nfs4_layoutcommit_release,
6564};
6565
6566int
Andy Adamsonef311532011-03-12 02:58:10 -05006567nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006568{
6569 struct rpc_message msg = {
6570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6571 .rpc_argp = &data->args,
6572 .rpc_resp = &data->res,
6573 .rpc_cred = data->cred,
6574 };
6575 struct rpc_task_setup task_setup_data = {
6576 .task = &data->task,
6577 .rpc_client = NFS_CLIENT(data->args.inode),
6578 .rpc_message = &msg,
6579 .callback_ops = &nfs4_layoutcommit_ops,
6580 .callback_data = data,
6581 .flags = RPC_TASK_ASYNC,
6582 };
6583 struct rpc_task *task;
6584 int status = 0;
6585
6586 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6587 "lbw: %llu inode %lu\n",
6588 data->task.tk_pid, sync,
6589 data->args.lastbytewritten,
6590 data->args.inode->i_ino);
6591
Trond Myklebust9d12b212012-01-17 22:04:25 -05006592 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006593 task = rpc_run_task(&task_setup_data);
6594 if (IS_ERR(task))
6595 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006596 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006597 goto out;
6598 status = nfs4_wait_for_completion_rpc_task(task);
6599 if (status != 0)
6600 goto out;
6601 status = task->tk_status;
6602out:
6603 dprintk("%s: status %d\n", __func__, status);
6604 rpc_put_task(task);
6605 return status;
6606}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006607
6608static int
6609_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6610 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6611{
6612 struct nfs41_secinfo_no_name_args args = {
6613 .style = SECINFO_STYLE_CURRENT_FH,
6614 };
6615 struct nfs4_secinfo_res res = {
6616 .flavors = flavors,
6617 };
6618 struct rpc_message msg = {
6619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6620 .rpc_argp = &args,
6621 .rpc_resp = &res,
6622 };
6623 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6624}
6625
6626static int
6627nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6628 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6629{
6630 struct nfs4_exception exception = { };
6631 int err;
6632 do {
6633 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6634 switch (err) {
6635 case 0:
6636 case -NFS4ERR_WRONGSEC:
6637 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006638 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006639 default:
6640 err = nfs4_handle_exception(server, err, &exception);
6641 }
6642 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006643out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006644 return err;
6645}
6646
6647static int
6648nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6649 struct nfs_fsinfo *info)
6650{
6651 int err;
6652 struct page *page;
6653 rpc_authflavor_t flavor;
6654 struct nfs4_secinfo_flavors *flavors;
6655
6656 page = alloc_page(GFP_KERNEL);
6657 if (!page) {
6658 err = -ENOMEM;
6659 goto out;
6660 }
6661
6662 flavors = page_address(page);
6663 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6664
6665 /*
6666 * Fall back on "guess and check" method if
6667 * the server doesn't support SECINFO_NO_NAME
6668 */
6669 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6670 err = nfs4_find_root_sec(server, fhandle, info);
6671 goto out_freepage;
6672 }
6673 if (err)
6674 goto out_freepage;
6675
6676 flavor = nfs_find_best_sec(flavors);
6677 if (err == 0)
6678 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6679
6680out_freepage:
6681 put_page(page);
6682 if (err == -EACCES)
6683 return -EPERM;
6684out:
6685 return err;
6686}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006687
6688static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006689{
6690 int status;
6691 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006692 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006693 };
6694 struct nfs41_test_stateid_res res;
6695 struct rpc_message msg = {
6696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6697 .rpc_argp = &args,
6698 .rpc_resp = &res,
6699 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006700
Chuck Lever38527b12012-07-11 16:30:23 -04006701 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006702 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006703 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006704 if (status != NFS_OK) {
6705 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006706 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006707 }
6708 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006709 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006710}
6711
Chuck Lever38527b12012-07-11 16:30:23 -04006712/**
6713 * nfs41_test_stateid - perform a TEST_STATEID operation
6714 *
6715 * @server: server / transport on which to perform the operation
6716 * @stateid: state ID to test
6717 *
6718 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6719 * Otherwise a negative NFS4ERR value is returned if the operation
6720 * failed or the state ID is not currently valid.
6721 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006722static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006723{
6724 struct nfs4_exception exception = { };
6725 int err;
6726 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006727 err = _nfs41_test_stateid(server, stateid);
6728 if (err != -NFS4ERR_DELAY)
6729 break;
6730 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006731 } while (exception.retry);
6732 return err;
6733}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006734
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006735static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006736{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006737 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006738 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006739 };
6740 struct nfs41_free_stateid_res res;
6741 struct rpc_message msg = {
6742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6743 .rpc_argp = &args,
6744 .rpc_resp = &res,
6745 };
Chuck Lever38527b12012-07-11 16:30:23 -04006746 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006747
Chuck Lever38527b12012-07-11 16:30:23 -04006748 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006749 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006750 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006751 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006752 dprintk("NFS reply free_stateid: %d\n", status);
6753 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006754}
6755
Chuck Lever38527b12012-07-11 16:30:23 -04006756/**
6757 * nfs41_free_stateid - perform a FREE_STATEID operation
6758 *
6759 * @server: server / transport on which to perform the operation
6760 * @stateid: state ID to release
6761 *
6762 * Returns NFS_OK if the server freed "stateid". Otherwise a
6763 * negative NFS4ERR value is returned.
6764 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006765static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006766{
6767 struct nfs4_exception exception = { };
6768 int err;
6769 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006770 err = _nfs4_free_stateid(server, stateid);
6771 if (err != -NFS4ERR_DELAY)
6772 break;
6773 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006774 } while (exception.retry);
6775 return err;
6776}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006777
6778static bool nfs41_match_stateid(const nfs4_stateid *s1,
6779 const nfs4_stateid *s2)
6780{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006781 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006782 return false;
6783
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006784 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006785 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006786 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006787 return true;
6788
6789 return false;
6790}
6791
Andy Adamson557134a2009-04-01 09:21:53 -04006792#endif /* CONFIG_NFS_V4_1 */
6793
Trond Myklebust36281ca2012-03-04 18:13:56 -05006794static bool nfs4_match_stateid(const nfs4_stateid *s1,
6795 const nfs4_stateid *s2)
6796{
Trond Myklebustf597c532012-03-04 18:13:56 -05006797 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006798}
6799
6800
Trond Myklebust17280172012-03-11 13:11:00 -04006801static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006802 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006803 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 .recover_open = nfs4_open_reclaim,
6805 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006806 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006807 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808};
6809
Andy Adamson591d71c2009-04-01 09:22:47 -04006810#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006811static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006812 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6813 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6814 .recover_open = nfs4_open_reclaim,
6815 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006816 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006817 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006818 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006819};
6820#endif /* CONFIG_NFS_V4_1 */
6821
Trond Myklebust17280172012-03-11 13:11:00 -04006822static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006823 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006824 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 .recover_open = nfs4_open_expired,
6826 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006827 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006828 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829};
6830
Andy Adamson591d71c2009-04-01 09:22:47 -04006831#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006832static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006833 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6834 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006835 .recover_open = nfs41_open_expired,
6836 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006837 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006838 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006839};
6840#endif /* CONFIG_NFS_V4_1 */
6841
Trond Myklebust17280172012-03-11 13:11:00 -04006842static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006843 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006844 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006845 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006846};
6847
6848#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006849static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006850 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006851 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006852 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006853};
6854#endif
6855
Trond Myklebust97dc1352010-06-16 09:52:26 -04006856static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6857 .minor_version = 0,
6858 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006859 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006860 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006861 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6862 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6863 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006864};
6865
6866#if defined(CONFIG_NFS_V4_1)
6867static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6868 .minor_version = 1,
6869 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006870 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006871 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006872 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6873 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6874 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006875};
6876#endif
6877
Trond Myklebust97dc1352010-06-16 09:52:26 -04006878const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6879 [0] = &nfs_v4_0_minor_ops,
6880#if defined(CONFIG_NFS_V4_1)
6881 [1] = &nfs_v4_1_minor_ops,
6882#endif
6883};
6884
Bryan Schumaker73a79702012-07-16 16:39:12 -04006885const struct inode_operations nfs4_dir_inode_operations = {
6886 .create = nfs_create,
6887 .lookup = nfs_lookup,
6888 .atomic_open = nfs_atomic_open,
6889 .link = nfs_link,
6890 .unlink = nfs_unlink,
6891 .symlink = nfs_symlink,
6892 .mkdir = nfs_mkdir,
6893 .rmdir = nfs_rmdir,
6894 .mknod = nfs_mknod,
6895 .rename = nfs_rename,
6896 .permission = nfs_permission,
6897 .getattr = nfs_getattr,
6898 .setattr = nfs_setattr,
6899 .getxattr = generic_getxattr,
6900 .setxattr = generic_setxattr,
6901 .listxattr = generic_listxattr,
6902 .removexattr = generic_removexattr,
6903};
6904
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006905static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006906 .permission = nfs_permission,
6907 .getattr = nfs_getattr,
6908 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006909 .getxattr = generic_getxattr,
6910 .setxattr = generic_setxattr,
6911 .listxattr = generic_listxattr,
6912 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006913};
6914
David Howells509de812006-08-22 20:06:11 -04006915const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 .version = 4, /* protocol version */
6917 .dentry_ops = &nfs4_dentry_operations,
6918 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006919 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006920 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006922 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006923 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 .getattr = nfs4_proc_getattr,
6925 .setattr = nfs4_proc_setattr,
6926 .lookup = nfs4_proc_lookup,
6927 .access = nfs4_proc_access,
6928 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 .create = nfs4_proc_create,
6930 .remove = nfs4_proc_remove,
6931 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006932 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 .unlink_done = nfs4_proc_unlink_done,
6934 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006935 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006936 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006937 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 .link = nfs4_proc_link,
6939 .symlink = nfs4_proc_symlink,
6940 .mkdir = nfs4_proc_mkdir,
6941 .rmdir = nfs4_proc_remove,
6942 .readdir = nfs4_proc_readdir,
6943 .mknod = nfs4_proc_mknod,
6944 .statfs = nfs4_proc_statfs,
6945 .fsinfo = nfs4_proc_fsinfo,
6946 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006947 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 .decode_dirent = nfs4_decode_dirent,
6949 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006950 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006951 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006952 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006954 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006955 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006956 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006958 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006959 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006961 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006962 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006963 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006964 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006965 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006966 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006967 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006968 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006969 .create_server = nfs4_create_server,
6970 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971};
6972
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006973static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6974 .prefix = XATTR_NAME_NFSV4_ACL,
6975 .list = nfs4_xattr_list_nfs4_acl,
6976 .get = nfs4_xattr_get_nfs4_acl,
6977 .set = nfs4_xattr_set_nfs4_acl,
6978};
6979
6980const struct xattr_handler *nfs4_xattr_handlers[] = {
6981 &nfs4_xattr_nfs4_acl_handler,
6982 NULL
6983};
6984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985/*
6986 * Local variables:
6987 * c-basic-offset: 8
6988 * End:
6989 */