blob: 21cfac7c2ff80adba52b7d05a3262e006fb8a9ce [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;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400107 case -NFS4ERR_ACCESS:
108 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700111 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/*
118 * This is our standard bitmap for GETATTR requests.
119 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400120const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 FATTR4_WORD0_TYPE
122 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_SIZE
124 | FATTR4_WORD0_FSID
125 | FATTR4_WORD0_FILEID,
126 FATTR4_WORD1_MODE
127 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
135};
136
Trond Myklebust1549210f2012-06-05 09:16:47 -0400137static const u32 nfs4_pnfs_open_bitmap[3] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
153};
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155const u32 nfs4_statfs_bitmap[2] = {
156 FATTR4_WORD0_FILES_AVAIL
157 | FATTR4_WORD0_FILES_FREE
158 | FATTR4_WORD0_FILES_TOTAL,
159 FATTR4_WORD1_SPACE_AVAIL
160 | FATTR4_WORD1_SPACE_FREE
161 | FATTR4_WORD1_SPACE_TOTAL
162};
163
Trond Myklebust4ce79712005-06-22 17:16:21 +0000164const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 FATTR4_WORD0_MAXLINK
166 | FATTR4_WORD0_MAXNAME,
167 0
168};
169
Fred Isamandae100c2011-07-30 20:52:37 -0400170const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 | FATTR4_WORD0_MAXREAD
172 | FATTR4_WORD0_MAXWRITE
173 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700174 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400175 | FATTR4_WORD1_FS_LAYOUT_TYPES,
176 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Manoj Naik830b8e32006-06-09 09:34:25 -0400179const u32 nfs4_fs_locations_bitmap[2] = {
180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID
185 | FATTR4_WORD0_FS_LOCATIONS,
186 FATTR4_WORD1_MODE
187 | FATTR4_WORD1_NUMLINKS
188 | FATTR4_WORD1_OWNER
189 | FATTR4_WORD1_OWNER_GROUP
190 | FATTR4_WORD1_RAWDEV
191 | FATTR4_WORD1_SPACE_USED
192 | FATTR4_WORD1_TIME_ACCESS
193 | FATTR4_WORD1_TIME_METADATA
194 | FATTR4_WORD1_TIME_MODIFY
195 | FATTR4_WORD1_MOUNTED_ON_FILEID
196};
197
Al Virobc4785c2006-10-19 23:28:51 -0700198static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct nfs4_readdir_arg *readdir)
200{
Al Viro0dbb4c62006-10-19 23:28:49 -0700201 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 BUG_ON(readdir->count < 80);
204 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000205 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
207 return;
208 }
209
210 readdir->cookie = 0;
211 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
212 if (cookie == 2)
213 return;
214
215 /*
216 * NFSv4 servers do not return entries for '.' and '..'
217 * Therefore, we fake these entries here. We let '.'
218 * have cookie 0 and '..' have cookie 1. Note that
219 * when talking to the server, we always send cookie 0
220 * instead of 1 or 2.
221 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800222 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (cookie == 0) {
225 *p++ = xdr_one; /* next */
226 *p++ = xdr_zero; /* cookie, first word */
227 *p++ = xdr_one; /* cookie, second word */
228 *p++ = xdr_one; /* entry len */
229 memcpy(p, ".\0\0\0", 4); /* entry */
230 p++;
231 *p++ = xdr_one; /* bitmap length */
232 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
233 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400234 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 *p++ = xdr_one; /* next */
238 *p++ = xdr_zero; /* cookie, first word */
239 *p++ = xdr_two; /* cookie, second word */
240 *p++ = xdr_two; /* entry len */
241 memcpy(p, "..\0\0", 4); /* entry */
242 p++;
243 *p++ = xdr_one; /* bitmap length */
244 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
245 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400246 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 readdir->pgbase = (char *)p - (char *)start;
249 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800250 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Trond Myklebust65de8722008-12-23 15:21:44 -0500253static int nfs4_wait_clnt_recover(struct nfs_client *clp)
254{
255 int res;
256
257 might_sleep();
258
Trond Myklebuste005e802008-12-23 15:21:48 -0500259 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400260 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400261 if (res)
262 return res;
263
264 if (clp->cl_cons_state < 0)
265 return clp->cl_cons_state;
266 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500267}
268
269static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
270{
271 int res = 0;
272
273 might_sleep();
274
275 if (*timeout <= 0)
276 *timeout = NFS4_POLL_RETRY_MIN;
277 if (*timeout > NFS4_POLL_RETRY_MAX)
278 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100279 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500280 if (fatal_signal_pending(current))
281 res = -ERESTARTSYS;
282 *timeout <<= 1;
283 return res;
284}
285
286/* This is the error handling routine for processes that are allowed
287 * to sleep.
288 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800289static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500290{
291 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500292 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500293 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 int ret = errorcode;
295
296 exception->retry = 0;
297 switch(errorcode) {
298 case 0:
299 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500300 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400301 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400302 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500303 exception->retry = 1;
304 return 0;
305 }
306 if (state == NULL)
307 break;
308 nfs4_schedule_stateid_recovery(server, state);
309 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500310 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500311 case -NFS4ERR_ADMIN_REVOKED:
312 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500313 if (state == NULL)
314 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400315 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500316 nfs4_schedule_stateid_recovery(server, state);
317 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400318 case -NFS4ERR_EXPIRED:
319 if (state != NULL)
320 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500321 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500322 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500323 nfs4_schedule_lease_recovery(clp);
324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400326 case -NFS4ERR_BADSESSION:
327 case -NFS4ERR_BADSLOT:
328 case -NFS4ERR_BAD_HIGH_SLOT:
329 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
330 case -NFS4ERR_DEADSESSION:
331 case -NFS4ERR_SEQ_FALSE_RETRY:
332 case -NFS4ERR_SEQ_MISORDERED:
333 dprintk("%s ERROR: %d Reset session\n", __func__,
334 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400335 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400336 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500337 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500338#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500340 if (exception->timeout > HZ) {
341 /* We have retried a decent amount, time to
342 * fail
343 */
344 ret = -EBUSY;
345 break;
346 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500347 case -NFS4ERR_GRACE:
348 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500349 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500350 ret = nfs4_delay(server->client, &exception->timeout);
351 if (ret != 0)
352 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400353 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500354 case -NFS4ERR_OLD_STATEID:
355 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800356 break;
357 case -NFS4ERR_BADOWNER:
358 /* The following works around a Linux server bug! */
359 case -NFS4ERR_BADNAME:
360 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
361 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
362 exception->retry = 1;
363 printk(KERN_WARNING "NFS: v4 server %s "
364 "does not accept raw "
365 "uid/gids. "
366 "Reenabling the idmapper.\n",
367 server->nfs_client->cl_hostname);
368 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500369 }
370 /* We failed to handle the error */
371 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500372wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500373 ret = nfs4_wait_clnt_recover(clp);
374 if (ret == 0)
375 exception->retry = 1;
376 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500377}
378
379
Trond Myklebust452e9352010-07-31 14:29:06 -0400380static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_lock(&clp->cl_lock);
383 if (time_before(clp->cl_last_renewal,timestamp))
384 clp->cl_last_renewal = timestamp;
385 spin_unlock(&clp->cl_lock);
386}
387
Trond Myklebust452e9352010-07-31 14:29:06 -0400388static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
389{
390 do_renew_lease(server->nfs_client, timestamp);
391}
392
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400393#if defined(CONFIG_NFS_V4_1)
394
Benny Halevy510b8172009-04-01 09:22:14 -0400395/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400396 * nfs4_free_slot - free a slot and efficiently update slot table.
397 *
398 * freeing a slot is trivially done by clearing its respective bit
399 * in the bitmap.
400 * If the freed slotid equals highest_used_slotid we want to update it
401 * so that the server would be able to size down the slot table if needed,
402 * otherwise we know that the highest_used_slotid is still in use.
403 * When updating highest_used_slotid there may be "holes" in the bitmap
404 * so we need to scan down from highest_used_slotid to 0 looking for the now
405 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500406 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 *
408 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400409 */
410static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500411nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400412{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500413 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400414 /* clear used bit in bitmap */
415 __clear_bit(slotid, tbl->used_slots);
416
417 /* update highest_used_slotid when it is freed */
418 if (slotid == tbl->highest_used_slotid) {
419 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500420 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400421 tbl->highest_used_slotid = slotid;
422 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500423 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400424 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500425 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
426 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427}
428
Trond Myklebust961a8282012-01-17 22:57:37 -0500429bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
430{
431 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
432 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400433}
434
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435/*
Andy Adamson42acd022011-01-06 02:04:34 +0000436 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437 */
Andy Adamson42acd022011-01-06 02:04:34 +0000438static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800439{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400440 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500441 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
442 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800443 return;
444 }
445
Trond Myklebust45d43c22012-02-06 19:38:51 -0500446 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800447 return;
448
Andy Adamson42acd022011-01-06 02:04:34 +0000449 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
450 complete(&ses->fc_slot_table.complete);
451}
452
453/*
454 * Signal state manager thread if session back channel is drained
455 */
456void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
457{
458 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500459 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000460 return;
461 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
462 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800463}
464
Trond Myklebustd185a332010-06-16 09:52:25 -0400465static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400466{
467 struct nfs4_slot_table *tbl;
468
Trond Myklebustd185a332010-06-16 09:52:25 -0400469 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400470 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400471 /* just wake up the next guy waiting since
472 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500473 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500474 return;
Andy Adamson13615872009-04-01 09:22:17 -0400475 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500476
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500477 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500478 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000479 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500480 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400481 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400482}
483
Trond Myklebust14516c32010-07-31 14:29:06 -0400484static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485{
486 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400488
489 /*
490 * sr_status remains 1 if an RPC level error occurred. The server
491 * may or may not have processed the sequence operation..
492 * Proceed as if the server received and processed the sequence
493 * operation.
494 */
495 if (res->sr_status == 1)
496 res->sr_status = NFS_OK;
497
Bryan Schumaker468f8612011-04-18 15:57:32 -0400498 /* don't increment the sequence number if the task wasn't sent */
499 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400500 goto out;
501
Andy Adamson691daf32009-12-04 15:55:39 -0500502 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400503 switch (res->sr_status) {
504 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400505 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400506 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400507 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400509 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500510 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500511 if (res->sr_status_flags != 0)
512 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 break;
514 case -NFS4ERR_DELAY:
515 /* The server detected a resend of the RPC call and
516 * returned NFS4ERR_DELAY as per Section 2.10.6.2
517 * of RFC5661.
518 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200519 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400520 __func__,
521 res->sr_slot - res->sr_session->fc_slot_table.slots,
522 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 goto out_retry;
524 default:
525 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400526 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527 }
528out:
529 /* The session may be reset by one of the error handlers. */
530 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400531 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400532 return 1;
533out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400534 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400535 goto out;
536 rpc_delay(task, NFS4_POLL_RETRY_MAX);
537 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400538}
539
Trond Myklebust14516c32010-07-31 14:29:06 -0400540static int nfs4_sequence_done(struct rpc_task *task,
541 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400542{
Trond Myklebust14516c32010-07-31 14:29:06 -0400543 if (res->sr_session == NULL)
544 return 1;
545 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400546}
547
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400548/*
Benny Halevy510b8172009-04-01 09:22:14 -0400549 * nfs4_find_slot - efficiently look for a free slot
550 *
551 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
552 * If found, we mark the slot as used, update the highest_used_slotid,
553 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500554 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 *
556 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400557 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500558static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800559nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400560{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500561 u32 slotid;
562 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400563
Trond Myklebust45d43c22012-02-06 19:38:51 -0500564 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400565 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
566 tbl->max_slots);
567 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
568 if (slotid >= tbl->max_slots)
569 goto out;
570 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500571 if (slotid > tbl->highest_used_slotid ||
572 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400573 tbl->highest_used_slotid = slotid;
574 ret_id = slotid;
575out:
576 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
577 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400578 return ret_id;
579}
580
Trond Myklebust9d12b212012-01-17 22:04:25 -0500581static void nfs41_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
583{
584 args->sa_session = NULL;
585 args->sa_cache_this = 0;
586 if (cache_reply)
587 args->sa_cache_this = 1;
588 res->sr_session = NULL;
589 res->sr_slot = NULL;
590}
591
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000592int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *args,
594 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595 struct rpc_task *task)
596{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400597 struct nfs4_slot *slot;
598 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500599 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600
601 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400603 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 return 0;
605
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606 tbl = &session->fc_slot_table;
607
608 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400609 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800610 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500611 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500612 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400613 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500614 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500615 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400616 }
617
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800618 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
619 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
620 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
621 spin_unlock(&tbl->slot_tbl_lock);
622 dprintk("%s enforce FIFO order\n", __func__);
623 return -EAGAIN;
624 }
625
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800626 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500627 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400628 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
629 spin_unlock(&tbl->slot_tbl_lock);
630 dprintk("<-- %s: no free slots\n", __func__);
631 return -EAGAIN;
632 }
633 spin_unlock(&tbl->slot_tbl_lock);
634
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800635 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400636 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400637 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400638 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400639
640 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
641
Benny Halevy99fe60d2009-04-01 09:22:29 -0400642 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400643 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400645 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400646 /*
647 * sr_status is only set in decode_sequence, and so will remain
648 * set to 1 if an rpc level failure occurs.
649 */
650 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651 return 0;
652}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000653EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654
Trond Myklebust035168a2010-06-16 09:52:26 -0400655int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 struct nfs4_sequence_args *args,
657 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 struct rpc_task *task)
659{
Trond Myklebust035168a2010-06-16 09:52:26 -0400660 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 int ret = 0;
662
Trond Myklebust9d12b212012-01-17 22:04:25 -0500663 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400665
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200666 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400667 __func__, session->clp, session, res->sr_slot ?
668 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400669
Trond Myklebust9d12b212012-01-17 22:04:25 -0500670 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671out:
672 dprintk("<-- %s status=%d\n", __func__, ret);
673 return ret;
674}
675
676struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400677 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678 struct nfs4_sequence_args *seq_args;
679 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680};
681
682static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
683{
684 struct nfs41_call_sync_data *data = calldata;
685
Trond Myklebust035168a2010-06-16 09:52:26 -0400686 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
687
688 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500689 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 return;
691 rpc_call_start(task);
692}
693
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
695{
696 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
697 nfs41_call_sync_prepare(task, calldata);
698}
699
Andy Adamson69ab40c2009-04-01 09:22:19 -0400700static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
701{
702 struct nfs41_call_sync_data *data = calldata;
703
Trond Myklebust14516c32010-07-31 14:29:06 -0400704 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400705}
706
Trond Myklebust17280172012-03-11 13:11:00 -0400707static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400709 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710};
711
Trond Myklebust17280172012-03-11 13:11:00 -0400712static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800713 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
714 .rpc_call_done = nfs41_call_sync_done,
715};
716
Bryan Schumaker7c513052011-03-24 17:12:24 +0000717static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
718 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
721 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800722 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723{
724 int ret;
725 struct rpc_task *task;
726 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400727 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 .seq_args = args,
729 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730 };
731 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 .rpc_message = msg,
734 .callback_ops = &nfs41_call_sync_ops,
735 .callback_data = &data
736 };
737
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800738 if (privileged)
739 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 task = rpc_run_task(&task_setup);
741 if (IS_ERR(task))
742 ret = PTR_ERR(task);
743 else {
744 ret = task->tk_status;
745 rpc_put_task(task);
746 }
747 return ret;
748}
749
Bryan Schumaker7c513052011-03-24 17:12:24 +0000750int _nfs4_call_sync_session(struct rpc_clnt *clnt,
751 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400752 struct rpc_message *msg,
753 struct nfs4_sequence_args *args,
754 struct nfs4_sequence_res *res,
755 int cache_reply)
756{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500757 nfs41_init_sequence(args, res, cache_reply);
758 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400759}
760
Trond Myklebustdf896452010-06-16 09:52:26 -0400761#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500762static inline
763void nfs41_init_sequence(struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res, int cache_reply)
765{
766}
767
Trond Myklebust14516c32010-07-31 14:29:06 -0400768static int nfs4_sequence_done(struct rpc_task *task,
769 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400770{
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400772}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400773#endif /* CONFIG_NFS_V4_1 */
774
Bryan Schumaker7c513052011-03-24 17:12:24 +0000775int _nfs4_call_sync(struct rpc_clnt *clnt,
776 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400777 struct rpc_message *msg,
778 struct nfs4_sequence_args *args,
779 struct nfs4_sequence_res *res,
780 int cache_reply)
781{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500782 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000783 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400784}
785
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000786static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000787int nfs4_call_sync(struct rpc_clnt *clnt,
788 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000789 struct rpc_message *msg,
790 struct nfs4_sequence_args *args,
791 struct nfs4_sequence_res *res,
792 int cache_reply)
793{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000794 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
795 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000796}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400797
Trond Myklebust38478b22006-05-25 01:40:57 -0400798static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Trond Myklebust38478b22006-05-25 01:40:57 -0400800 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Trond Myklebust38478b22006-05-25 01:40:57 -0400802 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400803 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700804 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400805 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700806 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 struct nfs_openargs o_arg;
813 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100814 struct nfs_open_confirmargs c_arg;
815 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500816 struct nfs4_string owner_name;
817 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400820 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100824 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400825 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100826 int rpc_status;
827 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828};
829
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400830
831static void nfs4_init_opendata_res(struct nfs4_opendata *p)
832{
833 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400834 p->o_res.seqid = p->o_arg.seqid;
835 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836 p->o_res.server = p->o_arg.server;
837 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500838 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400839}
840
Al Viro82a2c1b2011-06-22 18:30:55 -0400841static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400843 const struct iattr *attrs,
844 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845{
Al Viro82a2c1b2011-06-22 18:30:55 -0400846 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 struct inode *dir = parent->d_inode;
848 struct nfs_server *server = NFS_SERVER(dir);
849 struct nfs4_opendata *p;
850
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400851 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 if (p == NULL)
853 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400854 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 if (p->o_arg.seqid == NULL)
856 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 nfs_sb_active(dentry->d_sb);
858 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 p->dir = parent;
860 p->owner = sp;
861 atomic_inc(&sp->so_count);
862 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863 p->o_arg.open_flags = flags;
864 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700865 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
866 * will return permission denied for all bits until close */
867 if (!(flags & O_EXCL)) {
868 /* ask server to check for all possible rights as results
869 * are cached */
870 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
871 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
872 p->o_res.access_request = p->o_arg.access;
873 }
David Howells7539bba2006-08-22 20:06:09 -0400874 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400875 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
876 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400877 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100878 p->o_arg.server = server;
879 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400880 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500882 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500883 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400884
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 p->o_arg.u.attrs = &p->attrs;
886 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500887
888 verf[0] = jiffies;
889 verf[1] = current->pid;
890 memcpy(p->o_arg.u.verifier.data, verf,
891 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100892 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100893 p->c_arg.fh = &p->o_res.fh;
894 p->c_arg.stateid = &p->o_res.stateid;
895 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400896 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400897 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100898 return p;
899err_free:
900 kfree(p);
901err:
902 dput(parent);
903 return NULL;
904}
905
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400906static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100907{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400908 struct nfs4_opendata *p = container_of(kref,
909 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400910 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400911
912 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400913 if (p->state != NULL)
914 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400915 nfs4_put_state_owner(p->owner);
916 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400917 dput(p->dentry);
918 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500919 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400920 kfree(p);
921}
922
923static void nfs4_opendata_put(struct nfs4_opendata *p)
924{
925 if (p != NULL)
926 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100927}
928
Trond Myklebust06f814a2006-01-03 09:55:07 +0100929static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
930{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100931 int ret;
932
Trond Myklebust06f814a2006-01-03 09:55:07 +0100933 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100934 return ret;
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938{
939 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940
Trond Myklebust536e43d2012-01-17 22:04:26 -0500941 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 goto out;
943 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500945 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
946 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400947 break;
948 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500949 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
950 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400951 break;
952 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500953 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
954 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400955 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400957 return ret;
958}
959
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500962 if (delegation == NULL)
963 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500966 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400967 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500968 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 return 1;
970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100973{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100975 case FMODE_WRITE:
976 state->n_wronly++;
977 break;
978 case FMODE_READ:
979 state->n_rdonly++;
980 break;
981 case FMODE_READ|FMODE_WRITE:
982 state->n_rdwr++;
983 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100985}
986
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400988{
989 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500990 nfs4_stateid_copy(&state->stateid, stateid);
991 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400993 case FMODE_READ:
994 set_bit(NFS_O_RDONLY_STATE, &state->flags);
995 break;
996 case FMODE_WRITE:
997 set_bit(NFS_O_WRONLY_STATE, &state->flags);
998 break;
999 case FMODE_READ|FMODE_WRITE:
1000 set_bit(NFS_O_RDWR_STATE, &state->flags);
1001 }
1002}
1003
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001005{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001006 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001008 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009}
1010
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011static 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 -07001012{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 /*
1014 * Protect the call to nfs4_state_set_mode_locked and
1015 * serialise the stateid update
1016 */
1017 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001018 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001019 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001020 set_bit(NFS_DELEGATED_STATE, &state->flags);
1021 }
1022 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001024 write_sequnlock(&state->seqlock);
1025 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001027 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030static 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 -05001031{
1032 struct nfs_inode *nfsi = NFS_I(state->inode);
1033 struct nfs_delegation *deleg_cur;
1034 int ret = 0;
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001037
1038 rcu_read_lock();
1039 deleg_cur = rcu_dereference(nfsi->delegation);
1040 if (deleg_cur == NULL)
1041 goto no_delegation;
1042
1043 spin_lock(&deleg_cur->lock);
1044 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001046 goto no_delegation_unlock;
1047
1048 if (delegation == NULL)
1049 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001050 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001051 goto no_delegation_unlock;
1052
Trond Myklebustb7391f42008-12-23 15:21:52 -05001053 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001055 ret = 1;
1056no_delegation_unlock:
1057 spin_unlock(&deleg_cur->lock);
1058no_delegation:
1059 rcu_read_unlock();
1060
1061 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001063 ret = 1;
1064 }
1065
1066 return ret;
1067}
1068
1069
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071{
1072 struct nfs_delegation *delegation;
1073
1074 rcu_read_lock();
1075 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 rcu_read_unlock();
1078 return;
1079 }
1080 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001081 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082}
1083
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085{
1086 struct nfs4_state *state = opendata->state;
1087 struct nfs_inode *nfsi = NFS_I(state->inode);
1088 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001089 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001090 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001091 nfs4_stateid stateid;
1092 int ret = -EAGAIN;
1093
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001096 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001097 if (can_open_cached(state, fmode, open_mode)) {
1098 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 goto out_return_state;
1101 }
1102 spin_unlock(&state->owner->so_lock);
1103 }
Trond Myklebust34310432008-12-23 15:21:38 -05001104 rcu_read_lock();
1105 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001106 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001107 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001109 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001111 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001113 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001114 if (ret != 0)
1115 goto out;
1116 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001117
1118 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001120 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122out:
1123 return ERR_PTR(ret);
1124out_return_state:
1125 atomic_inc(&state->count);
1126 return state;
1127}
1128
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1130{
1131 struct inode *inode;
1132 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001133 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135
Trond Myklebustaac00a82007-07-05 19:02:21 -04001136 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001137 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001138 goto out;
1139 }
1140
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001141 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001143 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001146 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147 goto err;
1148 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149 state = nfs4_get_open_state(inode, data->owner);
1150 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001151 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001152 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001153 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001154 int delegation_flags = 0;
1155
Trond Myklebust003707c2007-07-05 18:07:55 -04001156 rcu_read_lock();
1157 delegation = rcu_dereference(NFS_I(inode)->delegation);
1158 if (delegation)
1159 delegation_flags = delegation->flags;
1160 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001161 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1162 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1163 "returning a delegation for "
1164 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001165 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001166 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001167 nfs_inode_set_delegation(state->inode,
1168 data->owner->so_cred,
1169 &data->o_res);
1170 else
1171 nfs_inode_reclaim_delegation(state->inode,
1172 data->owner->so_cred,
1173 &data->o_res);
1174 }
Trond Myklebust34310432008-12-23 15:21:38 -05001175
1176 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001178 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001179out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001180 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001181err_put_inode:
1182 iput(inode);
1183err:
1184 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001185}
1186
Trond Myklebust864472e2006-01-03 09:55:15 +01001187static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1188{
1189 struct nfs_inode *nfsi = NFS_I(state->inode);
1190 struct nfs_open_context *ctx;
1191
1192 spin_lock(&state->inode->i_lock);
1193 list_for_each_entry(ctx, &nfsi->open_files, list) {
1194 if (ctx->state != state)
1195 continue;
1196 get_nfs_open_context(ctx);
1197 spin_unlock(&state->inode->i_lock);
1198 return ctx;
1199 }
1200 spin_unlock(&state->inode->i_lock);
1201 return ERR_PTR(-ENOENT);
1202}
1203
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001204static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1205{
1206 struct nfs4_opendata *opendata;
1207
Al Viro3d4ff432011-06-22 18:40:12 -04001208 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001209 if (opendata == NULL)
1210 return ERR_PTR(-ENOMEM);
1211 opendata->state = state;
1212 atomic_inc(&state->count);
1213 return opendata;
1214}
1215
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001216static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001217{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 int ret;
1220
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001221 opendata->o_arg.open_flags = 0;
1222 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001223 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1224 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1225 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001226 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 if (ret != 0)
1228 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001230 if (IS_ERR(newstate))
1231 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001232 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 return 0;
1235}
1236
1237static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1238{
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 int ret;
1241
1242 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001243 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 smp_rmb();
1245 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001246 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001248 if (ret != 0)
1249 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001250 if (newstate != state)
1251 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001252 }
1253 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001254 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001256 if (ret != 0)
1257 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001258 if (newstate != state)
1259 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 }
1261 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001262 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001263 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001264 if (ret != 0)
1265 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001266 if (newstate != state)
1267 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 /*
1270 * We may have performed cached opens for all three recoveries.
1271 * Check if we need to update the current stateid.
1272 */
1273 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001274 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001275 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001276 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001277 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001278 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001279 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 return 0;
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/*
1284 * OPEN_RECLAIM:
1285 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001287static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001289 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001290 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int status;
1293
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001294 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1295 if (IS_ERR(opendata))
1296 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1298 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001299 rcu_read_lock();
1300 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001301 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001302 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001303 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 opendata->o_arg.u.delegation_type = delegation_type;
1305 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001306 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return status;
1308}
1309
Trond Myklebust539cd032007-06-05 11:46:42 -04001310static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct nfs_server *server = NFS_SERVER(state->inode);
1313 struct nfs4_exception exception = { };
1314 int err;
1315 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001316 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001317 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001318 break;
1319 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 } while (exception.retry);
1321 return err;
1322}
1323
Trond Myklebust864472e2006-01-03 09:55:15 +01001324static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1325{
1326 struct nfs_open_context *ctx;
1327 int ret;
1328
1329 ctx = nfs4_state_find_open_context(state);
1330 if (IS_ERR(ctx))
1331 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001332 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 put_nfs_open_context(ctx);
1334 return ret;
1335}
1336
Trond Myklebust13437e12007-07-06 15:10:43 -04001337static 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 -07001338{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001339 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001342 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1343 if (IS_ERR(opendata))
1344 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001345 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001346 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001348 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Trond Myklebust13437e12007-07-06 15:10:43 -04001352int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001355 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int err;
1357 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001358 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 switch (err) {
1360 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001361 case -ENOENT:
1362 case -ESTALE:
1363 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001364 case -NFS4ERR_BADSESSION:
1365 case -NFS4ERR_BADSLOT:
1366 case -NFS4ERR_BAD_HIGH_SLOT:
1367 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1368 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001369 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 case -NFS4ERR_STALE_CLIENTID:
1372 case -NFS4ERR_STALE_STATEID:
1373 case -NFS4ERR_EXPIRED:
1374 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001375 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001376 goto out;
1377 case -ERESTARTSYS:
1378 /*
1379 * The show must go on: exit, but mark the
1380 * stateid as needing recovery.
1381 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001382 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001383 case -NFS4ERR_ADMIN_REVOKED:
1384 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001385 nfs_inode_find_state_and_recover(state->inode,
1386 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001387 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001388 case -EKEYEXPIRED:
1389 /*
1390 * User RPCSEC_GSS context has expired.
1391 * We cannot recover this stateid now, so
1392 * skip it and allow recovery thread to
1393 * proceed.
1394 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001395 case -ENOMEM:
1396 err = 0;
1397 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 err = nfs4_handle_exception(server, err, &exception);
1400 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001401out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return err;
1403}
1404
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1406{
1407 struct nfs4_opendata *data = calldata;
1408
1409 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001410 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001411 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001412 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001413 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001414 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001415 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416}
1417
1418static void nfs4_open_confirm_release(void *calldata)
1419{
1420 struct nfs4_opendata *data = calldata;
1421 struct nfs4_state *state = NULL;
1422
1423 /* If this request hasn't been cancelled, do nothing */
1424 if (data->cancelled == 0)
1425 goto out_free;
1426 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001427 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001428 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001429 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001430 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001431 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001432out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001433 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001434}
1435
1436static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001437 .rpc_call_done = nfs4_open_confirm_done,
1438 .rpc_release = nfs4_open_confirm_release,
1439};
1440
1441/*
1442 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1443 */
1444static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1445{
1446 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1447 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001448 struct rpc_message msg = {
1449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1450 .rpc_argp = &data->c_arg,
1451 .rpc_resp = &data->c_res,
1452 .rpc_cred = data->owner->so_cred,
1453 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001454 struct rpc_task_setup task_setup_data = {
1455 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001457 .callback_ops = &nfs4_open_confirm_ops,
1458 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001459 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001460 .flags = RPC_TASK_ASYNC,
1461 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int status;
1463
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001464 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001465 data->rpc_done = 0;
1466 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001467 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001468 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001469 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001470 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001471 status = nfs4_wait_for_completion_rpc_task(task);
1472 if (status != 0) {
1473 data->cancelled = 1;
1474 smp_wmb();
1475 } else
1476 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001477 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return status;
1479}
1480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483 struct nfs4_opendata *data = calldata;
1484 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001485
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1487 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001488 /*
1489 * Check if we still need to send an OPEN call, or if we can use
1490 * a delegation instead.
1491 */
1492 if (data->state != NULL) {
1493 struct nfs_delegation *delegation;
1494
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001496 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001497 rcu_read_lock();
1498 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001499 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1500 can_open_delegated(delegation, data->o_arg.fmode))
1501 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001502 rcu_read_unlock();
1503 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001504 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001505 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001506 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001507 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001508 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1509 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001510 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001511 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001512 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001513 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001514 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001515 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001516 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001517unlock_no_action:
1518 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001519out_no_action:
1520 task->tk_action = NULL;
1521
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001524static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1525{
1526 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1527 nfs4_open_prepare(task, calldata);
1528}
1529
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530static void nfs4_open_done(struct rpc_task *task, void *calldata)
1531{
1532 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001535
Trond Myklebust14516c32010-07-31 14:29:06 -04001536 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1537 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001538
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 if (task->tk_status == 0) {
1540 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001541 case S_IFREG:
1542 break;
1543 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001545 break;
1546 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001548 break;
1549 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001551 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001552 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001553 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1554 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001555 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001556 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001558
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559static void nfs4_open_release(void *calldata)
1560{
1561 struct nfs4_opendata *data = calldata;
1562 struct nfs4_state *state = NULL;
1563
1564 /* If this request hasn't been cancelled, do nothing */
1565 if (data->cancelled == 0)
1566 goto out_free;
1567 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001568 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569 goto out_free;
1570 /* In case we need an open_confirm, no cleanup! */
1571 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1572 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001574 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001575 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001576out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001577 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001578}
1579
1580static const struct rpc_call_ops nfs4_open_ops = {
1581 .rpc_call_prepare = nfs4_open_prepare,
1582 .rpc_call_done = nfs4_open_done,
1583 .rpc_release = nfs4_open_release,
1584};
1585
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001586static const struct rpc_call_ops nfs4_recover_open_ops = {
1587 .rpc_call_prepare = nfs4_recover_open_prepare,
1588 .rpc_call_done = nfs4_open_done,
1589 .rpc_release = nfs4_open_release,
1590};
1591
1592static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001593{
1594 struct inode *dir = data->dir->d_inode;
1595 struct nfs_server *server = NFS_SERVER(dir);
1596 struct nfs_openargs *o_arg = &data->o_arg;
1597 struct nfs_openres *o_res = &data->o_res;
1598 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001599 struct rpc_message msg = {
1600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1601 .rpc_argp = o_arg,
1602 .rpc_resp = o_res,
1603 .rpc_cred = data->owner->so_cred,
1604 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001605 struct rpc_task_setup task_setup_data = {
1606 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001607 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001608 .callback_ops = &nfs4_open_ops,
1609 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001610 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001611 .flags = RPC_TASK_ASYNC,
1612 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613 int status;
1614
Trond Myklebust9d12b212012-01-17 22:04:25 -05001615 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001616 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001617 data->rpc_done = 0;
1618 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001619 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001620 if (isrecover)
1621 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001622 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001623 if (IS_ERR(task))
1624 return PTR_ERR(task);
1625 status = nfs4_wait_for_completion_rpc_task(task);
1626 if (status != 0) {
1627 data->cancelled = 1;
1628 smp_wmb();
1629 } else
1630 status = data->rpc_status;
1631 rpc_put_task(task);
1632
1633 return status;
1634}
1635
1636static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1637{
1638 struct inode *dir = data->dir->d_inode;
1639 struct nfs_openres *o_res = &data->o_res;
1640 int status;
1641
1642 status = nfs4_run_open_task(data, 1);
1643 if (status != 0 || !data->rpc_done)
1644 return status;
1645
Trond Myklebust6926afd2012-01-07 13:22:46 -05001646 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1647
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001648 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1649 status = _nfs4_proc_open_confirm(data);
1650 if (status != 0)
1651 return status;
1652 }
1653
1654 return status;
1655}
1656
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001657static int nfs4_opendata_access(struct rpc_cred *cred,
1658 struct nfs4_opendata *opendata,
1659 struct nfs4_state *state, fmode_t fmode)
1660{
1661 struct nfs_access_entry cache;
1662 u32 mask;
1663
1664 /* access call failed or for some reason the server doesn't
1665 * support any access modes -- defer access call until later */
1666 if (opendata->o_res.access_supported == 0)
1667 return 0;
1668
1669 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001670 /* don't check MAY_WRITE - a newly created file may not have
1671 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001672 if (fmode & FMODE_READ)
1673 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001674 if (fmode & FMODE_EXEC)
1675 mask |= MAY_EXEC;
1676
1677 cache.cred = cred;
1678 cache.jiffies = jiffies;
1679 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1680 nfs_access_add_cache(state->inode, &cache);
1681
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001682 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001683 return 0;
1684
1685 /* even though OPEN succeeded, access is denied. Close the file */
1686 nfs4_close_state(state, fmode);
1687 return -NFS4ERR_ACCESS;
1688}
1689
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001690/*
1691 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1692 */
1693static int _nfs4_proc_open(struct nfs4_opendata *data)
1694{
1695 struct inode *dir = data->dir->d_inode;
1696 struct nfs_server *server = NFS_SERVER(dir);
1697 struct nfs_openargs *o_arg = &data->o_arg;
1698 struct nfs_openres *o_res = &data->o_res;
1699 int status;
1700
1701 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001702 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001704 if (status != 0) {
1705 if (status == -NFS4ERR_BADNAME &&
1706 !(o_arg->open_flags & O_CREAT))
1707 return -ENOENT;
1708 return status;
1709 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001710
Trond Myklebust6926afd2012-01-07 13:22:46 -05001711 nfs_fattr_map_and_free_names(server, &data->f_attr);
1712
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001713 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001714 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001715 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1716 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001718 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001720 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001723 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Andy Adamsond83217c2011-03-01 01:34:17 +00001727static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001728{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001729 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001730 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001731
Trond Myklebusta78cb572009-08-09 15:06:19 -04001732 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001733 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001734 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001735 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001736 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1737 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001738 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001739 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001740 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001741 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001742 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001743}
1744
Andy Adamsond83217c2011-03-01 01:34:17 +00001745static int nfs4_recover_expired_lease(struct nfs_server *server)
1746{
1747 return nfs4_client_recover_expired_lease(server->nfs_client);
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750/*
1751 * OPEN_EXPIRED:
1752 * reclaim state on the server after a network partition.
1753 * Assumes caller holds the appropriate lock
1754 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001755static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001757 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001758 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001760 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1761 if (IS_ERR(opendata))
1762 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001763 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001764 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001765 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001766 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001767 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001770static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001771{
Trond Myklebust539cd032007-06-05 11:46:42 -04001772 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001773 struct nfs4_exception exception = { };
1774 int err;
1775
1776 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001777 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001778 switch (err) {
1779 default:
1780 goto out;
1781 case -NFS4ERR_GRACE:
1782 case -NFS4ERR_DELAY:
1783 nfs4_handle_exception(server, err, &exception);
1784 err = 0;
1785 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001786 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001787out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001788 return err;
1789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001794 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Trond Myklebust864472e2006-01-03 09:55:15 +01001796 ctx = nfs4_state_find_open_context(state);
1797 if (IS_ERR(ctx))
1798 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001799 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001800 put_nfs_open_context(ctx);
1801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001804#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001805static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001806{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001807 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001808 nfs4_stateid *stateid = &state->stateid;
1809 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001810
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001811 /* If a state reset has been done, test_stateid is unneeded */
1812 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1813 return;
1814
1815 status = nfs41_test_stateid(server, stateid);
1816 if (status != NFS_OK) {
1817 /* Free the stateid unless the server explicitly
1818 * informs us the stateid is unrecognized. */
1819 if (status != -NFS4ERR_BAD_STATEID)
1820 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001821 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001822
Bryan Schumaker69388672012-09-26 15:25:52 -04001823 write_seqlock(&state->seqlock);
1824 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1825 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001826 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1827 }
1828}
1829
1830/**
1831 * nfs41_check_open_stateid - possibly free an open stateid
1832 *
1833 * @state: NFSv4 state for an inode
1834 *
1835 * Returns NFS_OK if recovery for this stateid is now finished.
1836 * Otherwise a negative NFS4ERR value is returned.
1837 */
1838static int nfs41_check_open_stateid(struct nfs4_state *state)
1839{
1840 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001841 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001842 int status;
1843
1844 /* If a state reset has been done, test_stateid is unneeded */
1845 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1846 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1847 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1848 return -NFS4ERR_BAD_STATEID;
1849
1850 status = nfs41_test_stateid(server, stateid);
1851 if (status != NFS_OK) {
1852 /* Free the stateid unless the server explicitly
1853 * informs us the stateid is unrecognized. */
1854 if (status != -NFS4ERR_BAD_STATEID)
1855 nfs41_free_stateid(server, stateid);
1856
1857 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1858 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1859 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001860 }
1861 return status;
1862}
1863
1864static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1865{
Chuck Levereb64cf92012-07-11 16:30:05 -04001866 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001867
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001868 nfs41_clear_delegation_stateid(state);
1869 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001870 if (status != NFS_OK)
1871 status = nfs4_open_expired(sp, state);
1872 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001873}
1874#endif
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001877 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1878 * fields corresponding to attributes that were used to store the verifier.
1879 * Make sure we clobber those fields in the later setattr call
1880 */
1881static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1882{
1883 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1884 !(sattr->ia_valid & ATTR_ATIME_SET))
1885 sattr->ia_valid |= ATTR_ATIME;
1886
1887 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1888 !(sattr->ia_valid & ATTR_MTIME_SET))
1889 sattr->ia_valid |= ATTR_MTIME;
1890}
1891
1892/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001893 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Andy Adamson82be4172012-05-23 05:02:35 -04001895static int _nfs4_do_open(struct inode *dir,
1896 struct dentry *dentry,
1897 fmode_t fmode,
1898 int flags,
1899 struct iattr *sattr,
1900 struct rpc_cred *cred,
1901 struct nfs4_state **res,
1902 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct nfs4_state_owner *sp;
1905 struct nfs4_state *state = NULL;
1906 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001907 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001908 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001912 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1913 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1915 goto out_err;
1916 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001917 status = nfs4_recover_expired_lease(server);
1918 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001919 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001920 if (dentry->d_inode != NULL)
1921 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001922 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001923 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001924 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001925 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Andy Adamson82be4172012-05-23 05:02:35 -04001927 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1928 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1929 if (!opendata->f_attr.mdsthreshold)
1930 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001931 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001932 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001933 if (dentry->d_inode != NULL)
1934 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001935
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001936 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001938 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001940 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001941 status = PTR_ERR(state);
1942 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001943 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001944 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001945 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001946
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001947 status = nfs4_opendata_access(cred, opendata, state, fmode);
1948 if (status != 0)
1949 goto err_opendata_put;
1950
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001951 if (opendata->o_arg.open_flags & O_EXCL) {
1952 nfs4_exclusive_attrset(opendata, sattr);
1953
1954 nfs_fattr_init(opendata->o_res.f_attr);
1955 status = nfs4_do_setattr(state->inode, cred,
1956 opendata->o_res.f_attr, sattr,
1957 state);
1958 if (status == 0)
1959 nfs_setattr_update_inode(state->inode, sattr);
1960 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1961 }
Andy Adamson82be4172012-05-23 05:02:35 -04001962
1963 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1964 *ctx_th = opendata->f_attr.mdsthreshold;
1965 else
1966 kfree(opendata->f_attr.mdsthreshold);
1967 opendata->f_attr.mdsthreshold = NULL;
1968
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001969 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 *res = state;
1972 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001973err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001974 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001975 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001976err_put_state_owner:
1977 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 *res = NULL;
1980 return status;
1981}
1982
1983
Andy Adamson82be4172012-05-23 05:02:35 -04001984static struct nfs4_state *nfs4_do_open(struct inode *dir,
1985 struct dentry *dentry,
1986 fmode_t fmode,
1987 int flags,
1988 struct iattr *sattr,
1989 struct rpc_cred *cred,
1990 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct nfs4_exception exception = { };
1993 struct nfs4_state *res;
1994 int status;
1995
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001996 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001998 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1999 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (status == 0)
2001 break;
2002 /* NOTE: BAD_SEQID means the server and client disagree about the
2003 * book-keeping w.r.t. state-changing operations
2004 * (OPEN/CLOSE/LOCK/LOCKU...)
2005 * It is actually a sign of a bug on the client or on the server.
2006 *
2007 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002008 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 * have unhashed the old state_owner for us, and that we can
2010 * therefore safely retry using a new one. We should still warn
2011 * the user though...
2012 */
2013 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002014 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002015 " returned a bad sequence-id error!\n",
2016 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 exception.retry = 1;
2018 continue;
2019 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002020 /*
2021 * BAD_STATEID on OPEN means that the server cancelled our
2022 * state before it received the OPEN_CONFIRM.
2023 * Recover by retrying the request as per the discussion
2024 * on Page 181 of RFC3530.
2025 */
2026 if (status == -NFS4ERR_BAD_STATEID) {
2027 exception.retry = 1;
2028 continue;
2029 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002030 if (status == -EAGAIN) {
2031 /* We must have found a delegation */
2032 exception.retry = 1;
2033 continue;
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2036 status, &exception));
2037 } while (exception.retry);
2038 return res;
2039}
2040
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002041static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2042 struct nfs_fattr *fattr, struct iattr *sattr,
2043 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002045 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002047 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 .iap = sattr,
2049 .server = server,
2050 .bitmask = server->attr_bitmask,
2051 };
2052 struct nfs_setattrres res = {
2053 .fattr = fattr,
2054 .server = server,
2055 };
2056 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2058 .rpc_argp = &arg,
2059 .rpc_resp = &res,
2060 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002062 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002063 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Trond Myklebust0e574af2005-10-27 22:12:38 -04002065 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Trond Myklebust4fc87962012-03-08 17:42:01 -05002067 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002068 struct nfs_lockowner lockowner = {
2069 .l_owner = current->files,
2070 .l_pid = current->tgid,
2071 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002072 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002073 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002074 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2075 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002076 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002077 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002078 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Bryan Schumaker7c513052011-03-24 17:12:24 +00002080 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002081 if (status == 0 && state != NULL)
2082 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002083 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002086static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2087 struct nfs_fattr *fattr, struct iattr *sattr,
2088 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002090 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002091 struct nfs4_exception exception = {
2092 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002093 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002094 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int err;
2096 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002097 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2098 switch (err) {
2099 case -NFS4ERR_OPENMODE:
2100 if (state && !(state->state & FMODE_WRITE)) {
2101 err = -EBADF;
2102 if (sattr->ia_valid & ATTR_OPEN)
2103 err = -EACCES;
2104 goto out;
2105 }
2106 }
2107 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002109out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return err;
2111}
2112
2113struct nfs4_closedata {
2114 struct inode *inode;
2115 struct nfs4_state *state;
2116 struct nfs_closeargs arg;
2117 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002118 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002119 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002120 bool roc;
2121 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122};
2123
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002124static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002125{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002126 struct nfs4_closedata *calldata = data;
2127 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002128 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002129
Fred Isamanf7e89172011-01-06 11:36:32 +00002130 if (calldata->roc)
2131 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002132 nfs4_put_open_state(calldata->state);
2133 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002134 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002135 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002136 kfree(calldata);
2137}
2138
Trond Myklebust88069f72009-12-08 08:33:16 -05002139static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2140 fmode_t fmode)
2141{
2142 spin_lock(&state->owner->so_lock);
2143 if (!(fmode & FMODE_READ))
2144 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2145 if (!(fmode & FMODE_WRITE))
2146 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2147 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2148 spin_unlock(&state->owner->so_lock);
2149}
2150
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002151static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002153 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 struct nfs_server *server = NFS_SERVER(calldata->inode);
2156
Chuck Levera3ca5652012-03-01 17:00:40 -05002157 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002158 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2159 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /* hmm. we are done with the inode, and in the process of freeing
2161 * the state_owner. we keep this around to process errors
2162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 switch (task->tk_status) {
2164 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002165 if (calldata->roc)
2166 pnfs_roc_set_barrier(state->inode,
2167 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002168 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002169 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002170 nfs4_close_clear_stateid_flags(state,
2171 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 break;
2173 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002174 case -NFS4ERR_OLD_STATEID:
2175 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002177 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002180 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2181 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002183 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002184 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002185 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002188static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002190 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002191 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002192 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002193 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002194
Chuck Levera3ca5652012-03-01 17:00:40 -05002195 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002196 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002197 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002198
Trond Myklebust88069f72009-12-08 08:33:16 -05002199 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2200 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002201 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002202 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002203 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002204 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002205 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2206 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2207 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002208 }
2209 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002210 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2211 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2212 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002213 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002214 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002215 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002216
2217 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002218 /* Note: exit _without_ calling nfs4_close_done */
2219 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002220 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002221 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002222
Fred Isamanf7e89172011-01-06 11:36:32 +00002223 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002224 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002225 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002226 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002227 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002228 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002229
Trond Myklebust516a6af2005-10-27 22:12:41 -04002230 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002231 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002232 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002233 &calldata->arg.seq_args,
2234 &calldata->res.seq_res,
2235 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002236 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002237 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002238out:
2239 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002242static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002243 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002244 .rpc_call_done = nfs4_close_done,
2245 .rpc_release = nfs4_free_closedata,
2246};
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/*
2249 * It is possible for data to be read/written from a mem-mapped file
2250 * after the sys_close call (which hits the vfs layer as a flush).
2251 * This means that we can't safely call nfsv4 close on a file until
2252 * the inode is cleared. This in turn means that we are not good
2253 * NFSv4 citizens - we do not indicate to the server to update the file's
2254 * share state even when we are done with one of the three share
2255 * stateid's in the inode.
2256 *
2257 * NOTE: Caller must be holding the sp->so_owner semaphore!
2258 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002259int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002261 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002263 struct nfs4_state_owner *sp = state->owner;
2264 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002265 struct rpc_message msg = {
2266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2267 .rpc_cred = state->owner->so_cred,
2268 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002269 struct rpc_task_setup task_setup_data = {
2270 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002271 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002272 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002273 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002274 .flags = RPC_TASK_ASYNC,
2275 };
Trond Myklebust95121352005-10-18 14:20:12 -07002276 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002278 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002280 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002281 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002282 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002284 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002285 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002287 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002288 if (calldata->arg.seqid == NULL)
2289 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002290 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002291 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002292 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002293 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002294 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002295 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002296 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002297
Trond Myklebust1174dd12010-12-21 10:52:24 -05002298 msg.rpc_argp = &calldata->arg;
2299 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002300 task_setup_data.callback_data = calldata;
2301 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002302 if (IS_ERR(task))
2303 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002304 status = 0;
2305 if (wait)
2306 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002307 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002308 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002309out_free_calldata:
2310 kfree(calldata);
2311out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002312 nfs4_put_open_state(state);
2313 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002314 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Trond Myklebust2b484292010-09-17 10:56:51 -04002317static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002318nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 struct nfs4_state *state;
2321
Trond Myklebust565277f2007-10-15 18:17:53 -04002322 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002323 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2324 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002325 if (IS_ERR(state))
2326 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002327 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002328 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Trond Myklebust1185a552009-12-03 15:54:02 -05002331static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002332{
2333 if (ctx->state == NULL)
2334 return;
2335 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002336 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002337 else
Al Viro643168c2011-06-22 18:20:23 -04002338 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2342{
Benny Halevy43652ad2009-04-01 09:21:54 -04002343 struct nfs4_server_caps_arg args = {
2344 .fhandle = fhandle,
2345 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 struct nfs4_server_caps_res res = {};
2347 struct rpc_message msg = {
2348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002349 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 .rpc_resp = &res,
2351 };
2352 int status;
2353
Bryan Schumaker7c513052011-03-24 17:12:24 +00002354 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (status == 0) {
2356 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002357 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2358 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2359 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2360 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2361 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2363 server->caps |= NFS_CAP_ACLS;
2364 if (res.has_links != 0)
2365 server->caps |= NFS_CAP_HARDLINKS;
2366 if (res.has_symlinks != 0)
2367 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002368 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2369 server->caps |= NFS_CAP_FILEID;
2370 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2371 server->caps |= NFS_CAP_MODE;
2372 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2373 server->caps |= NFS_CAP_NLINK;
2374 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2375 server->caps |= NFS_CAP_OWNER;
2376 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2377 server->caps |= NFS_CAP_OWNER_GROUP;
2378 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2379 server->caps |= NFS_CAP_ATIME;
2380 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2381 server->caps |= NFS_CAP_CTIME;
2382 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2383 server->caps |= NFS_CAP_MTIME;
2384
Trond Myklebusta65318b2009-03-11 14:10:28 -04002385 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2386 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2387 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002389 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return status;
2393}
2394
Trond Myklebust55a97592006-06-09 09:34:19 -04002395int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 struct nfs4_exception exception = { };
2398 int err;
2399 do {
2400 err = nfs4_handle_exception(server,
2401 _nfs4_server_capabilities(server, fhandle),
2402 &exception);
2403 } while (exception.retry);
2404 return err;
2405}
2406
2407static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2408 struct nfs_fsinfo *info)
2409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 struct nfs4_lookup_root_arg args = {
2411 .bitmask = nfs4_fattr_bitmap,
2412 };
2413 struct nfs4_lookup_res res = {
2414 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002415 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 .fh = fhandle,
2417 };
2418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2420 .rpc_argp = &args,
2421 .rpc_resp = &res,
2422 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002423
Trond Myklebust0e574af2005-10-27 22:12:38 -04002424 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
2428static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2429 struct nfs_fsinfo *info)
2430{
2431 struct nfs4_exception exception = { };
2432 int err;
2433 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002434 err = _nfs4_lookup_root(server, fhandle, info);
2435 switch (err) {
2436 case 0:
2437 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002438 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002439 default:
2440 err = nfs4_handle_exception(server, err, &exception);
2441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002443out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return err;
2445}
2446
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002447static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2448 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2449{
2450 struct rpc_auth *auth;
2451 int ret;
2452
2453 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002454 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002455 ret = -EIO;
2456 goto out;
2457 }
2458 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002459out:
2460 return ret;
2461}
2462
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002463static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002464 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002466 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002467 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002469 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2470 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002471
2472 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002473 /* AUTH_UNIX is the default flavor if none was specified,
2474 * thus has already been tried. */
2475 if (flav_array[i] == RPC_AUTH_UNIX)
2476 continue;
2477
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002478 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002479 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002480 continue;
2481 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002482 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002483 /*
2484 * -EACCESS could mean that the user doesn't have correct permissions
2485 * to access the mount. It could also mean that we tried to mount
2486 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2487 * existing mount programs don't handle -EACCES very well so it should
2488 * be mapped to -EPERM instead.
2489 */
2490 if (status == -EACCES)
2491 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002492 return status;
2493}
2494
2495/*
2496 * get the file handle for the "/" directory on the server
2497 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002498int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2499 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002500{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002501 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002502 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002503 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2504 /*
2505 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2506 * by nfs4_map_errors() as this function exits.
2507 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002508 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (status == 0)
2510 status = nfs4_server_capabilities(server, fhandle);
2511 if (status == 0)
2512 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002513 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Bryan Schumakerbae36242012-05-10 15:07:31 -04002516static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2517 struct nfs_fsinfo *info)
2518{
2519 int error;
2520 struct nfs_fattr *fattr = info->fattr;
2521
2522 error = nfs4_server_capabilities(server, mntfh);
2523 if (error < 0) {
2524 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2525 return error;
2526 }
2527
2528 error = nfs4_proc_getattr(server, mntfh, fattr);
2529 if (error < 0) {
2530 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2531 return error;
2532 }
2533
2534 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2535 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2536 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2537
2538 return error;
2539}
2540
Manoj Naik6b97fd32006-06-09 09:34:29 -04002541/*
2542 * Get locations and (maybe) other attributes of a referral.
2543 * Note that we'll actually follow the referral later when
2544 * we detect fsid mismatch in inode revalidation
2545 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002546static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2547 const struct qstr *name, struct nfs_fattr *fattr,
2548 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002549{
2550 int status = -ENOMEM;
2551 struct page *page = NULL;
2552 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002553
2554 page = alloc_page(GFP_KERNEL);
2555 if (page == NULL)
2556 goto out;
2557 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2558 if (locations == NULL)
2559 goto out;
2560
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002561 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002562 if (status != 0)
2563 goto out;
2564 /* Make sure server returned a different fsid for the referral */
2565 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002566 dprintk("%s: server did not return a different fsid for"
2567 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002568 status = -EIO;
2569 goto out;
2570 }
Andy Adamson533eb462011-06-13 18:25:56 -04002571 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2572 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002573
Andy Adamson533eb462011-06-13 18:25:56 -04002574 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002575 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002576 memset(fhandle, 0, sizeof(struct nfs_fh));
2577out:
2578 if (page)
2579 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002580 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002581 return status;
2582}
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2585{
2586 struct nfs4_getattr_arg args = {
2587 .fh = fhandle,
2588 .bitmask = server->attr_bitmask,
2589 };
2590 struct nfs4_getattr_res res = {
2591 .fattr = fattr,
2592 .server = server,
2593 };
2594 struct rpc_message msg = {
2595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2596 .rpc_argp = &args,
2597 .rpc_resp = &res,
2598 };
2599
Trond Myklebust0e574af2005-10-27 22:12:38 -04002600 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002601 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
2604static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2605{
2606 struct nfs4_exception exception = { };
2607 int err;
2608 do {
2609 err = nfs4_handle_exception(server,
2610 _nfs4_proc_getattr(server, fhandle, fattr),
2611 &exception);
2612 } while (exception.retry);
2613 return err;
2614}
2615
2616/*
2617 * The file is not closed if it is opened due to the a request to change
2618 * the size of the file. The open call will not be needed once the
2619 * VFS layer lookup-intents are implemented.
2620 *
2621 * Close is called when the inode is destroyed.
2622 * If we haven't opened the file for O_WRONLY, we
2623 * need to in the size_change case to obtain a stateid.
2624 *
2625 * Got race?
2626 * Because OPEN is always done by name in nfsv4, it is
2627 * possible that we opened a different file by the same
2628 * name. We can recognize this race condition, but we
2629 * can't do anything about it besides returning an error.
2630 *
2631 * This will be fixed with VFS changes (lookup-intent).
2632 */
2633static int
2634nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2635 struct iattr *sattr)
2636{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002637 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002638 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002639 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 int status;
2641
Benny Halevy8a1636c2010-07-14 15:43:57 -04002642 if (pnfs_ld_layoutret_on_setattr(inode))
2643 pnfs_return_layout(inode);
2644
Trond Myklebust0e574af2005-10-27 22:12:38 -04002645 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Andy Adamson26699402012-05-30 16:12:24 -04002647 /* Deal with open(O_TRUNC) */
2648 if (sattr->ia_valid & ATTR_OPEN)
2649 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2650
2651 /* Optimization: if the end result is no change, don't RPC */
2652 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2653 return 0;
2654
Trond Myklebustd5308382005-11-04 15:33:38 -05002655 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002656 if (sattr->ia_valid & ATTR_FILE) {
2657 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002658
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002659 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002660 if (ctx) {
2661 cred = ctx->cred;
2662 state = ctx->state;
2663 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002664 }
2665
2666 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002667 if (status == 0)
2668 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return status;
2670}
2671
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002672static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2673 const struct qstr *name, struct nfs_fh *fhandle,
2674 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002675{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002676 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002677 int status;
2678 struct nfs4_lookup_arg args = {
2679 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002680 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002681 .name = name,
2682 };
2683 struct nfs4_lookup_res res = {
2684 .server = server,
2685 .fattr = fattr,
2686 .fh = fhandle,
2687 };
2688 struct rpc_message msg = {
2689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2690 .rpc_argp = &args,
2691 .rpc_resp = &res,
2692 };
2693
2694 nfs_fattr_init(fattr);
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002697 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 dprintk("NFS reply lookup: %d\n", status);
2699 return status;
2700}
2701
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002702static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002703{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002704 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002705 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002706 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2707 fattr->nlink = 2;
2708}
2709
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002710static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2711 struct qstr *name, struct nfs_fh *fhandle,
2712 struct nfs_fattr *fattr)
2713{
2714 struct nfs4_exception exception = { };
2715 struct rpc_clnt *client = *clnt;
2716 int err;
2717 do {
2718 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2719 switch (err) {
2720 case -NFS4ERR_BADNAME:
2721 err = -ENOENT;
2722 goto out;
2723 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002724 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002725 goto out;
2726 case -NFS4ERR_WRONGSEC:
2727 err = -EPERM;
2728 if (client != *clnt)
2729 goto out;
2730
2731 client = nfs4_create_sec_client(client, dir, name);
2732 if (IS_ERR(client))
2733 return PTR_ERR(client);
2734
2735 exception.retry = 1;
2736 break;
2737 default:
2738 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2739 }
2740 } while (exception.retry);
2741
2742out:
2743 if (err == 0)
2744 *clnt = client;
2745 else if (client != *clnt)
2746 rpc_shutdown_client(client);
2747
2748 return err;
2749}
2750
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002751static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002752 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002754 int status;
2755 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002756
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002757 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2758 if (client != NFS_CLIENT(dir)) {
2759 rpc_shutdown_client(client);
2760 nfs_fixup_secinfo_attributes(fattr);
2761 }
2762 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002765struct rpc_clnt *
2766nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2767 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2768{
2769 int status;
2770 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2771
2772 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2773 if (status < 0) {
2774 rpc_shutdown_client(client);
2775 return ERR_PTR(status);
2776 }
2777 return client;
2778}
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2781{
Trond Myklebust76b32992007-08-10 17:45:11 -04002782 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 struct nfs4_accessargs args = {
2784 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002785 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002787 struct nfs4_accessres res = {
2788 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002789 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 struct rpc_message msg = {
2791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2792 .rpc_argp = &args,
2793 .rpc_resp = &res,
2794 .rpc_cred = entry->cred,
2795 };
2796 int mode = entry->mask;
2797 int status;
2798
2799 /*
2800 * Determine which access bits we want to ask for...
2801 */
2802 if (mode & MAY_READ)
2803 args.access |= NFS4_ACCESS_READ;
2804 if (S_ISDIR(inode->i_mode)) {
2805 if (mode & MAY_WRITE)
2806 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2807 if (mode & MAY_EXEC)
2808 args.access |= NFS4_ACCESS_LOOKUP;
2809 } else {
2810 if (mode & MAY_WRITE)
2811 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2812 if (mode & MAY_EXEC)
2813 args.access |= NFS4_ACCESS_EXECUTE;
2814 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002815
2816 res.fattr = nfs_alloc_fattr();
2817 if (res.fattr == NULL)
2818 return -ENOMEM;
2819
Bryan Schumaker7c513052011-03-24 17:12:24 +00002820 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002822 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002823 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002825 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return status;
2827}
2828
2829static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2830{
2831 struct nfs4_exception exception = { };
2832 int err;
2833 do {
2834 err = nfs4_handle_exception(NFS_SERVER(inode),
2835 _nfs4_proc_access(inode, entry),
2836 &exception);
2837 } while (exception.retry);
2838 return err;
2839}
2840
2841/*
2842 * TODO: For the time being, we don't try to get any attributes
2843 * along with any of the zero-copy operations READ, READDIR,
2844 * READLINK, WRITE.
2845 *
2846 * In the case of the first three, we want to put the GETATTR
2847 * after the read-type operation -- this is because it is hard
2848 * to predict the length of a GETATTR response in v4, and thus
2849 * align the READ data correctly. This means that the GETATTR
2850 * may end up partially falling into the page cache, and we should
2851 * shift it into the 'tail' of the xdr_buf before processing.
2852 * To do this efficiently, we need to know the total length
2853 * of data received, which doesn't seem to be available outside
2854 * of the RPC layer.
2855 *
2856 * In the case of WRITE, we also want to put the GETATTR after
2857 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002858 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 *
2860 * Both of these changes to the XDR layer would in fact be quite
2861 * minor, but I decided to leave them for a subsequent patch.
2862 */
2863static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2864 unsigned int pgbase, unsigned int pglen)
2865{
2866 struct nfs4_readlink args = {
2867 .fh = NFS_FH(inode),
2868 .pgbase = pgbase,
2869 .pglen = pglen,
2870 .pages = &page,
2871 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002872 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct rpc_message msg = {
2874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2875 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002876 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 };
2878
Bryan Schumaker7c513052011-03-24 17:12:24 +00002879 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 -07002880}
2881
2882static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2883 unsigned int pgbase, unsigned int pglen)
2884{
2885 struct nfs4_exception exception = { };
2886 int err;
2887 do {
2888 err = nfs4_handle_exception(NFS_SERVER(inode),
2889 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2890 &exception);
2891 } while (exception.retry);
2892 return err;
2893}
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002896 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898static int
2899nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002900 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002902 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 int status = 0;
2905
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002906 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2907 if (IS_ERR(ctx))
2908 return PTR_ERR(ctx);
2909
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002910 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002911 state = nfs4_do_open(dir, dentry, ctx->mode,
2912 flags, sattr, ctx->cred,
2913 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002914 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (IS_ERR(state)) {
2916 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002917 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002919 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002920 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002921 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002923 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return status;
2925}
2926
2927static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2928{
Trond Myklebust16e42952005-10-27 22:12:44 -04002929 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002930 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002932 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002934 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002935 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002936 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2939 .rpc_argp = &args,
2940 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002942 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002943
Bryan Schumaker7c513052011-03-24 17:12:24 +00002944 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002945 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002946 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return status;
2948}
2949
2950static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2951{
2952 struct nfs4_exception exception = { };
2953 int err;
2954 do {
2955 err = nfs4_handle_exception(NFS_SERVER(dir),
2956 _nfs4_proc_remove(dir, name),
2957 &exception);
2958 } while (exception.retry);
2959 return err;
2960}
2961
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002962static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002964 struct nfs_server *server = NFS_SERVER(dir);
2965 struct nfs_removeargs *args = msg->rpc_argp;
2966 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002968 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002970 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002973static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2974{
2975 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2976 &data->args.seq_args,
2977 &data->res.seq_res,
2978 task))
2979 return;
2980 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002983static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002985 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2986
Trond Myklebust14516c32010-07-31 14:29:06 -04002987 if (!nfs4_sequence_done(task, &res->seq_res))
2988 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002989 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002990 return 0;
2991 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002992 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Jeff Laytond3d41522010-09-17 17:31:57 -04002995static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2996{
2997 struct nfs_server *server = NFS_SERVER(dir);
2998 struct nfs_renameargs *arg = msg->rpc_argp;
2999 struct nfs_renameres *res = msg->rpc_resp;
3000
3001 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003002 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003003 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003004}
3005
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003006static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3007{
3008 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3009 &data->args.seq_args,
3010 &data->res.seq_res,
3011 task))
3012 return;
3013 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003014}
3015
3016static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3017 struct inode *new_dir)
3018{
3019 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3020
3021 if (!nfs4_sequence_done(task, &res->seq_res))
3022 return 0;
3023 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3024 return 0;
3025
3026 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003027 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003028 return 1;
3029}
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3032 struct inode *new_dir, struct qstr *new_name)
3033{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003034 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003035 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 .old_dir = NFS_FH(old_dir),
3037 .new_dir = NFS_FH(new_dir),
3038 .old_name = old_name,
3039 .new_name = new_name,
3040 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003041 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003042 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 struct rpc_message msg = {
3045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3046 .rpc_argp = &arg,
3047 .rpc_resp = &res,
3048 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003049 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Bryan Schumaker7c513052011-03-24 17:12:24 +00003051 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 if (!status) {
3053 update_changeattr(old_dir, &res.old_cinfo);
3054 update_changeattr(new_dir, &res.new_cinfo);
3055 }
3056 return status;
3057}
3058
3059static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3060 struct inode *new_dir, struct qstr *new_name)
3061{
3062 struct nfs4_exception exception = { };
3063 int err;
3064 do {
3065 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3066 _nfs4_proc_rename(old_dir, old_name,
3067 new_dir, new_name),
3068 &exception);
3069 } while (exception.retry);
3070 return err;
3071}
3072
3073static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3074{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003075 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct nfs4_link_arg arg = {
3077 .fh = NFS_FH(inode),
3078 .dir_fh = NFS_FH(dir),
3079 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003080 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003082 struct nfs4_link_res res = {
3083 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 struct rpc_message msg = {
3086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3087 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003088 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003090 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Trond Myklebust136f2622010-04-16 16:22:49 -04003092 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003093 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003094 goto out;
3095
Bryan Schumaker7c513052011-03-24 17:12:24 +00003096 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003097 if (!status) {
3098 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003099 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003100 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003101out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003102 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return status;
3104}
3105
3106static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3107{
3108 struct nfs4_exception exception = { };
3109 int err;
3110 do {
3111 err = nfs4_handle_exception(NFS_SERVER(inode),
3112 _nfs4_proc_link(inode, dir, name),
3113 &exception);
3114 } while (exception.retry);
3115 return err;
3116}
3117
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003118struct nfs4_createdata {
3119 struct rpc_message msg;
3120 struct nfs4_create_arg arg;
3121 struct nfs4_create_res res;
3122 struct nfs_fh fh;
3123 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003124};
3125
3126static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3127 struct qstr *name, struct iattr *sattr, u32 ftype)
3128{
3129 struct nfs4_createdata *data;
3130
3131 data = kzalloc(sizeof(*data), GFP_KERNEL);
3132 if (data != NULL) {
3133 struct nfs_server *server = NFS_SERVER(dir);
3134
3135 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3136 data->msg.rpc_argp = &data->arg;
3137 data->msg.rpc_resp = &data->res;
3138 data->arg.dir_fh = NFS_FH(dir);
3139 data->arg.server = server;
3140 data->arg.name = name;
3141 data->arg.attrs = sattr;
3142 data->arg.ftype = ftype;
3143 data->arg.bitmask = server->attr_bitmask;
3144 data->res.server = server;
3145 data->res.fh = &data->fh;
3146 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003147 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003148 }
3149 return data;
3150}
3151
3152static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3153{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003154 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003155 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003156 if (status == 0) {
3157 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003158 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3159 }
3160 return status;
3161}
3162
3163static void nfs4_free_createdata(struct nfs4_createdata *data)
3164{
3165 kfree(data);
3166}
3167
Chuck Lever4f390c12006-08-22 20:06:22 -04003168static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003169 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003171 struct nfs4_createdata *data;
3172 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Chuck Lever94a6d752006-08-22 20:06:23 -04003174 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003175 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003176
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003177 status = -ENOMEM;
3178 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3179 if (data == NULL)
3180 goto out;
3181
3182 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3183 data->arg.u.symlink.pages = &page;
3184 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003186 status = nfs4_do_create(dir, dentry, data);
3187
3188 nfs4_free_createdata(data);
3189out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return status;
3191}
3192
Chuck Lever4f390c12006-08-22 20:06:22 -04003193static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003194 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
3196 struct nfs4_exception exception = { };
3197 int err;
3198 do {
3199 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003200 _nfs4_proc_symlink(dir, dentry, page,
3201 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 &exception);
3203 } while (exception.retry);
3204 return err;
3205}
3206
3207static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3208 struct iattr *sattr)
3209{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210 struct nfs4_createdata *data;
3211 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003213 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3214 if (data == NULL)
3215 goto out;
3216
3217 status = nfs4_do_create(dir, dentry, data);
3218
3219 nfs4_free_createdata(data);
3220out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return status;
3222}
3223
3224static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3225 struct iattr *sattr)
3226{
3227 struct nfs4_exception exception = { };
3228 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003229
3230 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 do {
3232 err = nfs4_handle_exception(NFS_SERVER(dir),
3233 _nfs4_proc_mkdir(dir, dentry, sattr),
3234 &exception);
3235 } while (exception.retry);
3236 return err;
3237}
3238
3239static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003240 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
3242 struct inode *dir = dentry->d_inode;
3243 struct nfs4_readdir_arg args = {
3244 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003245 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 .pgbase = 0,
3247 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003248 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003249 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 };
3251 struct nfs4_readdir_res res;
3252 struct rpc_message msg = {
3253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3254 .rpc_argp = &args,
3255 .rpc_resp = &res,
3256 .rpc_cred = cred,
3257 };
3258 int status;
3259
Harvey Harrison3110ff82008-05-02 13:42:44 -07003260 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003261 dentry->d_parent->d_name.name,
3262 dentry->d_name.name,
3263 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003264 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003266 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 -05003267 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003268 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003269 status += args.pgbase;
3270 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003271
3272 nfs_invalidate_atime(dir);
3273
Harvey Harrison3110ff82008-05-02 13:42:44 -07003274 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return status;
3276}
3277
3278static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003279 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
3281 struct nfs4_exception exception = { };
3282 int err;
3283 do {
3284 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3285 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003286 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 &exception);
3288 } while (exception.retry);
3289 return err;
3290}
3291
3292static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3293 struct iattr *sattr, dev_t rdev)
3294{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003295 struct nfs4_createdata *data;
3296 int mode = sattr->ia_mode;
3297 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3300 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003301
3302 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3303 if (data == NULL)
3304 goto out;
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003307 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003309 data->arg.ftype = NF4BLK;
3310 data->arg.u.device.specdata1 = MAJOR(rdev);
3311 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 }
3313 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003314 data->arg.ftype = NF4CHR;
3315 data->arg.u.device.specdata1 = MAJOR(rdev);
3316 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003319 status = nfs4_do_create(dir, dentry, data);
3320
3321 nfs4_free_createdata(data);
3322out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return status;
3324}
3325
3326static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3327 struct iattr *sattr, dev_t rdev)
3328{
3329 struct nfs4_exception exception = { };
3330 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003331
3332 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 do {
3334 err = nfs4_handle_exception(NFS_SERVER(dir),
3335 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3336 &exception);
3337 } while (exception.retry);
3338 return err;
3339}
3340
3341static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3342 struct nfs_fsstat *fsstat)
3343{
3344 struct nfs4_statfs_arg args = {
3345 .fh = fhandle,
3346 .bitmask = server->attr_bitmask,
3347 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003348 struct nfs4_statfs_res res = {
3349 .fsstat = fsstat,
3350 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 struct rpc_message msg = {
3352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3353 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003354 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 };
3356
Trond Myklebust0e574af2005-10-27 22:12:38 -04003357 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003358 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
3361static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3362{
3363 struct nfs4_exception exception = { };
3364 int err;
3365 do {
3366 err = nfs4_handle_exception(server,
3367 _nfs4_proc_statfs(server, fhandle, fsstat),
3368 &exception);
3369 } while (exception.retry);
3370 return err;
3371}
3372
3373static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3374 struct nfs_fsinfo *fsinfo)
3375{
3376 struct nfs4_fsinfo_arg args = {
3377 .fh = fhandle,
3378 .bitmask = server->attr_bitmask,
3379 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003380 struct nfs4_fsinfo_res res = {
3381 .fsinfo = fsinfo,
3382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3385 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003386 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 };
3388
Bryan Schumaker7c513052011-03-24 17:12:24 +00003389 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
3392static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3393{
3394 struct nfs4_exception exception = { };
3395 int err;
3396
3397 do {
3398 err = nfs4_handle_exception(server,
3399 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3400 &exception);
3401 } while (exception.retry);
3402 return err;
3403}
3404
3405static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3406{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003407 int error;
3408
Trond Myklebust0e574af2005-10-27 22:12:38 -04003409 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003410 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003411 if (error == 0) {
3412 /* block layout checks this! */
3413 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003414 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003415 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003416
3417 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
3420static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3421 struct nfs_pathconf *pathconf)
3422{
3423 struct nfs4_pathconf_arg args = {
3424 .fh = fhandle,
3425 .bitmask = server->attr_bitmask,
3426 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003427 struct nfs4_pathconf_res res = {
3428 .pathconf = pathconf,
3429 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 struct rpc_message msg = {
3431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3432 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003433 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 };
3435
3436 /* None of the pathconf attributes are mandatory to implement */
3437 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3438 memset(pathconf, 0, sizeof(*pathconf));
3439 return 0;
3440 }
3441
Trond Myklebust0e574af2005-10-27 22:12:38 -04003442 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003443 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
3446static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3447 struct nfs_pathconf *pathconf)
3448{
3449 struct nfs4_exception exception = { };
3450 int err;
3451
3452 do {
3453 err = nfs4_handle_exception(server,
3454 _nfs4_proc_pathconf(server, fhandle, pathconf),
3455 &exception);
3456 } while (exception.retry);
3457 return err;
3458}
3459
Benny Halevyd20581a2011-05-22 19:52:03 +03003460void __nfs4_read_done_cb(struct nfs_read_data *data)
3461{
Fred Isamancd841602012-04-20 14:47:44 -04003462 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003463}
3464
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003465static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Fred Isamancd841602012-04-20 14:47:44 -04003467 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003469 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003470 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003471 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003473
Benny Halevyd20581a2011-05-22 19:52:03 +03003474 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003476 renew_lease(server, data->timestamp);
3477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003480static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3481{
3482
3483 dprintk("--> %s\n", __func__);
3484
3485 if (!nfs4_sequence_done(task, &data->res.seq_res))
3486 return -EAGAIN;
3487
Benny Halevyd20581a2011-05-22 19:52:03 +03003488 return data->read_done_cb ? data->read_done_cb(task, data) :
3489 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003490}
3491
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003492static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003495 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003496 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003497 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003500static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3501{
Fred Isamancd841602012-04-20 14:47:44 -04003502 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003503 &data->args.seq_args,
3504 &data->res.seq_res,
3505 task))
3506 return;
3507 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508}
3509
Fred Isamanb029bc92011-03-03 15:13:42 +00003510static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
Fred Isamancd841602012-04-20 14:47:44 -04003512 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003514 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003515 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003516 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003518 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003520 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003521 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
Fred Isamanb029bc92011-03-03 15:13:42 +00003525static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3526{
3527 if (!nfs4_sequence_done(task, &data->res.seq_res))
3528 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003529 return data->write_done_cb ? data->write_done_cb(task, data) :
3530 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003531}
3532
Trond Myklebust5a37f852012-04-28 14:55:16 -04003533static
3534bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003535{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003536 const struct nfs_pgio_header *hdr = data->header;
3537
3538 /* Don't request attributes for pNFS or O_DIRECT writes */
3539 if (data->ds_clp != NULL || hdr->dreq != NULL)
3540 return false;
3541 /* Otherwise, request attributes if and only if we don't hold
3542 * a delegation
3543 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003544 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003545}
Fred Isamana69aef12011-03-03 15:13:47 +00003546
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003547static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Fred Isamancd841602012-04-20 14:47:44 -04003549 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003550
Trond Myklebust5a37f852012-04-28 14:55:16 -04003551 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003552 data->args.bitmask = NULL;
3553 data->res.fattr = NULL;
3554 } else
3555 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003556
Fred Isamanb029bc92011-03-03 15:13:42 +00003557 if (!data->write_done_cb)
3558 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003559 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 data->timestamp = jiffies;
3561
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003562 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
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
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003566static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3567{
Fred Isamancd841602012-04-20 14:47:44 -04003568 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003569 &data->args.seq_args,
3570 &data->res.seq_res,
3571 task))
3572 return;
3573 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Fred Isaman0b7c0152012-04-20 14:47:39 -04003576static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3577{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3579 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003580 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 return;
3583 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003584}
3585
Fred Isaman0b7c0152012-04-20 14:47:39 -04003586static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
3588 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003589
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003590 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003591 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003592 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Fred Isaman0b7c0152012-04-20 14:47:39 -04003597static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003598{
3599 if (!nfs4_sequence_done(task, &data->res.seq_res))
3600 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003601 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003602}
3603
Fred Isaman0b7c0152012-04-20 14:47:39 -04003604static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003606 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003607
Fred Isaman0b7c0152012-04-20 14:47:39 -04003608 if (data->commit_done_cb == NULL)
3609 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003610 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003611 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003612 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613}
3614
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003615struct nfs4_renewdata {
3616 struct nfs_client *client;
3617 unsigned long timestamp;
3618};
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620/*
3621 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3622 * standalone procedure for queueing an asynchronous RENEW.
3623 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003624static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003625{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003626 struct nfs4_renewdata *data = calldata;
3627 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003628
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003629 if (atomic_read(&clp->cl_count) > 1)
3630 nfs4_schedule_state_renewal(clp);
3631 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003632 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003633}
3634
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003635static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003637 struct nfs4_renewdata *data = calldata;
3638 struct nfs_client *clp = data->client;
3639 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003642 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003643 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3644 return;
3645 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003646 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003647 return;
3648 }
3649 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003651 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003654static const struct rpc_call_ops nfs4_renew_ops = {
3655 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003656 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003657};
3658
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003659static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
3661 struct rpc_message msg = {
3662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3663 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003664 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003666 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003668 if (renew_flags == 0)
3669 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003670 if (!atomic_inc_not_zero(&clp->cl_count))
3671 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003672 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003673 if (data == NULL)
3674 return -ENOMEM;
3675 data->client = clp;
3676 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003678 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003681static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
3683 struct rpc_message msg = {
3684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3685 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003686 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 };
3688 unsigned long now = jiffies;
3689 int status;
3690
3691 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3692 if (status < 0)
3693 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003694 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return 0;
3696}
3697
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003698static inline int nfs4_server_supports_acls(struct nfs_server *server)
3699{
3700 return (server->caps & NFS_CAP_ACLS)
3701 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3702 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3703}
3704
Trond Myklebust21f498c2012-08-24 10:59:25 -04003705/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3706 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003707 * the stack.
3708 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003709#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003710
Neil Hormane9e3d722011-03-04 19:26:03 -05003711static int buf_to_pages_noslab(const void *buf, size_t buflen,
3712 struct page **pages, unsigned int *pgbase)
3713{
3714 struct page *newpage, **spages;
3715 int rc = 0;
3716 size_t len;
3717 spages = pages;
3718
3719 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003720 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003721 newpage = alloc_page(GFP_KERNEL);
3722
3723 if (newpage == NULL)
3724 goto unwind;
3725 memcpy(page_address(newpage), buf, len);
3726 buf += len;
3727 buflen -= len;
3728 *pages++ = newpage;
3729 rc++;
3730 } while (buflen != 0);
3731
3732 return rc;
3733
3734unwind:
3735 for(; rc > 0; rc--)
3736 __free_page(spages[rc-1]);
3737 return -ENOMEM;
3738}
3739
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740struct nfs4_cached_acl {
3741 int cached;
3742 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003743 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744};
3745
3746static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003747{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003748 struct nfs_inode *nfsi = NFS_I(inode);
3749
3750 spin_lock(&inode->i_lock);
3751 kfree(nfsi->nfs4_acl);
3752 nfsi->nfs4_acl = acl;
3753 spin_unlock(&inode->i_lock);
3754}
3755
3756static void nfs4_zap_acl_attr(struct inode *inode)
3757{
3758 nfs4_set_cached_acl(inode, NULL);
3759}
3760
3761static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3762{
3763 struct nfs_inode *nfsi = NFS_I(inode);
3764 struct nfs4_cached_acl *acl;
3765 int ret = -ENOENT;
3766
3767 spin_lock(&inode->i_lock);
3768 acl = nfsi->nfs4_acl;
3769 if (acl == NULL)
3770 goto out;
3771 if (buf == NULL) /* user is just asking for length */
3772 goto out_len;
3773 if (acl->cached == 0)
3774 goto out;
3775 ret = -ERANGE; /* see getxattr(2) man page */
3776 if (acl->len > buflen)
3777 goto out;
3778 memcpy(buf, acl->data, acl->len);
3779out_len:
3780 ret = acl->len;
3781out:
3782 spin_unlock(&inode->i_lock);
3783 return ret;
3784}
3785
Sachin Prabhu5794d212012-04-17 14:36:40 +01003786static 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 +00003787{
3788 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003789 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003790
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003791 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003792 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003793 if (acl == NULL)
3794 goto out;
3795 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003796 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003797 } else {
3798 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3799 if (acl == NULL)
3800 goto out;
3801 acl->cached = 0;
3802 }
3803 acl->len = acl_len;
3804out:
3805 nfs4_set_cached_acl(inode, acl);
3806}
3807
Andy Adamsonbf118a32011-12-07 11:55:27 -05003808/*
3809 * The getxattr API returns the required buffer length when called with a
3810 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3811 * the required buf. On a NULL buf, we send a page of data to the server
3812 * guessing that the ACL request can be serviced by a page. If so, we cache
3813 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3814 * the cache. If not so, we throw away the page, and cache the required
3815 * length. The next getxattr call will then produce another round trip to
3816 * the server, this time with the input buf of the required size.
3817 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003818static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003819{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003820 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003821 struct nfs_getaclargs args = {
3822 .fh = NFS_FH(inode),
3823 .acl_pages = pages,
3824 .acl_len = buflen,
3825 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003826 struct nfs_getaclres res = {
3827 .acl_len = buflen,
3828 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003829 struct rpc_message msg = {
3830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3831 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003832 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003833 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003834 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3835 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003836
Andy Adamsonbf118a32011-12-07 11:55:27 -05003837 /* As long as we're doing a round trip to the server anyway,
3838 * let's be prepared for a page of acl data. */
3839 if (npages == 0)
3840 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003841 if (npages > ARRAY_SIZE(pages))
3842 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003843
Andy Adamsonbf118a32011-12-07 11:55:27 -05003844 for (i = 0; i < npages; i++) {
3845 pages[i] = alloc_page(GFP_KERNEL);
3846 if (!pages[i])
3847 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003848 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003849
3850 /* for decoding across pages */
3851 res.acl_scratch = alloc_page(GFP_KERNEL);
3852 if (!res.acl_scratch)
3853 goto out_free;
3854
Andy Adamsonbf118a32011-12-07 11:55:27 -05003855 args.acl_len = npages * PAGE_SIZE;
3856 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003857
Peng Taode040be2012-01-10 22:42:47 +08003858 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003859 __func__, buf, buflen, npages, args.acl_len);
3860 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3861 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862 if (ret)
3863 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003864
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003865 /* Handle the case where the passed-in buffer is too short */
3866 if (res.acl_flags & NFS4_ACL_TRUNC) {
3867 /* Did the user only issue a request for the acl length? */
3868 if (buf == NULL)
3869 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003870 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003871 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003872 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003873 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3874 if (buf)
3875 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3876out_ok:
3877 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003878out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003879 for (i = 0; i < npages; i++)
3880 if (pages[i])
3881 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003882 if (res.acl_scratch)
3883 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003884 return ret;
3885}
3886
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003887static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3888{
3889 struct nfs4_exception exception = { };
3890 ssize_t ret;
3891 do {
3892 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3893 if (ret >= 0)
3894 break;
3895 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3896 } while (exception.retry);
3897 return ret;
3898}
3899
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003900static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3901{
3902 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003903 int ret;
3904
3905 if (!nfs4_server_supports_acls(server))
3906 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003907 ret = nfs_revalidate_inode(server, inode);
3908 if (ret < 0)
3909 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003910 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3911 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003912 ret = nfs4_read_cached_acl(inode, buf, buflen);
3913 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003914 /* -ENOENT is returned if there is no ACL or if there is an ACL
3915 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003916 return ret;
3917 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003918}
3919
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003920static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003921{
3922 struct nfs_server *server = NFS_SERVER(inode);
3923 struct page *pages[NFS4ACL_MAXPAGES];
3924 struct nfs_setaclargs arg = {
3925 .fh = NFS_FH(inode),
3926 .acl_pages = pages,
3927 .acl_len = buflen,
3928 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003929 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003930 struct rpc_message msg = {
3931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3932 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003933 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003934 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003935 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003936 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003937
3938 if (!nfs4_server_supports_acls(server))
3939 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003940 if (npages > ARRAY_SIZE(pages))
3941 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003942 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3943 if (i < 0)
3944 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003945 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003946 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003947
3948 /*
3949 * Free each page after tx, so the only ref left is
3950 * held by the network stack
3951 */
3952 for (; i > 0; i--)
3953 put_page(pages[i-1]);
3954
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003955 /*
3956 * Acl update can result in inode attribute update.
3957 * so mark the attribute cache invalid.
3958 */
3959 spin_lock(&inode->i_lock);
3960 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3961 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003962 nfs_access_zap_cache(inode);
3963 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003964 return ret;
3965}
3966
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003967static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3968{
3969 struct nfs4_exception exception = { };
3970 int err;
3971 do {
3972 err = nfs4_handle_exception(NFS_SERVER(inode),
3973 __nfs4_proc_set_acl(inode, buf, buflen),
3974 &exception);
3975 } while (exception.retry);
3976 return err;
3977}
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003980nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003982 struct nfs_client *clp = server->nfs_client;
3983
3984 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 return 0;
3986 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003987 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003988 case -NFS4ERR_ADMIN_REVOKED:
3989 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003990 if (state == NULL)
3991 break;
3992 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003993 case -NFS4ERR_OPENMODE:
3994 if (state == NULL)
3995 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003996 nfs4_schedule_stateid_recovery(server, state);
3997 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003998 case -NFS4ERR_EXPIRED:
3999 if (state != NULL)
4000 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004002 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004003 nfs4_schedule_lease_recovery(clp);
4004 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004005#if defined(CONFIG_NFS_V4_1)
4006 case -NFS4ERR_BADSESSION:
4007 case -NFS4ERR_BADSLOT:
4008 case -NFS4ERR_BAD_HIGH_SLOT:
4009 case -NFS4ERR_DEADSESSION:
4010 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4011 case -NFS4ERR_SEQ_FALSE_RETRY:
4012 case -NFS4ERR_SEQ_MISORDERED:
4013 dprintk("%s ERROR %d, Reset session\n", __func__,
4014 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004015 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004016 task->tk_status = 0;
4017 return -EAGAIN;
4018#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004020 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004021 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004022 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4024 task->tk_status = 0;
4025 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004026 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 case -NFS4ERR_OLD_STATEID:
4028 task->tk_status = 0;
4029 return -EAGAIN;
4030 }
4031 task->tk_status = nfs4_map_errors(task->tk_status);
4032 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004033wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004034 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004035 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4036 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4037 task->tk_status = 0;
4038 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Chuck Leverf0920752012-05-21 22:45:41 -04004041static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4042 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004043{
4044 __be32 verf[2];
4045
Chuck Lever2c820d92012-05-21 22:45:33 -04004046 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4047 /* An impossible timestamp guarantees this value
4048 * will never match a generated boot time. */
4049 verf[0] = 0;
4050 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4051 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004052 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4053 verf[0] = (__be32)nn->boot_time.tv_sec;
4054 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004055 }
Chuck Levercd937102012-03-02 17:14:31 -05004056 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4057}
4058
Chuck Levere984a552012-09-14 17:24:21 -04004059static unsigned int
4060nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4061 char *buf, size_t len)
4062{
4063 unsigned int result;
4064
4065 rcu_read_lock();
4066 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4067 clp->cl_ipaddr,
4068 rpc_peeraddr2str(clp->cl_rpcclient,
4069 RPC_DISPLAY_ADDR),
4070 rpc_peeraddr2str(clp->cl_rpcclient,
4071 RPC_DISPLAY_PROTO));
4072 rcu_read_unlock();
4073 return result;
4074}
4075
4076static unsigned int
4077nfs4_init_uniform_client_string(const struct nfs_client *clp,
4078 char *buf, size_t len)
4079{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004080 char *nodename = clp->cl_rpcclient->cl_nodename;
4081
4082 if (nfs4_client_id_uniquifier[0] != '\0')
4083 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004084 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4085 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004086 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004087}
4088
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004089/**
4090 * nfs4_proc_setclientid - Negotiate client ID
4091 * @clp: state data structure
4092 * @program: RPC program for NFSv4 callback service
4093 * @port: IP port number for NFS4 callback service
4094 * @cred: RPC credential to use for this call
4095 * @res: where to place the result
4096 *
4097 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4098 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004099int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4100 unsigned short port, struct rpc_cred *cred,
4101 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
4103 nfs4_verifier sc_verifier;
4104 struct nfs4_setclientid setclientid = {
4105 .sc_verifier = &sc_verifier,
4106 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004107 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 };
4109 struct rpc_message msg = {
4110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4111 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004112 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004113 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004115 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Chuck Leverde734832012-07-11 16:30:50 -04004117 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004118 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004119 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4120 setclientid.sc_name_len =
4121 nfs4_init_uniform_client_string(clp,
4122 setclientid.sc_name,
4123 sizeof(setclientid.sc_name));
4124 else
4125 setclientid.sc_name_len =
4126 nfs4_init_nonuniform_client_string(clp,
4127 setclientid.sc_name,
4128 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004129 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004130 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004131 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004132 sizeof(setclientid.sc_netid),
4133 rpc_peeraddr2str(clp->cl_rpcclient,
4134 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004135 rcu_read_unlock();
4136 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004137 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 clp->cl_ipaddr, port >> 8, port & 255);
4139
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004140 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4141 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4142 setclientid.sc_name_len, setclientid.sc_name);
4143 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4144 dprintk("NFS reply setclientid: %d\n", status);
4145 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004148/**
4149 * nfs4_proc_setclientid_confirm - Confirm client ID
4150 * @clp: state data structure
4151 * @res: result of a previous SETCLIENTID
4152 * @cred: RPC credential to use for this call
4153 *
4154 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4155 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004156int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004157 struct nfs4_setclientid_res *arg,
4158 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
4160 struct nfs_fsinfo fsinfo;
4161 struct rpc_message msg = {
4162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004163 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004165 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 };
4167 unsigned long now;
4168 int status;
4169
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004170 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4171 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4172 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004174 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 if (status == 0) {
4176 spin_lock(&clp->cl_lock);
4177 clp->cl_lease_time = fsinfo.lease_time * HZ;
4178 clp->cl_last_renewal = now;
4179 spin_unlock(&clp->cl_lock);
4180 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004181 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return status;
4183}
4184
Trond Myklebustfe650402006-01-03 09:55:18 +01004185struct nfs4_delegreturndata {
4186 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004187 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004188 struct nfs_fh fh;
4189 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004190 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004191 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004192 int rpc_status;
4193};
4194
Trond Myklebustfe650402006-01-03 09:55:18 +01004195static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4196{
4197 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004198
Trond Myklebust14516c32010-07-31 14:29:06 -04004199 if (!nfs4_sequence_done(task, &data->res.seq_res))
4200 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004201
Ricardo Labiaga79708862009-12-07 09:23:21 -05004202 switch (task->tk_status) {
4203 case -NFS4ERR_STALE_STATEID:
4204 case -NFS4ERR_EXPIRED:
4205 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004206 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004207 break;
4208 default:
4209 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4210 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004211 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004212 return;
4213 }
4214 }
4215 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004216}
4217
4218static void nfs4_delegreturn_release(void *calldata)
4219{
Trond Myklebustfe650402006-01-03 09:55:18 +01004220 kfree(calldata);
4221}
4222
Andy Adamson938e1012009-04-01 09:22:28 -04004223#if defined(CONFIG_NFS_V4_1)
4224static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4225{
4226 struct nfs4_delegreturndata *d_data;
4227
4228 d_data = (struct nfs4_delegreturndata *)data;
4229
Trond Myklebust035168a2010-06-16 09:52:26 -04004230 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004231 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004232 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004233 return;
4234 rpc_call_start(task);
4235}
4236#endif /* CONFIG_NFS_V4_1 */
4237
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004238static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004239#if defined(CONFIG_NFS_V4_1)
4240 .rpc_call_prepare = nfs4_delegreturn_prepare,
4241#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004242 .rpc_call_done = nfs4_delegreturn_done,
4243 .rpc_release = nfs4_delegreturn_release,
4244};
4245
Trond Myklebuste6f81072008-01-24 18:14:34 -05004246static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004247{
4248 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004249 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004250 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004251 struct rpc_message msg = {
4252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4253 .rpc_cred = cred,
4254 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004255 struct rpc_task_setup task_setup_data = {
4256 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004257 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004258 .callback_ops = &nfs4_delegreturn_ops,
4259 .flags = RPC_TASK_ASYNC,
4260 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004261 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004262
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004263 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004264 if (data == NULL)
4265 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004266 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004267 data->args.fhandle = &data->fh;
4268 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004269 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004270 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004271 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004272 data->res.fattr = &data->fattr;
4273 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004274 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004275 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004276 data->rpc_status = 0;
4277
Trond Myklebustc970aa82007-07-14 15:39:59 -04004278 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004279 msg.rpc_argp = &data->args;
4280 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004281 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004282 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004283 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004284 if (!issync)
4285 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004286 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004287 if (status != 0)
4288 goto out;
4289 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004290 if (status == 0)
4291 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4292 else
4293 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004294out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004295 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004296 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
Trond Myklebuste6f81072008-01-24 18:14:34 -05004299int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 struct nfs_server *server = NFS_SERVER(inode);
4302 struct nfs4_exception exception = { };
4303 int err;
4304 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004305 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 switch (err) {
4307 case -NFS4ERR_STALE_STATEID:
4308 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 case 0:
4310 return 0;
4311 }
4312 err = nfs4_handle_exception(server, err, &exception);
4313 } while (exception.retry);
4314 return err;
4315}
4316
4317#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4318#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4319
4320/*
4321 * sleep, with exponential backoff, and retry the LOCK operation.
4322 */
4323static unsigned long
4324nfs4_set_lock_task_retry(unsigned long timeout)
4325{
Jeff Laytond3103102011-12-01 22:44:39 +01004326 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 timeout <<= 1;
4328 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4329 return NFS4_LOCK_MAXTIMEOUT;
4330 return timeout;
4331}
4332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4334{
4335 struct inode *inode = state->inode;
4336 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004337 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004338 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004340 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004342 struct nfs_lockt_res res = {
4343 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 };
4345 struct rpc_message msg = {
4346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4347 .rpc_argp = &arg,
4348 .rpc_resp = &res,
4349 .rpc_cred = state->owner->so_cred,
4350 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 struct nfs4_lock_state *lsp;
4352 int status;
4353
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004354 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004355 status = nfs4_set_lock_state(state, request);
4356 if (status != 0)
4357 goto out;
4358 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004359 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004360 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004361 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004362 switch (status) {
4363 case 0:
4364 request->fl_type = F_UNLCK;
4365 break;
4366 case -NFS4ERR_DENIED:
4367 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004369 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004370out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 return status;
4372}
4373
4374static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4375{
4376 struct nfs4_exception exception = { };
4377 int err;
4378
4379 do {
4380 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4381 _nfs4_proc_getlk(state, cmd, request),
4382 &exception);
4383 } while (exception.retry);
4384 return err;
4385}
4386
4387static int do_vfs_lock(struct file *file, struct file_lock *fl)
4388{
4389 int res = 0;
4390 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4391 case FL_POSIX:
4392 res = posix_lock_file_wait(file, fl);
4393 break;
4394 case FL_FLOCK:
4395 res = flock_lock_file_wait(file, fl);
4396 break;
4397 default:
4398 BUG();
4399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 return res;
4401}
4402
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004404 struct nfs_locku_args arg;
4405 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 struct nfs4_lock_state *lsp;
4407 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004408 struct file_lock fl;
4409 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004410 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004411};
4412
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004413static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4414 struct nfs_open_context *ctx,
4415 struct nfs4_lock_state *lsp,
4416 struct nfs_seqid *seqid)
4417{
4418 struct nfs4_unlockdata *p;
4419 struct inode *inode = lsp->ls_state->inode;
4420
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004421 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004422 if (p == NULL)
4423 return NULL;
4424 p->arg.fh = NFS_FH(inode);
4425 p->arg.fl = &p->fl;
4426 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004427 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004428 p->arg.stateid = &lsp->ls_stateid;
4429 p->lsp = lsp;
4430 atomic_inc(&lsp->ls_count);
4431 /* Ensure we don't close file until we're done freeing locks! */
4432 p->ctx = get_nfs_open_context(ctx);
4433 memcpy(&p->fl, fl, sizeof(p->fl));
4434 p->server = NFS_SERVER(inode);
4435 return p;
4436}
4437
Trond Myklebust06f814a2006-01-03 09:55:07 +01004438static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004439{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004440 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004441 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004442 nfs4_put_lock_state(calldata->lsp);
4443 put_nfs_open_context(calldata->ctx);
4444 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004445}
4446
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004447static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004448{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004449 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004450
Trond Myklebust14516c32010-07-31 14:29:06 -04004451 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4452 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004453 switch (task->tk_status) {
4454 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004455 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4456 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004457 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004458 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004459 case -NFS4ERR_BAD_STATEID:
4460 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004461 case -NFS4ERR_STALE_STATEID:
4462 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004463 break;
4464 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004465 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004466 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004467 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004468}
4469
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004470static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004471{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004472 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004474 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004475 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004476 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004477 /* Note: exit _without_ running nfs4_locku_done */
4478 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004479 return;
4480 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004481 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004482 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004483 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004484 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004485 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004486 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004489static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004490 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004491 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004492 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004493};
4494
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004495static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4496 struct nfs_open_context *ctx,
4497 struct nfs4_lock_state *lsp,
4498 struct nfs_seqid *seqid)
4499{
4500 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004501 struct rpc_message msg = {
4502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4503 .rpc_cred = ctx->cred,
4504 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004505 struct rpc_task_setup task_setup_data = {
4506 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004507 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004508 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004509 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004510 .flags = RPC_TASK_ASYNC,
4511 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512
Frank Filz137d6ac2007-07-09 15:32:29 -07004513 /* Ensure this is an unlock - when canceling a lock, the
4514 * canceled lock is passed in, and it won't be an unlock.
4515 */
4516 fl->fl_type = F_UNLCK;
4517
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4519 if (data == NULL) {
4520 nfs_free_seqid(seqid);
4521 return ERR_PTR(-ENOMEM);
4522 }
4523
Trond Myklebust9d12b212012-01-17 22:04:25 -05004524 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004525 msg.rpc_argp = &data->arg;
4526 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004527 task_setup_data.callback_data = data;
4528 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529}
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4532{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004533 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004534 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004535 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004536 struct rpc_task *task;
4537 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004538 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Trond Myklebust9b073572006-06-29 16:38:34 -04004540 status = nfs4_set_lock_state(state, request);
4541 /* Unlock _before_ we do the RPC call */
4542 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004543 down_read(&nfsi->rwsem);
4544 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4545 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004546 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004547 }
4548 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004549 if (status != 0)
4550 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004551 /* Is this a delegated lock? */
4552 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004553 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004554 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004555 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004556 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004557 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004558 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004559 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004560 status = PTR_ERR(task);
4561 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004562 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004563 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004564 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004565out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004566 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004567 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568}
4569
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004570struct nfs4_lockdata {
4571 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004572 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004573 struct nfs4_lock_state *lsp;
4574 struct nfs_open_context *ctx;
4575 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004576 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577 int rpc_status;
4578 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004579 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580};
4581
4582static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004583 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4584 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585{
4586 struct nfs4_lockdata *p;
4587 struct inode *inode = lsp->ls_state->inode;
4588 struct nfs_server *server = NFS_SERVER(inode);
4589
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004590 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591 if (p == NULL)
4592 return NULL;
4593
4594 p->arg.fh = NFS_FH(inode);
4595 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004596 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004597 if (p->arg.open_seqid == NULL)
4598 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004599 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004600 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004601 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004603 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004604 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004605 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004606 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004607 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004608 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609 atomic_inc(&lsp->ls_count);
4610 p->ctx = get_nfs_open_context(ctx);
4611 memcpy(&p->fl, fl, sizeof(p->fl));
4612 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004613out_free_seqid:
4614 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004615out_free:
4616 kfree(p);
4617 return NULL;
4618}
4619
4620static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4621{
4622 struct nfs4_lockdata *data = calldata;
4623 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Harvey Harrison3110ff82008-05-02 13:42:44 -07004625 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004626 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4627 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004628 /* Do we need to do an open_to_lock_owner? */
4629 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004630 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4631 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632 data->arg.open_stateid = &state->stateid;
4633 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004634 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004635 } else
4636 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004637 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004638 if (nfs4_setup_sequence(data->server,
4639 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004640 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004641 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004642 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004643 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004644}
4645
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004646static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4647{
4648 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4649 nfs4_lock_prepare(task, calldata);
4650}
4651
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4653{
4654 struct nfs4_lockdata *data = calldata;
4655
Harvey Harrison3110ff82008-05-02 13:42:44 -07004656 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657
Trond Myklebust14516c32010-07-31 14:29:06 -04004658 if (!nfs4_sequence_done(task, &data->res.seq_res))
4659 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004660
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004661 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004662 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004663 if (data->rpc_status == 0)
4664 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4665 else
4666 goto out;
4667 }
4668 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004669 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004670 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004671 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004674 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675}
4676
4677static void nfs4_lock_release(void *calldata)
4678{
4679 struct nfs4_lockdata *data = calldata;
4680
Harvey Harrison3110ff82008-05-02 13:42:44 -07004681 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004682 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683 if (data->cancelled != 0) {
4684 struct rpc_task *task;
4685 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4686 data->arg.lock_seqid);
4687 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004688 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004689 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004690 } else
4691 nfs_free_seqid(data->arg.lock_seqid);
4692 nfs4_put_lock_state(data->lsp);
4693 put_nfs_open_context(data->ctx);
4694 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004695 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004696}
4697
4698static const struct rpc_call_ops nfs4_lock_ops = {
4699 .rpc_call_prepare = nfs4_lock_prepare,
4700 .rpc_call_done = nfs4_lock_done,
4701 .rpc_release = nfs4_lock_release,
4702};
4703
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004704static const struct rpc_call_ops nfs4_recover_lock_ops = {
4705 .rpc_call_prepare = nfs4_recover_lock_prepare,
4706 .rpc_call_done = nfs4_lock_done,
4707 .rpc_release = nfs4_lock_release,
4708};
4709
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004710static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4711{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004712 switch (error) {
4713 case -NFS4ERR_ADMIN_REVOKED:
4714 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004715 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004716 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004717 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004718 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004719 break;
4720 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004721 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004722 case -NFS4ERR_EXPIRED:
4723 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004724 };
4725}
4726
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004727static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004728{
4729 struct nfs4_lockdata *data;
4730 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004731 struct rpc_message msg = {
4732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4733 .rpc_cred = state->owner->so_cred,
4734 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004735 struct rpc_task_setup task_setup_data = {
4736 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004737 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004738 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004739 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004740 .flags = RPC_TASK_ASYNC,
4741 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004742 int ret;
4743
Harvey Harrison3110ff82008-05-02 13:42:44 -07004744 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004745 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004746 fl->fl_u.nfs4_fl.owner,
4747 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004748 if (data == NULL)
4749 return -ENOMEM;
4750 if (IS_SETLKW(cmd))
4751 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004752 if (recovery_type > NFS_LOCK_NEW) {
4753 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004754 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004755 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4756 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004757 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004758 msg.rpc_argp = &data->arg;
4759 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004760 task_setup_data.callback_data = data;
4761 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004762 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004763 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764 ret = nfs4_wait_for_completion_rpc_task(task);
4765 if (ret == 0) {
4766 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004767 if (ret)
4768 nfs4_handle_setlk_error(data->server, data->lsp,
4769 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004770 } else
4771 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004772 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004773 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004774 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776
4777static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4778{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004779 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004780 struct nfs4_exception exception = {
4781 .inode = state->inode,
4782 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004783 int err;
4784
4785 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004786 /* Cache the lock if possible... */
4787 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4788 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004789 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004790 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004791 break;
4792 nfs4_handle_exception(server, err, &exception);
4793 } while (exception.retry);
4794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795}
4796
4797static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4798{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004799 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004800 struct nfs4_exception exception = {
4801 .inode = state->inode,
4802 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004803 int err;
4804
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004805 err = nfs4_set_lock_state(state, request);
4806 if (err != 0)
4807 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004808 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004809 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4810 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004811 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004812 switch (err) {
4813 default:
4814 goto out;
4815 case -NFS4ERR_GRACE:
4816 case -NFS4ERR_DELAY:
4817 nfs4_handle_exception(server, err, &exception);
4818 err = 0;
4819 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004820 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004821out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004825#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004826/**
4827 * nfs41_check_expired_locks - possibly free a lock stateid
4828 *
4829 * @state: NFSv4 state for an inode
4830 *
4831 * Returns NFS_OK if recovery for this stateid is now finished.
4832 * Otherwise a negative NFS4ERR value is returned.
4833 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004834static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004835{
Chuck Levereb64cf92012-07-11 16:30:05 -04004836 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004837 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004838 struct nfs_server *server = NFS_SERVER(state->inode);
4839
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004840 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004841 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004842 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4843 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004844 /* Free the stateid unless the server
4845 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004846 if (status != -NFS4ERR_BAD_STATEID)
4847 nfs41_free_stateid(server,
4848 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004849 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004850 ret = status;
4851 }
4852 }
4853 };
4854
4855 return ret;
4856}
4857
4858static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4859{
4860 int status = NFS_OK;
4861
4862 if (test_bit(LK_STATE_IN_USE, &state->flags))
4863 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004864 if (status != NFS_OK)
4865 status = nfs4_lock_expired(state, request);
4866 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004867}
4868#endif
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4871{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004872 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004873 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004874 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004876 if ((fl_flags & FL_POSIX) &&
4877 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4878 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004879 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004880 status = nfs4_set_lock_state(state, request);
4881 if (status != 0)
4882 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004883 request->fl_flags |= FL_ACCESS;
4884 status = do_vfs_lock(request->fl_file, request);
4885 if (status < 0)
4886 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004887 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004888 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004889 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004890 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004891 request->fl_flags = fl_flags & ~FL_SLEEP;
4892 status = do_vfs_lock(request->fl_file, request);
4893 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004894 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004895 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004896 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004897 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004898 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004899 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004900 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004901 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4902 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004903out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004904 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004905out:
4906 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return status;
4908}
4909
4910static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4911{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004912 struct nfs4_exception exception = {
4913 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004914 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004915 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 int err;
4917
4918 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004919 err = _nfs4_proc_setlk(state, cmd, request);
4920 if (err == -NFS4ERR_DENIED)
4921 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004923 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 } while (exception.retry);
4925 return err;
4926}
4927
4928static int
4929nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4930{
4931 struct nfs_open_context *ctx;
4932 struct nfs4_state *state;
4933 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4934 int status;
4935
4936 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004937 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 state = ctx->state;
4939
4940 if (request->fl_start < 0 || request->fl_end < 0)
4941 return -EINVAL;
4942
Trond Myklebustd9531262009-07-21 19:22:38 -04004943 if (IS_GETLK(cmd)) {
4944 if (state != NULL)
4945 return nfs4_proc_getlk(state, F_GETLK, request);
4946 return 0;
4947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4950 return -EINVAL;
4951
Trond Myklebustd9531262009-07-21 19:22:38 -04004952 if (request->fl_type == F_UNLCK) {
4953 if (state != NULL)
4954 return nfs4_proc_unlck(state, cmd, request);
4955 return 0;
4956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Trond Myklebustd9531262009-07-21 19:22:38 -04004958 if (state == NULL)
4959 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004960 /*
4961 * Don't rely on the VFS having checked the file open mode,
4962 * since it won't do this for flock() locks.
4963 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004964 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004965 case F_RDLCK:
4966 if (!(filp->f_mode & FMODE_READ))
4967 return -EBADF;
4968 break;
4969 case F_WRLCK:
4970 if (!(filp->f_mode & FMODE_WRITE))
4971 return -EBADF;
4972 }
4973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 do {
4975 status = nfs4_proc_setlk(state, cmd, request);
4976 if ((status != -EAGAIN) || IS_SETLK(cmd))
4977 break;
4978 timeout = nfs4_set_lock_task_retry(timeout);
4979 status = -ERESTARTSYS;
4980 if (signalled())
4981 break;
4982 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 return status;
4984}
4985
Trond Myklebust888e6942005-11-04 15:38:11 -05004986int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4987{
4988 struct nfs_server *server = NFS_SERVER(state->inode);
4989 struct nfs4_exception exception = { };
4990 int err;
4991
4992 err = nfs4_set_lock_state(state, fl);
4993 if (err != 0)
4994 goto out;
4995 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004996 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004997 switch (err) {
4998 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004999 printk(KERN_ERR "NFS: %s: unhandled error "
5000 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005001 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005002 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005003 goto out;
5004 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005005 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005006 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005007 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005008 nfs4_schedule_lease_recovery(server->nfs_client);
5009 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005010 case -NFS4ERR_BADSESSION:
5011 case -NFS4ERR_BADSLOT:
5012 case -NFS4ERR_BAD_HIGH_SLOT:
5013 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5014 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005015 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005016 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005017 case -ERESTARTSYS:
5018 /*
5019 * The show must go on: exit, but mark the
5020 * stateid as needing recovery.
5021 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005022 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005023 case -NFS4ERR_ADMIN_REVOKED:
5024 case -NFS4ERR_BAD_STATEID:
5025 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005026 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005027 err = 0;
5028 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005029 case -EKEYEXPIRED:
5030 /*
5031 * User RPCSEC_GSS context has expired.
5032 * We cannot recover this stateid now, so
5033 * skip it and allow recovery thread to
5034 * proceed.
5035 */
5036 err = 0;
5037 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005038 case -ENOMEM:
5039 case -NFS4ERR_DENIED:
5040 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5041 err = 0;
5042 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005043 case -NFS4ERR_DELAY:
5044 break;
5045 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005046 err = nfs4_handle_exception(server, err, &exception);
5047 } while (exception.retry);
5048out:
5049 return err;
5050}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005051
Trond Myklebustcf470c32012-03-07 13:49:12 -05005052struct nfs_release_lockowner_data {
5053 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005054 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005055 struct nfs_release_lockowner_args args;
5056};
5057
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005058static void nfs4_release_lockowner_release(void *calldata)
5059{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005060 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005061 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005062 kfree(calldata);
5063}
5064
Trond Myklebust17280172012-03-11 13:11:00 -04005065static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005066 .rpc_release = nfs4_release_lockowner_release,
5067};
5068
Trond Myklebustcf470c32012-03-07 13:49:12 -05005069int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005070{
5071 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005072 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005073 struct rpc_message msg = {
5074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5075 };
5076
5077 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005078 return -EINVAL;
5079 data = kmalloc(sizeof(*data), GFP_NOFS);
5080 if (!data)
5081 return -ENOMEM;
5082 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005083 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005084 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5085 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5086 data->args.lock_owner.s_dev = server->s_dev;
5087 msg.rpc_argp = &data->args;
5088 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5089 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005090}
5091
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005092#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5093
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005094static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5095 const void *buf, size_t buflen,
5096 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005097{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005098 if (strcmp(key, "") != 0)
5099 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005100
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005101 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005102}
5103
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005104static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5105 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005106{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005107 if (strcmp(key, "") != 0)
5108 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005109
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005110 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005111}
5112
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005113static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5114 size_t list_len, const char *name,
5115 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005116{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005117 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005118
J. Bruce Fields096455a2006-03-20 23:23:42 -05005119 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5120 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005121
5122 if (list && len <= list_len)
5123 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005124 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005125}
5126
Andy Adamson533eb462011-06-13 18:25:56 -04005127/*
5128 * nfs_fhget will use either the mounted_on_fileid or the fileid
5129 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005130static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5131{
Andy Adamson533eb462011-06-13 18:25:56 -04005132 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5133 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5134 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005135 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005136 return;
5137
5138 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005139 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005140 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5141 fattr->nlink = 2;
5142}
5143
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005144static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5145 const struct qstr *name,
5146 struct nfs4_fs_locations *fs_locations,
5147 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005148{
5149 struct nfs_server *server = NFS_SERVER(dir);
5150 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005151 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005152 };
5153 struct nfs4_fs_locations_arg args = {
5154 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005155 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005156 .page = page,
5157 .bitmask = bitmask,
5158 };
Benny Halevy22958462009-04-01 09:22:02 -04005159 struct nfs4_fs_locations_res res = {
5160 .fs_locations = fs_locations,
5161 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005162 struct rpc_message msg = {
5163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5164 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005165 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005166 };
5167 int status;
5168
Harvey Harrison3110ff82008-05-02 13:42:44 -07005169 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005170
5171 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5172 * is not supported */
5173 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5174 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5175 else
5176 bitmask[0] |= FATTR4_WORD0_FILEID;
5177
Trond Myklebustc228fd32007-01-13 02:28:11 -05005178 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005179 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005180 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005181 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005182 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005183 return status;
5184}
5185
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005186int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5187 const struct qstr *name,
5188 struct nfs4_fs_locations *fs_locations,
5189 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005190{
5191 struct nfs4_exception exception = { };
5192 int err;
5193 do {
5194 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005195 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005196 &exception);
5197 } while (exception.retry);
5198 return err;
5199}
5200
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005201static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5202{
5203 int status;
5204 struct nfs4_secinfo_arg args = {
5205 .dir_fh = NFS_FH(dir),
5206 .name = name,
5207 };
5208 struct nfs4_secinfo_res res = {
5209 .flavors = flavors,
5210 };
5211 struct rpc_message msg = {
5212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5213 .rpc_argp = &args,
5214 .rpc_resp = &res,
5215 };
5216
5217 dprintk("NFS call secinfo %s\n", name->name);
5218 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5219 dprintk("NFS reply secinfo: %d\n", status);
5220 return status;
5221}
5222
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005223int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5224 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005225{
5226 struct nfs4_exception exception = { };
5227 int err;
5228 do {
5229 err = nfs4_handle_exception(NFS_SERVER(dir),
5230 _nfs4_proc_secinfo(dir, name, flavors),
5231 &exception);
5232 } while (exception.retry);
5233 return err;
5234}
5235
Andy Adamson557134a2009-04-01 09:21:53 -04005236#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005237/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005238 * Check the exchange flags returned by the server for invalid flags, having
5239 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5240 * DS flags set.
5241 */
5242static int nfs4_check_cl_exchange_flags(u32 flags)
5243{
5244 if (flags & ~EXCHGID4_FLAG_MASK_R)
5245 goto out_inval;
5246 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5247 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5248 goto out_inval;
5249 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5250 goto out_inval;
5251 return NFS_OK;
5252out_inval:
5253 return -NFS4ERR_INVAL;
5254}
5255
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005256static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005257nfs41_same_server_scope(struct nfs41_server_scope *a,
5258 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005259{
5260 if (a->server_scope_sz == b->server_scope_sz &&
5261 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5262 return true;
5263
5264 return false;
5265}
5266
Andy Adamson357f54d2010-12-14 10:11:57 -05005267/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005268 * nfs4_proc_bind_conn_to_session()
5269 *
5270 * The 4.1 client currently uses the same TCP connection for the
5271 * fore and backchannel.
5272 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005273int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005274{
5275 int status;
5276 struct nfs41_bind_conn_to_session_res res;
5277 struct rpc_message msg = {
5278 .rpc_proc =
5279 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5280 .rpc_argp = clp,
5281 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005282 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005283 };
5284
5285 dprintk("--> %s\n", __func__);
5286 BUG_ON(clp == NULL);
5287
5288 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5289 if (unlikely(res.session == NULL)) {
5290 status = -ENOMEM;
5291 goto out;
5292 }
5293
5294 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5295 if (status == 0) {
5296 if (memcmp(res.session->sess_id.data,
5297 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5298 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5299 status = -EIO;
5300 goto out_session;
5301 }
5302 if (res.dir != NFS4_CDFS4_BOTH) {
5303 dprintk("NFS: %s: Unexpected direction from server\n",
5304 __func__);
5305 status = -EIO;
5306 goto out_session;
5307 }
5308 if (res.use_conn_in_rdma_mode) {
5309 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5310 __func__);
5311 status = -EIO;
5312 goto out_session;
5313 }
5314 }
5315out_session:
5316 kfree(res.session);
5317out:
5318 dprintk("<-- %s status= %d\n", __func__, status);
5319 return status;
5320}
5321
5322/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005323 * nfs4_proc_exchange_id()
5324 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005325 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5326 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005327 * Since the clientid has expired, all compounds using sessions
5328 * associated with the stale clientid will be returning
5329 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5330 * be in some phase of session reset.
5331 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005332int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005333{
5334 nfs4_verifier verifier;
5335 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005336 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005337 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005338 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005339 };
5340 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005341 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005342 };
5343 int status;
5344 struct rpc_message msg = {
5345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5346 .rpc_argp = &args,
5347 .rpc_resp = &res,
5348 .rpc_cred = cred,
5349 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005350
Chuck Leverf0920752012-05-21 22:45:41 -04005351 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005352 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5353 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005354 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5355 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5356 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005357
Chuck Leveracdeb692012-05-21 22:46:16 -04005358 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005359 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005360 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005361 status = -ENOMEM;
5362 goto out;
5363 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005364
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005365 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005366 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005367 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005368 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005369 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005370 }
5371
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005372 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005373 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005374 status = -ENOMEM;
5375 goto out_server_scope;
5376 }
5377
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005378 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005379 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005380 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005381
Chuck Lever177313f2012-05-21 22:44:58 -04005382 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005383 clp->cl_clientid = res.clientid;
5384 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5385 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5386 clp->cl_seqid = res.seqid;
5387
Chuck Leveracdeb692012-05-21 22:46:16 -04005388 kfree(clp->cl_serverowner);
5389 clp->cl_serverowner = res.server_owner;
5390 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005391
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005392 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005393 kfree(clp->cl_implid);
5394 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005395
Chuck Lever177313f2012-05-21 22:44:58 -04005396 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005397 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005398 res.server_scope)) {
5399 dprintk("%s: server_scope mismatch detected\n",
5400 __func__);
5401 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005402 kfree(clp->cl_serverscope);
5403 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005404 }
5405
Chuck Lever177313f2012-05-21 22:44:58 -04005406 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005407 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005408 goto out;
5409 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005410 } else
5411 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005412
Chuck Leveracdeb692012-05-21 22:46:16 -04005413out_server_owner:
5414 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005415out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005416 kfree(res.server_scope);
5417out:
Chuck Lever177313f2012-05-21 22:44:58 -04005418 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005419 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005420 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005421 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005422 clp->cl_implid->date.seconds,
5423 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005424 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005425 return status;
5426}
5427
Trond Myklebust66245532012-05-25 17:18:09 -04005428static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5429 struct rpc_cred *cred)
5430{
5431 struct rpc_message msg = {
5432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5433 .rpc_argp = clp,
5434 .rpc_cred = cred,
5435 };
5436 int status;
5437
5438 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5439 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005440 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005441 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5442 return status;
5443}
5444
5445static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5446 struct rpc_cred *cred)
5447{
5448 unsigned int loop;
5449 int ret;
5450
5451 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5452 ret = _nfs4_proc_destroy_clientid(clp, cred);
5453 switch (ret) {
5454 case -NFS4ERR_DELAY:
5455 case -NFS4ERR_CLIENTID_BUSY:
5456 ssleep(1);
5457 break;
5458 default:
5459 return ret;
5460 }
5461 }
5462 return 0;
5463}
5464
5465int nfs4_destroy_clientid(struct nfs_client *clp)
5466{
5467 struct rpc_cred *cred;
5468 int ret = 0;
5469
5470 if (clp->cl_mvops->minor_version < 1)
5471 goto out;
5472 if (clp->cl_exchange_flags == 0)
5473 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005474 if (clp->cl_preserve_clid)
5475 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005476 cred = nfs4_get_exchange_id_cred(clp);
5477 ret = nfs4_proc_destroy_clientid(clp, cred);
5478 if (cred)
5479 put_rpccred(cred);
5480 switch (ret) {
5481 case 0:
5482 case -NFS4ERR_STALE_CLIENTID:
5483 clp->cl_exchange_flags = 0;
5484 }
5485out:
5486 return ret;
5487}
5488
Andy Adamson2050f0c2009-04-01 09:22:30 -04005489struct nfs4_get_lease_time_data {
5490 struct nfs4_get_lease_time_args *args;
5491 struct nfs4_get_lease_time_res *res;
5492 struct nfs_client *clp;
5493};
5494
5495static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5496 void *calldata)
5497{
5498 int ret;
5499 struct nfs4_get_lease_time_data *data =
5500 (struct nfs4_get_lease_time_data *)calldata;
5501
5502 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005503 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005504 /* just setup sequence, do not trigger session recovery
5505 since we're invoked within one */
5506 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005507 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005508 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005509
5510 BUG_ON(ret == -EAGAIN);
5511 rpc_call_start(task);
5512 dprintk("<-- %s\n", __func__);
5513}
5514
5515/*
5516 * Called from nfs4_state_manager thread for session setup, so don't recover
5517 * from sequence operation or clientid errors.
5518 */
5519static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5520{
5521 struct nfs4_get_lease_time_data *data =
5522 (struct nfs4_get_lease_time_data *)calldata;
5523
5524 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005525 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5526 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005527 switch (task->tk_status) {
5528 case -NFS4ERR_DELAY:
5529 case -NFS4ERR_GRACE:
5530 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5531 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5532 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005533 /* fall through */
5534 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005535 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005536 return;
5537 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005538 dprintk("<-- %s\n", __func__);
5539}
5540
Trond Myklebust17280172012-03-11 13:11:00 -04005541static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005542 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5543 .rpc_call_done = nfs4_get_lease_time_done,
5544};
5545
5546int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5547{
5548 struct rpc_task *task;
5549 struct nfs4_get_lease_time_args args;
5550 struct nfs4_get_lease_time_res res = {
5551 .lr_fsinfo = fsinfo,
5552 };
5553 struct nfs4_get_lease_time_data data = {
5554 .args = &args,
5555 .res = &res,
5556 .clp = clp,
5557 };
5558 struct rpc_message msg = {
5559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5560 .rpc_argp = &args,
5561 .rpc_resp = &res,
5562 };
5563 struct rpc_task_setup task_setup = {
5564 .rpc_client = clp->cl_rpcclient,
5565 .rpc_message = &msg,
5566 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005567 .callback_data = &data,
5568 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005569 };
5570 int status;
5571
Trond Myklebust9d12b212012-01-17 22:04:25 -05005572 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005573 dprintk("--> %s\n", __func__);
5574 task = rpc_run_task(&task_setup);
5575
5576 if (IS_ERR(task))
5577 status = PTR_ERR(task);
5578 else {
5579 status = task->tk_status;
5580 rpc_put_task(task);
5581 }
5582 dprintk("<-- %s return %d\n", __func__, status);
5583
5584 return status;
5585}
5586
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005587static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5588{
5589 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5590}
5591
5592static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5593 struct nfs4_slot *new,
5594 u32 max_slots,
5595 u32 ivalue)
5596{
5597 struct nfs4_slot *old = NULL;
5598 u32 i;
5599
5600 spin_lock(&tbl->slot_tbl_lock);
5601 if (new) {
5602 old = tbl->slots;
5603 tbl->slots = new;
5604 tbl->max_slots = max_slots;
5605 }
5606 tbl->highest_used_slotid = -1; /* no slot is currently used */
5607 for (i = 0; i < tbl->max_slots; i++)
5608 tbl->slots[i].seq_nr = ivalue;
5609 spin_unlock(&tbl->slot_tbl_lock);
5610 kfree(old);
5611}
5612
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005613/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005614 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005615 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005616static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5617 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005618{
Andy Adamson104aeba2010-01-14 17:45:10 -05005619 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005620 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005621
Andy Adamson104aeba2010-01-14 17:45:10 -05005622 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5623 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005624
Andy Adamson104aeba2010-01-14 17:45:10 -05005625 /* Does the newly negotiated max_reqs match the existing slot table? */
5626 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005627 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005628 if (!new)
5629 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005630 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005631 ret = 0;
5632
5633 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005634 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5635 tbl, tbl->slots, tbl->max_slots);
5636out:
5637 dprintk("<-- %s: return %d\n", __func__, ret);
5638 return ret;
5639}
5640
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005641/* Destroy the slot table */
5642static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5643{
5644 if (session->fc_slot_table.slots != NULL) {
5645 kfree(session->fc_slot_table.slots);
5646 session->fc_slot_table.slots = NULL;
5647 }
5648 if (session->bc_slot_table.slots != NULL) {
5649 kfree(session->bc_slot_table.slots);
5650 session->bc_slot_table.slots = NULL;
5651 }
5652 return;
5653}
5654
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005655/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005656 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005657 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005658static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005659{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005660 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005661 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005662
Andy Adamsonaacd5532011-11-09 13:58:21 -05005663 dprintk("--> %s\n", __func__);
5664 /* Fore channel */
5665 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005666 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5667 if (status) /* -ENOMEM */
5668 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005669 /* Back channel */
5670 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005671 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5672 if (status && tbl->slots == NULL)
5673 /* Fore and back channel share a connection so get
5674 * both slot tables or neither */
5675 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005676 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005677}
5678
5679struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5680{
5681 struct nfs4_session *session;
5682 struct nfs4_slot_table *tbl;
5683
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005684 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005685 if (!session)
5686 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005687
Andy Adamson557134a2009-04-01 09:21:53 -04005688 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005689 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005690 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005691 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005692 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005693
5694 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005695 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005696 spin_lock_init(&tbl->slot_tbl_lock);
5697 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005698 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005699
Trond Myklebust1055d762010-06-16 09:52:27 -04005700 session->session_state = 1<<NFS4_SESSION_INITING;
5701
Andy Adamson557134a2009-04-01 09:21:53 -04005702 session->clp = clp;
5703 return session;
5704}
5705
5706void nfs4_destroy_session(struct nfs4_session *session)
5707{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005708 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005709 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005710
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005711 cred = nfs4_get_exchange_id_cred(session->clp);
5712 nfs4_proc_destroy_session(session, cred);
5713 if (cred)
5714 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005715
5716 rcu_read_lock();
5717 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5718 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005719 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005720 __func__, xprt);
5721 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005722 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005723 kfree(session);
5724}
5725
Andy Adamsonfc931582009-04-01 09:22:31 -04005726/*
5727 * Initialize the values to be used by the client in CREATE_SESSION
5728 * If nfs4_init_session set the fore channel request and response sizes,
5729 * use them.
5730 *
5731 * Set the back channel max_resp_sz_cached to zero to force the client to
5732 * always set csa_cachethis to FALSE because the current implementation
5733 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5734 */
5735static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5736{
5737 struct nfs4_session *session = args->client->cl_session;
5738 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5739 mxresp_sz = session->fc_attrs.max_resp_sz;
5740
5741 if (mxrqst_sz == 0)
5742 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5743 if (mxresp_sz == 0)
5744 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5745 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005746 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5747 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005748 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005749 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005750
5751 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005752 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005753 __func__,
5754 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005755 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005756
5757 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005758 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5759 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5760 args->bc_attrs.max_resp_sz_cached = 0;
5761 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5762 args->bc_attrs.max_reqs = 1;
5763
5764 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5765 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5766 __func__,
5767 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5768 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5769 args->bc_attrs.max_reqs);
5770}
5771
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005772static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005773{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005774 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5775 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5776
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005777 if (rcvd->max_resp_sz > sent->max_resp_sz)
5778 return -EINVAL;
5779 /*
5780 * Our requested max_ops is the minimum we need; we're not
5781 * prepared to break up compounds into smaller pieces than that.
5782 * So, no point even trying to continue if the server won't
5783 * cooperate:
5784 */
5785 if (rcvd->max_ops < sent->max_ops)
5786 return -EINVAL;
5787 if (rcvd->max_reqs == 0)
5788 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005789 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5790 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005791 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005792}
5793
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005794static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5795{
5796 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5797 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005798
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005799 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5800 return -EINVAL;
5801 if (rcvd->max_resp_sz < sent->max_resp_sz)
5802 return -EINVAL;
5803 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5804 return -EINVAL;
5805 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005806 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005807 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005808 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005809 return -EINVAL;
5810 return 0;
5811}
Andy Adamson8d353012009-04-01 09:22:32 -04005812
Andy Adamson8d353012009-04-01 09:22:32 -04005813static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5814 struct nfs4_session *session)
5815{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005816 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005817
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005818 ret = nfs4_verify_fore_channel_attrs(args, session);
5819 if (ret)
5820 return ret;
5821 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005822}
5823
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005824static int _nfs4_proc_create_session(struct nfs_client *clp,
5825 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005826{
5827 struct nfs4_session *session = clp->cl_session;
5828 struct nfs41_create_session_args args = {
5829 .client = clp,
5830 .cb_program = NFS4_CALLBACK,
5831 };
5832 struct nfs41_create_session_res res = {
5833 .client = clp,
5834 };
5835 struct rpc_message msg = {
5836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5837 .rpc_argp = &args,
5838 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005839 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005840 };
5841 int status;
5842
5843 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005844 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005845
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005846 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005847
Andy Adamson8d353012009-04-01 09:22:32 -04005848 if (!status)
5849 /* Verify the session's negotiated channel_attrs values */
5850 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005851 if (!status) {
5852 /* Increment the clientid slot sequence id */
5853 clp->cl_seqid++;
5854 }
5855
5856 return status;
5857}
5858
5859/*
5860 * Issues a CREATE_SESSION operation to the server.
5861 * It is the responsibility of the caller to verify the session is
5862 * expired before calling this routine.
5863 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005864int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005865{
5866 int status;
5867 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005868 struct nfs4_session *session = clp->cl_session;
5869
5870 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5871
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005872 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005873 if (status)
5874 goto out;
5875
Andy Adamsonaacd5532011-11-09 13:58:21 -05005876 /* Init or reset the session slot tables */
5877 status = nfs4_setup_session_slot_tables(session);
5878 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005879 if (status)
5880 goto out;
5881
5882 ptr = (unsigned *)&session->sess_id.data[0];
5883 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5884 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005885out:
5886 dprintk("<-- %s\n", __func__);
5887 return status;
5888}
5889
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005890/*
5891 * Issue the over-the-wire RPC DESTROY_SESSION.
5892 * The caller must serialize access to this routine.
5893 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005894int nfs4_proc_destroy_session(struct nfs4_session *session,
5895 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005896{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005897 struct rpc_message msg = {
5898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5899 .rpc_argp = session,
5900 .rpc_cred = cred,
5901 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005902 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005903
5904 dprintk("--> nfs4_proc_destroy_session\n");
5905
5906 /* session is still being setup */
5907 if (session->clp->cl_cons_state != NFS_CS_READY)
5908 return status;
5909
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005910 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005911
5912 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005913 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005914 "Session has been destroyed regardless...\n", status);
5915
5916 dprintk("<-- nfs4_proc_destroy_session\n");
5917 return status;
5918}
5919
Trond Myklebust7b38c362012-05-23 13:23:31 -04005920/*
5921 * With sessions, the client is not marked ready until after a
5922 * successful EXCHANGE_ID and CREATE_SESSION.
5923 *
5924 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5925 * other versions of NFS can be tried.
5926 */
5927static int nfs41_check_session_ready(struct nfs_client *clp)
5928{
5929 int ret;
5930
5931 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5932 ret = nfs4_client_recover_expired_lease(clp);
5933 if (ret)
5934 return ret;
5935 }
5936 if (clp->cl_cons_state < NFS_CS_READY)
5937 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005938 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005939 return 0;
5940}
5941
Trond Myklebustfccba802009-07-21 16:48:07 -04005942int nfs4_init_session(struct nfs_server *server)
5943{
5944 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005945 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005946 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005947
5948 if (!nfs4_has_session(clp))
5949 return 0;
5950
Trond Myklebust1055d762010-06-16 09:52:27 -04005951 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005952 spin_lock(&clp->cl_lock);
5953 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005954
Trond Myklebust7b38c362012-05-23 13:23:31 -04005955 rsize = server->rsize;
5956 if (rsize == 0)
5957 rsize = NFS_MAX_FILE_IO_SIZE;
5958 wsize = server->wsize;
5959 if (wsize == 0)
5960 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005961
Trond Myklebust7b38c362012-05-23 13:23:31 -04005962 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5963 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5964 }
5965 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005966
Trond Myklebust7b38c362012-05-23 13:23:31 -04005967 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005968}
5969
Trond Myklebust7b38c362012-05-23 13:23:31 -04005970int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005971{
5972 struct nfs4_session *session = clp->cl_session;
5973 int ret;
5974
Trond Myklebust7b38c362012-05-23 13:23:31 -04005975 spin_lock(&clp->cl_lock);
5976 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5977 /*
5978 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5979 * DS lease to be equal to the MDS lease.
5980 */
5981 clp->cl_lease_time = lease_time;
5982 clp->cl_last_renewal = jiffies;
5983 }
5984 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005985
Trond Myklebust7b38c362012-05-23 13:23:31 -04005986 ret = nfs41_check_session_ready(clp);
5987 if (ret)
5988 return ret;
5989 /* Test for the DS role */
5990 if (!is_ds_client(clp))
5991 return -ENODEV;
5992 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005993}
5994EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5995
5996
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005997/*
5998 * Renew the cl_session lease.
5999 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006000struct nfs4_sequence_data {
6001 struct nfs_client *clp;
6002 struct nfs4_sequence_args args;
6003 struct nfs4_sequence_res res;
6004};
6005
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006006static void nfs41_sequence_release(void *data)
6007{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006008 struct nfs4_sequence_data *calldata = data;
6009 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006010
Alexandros Batsakis71358402010-02-05 03:45:05 -08006011 if (atomic_read(&clp->cl_count) > 1)
6012 nfs4_schedule_state_renewal(clp);
6013 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006014 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006015}
6016
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006017static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6018{
6019 switch(task->tk_status) {
6020 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006021 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6022 return -EAGAIN;
6023 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006024 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006025 }
6026 return 0;
6027}
6028
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006029static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006030{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006031 struct nfs4_sequence_data *calldata = data;
6032 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006033
Trond Myklebust14516c32010-07-31 14:29:06 -04006034 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6035 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006036
6037 if (task->tk_status < 0) {
6038 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006039 if (atomic_read(&clp->cl_count) == 1)
6040 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006041
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006042 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6043 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006044 return;
6045 }
6046 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006047 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006048out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006049 dprintk("<-- %s\n", __func__);
6050}
6051
6052static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6053{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006054 struct nfs4_sequence_data *calldata = data;
6055 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006056 struct nfs4_sequence_args *args;
6057 struct nfs4_sequence_res *res;
6058
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006059 args = task->tk_msg.rpc_argp;
6060 res = task->tk_msg.rpc_resp;
6061
Trond Myklebust9d12b212012-01-17 22:04:25 -05006062 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006063 return;
6064 rpc_call_start(task);
6065}
6066
6067static const struct rpc_call_ops nfs41_sequence_ops = {
6068 .rpc_call_done = nfs41_sequence_call_done,
6069 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006070 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006071};
6072
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006073static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006074{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006075 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006076 struct rpc_message msg = {
6077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6078 .rpc_cred = cred,
6079 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006080 struct rpc_task_setup task_setup_data = {
6081 .rpc_client = clp->cl_rpcclient,
6082 .rpc_message = &msg,
6083 .callback_ops = &nfs41_sequence_ops,
6084 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6085 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006086
Alexandros Batsakis71358402010-02-05 03:45:05 -08006087 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006088 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006089 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006090 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006091 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006092 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006093 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006094 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006095 msg.rpc_argp = &calldata->args;
6096 msg.rpc_resp = &calldata->res;
6097 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006098 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006099
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006100 return rpc_run_task(&task_setup_data);
6101}
6102
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006103static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006104{
6105 struct rpc_task *task;
6106 int ret = 0;
6107
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006108 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6109 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006110 task = _nfs41_proc_sequence(clp, cred);
6111 if (IS_ERR(task))
6112 ret = PTR_ERR(task);
6113 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006114 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006115 dprintk("<-- %s status=%d\n", __func__, ret);
6116 return ret;
6117}
6118
6119static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6120{
6121 struct rpc_task *task;
6122 int ret;
6123
6124 task = _nfs41_proc_sequence(clp, cred);
6125 if (IS_ERR(task)) {
6126 ret = PTR_ERR(task);
6127 goto out;
6128 }
6129 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006130 if (!ret) {
6131 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6132
6133 if (task->tk_status == 0)
6134 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006135 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006136 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006137 rpc_put_task(task);
6138out:
6139 dprintk("<-- %s status=%d\n", __func__, ret);
6140 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006141}
6142
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006143struct nfs4_reclaim_complete_data {
6144 struct nfs_client *clp;
6145 struct nfs41_reclaim_complete_args arg;
6146 struct nfs41_reclaim_complete_res res;
6147};
6148
6149static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6150{
6151 struct nfs4_reclaim_complete_data *calldata = data;
6152
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006153 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006154 if (nfs41_setup_sequence(calldata->clp->cl_session,
6155 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006156 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006157 return;
6158
6159 rpc_call_start(task);
6160}
6161
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006162static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6163{
6164 switch(task->tk_status) {
6165 case 0:
6166 case -NFS4ERR_COMPLETE_ALREADY:
6167 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6168 break;
6169 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006170 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006171 /* fall through */
6172 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006173 return -EAGAIN;
6174 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006175 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006176 }
6177 return 0;
6178}
6179
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006180static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6181{
6182 struct nfs4_reclaim_complete_data *calldata = data;
6183 struct nfs_client *clp = calldata->clp;
6184 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6185
6186 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006187 if (!nfs41_sequence_done(task, res))
6188 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006189
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006190 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6191 rpc_restart_call_prepare(task);
6192 return;
6193 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006194 dprintk("<-- %s\n", __func__);
6195}
6196
6197static void nfs4_free_reclaim_complete_data(void *data)
6198{
6199 struct nfs4_reclaim_complete_data *calldata = data;
6200
6201 kfree(calldata);
6202}
6203
6204static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6205 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6206 .rpc_call_done = nfs4_reclaim_complete_done,
6207 .rpc_release = nfs4_free_reclaim_complete_data,
6208};
6209
6210/*
6211 * Issue a global reclaim complete.
6212 */
6213static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6214{
6215 struct nfs4_reclaim_complete_data *calldata;
6216 struct rpc_task *task;
6217 struct rpc_message msg = {
6218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6219 };
6220 struct rpc_task_setup task_setup_data = {
6221 .rpc_client = clp->cl_rpcclient,
6222 .rpc_message = &msg,
6223 .callback_ops = &nfs4_reclaim_complete_call_ops,
6224 .flags = RPC_TASK_ASYNC,
6225 };
6226 int status = -ENOMEM;
6227
6228 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006229 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006230 if (calldata == NULL)
6231 goto out;
6232 calldata->clp = clp;
6233 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006234
Trond Myklebust9d12b212012-01-17 22:04:25 -05006235 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006236 msg.rpc_argp = &calldata->arg;
6237 msg.rpc_resp = &calldata->res;
6238 task_setup_data.callback_data = calldata;
6239 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006240 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006241 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006242 goto out;
6243 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006244 status = nfs4_wait_for_completion_rpc_task(task);
6245 if (status == 0)
6246 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006247 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006248 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006249out:
6250 dprintk("<-- %s status=%d\n", __func__, status);
6251 return status;
6252}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006253
6254static void
6255nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6256{
6257 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006258 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006259
6260 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006261 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6262 * right now covering the LAYOUTGET we are about to send.
6263 * However, that is not so catastrophic, and there seems
6264 * to be no way to prevent it completely.
6265 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006266 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006267 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006268 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006269 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6270 NFS_I(lgp->args.inode)->layout,
6271 lgp->args.ctx->state)) {
6272 rpc_exit(task, NFS4_OK);
6273 return;
6274 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006275 rpc_call_start(task);
6276}
6277
6278static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6279{
6280 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006281 struct inode *inode = lgp->args.inode;
6282 struct nfs_server *server = NFS_SERVER(inode);
6283 struct pnfs_layout_hdr *lo;
6284 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006285
6286 dprintk("--> %s\n", __func__);
6287
6288 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006289 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006290
6291 switch (task->tk_status) {
6292 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006293 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006294 case -NFS4ERR_LAYOUTTRYLATER:
6295 case -NFS4ERR_RECALLCONFLICT:
6296 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006297 break;
6298 case -NFS4ERR_EXPIRED:
6299 case -NFS4ERR_BAD_STATEID:
6300 spin_lock(&inode->i_lock);
6301 lo = NFS_I(inode)->layout;
6302 if (!lo || list_empty(&lo->plh_segs)) {
6303 spin_unlock(&inode->i_lock);
6304 /* If the open stateid was bad, then recover it. */
6305 state = lgp->args.ctx->state;
6306 } else {
6307 LIST_HEAD(head);
6308
6309 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6310 spin_unlock(&inode->i_lock);
6311 /* Mark the bad layout state as invalid, then
6312 * retry using the open stateid. */
6313 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006314 }
6315 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006316 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6317 rpc_restart_call_prepare(task);
6318out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006319 dprintk("<-- %s\n", __func__);
6320}
6321
Idan Kedar85541162012-08-02 11:47:10 +03006322static size_t max_response_pages(struct nfs_server *server)
6323{
6324 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6325 return nfs_page_array_len(0, max_resp_sz);
6326}
6327
6328static void nfs4_free_pages(struct page **pages, size_t size)
6329{
6330 int i;
6331
6332 if (!pages)
6333 return;
6334
6335 for (i = 0; i < size; i++) {
6336 if (!pages[i])
6337 break;
6338 __free_page(pages[i]);
6339 }
6340 kfree(pages);
6341}
6342
6343static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6344{
6345 struct page **pages;
6346 int i;
6347
6348 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6349 if (!pages) {
6350 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6351 return NULL;
6352 }
6353
6354 for (i = 0; i < size; i++) {
6355 pages[i] = alloc_page(gfp_flags);
6356 if (!pages[i]) {
6357 dprintk("%s: failed to allocate page\n", __func__);
6358 nfs4_free_pages(pages, size);
6359 return NULL;
6360 }
6361 }
6362
6363 return pages;
6364}
6365
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006366static void nfs4_layoutget_release(void *calldata)
6367{
6368 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006369 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6370 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006371
6372 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006373 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006374 put_nfs_open_context(lgp->args.ctx);
6375 kfree(calldata);
6376 dprintk("<-- %s\n", __func__);
6377}
6378
6379static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6380 .rpc_call_prepare = nfs4_layoutget_prepare,
6381 .rpc_call_done = nfs4_layoutget_done,
6382 .rpc_release = nfs4_layoutget_release,
6383};
6384
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006385struct pnfs_layout_segment *
6386nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006387{
6388 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006389 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006390 struct rpc_task *task;
6391 struct rpc_message msg = {
6392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6393 .rpc_argp = &lgp->args,
6394 .rpc_resp = &lgp->res,
6395 };
6396 struct rpc_task_setup task_setup_data = {
6397 .rpc_client = server->client,
6398 .rpc_message = &msg,
6399 .callback_ops = &nfs4_layoutget_call_ops,
6400 .callback_data = lgp,
6401 .flags = RPC_TASK_ASYNC,
6402 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006403 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006404 int status = 0;
6405
6406 dprintk("--> %s\n", __func__);
6407
Idan Kedar85541162012-08-02 11:47:10 +03006408 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6409 if (!lgp->args.layout.pages) {
6410 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006411 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006412 }
6413 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6414
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006415 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006416 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006417 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006418 task = rpc_run_task(&task_setup_data);
6419 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006420 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006421 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006422 if (status == 0)
6423 status = task->tk_status;
6424 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006425 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006426 rpc_put_task(task);
6427 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006428 if (status)
6429 return ERR_PTR(status);
6430 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006431}
6432
Benny Halevycbe82602011-05-22 19:52:37 +03006433static void
6434nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6435{
6436 struct nfs4_layoutreturn *lrp = calldata;
6437
6438 dprintk("--> %s\n", __func__);
6439 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006440 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006441 return;
6442 rpc_call_start(task);
6443}
6444
6445static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6446{
6447 struct nfs4_layoutreturn *lrp = calldata;
6448 struct nfs_server *server;
6449
6450 dprintk("--> %s\n", __func__);
6451
6452 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6453 return;
6454
6455 server = NFS_SERVER(lrp->args.inode);
6456 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006457 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006458 return;
6459 }
Benny Halevycbe82602011-05-22 19:52:37 +03006460 dprintk("<-- %s\n", __func__);
6461}
6462
6463static void nfs4_layoutreturn_release(void *calldata)
6464{
6465 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006466 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006467
6468 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006469 spin_lock(&lo->plh_inode->i_lock);
6470 if (lrp->res.lrs_present)
6471 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6472 lo->plh_block_lgets--;
6473 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006474 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006475 kfree(calldata);
6476 dprintk("<-- %s\n", __func__);
6477}
6478
6479static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6480 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6481 .rpc_call_done = nfs4_layoutreturn_done,
6482 .rpc_release = nfs4_layoutreturn_release,
6483};
6484
6485int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6486{
6487 struct rpc_task *task;
6488 struct rpc_message msg = {
6489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6490 .rpc_argp = &lrp->args,
6491 .rpc_resp = &lrp->res,
6492 };
6493 struct rpc_task_setup task_setup_data = {
6494 .rpc_client = lrp->clp->cl_rpcclient,
6495 .rpc_message = &msg,
6496 .callback_ops = &nfs4_layoutreturn_call_ops,
6497 .callback_data = lrp,
6498 };
6499 int status;
6500
6501 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006502 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006503 task = rpc_run_task(&task_setup_data);
6504 if (IS_ERR(task))
6505 return PTR_ERR(task);
6506 status = task->tk_status;
6507 dprintk("<-- %s status=%d\n", __func__, status);
6508 rpc_put_task(task);
6509 return status;
6510}
6511
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006512/*
6513 * Retrieve the list of Data Server devices from the MDS.
6514 */
6515static int _nfs4_getdevicelist(struct nfs_server *server,
6516 const struct nfs_fh *fh,
6517 struct pnfs_devicelist *devlist)
6518{
6519 struct nfs4_getdevicelist_args args = {
6520 .fh = fh,
6521 .layoutclass = server->pnfs_curr_ld->id,
6522 };
6523 struct nfs4_getdevicelist_res res = {
6524 .devlist = devlist,
6525 };
6526 struct rpc_message msg = {
6527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6528 .rpc_argp = &args,
6529 .rpc_resp = &res,
6530 };
6531 int status;
6532
6533 dprintk("--> %s\n", __func__);
6534 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6535 &res.seq_res, 0);
6536 dprintk("<-- %s status=%d\n", __func__, status);
6537 return status;
6538}
6539
6540int nfs4_proc_getdevicelist(struct nfs_server *server,
6541 const struct nfs_fh *fh,
6542 struct pnfs_devicelist *devlist)
6543{
6544 struct nfs4_exception exception = { };
6545 int err;
6546
6547 do {
6548 err = nfs4_handle_exception(server,
6549 _nfs4_getdevicelist(server, fh, devlist),
6550 &exception);
6551 } while (exception.retry);
6552
6553 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6554 err, devlist->num_devs);
6555
6556 return err;
6557}
6558EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6559
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006560static int
6561_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6562{
6563 struct nfs4_getdeviceinfo_args args = {
6564 .pdev = pdev,
6565 };
6566 struct nfs4_getdeviceinfo_res res = {
6567 .pdev = pdev,
6568 };
6569 struct rpc_message msg = {
6570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6571 .rpc_argp = &args,
6572 .rpc_resp = &res,
6573 };
6574 int status;
6575
6576 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006577 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006578 dprintk("<-- %s status=%d\n", __func__, status);
6579
6580 return status;
6581}
6582
6583int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6584{
6585 struct nfs4_exception exception = { };
6586 int err;
6587
6588 do {
6589 err = nfs4_handle_exception(server,
6590 _nfs4_proc_getdeviceinfo(server, pdev),
6591 &exception);
6592 } while (exception.retry);
6593 return err;
6594}
6595EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6596
Andy Adamson863a3c62011-03-23 13:27:54 +00006597static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6598{
6599 struct nfs4_layoutcommit_data *data = calldata;
6600 struct nfs_server *server = NFS_SERVER(data->args.inode);
6601
6602 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006603 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006604 return;
6605 rpc_call_start(task);
6606}
6607
6608static void
6609nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6610{
6611 struct nfs4_layoutcommit_data *data = calldata;
6612 struct nfs_server *server = NFS_SERVER(data->args.inode);
6613
6614 if (!nfs4_sequence_done(task, &data->res.seq_res))
6615 return;
6616
6617 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006618 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6619 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6620 case -NFS4ERR_BADLAYOUT: /* no layout */
6621 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006622 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006623 break;
6624 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006625 nfs_post_op_update_inode_force_wcc(data->args.inode,
6626 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006627 break;
6628 default:
6629 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6630 rpc_restart_call_prepare(task);
6631 return;
6632 }
6633 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006634}
6635
6636static void nfs4_layoutcommit_release(void *calldata)
6637{
6638 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006639 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006640 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006641
Andy Adamsondb29c082011-07-30 20:52:38 -04006642 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006643 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006644 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6645 list_del_init(&lseg->pls_lc_list);
6646 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6647 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006648 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006649 }
Peng Tao92407e72011-10-23 20:21:17 -07006650
6651 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6652 smp_mb__after_clear_bit();
6653 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6654
Andy Adamson863a3c62011-03-23 13:27:54 +00006655 put_rpccred(data->cred);
6656 kfree(data);
6657}
6658
6659static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6660 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6661 .rpc_call_done = nfs4_layoutcommit_done,
6662 .rpc_release = nfs4_layoutcommit_release,
6663};
6664
6665int
Andy Adamsonef311532011-03-12 02:58:10 -05006666nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006667{
6668 struct rpc_message msg = {
6669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6670 .rpc_argp = &data->args,
6671 .rpc_resp = &data->res,
6672 .rpc_cred = data->cred,
6673 };
6674 struct rpc_task_setup task_setup_data = {
6675 .task = &data->task,
6676 .rpc_client = NFS_CLIENT(data->args.inode),
6677 .rpc_message = &msg,
6678 .callback_ops = &nfs4_layoutcommit_ops,
6679 .callback_data = data,
6680 .flags = RPC_TASK_ASYNC,
6681 };
6682 struct rpc_task *task;
6683 int status = 0;
6684
6685 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6686 "lbw: %llu inode %lu\n",
6687 data->task.tk_pid, sync,
6688 data->args.lastbytewritten,
6689 data->args.inode->i_ino);
6690
Trond Myklebust9d12b212012-01-17 22:04:25 -05006691 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006692 task = rpc_run_task(&task_setup_data);
6693 if (IS_ERR(task))
6694 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006695 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006696 goto out;
6697 status = nfs4_wait_for_completion_rpc_task(task);
6698 if (status != 0)
6699 goto out;
6700 status = task->tk_status;
6701out:
6702 dprintk("%s: status %d\n", __func__, status);
6703 rpc_put_task(task);
6704 return status;
6705}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006706
6707static int
6708_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6709 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6710{
6711 struct nfs41_secinfo_no_name_args args = {
6712 .style = SECINFO_STYLE_CURRENT_FH,
6713 };
6714 struct nfs4_secinfo_res res = {
6715 .flavors = flavors,
6716 };
6717 struct rpc_message msg = {
6718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6719 .rpc_argp = &args,
6720 .rpc_resp = &res,
6721 };
6722 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6723}
6724
6725static int
6726nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6727 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6728{
6729 struct nfs4_exception exception = { };
6730 int err;
6731 do {
6732 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6733 switch (err) {
6734 case 0:
6735 case -NFS4ERR_WRONGSEC:
6736 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006737 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006738 default:
6739 err = nfs4_handle_exception(server, err, &exception);
6740 }
6741 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006742out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006743 return err;
6744}
6745
6746static int
6747nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6748 struct nfs_fsinfo *info)
6749{
6750 int err;
6751 struct page *page;
6752 rpc_authflavor_t flavor;
6753 struct nfs4_secinfo_flavors *flavors;
6754
6755 page = alloc_page(GFP_KERNEL);
6756 if (!page) {
6757 err = -ENOMEM;
6758 goto out;
6759 }
6760
6761 flavors = page_address(page);
6762 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6763
6764 /*
6765 * Fall back on "guess and check" method if
6766 * the server doesn't support SECINFO_NO_NAME
6767 */
6768 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6769 err = nfs4_find_root_sec(server, fhandle, info);
6770 goto out_freepage;
6771 }
6772 if (err)
6773 goto out_freepage;
6774
6775 flavor = nfs_find_best_sec(flavors);
6776 if (err == 0)
6777 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6778
6779out_freepage:
6780 put_page(page);
6781 if (err == -EACCES)
6782 return -EPERM;
6783out:
6784 return err;
6785}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006786
6787static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006788{
6789 int status;
6790 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006791 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006792 };
6793 struct nfs41_test_stateid_res res;
6794 struct rpc_message msg = {
6795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6796 .rpc_argp = &args,
6797 .rpc_resp = &res,
6798 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006799
Chuck Lever38527b12012-07-11 16:30:23 -04006800 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006801 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006802 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006803 if (status != NFS_OK) {
6804 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006805 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006806 }
6807 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006808 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006809}
6810
Chuck Lever38527b12012-07-11 16:30:23 -04006811/**
6812 * nfs41_test_stateid - perform a TEST_STATEID operation
6813 *
6814 * @server: server / transport on which to perform the operation
6815 * @stateid: state ID to test
6816 *
6817 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6818 * Otherwise a negative NFS4ERR value is returned if the operation
6819 * failed or the state ID is not currently valid.
6820 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006821static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006822{
6823 struct nfs4_exception exception = { };
6824 int err;
6825 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006826 err = _nfs41_test_stateid(server, stateid);
6827 if (err != -NFS4ERR_DELAY)
6828 break;
6829 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006830 } while (exception.retry);
6831 return err;
6832}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006833
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006834static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006835{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006836 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006837 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006838 };
6839 struct nfs41_free_stateid_res res;
6840 struct rpc_message msg = {
6841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6842 .rpc_argp = &args,
6843 .rpc_resp = &res,
6844 };
Chuck Lever38527b12012-07-11 16:30:23 -04006845 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006846
Chuck Lever38527b12012-07-11 16:30:23 -04006847 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006848 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006849 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006850 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006851 dprintk("NFS reply free_stateid: %d\n", status);
6852 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006853}
6854
Chuck Lever38527b12012-07-11 16:30:23 -04006855/**
6856 * nfs41_free_stateid - perform a FREE_STATEID operation
6857 *
6858 * @server: server / transport on which to perform the operation
6859 * @stateid: state ID to release
6860 *
6861 * Returns NFS_OK if the server freed "stateid". Otherwise a
6862 * negative NFS4ERR value is returned.
6863 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006864static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006865{
6866 struct nfs4_exception exception = { };
6867 int err;
6868 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006869 err = _nfs4_free_stateid(server, stateid);
6870 if (err != -NFS4ERR_DELAY)
6871 break;
6872 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006873 } while (exception.retry);
6874 return err;
6875}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006876
6877static bool nfs41_match_stateid(const nfs4_stateid *s1,
6878 const nfs4_stateid *s2)
6879{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006880 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006881 return false;
6882
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006883 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006884 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006885 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006886 return true;
6887
6888 return false;
6889}
6890
Andy Adamson557134a2009-04-01 09:21:53 -04006891#endif /* CONFIG_NFS_V4_1 */
6892
Trond Myklebust36281ca2012-03-04 18:13:56 -05006893static bool nfs4_match_stateid(const nfs4_stateid *s1,
6894 const nfs4_stateid *s2)
6895{
Trond Myklebustf597c532012-03-04 18:13:56 -05006896 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006897}
6898
6899
Trond Myklebust17280172012-03-11 13:11:00 -04006900static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006901 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006902 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 .recover_open = nfs4_open_reclaim,
6904 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006905 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006906 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006907 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908};
6909
Andy Adamson591d71c2009-04-01 09:22:47 -04006910#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006911static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006912 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6913 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6914 .recover_open = nfs4_open_reclaim,
6915 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006916 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006917 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006918 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006919 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006920};
6921#endif /* CONFIG_NFS_V4_1 */
6922
Trond Myklebust17280172012-03-11 13:11:00 -04006923static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006924 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006925 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 .recover_open = nfs4_open_expired,
6927 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006928 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006929 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930};
6931
Andy Adamson591d71c2009-04-01 09:22:47 -04006932#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006933static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006934 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6935 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006936 .recover_open = nfs41_open_expired,
6937 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006938 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006939 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006940};
6941#endif /* CONFIG_NFS_V4_1 */
6942
Trond Myklebust17280172012-03-11 13:11:00 -04006943static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006944 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006945 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006946 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006947};
6948
6949#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006950static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006951 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006952 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006953 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006954};
6955#endif
6956
Trond Myklebust97dc1352010-06-16 09:52:26 -04006957static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6958 .minor_version = 0,
6959 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006960 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006961 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006962 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6963 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6964 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006965};
6966
6967#if defined(CONFIG_NFS_V4_1)
6968static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6969 .minor_version = 1,
6970 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006971 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006972 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006973 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6974 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6975 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006976};
6977#endif
6978
Trond Myklebust97dc1352010-06-16 09:52:26 -04006979const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6980 [0] = &nfs_v4_0_minor_ops,
6981#if defined(CONFIG_NFS_V4_1)
6982 [1] = &nfs_v4_1_minor_ops,
6983#endif
6984};
6985
Bryan Schumaker73a79702012-07-16 16:39:12 -04006986const struct inode_operations nfs4_dir_inode_operations = {
6987 .create = nfs_create,
6988 .lookup = nfs_lookup,
6989 .atomic_open = nfs_atomic_open,
6990 .link = nfs_link,
6991 .unlink = nfs_unlink,
6992 .symlink = nfs_symlink,
6993 .mkdir = nfs_mkdir,
6994 .rmdir = nfs_rmdir,
6995 .mknod = nfs_mknod,
6996 .rename = nfs_rename,
6997 .permission = nfs_permission,
6998 .getattr = nfs_getattr,
6999 .setattr = nfs_setattr,
7000 .getxattr = generic_getxattr,
7001 .setxattr = generic_setxattr,
7002 .listxattr = generic_listxattr,
7003 .removexattr = generic_removexattr,
7004};
7005
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007006static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007007 .permission = nfs_permission,
7008 .getattr = nfs_getattr,
7009 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007010 .getxattr = generic_getxattr,
7011 .setxattr = generic_setxattr,
7012 .listxattr = generic_listxattr,
7013 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007014};
7015
David Howells509de812006-08-22 20:06:11 -04007016const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 .version = 4, /* protocol version */
7018 .dentry_ops = &nfs4_dentry_operations,
7019 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007020 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007021 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007023 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007024 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 .getattr = nfs4_proc_getattr,
7026 .setattr = nfs4_proc_setattr,
7027 .lookup = nfs4_proc_lookup,
7028 .access = nfs4_proc_access,
7029 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 .create = nfs4_proc_create,
7031 .remove = nfs4_proc_remove,
7032 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007033 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 .unlink_done = nfs4_proc_unlink_done,
7035 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007036 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007037 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007038 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 .link = nfs4_proc_link,
7040 .symlink = nfs4_proc_symlink,
7041 .mkdir = nfs4_proc_mkdir,
7042 .rmdir = nfs4_proc_remove,
7043 .readdir = nfs4_proc_readdir,
7044 .mknod = nfs4_proc_mknod,
7045 .statfs = nfs4_proc_statfs,
7046 .fsinfo = nfs4_proc_fsinfo,
7047 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007048 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 .decode_dirent = nfs4_decode_dirent,
7050 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007051 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007052 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007053 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007055 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007056 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007057 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007059 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007060 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007062 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007063 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007064 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007065 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007066 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007067 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007068 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007069 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007070 .create_server = nfs4_create_server,
7071 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072};
7073
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007074static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7075 .prefix = XATTR_NAME_NFSV4_ACL,
7076 .list = nfs4_xattr_list_nfs4_acl,
7077 .get = nfs4_xattr_get_nfs4_acl,
7078 .set = nfs4_xattr_set_nfs4_acl,
7079};
7080
7081const struct xattr_handler *nfs4_xattr_handlers[] = {
7082 &nfs4_xattr_nfs4_acl_handler,
7083 NULL
7084};
7085
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086/*
7087 * Local variables:
7088 * c-basic-offset: 8
7089 * End:
7090 */