blob: 5e373c30e8d42cdcb534dc981ce350e001e4b19e [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 Myklebustef159e92012-02-06 19:50:40 -050075static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040083static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040084static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040085static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
87 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
90static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050093static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Trond Myklebust52567b02009-10-23 14:46:42 -040095 if (err >= -1000)
96 return err;
97 switch (err) {
98 case -NFS4ERR_RESOURCE:
99 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000100 case -NFS4ERR_WRONGSEC:
101 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800102 case -NFS4ERR_BADOWNER:
103 case -NFS4ERR_BADNAME:
104 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400105 case -NFS4ERR_SHARE_DENIED:
106 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400107 case -NFS4ERR_MINOR_VERS_MISMATCH:
108 return -EPROTONOSUPPORT;
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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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);
David Howells7539bba2006-08-22 20:06:09 -0400865 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400866 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
867 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400868 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100869 p->o_arg.server = server;
870 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400871 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100872 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500873 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500874 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400875
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100876 p->o_arg.u.attrs = &p->attrs;
877 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500878
879 verf[0] = jiffies;
880 verf[1] = current->pid;
881 memcpy(p->o_arg.u.verifier.data, verf,
882 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100883 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100884 p->c_arg.fh = &p->o_res.fh;
885 p->c_arg.stateid = &p->o_res.stateid;
886 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400887 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400888 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100889 return p;
890err_free:
891 kfree(p);
892err:
893 dput(parent);
894 return NULL;
895}
896
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400897static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100898{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400899 struct nfs4_opendata *p = container_of(kref,
900 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400901 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400902
903 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400904 if (p->state != NULL)
905 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400906 nfs4_put_state_owner(p->owner);
907 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400908 dput(p->dentry);
909 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500910 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400911 kfree(p);
912}
913
914static void nfs4_opendata_put(struct nfs4_opendata *p)
915{
916 if (p != NULL)
917 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100918}
919
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
921{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100922 int ret;
923
Trond Myklebust06f814a2006-01-03 09:55:07 +0100924 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100925 return ret;
926}
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400929{
930 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931
Trond Myklebust536e43d2012-01-17 22:04:26 -0500932 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 goto out;
934 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400935 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
937 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 break;
939 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500940 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
941 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942 break;
943 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500944 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
945 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400946 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400948 return ret;
949}
950
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500953 if (delegation == NULL)
954 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400956 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500957 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500959 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400960 return 1;
961}
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100964{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100966 case FMODE_WRITE:
967 state->n_wronly++;
968 break;
969 case FMODE_READ:
970 state->n_rdonly++;
971 break;
972 case FMODE_READ|FMODE_WRITE:
973 state->n_rdwr++;
974 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100976}
977
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400979{
980 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500981 nfs4_stateid_copy(&state->stateid, stateid);
982 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 case FMODE_READ:
985 set_bit(NFS_O_RDONLY_STATE, &state->flags);
986 break;
987 case FMODE_WRITE:
988 set_bit(NFS_O_WRONLY_STATE, &state->flags);
989 break;
990 case FMODE_READ|FMODE_WRITE:
991 set_bit(NFS_O_RDWR_STATE, &state->flags);
992 }
993}
994
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400996{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400997 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400999 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001000}
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002static 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 -07001003{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001004 /*
1005 * Protect the call to nfs4_state_set_mode_locked and
1006 * serialise the stateid update
1007 */
1008 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001010 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001011 set_bit(NFS_DELEGATED_STATE, &state->flags);
1012 }
1013 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001015 write_sequnlock(&state->seqlock);
1016 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001018 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021static 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 -05001022{
1023 struct nfs_inode *nfsi = NFS_I(state->inode);
1024 struct nfs_delegation *deleg_cur;
1025 int ret = 0;
1026
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001028
1029 rcu_read_lock();
1030 deleg_cur = rcu_dereference(nfsi->delegation);
1031 if (deleg_cur == NULL)
1032 goto no_delegation;
1033
1034 spin_lock(&deleg_cur->lock);
1035 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
1039 if (delegation == NULL)
1040 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001041 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001042 goto no_delegation_unlock;
1043
Trond Myklebustb7391f42008-12-23 15:21:52 -05001044 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001046 ret = 1;
1047no_delegation_unlock:
1048 spin_unlock(&deleg_cur->lock);
1049no_delegation:
1050 rcu_read_unlock();
1051
1052 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001054 ret = 1;
1055 }
1056
1057 return ret;
1058}
1059
1060
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062{
1063 struct nfs_delegation *delegation;
1064
1065 rcu_read_lock();
1066 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 rcu_read_unlock();
1069 return;
1070 }
1071 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001072 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073}
1074
Trond Myklebust6ee41262007-07-08 14:11:36 -04001075static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076{
1077 struct nfs4_state *state = opendata->state;
1078 struct nfs_inode *nfsi = NFS_I(state->inode);
1079 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001080 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082 nfs4_stateid stateid;
1083 int ret = -EAGAIN;
1084
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001087 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 if (can_open_cached(state, fmode, open_mode)) {
1089 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 goto out_return_state;
1092 }
1093 spin_unlock(&state->owner->so_lock);
1094 }
Trond Myklebust34310432008-12-23 15:21:38 -05001095 rcu_read_lock();
1096 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001097 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001098 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001100 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001102 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001104 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001105 if (ret != 0)
1106 goto out;
1107 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001108
1109 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001111 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113out:
1114 return ERR_PTR(ret);
1115out_return_state:
1116 atomic_inc(&state->count);
1117 return state;
1118}
1119
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001120static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1121{
1122 struct inode *inode;
1123 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001124 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001125 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001126
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001128 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001129 goto out;
1130 }
1131
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001133 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001136 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001137 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001138 goto err;
1139 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001140 state = nfs4_get_open_state(inode, data->owner);
1141 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001142 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001143 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001144 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001145 int delegation_flags = 0;
1146
Trond Myklebust003707c2007-07-05 18:07:55 -04001147 rcu_read_lock();
1148 delegation = rcu_dereference(NFS_I(inode)->delegation);
1149 if (delegation)
1150 delegation_flags = delegation->flags;
1151 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001152 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1153 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1154 "returning a delegation for "
1155 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001156 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001157 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001158 nfs_inode_set_delegation(state->inode,
1159 data->owner->so_cred,
1160 &data->o_res);
1161 else
1162 nfs_inode_reclaim_delegation(state->inode,
1163 data->owner->so_cred,
1164 &data->o_res);
1165 }
Trond Myklebust34310432008-12-23 15:21:38 -05001166
1167 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001168 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001169 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001170out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001171 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001172err_put_inode:
1173 iput(inode);
1174err:
1175 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001176}
1177
Trond Myklebust864472e2006-01-03 09:55:15 +01001178static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1179{
1180 struct nfs_inode *nfsi = NFS_I(state->inode);
1181 struct nfs_open_context *ctx;
1182
1183 spin_lock(&state->inode->i_lock);
1184 list_for_each_entry(ctx, &nfsi->open_files, list) {
1185 if (ctx->state != state)
1186 continue;
1187 get_nfs_open_context(ctx);
1188 spin_unlock(&state->inode->i_lock);
1189 return ctx;
1190 }
1191 spin_unlock(&state->inode->i_lock);
1192 return ERR_PTR(-ENOENT);
1193}
1194
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001195static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1196{
1197 struct nfs4_opendata *opendata;
1198
Al Viro3d4ff432011-06-22 18:40:12 -04001199 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001200 if (opendata == NULL)
1201 return ERR_PTR(-ENOMEM);
1202 opendata->state = state;
1203 atomic_inc(&state->count);
1204 return opendata;
1205}
1206
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001207static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001208{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 int ret;
1211
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001212 opendata->o_arg.open_flags = 0;
1213 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1215 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1216 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001217 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 if (ret != 0)
1219 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001220 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001221 if (IS_ERR(newstate))
1222 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001223 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001224 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 return 0;
1226}
1227
1228static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1229{
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 int ret;
1232
1233 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001234 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 smp_rmb();
1236 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001237 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001238 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 if (ret != 0)
1240 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001241 if (newstate != state)
1242 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001243 }
1244 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001245 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001246 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001247 if (ret != 0)
1248 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001249 if (newstate != state)
1250 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 }
1252 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001253 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001254 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001255 if (ret != 0)
1256 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001257 if (newstate != state)
1258 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001259 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001260 /*
1261 * We may have performed cached opens for all three recoveries.
1262 * Check if we need to update the current stateid.
1263 */
1264 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001265 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001266 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001267 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001268 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001269 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001270 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 return 0;
1272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/*
1275 * OPEN_RECLAIM:
1276 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001278static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001280 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001281 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int status;
1284
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001285 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1286 if (IS_ERR(opendata))
1287 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1289 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001290 rcu_read_lock();
1291 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001292 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001293 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001294 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001295 opendata->o_arg.u.delegation_type = delegation_type;
1296 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return status;
1299}
1300
Trond Myklebust539cd032007-06-05 11:46:42 -04001301static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct nfs_server *server = NFS_SERVER(state->inode);
1304 struct nfs4_exception exception = { };
1305 int err;
1306 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001307 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001308 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001309 break;
1310 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 } while (exception.retry);
1312 return err;
1313}
1314
Trond Myklebust864472e2006-01-03 09:55:15 +01001315static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1316{
1317 struct nfs_open_context *ctx;
1318 int ret;
1319
1320 ctx = nfs4_state_find_open_context(state);
1321 if (IS_ERR(ctx))
1322 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001323 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 put_nfs_open_context(ctx);
1325 return ret;
1326}
1327
Trond Myklebust13437e12007-07-06 15:10:43 -04001328static 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 -07001329{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001331 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001333 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1334 if (IS_ERR(opendata))
1335 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001336 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001337 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001339 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Trond Myklebust13437e12007-07-06 15:10:43 -04001343int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001346 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int err;
1348 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001349 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 switch (err) {
1351 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001352 case -ENOENT:
1353 case -ESTALE:
1354 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001355 case -NFS4ERR_BADSESSION:
1356 case -NFS4ERR_BADSLOT:
1357 case -NFS4ERR_BAD_HIGH_SLOT:
1358 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1359 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001360 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 case -NFS4ERR_STALE_CLIENTID:
1363 case -NFS4ERR_STALE_STATEID:
1364 case -NFS4ERR_EXPIRED:
1365 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001366 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001367 goto out;
1368 case -ERESTARTSYS:
1369 /*
1370 * The show must go on: exit, but mark the
1371 * stateid as needing recovery.
1372 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001373 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001374 case -NFS4ERR_ADMIN_REVOKED:
1375 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001376 nfs_inode_find_state_and_recover(state->inode,
1377 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001378 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001379 case -EKEYEXPIRED:
1380 /*
1381 * User RPCSEC_GSS context has expired.
1382 * We cannot recover this stateid now, so
1383 * skip it and allow recovery thread to
1384 * proceed.
1385 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001386 case -ENOMEM:
1387 err = 0;
1388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 err = nfs4_handle_exception(server, err, &exception);
1391 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001392out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return err;
1394}
1395
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001396static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1397{
1398 struct nfs4_opendata *data = calldata;
1399
1400 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001401 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001402 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001403 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001404 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001405 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001406 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001407}
1408
1409static void nfs4_open_confirm_release(void *calldata)
1410{
1411 struct nfs4_opendata *data = calldata;
1412 struct nfs4_state *state = NULL;
1413
1414 /* If this request hasn't been cancelled, do nothing */
1415 if (data->cancelled == 0)
1416 goto out_free;
1417 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001418 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001420 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001421 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001422 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001424 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001425}
1426
1427static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001428 .rpc_call_done = nfs4_open_confirm_done,
1429 .rpc_release = nfs4_open_confirm_release,
1430};
1431
1432/*
1433 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1434 */
1435static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1436{
1437 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1438 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001439 struct rpc_message msg = {
1440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1441 .rpc_argp = &data->c_arg,
1442 .rpc_resp = &data->c_res,
1443 .rpc_cred = data->owner->so_cred,
1444 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001445 struct rpc_task_setup task_setup_data = {
1446 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001447 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001448 .callback_ops = &nfs4_open_confirm_ops,
1449 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001450 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001451 .flags = RPC_TASK_ASYNC,
1452 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int status;
1454
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001455 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001456 data->rpc_done = 0;
1457 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001458 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001459 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001460 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001461 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001462 status = nfs4_wait_for_completion_rpc_task(task);
1463 if (status != 0) {
1464 data->cancelled = 1;
1465 smp_wmb();
1466 } else
1467 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001468 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return status;
1470}
1471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001474 struct nfs4_opendata *data = calldata;
1475 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001476
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1478 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001479 /*
1480 * Check if we still need to send an OPEN call, or if we can use
1481 * a delegation instead.
1482 */
1483 if (data->state != NULL) {
1484 struct nfs_delegation *delegation;
1485
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001486 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001487 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001488 rcu_read_lock();
1489 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001490 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1491 can_open_delegated(delegation, data->o_arg.fmode))
1492 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001493 rcu_read_unlock();
1494 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001495 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001496 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001497 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001498 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001499 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1500 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001501 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001502 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001503 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001504 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001505 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001506 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001507 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001508unlock_no_action:
1509 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001510out_no_action:
1511 task->tk_action = NULL;
1512
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001515static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1516{
1517 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1518 nfs4_open_prepare(task, calldata);
1519}
1520
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521static void nfs4_open_done(struct rpc_task *task, void *calldata)
1522{
1523 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001526
Trond Myklebust14516c32010-07-31 14:29:06 -04001527 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1528 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001529
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 if (task->tk_status == 0) {
1531 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001532 case S_IFREG:
1533 break;
1534 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001536 break;
1537 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001539 break;
1540 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001542 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001543 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001544 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1545 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001546 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001547 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001549
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550static void nfs4_open_release(void *calldata)
1551{
1552 struct nfs4_opendata *data = calldata;
1553 struct nfs4_state *state = NULL;
1554
1555 /* If this request hasn't been cancelled, do nothing */
1556 if (data->cancelled == 0)
1557 goto out_free;
1558 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001559 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560 goto out_free;
1561 /* In case we need an open_confirm, no cleanup! */
1562 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1563 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001564 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001565 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001566 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001568 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569}
1570
1571static const struct rpc_call_ops nfs4_open_ops = {
1572 .rpc_call_prepare = nfs4_open_prepare,
1573 .rpc_call_done = nfs4_open_done,
1574 .rpc_release = nfs4_open_release,
1575};
1576
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001577static const struct rpc_call_ops nfs4_recover_open_ops = {
1578 .rpc_call_prepare = nfs4_recover_open_prepare,
1579 .rpc_call_done = nfs4_open_done,
1580 .rpc_release = nfs4_open_release,
1581};
1582
1583static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001584{
1585 struct inode *dir = data->dir->d_inode;
1586 struct nfs_server *server = NFS_SERVER(dir);
1587 struct nfs_openargs *o_arg = &data->o_arg;
1588 struct nfs_openres *o_res = &data->o_res;
1589 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001590 struct rpc_message msg = {
1591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1592 .rpc_argp = o_arg,
1593 .rpc_resp = o_res,
1594 .rpc_cred = data->owner->so_cred,
1595 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001596 struct rpc_task_setup task_setup_data = {
1597 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001598 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001599 .callback_ops = &nfs4_open_ops,
1600 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001601 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001602 .flags = RPC_TASK_ASYNC,
1603 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001604 int status;
1605
Trond Myklebust9d12b212012-01-17 22:04:25 -05001606 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001607 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001608 data->rpc_done = 0;
1609 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001610 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001611 if (isrecover)
1612 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001613 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001614 if (IS_ERR(task))
1615 return PTR_ERR(task);
1616 status = nfs4_wait_for_completion_rpc_task(task);
1617 if (status != 0) {
1618 data->cancelled = 1;
1619 smp_wmb();
1620 } else
1621 status = data->rpc_status;
1622 rpc_put_task(task);
1623
1624 return status;
1625}
1626
1627static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1628{
1629 struct inode *dir = data->dir->d_inode;
1630 struct nfs_openres *o_res = &data->o_res;
1631 int status;
1632
1633 status = nfs4_run_open_task(data, 1);
1634 if (status != 0 || !data->rpc_done)
1635 return status;
1636
Trond Myklebust6926afd2012-01-07 13:22:46 -05001637 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1638
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001639 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1640 status = _nfs4_proc_open_confirm(data);
1641 if (status != 0)
1642 return status;
1643 }
1644
1645 return status;
1646}
1647
1648/*
1649 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1650 */
1651static int _nfs4_proc_open(struct nfs4_opendata *data)
1652{
1653 struct inode *dir = data->dir->d_inode;
1654 struct nfs_server *server = NFS_SERVER(dir);
1655 struct nfs_openargs *o_arg = &data->o_arg;
1656 struct nfs_openres *o_res = &data->o_res;
1657 int status;
1658
1659 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001660 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001662 if (status != 0) {
1663 if (status == -NFS4ERR_BADNAME &&
1664 !(o_arg->open_flags & O_CREAT))
1665 return -ENOENT;
1666 return status;
1667 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001668
Trond Myklebust6926afd2012-01-07 13:22:46 -05001669 nfs_fattr_map_and_free_names(server, &data->f_attr);
1670
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001671 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001672 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001673 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1674 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001676 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001681 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Andy Adamsond83217c2011-03-01 01:34:17 +00001685static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001686{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001687 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001688 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001689
Trond Myklebusta78cb572009-08-09 15:06:19 -04001690 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001691 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001692 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001693 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001694 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1695 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001696 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001697 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001698 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001699 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001700 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001701}
1702
Andy Adamsond83217c2011-03-01 01:34:17 +00001703static int nfs4_recover_expired_lease(struct nfs_server *server)
1704{
1705 return nfs4_client_recover_expired_lease(server->nfs_client);
1706}
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708/*
1709 * OPEN_EXPIRED:
1710 * reclaim state on the server after a network partition.
1711 * Assumes caller holds the appropriate lock
1712 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001713static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001715 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001716 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001718 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1719 if (IS_ERR(opendata))
1720 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001721 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001722 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001723 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001724 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001728static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729{
Trond Myklebust539cd032007-06-05 11:46:42 -04001730 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001731 struct nfs4_exception exception = { };
1732 int err;
1733
1734 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001735 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001736 switch (err) {
1737 default:
1738 goto out;
1739 case -NFS4ERR_GRACE:
1740 case -NFS4ERR_DELAY:
1741 nfs4_handle_exception(server, err, &exception);
1742 err = 0;
1743 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001744 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001745out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001746 return err;
1747}
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Trond Myklebust864472e2006-01-03 09:55:15 +01001754 ctx = nfs4_state_find_open_context(state);
1755 if (IS_ERR(ctx))
1756 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001757 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001758 put_nfs_open_context(ctx);
1759 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001763static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001764{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001765 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001766 nfs4_stateid *stateid = &state->stateid;
1767 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001768
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001769 /* If a state reset has been done, test_stateid is unneeded */
1770 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1771 return;
1772
1773 status = nfs41_test_stateid(server, stateid);
1774 if (status != NFS_OK) {
1775 /* Free the stateid unless the server explicitly
1776 * informs us the stateid is unrecognized. */
1777 if (status != -NFS4ERR_BAD_STATEID)
1778 nfs41_free_stateid(server, stateid);
1779
1780 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1781 }
1782}
1783
1784/**
1785 * nfs41_check_open_stateid - possibly free an open stateid
1786 *
1787 * @state: NFSv4 state for an inode
1788 *
1789 * Returns NFS_OK if recovery for this stateid is now finished.
1790 * Otherwise a negative NFS4ERR value is returned.
1791 */
1792static int nfs41_check_open_stateid(struct nfs4_state *state)
1793{
1794 struct nfs_server *server = NFS_SERVER(state->inode);
1795 nfs4_stateid *stateid = &state->stateid;
1796 int status;
1797
1798 /* If a state reset has been done, test_stateid is unneeded */
1799 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1800 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1801 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1802 return -NFS4ERR_BAD_STATEID;
1803
1804 status = nfs41_test_stateid(server, stateid);
1805 if (status != NFS_OK) {
1806 /* Free the stateid unless the server explicitly
1807 * informs us the stateid is unrecognized. */
1808 if (status != -NFS4ERR_BAD_STATEID)
1809 nfs41_free_stateid(server, stateid);
1810
1811 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1812 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1813 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001814 }
1815 return status;
1816}
1817
1818static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1819{
Chuck Levereb64cf92012-07-11 16:30:05 -04001820 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001821
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001822 nfs41_clear_delegation_stateid(state);
1823 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001824 if (status != NFS_OK)
1825 status = nfs4_open_expired(sp, state);
1826 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001827}
1828#endif
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001831 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1832 * fields corresponding to attributes that were used to store the verifier.
1833 * Make sure we clobber those fields in the later setattr call
1834 */
1835static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1836{
1837 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1838 !(sattr->ia_valid & ATTR_ATIME_SET))
1839 sattr->ia_valid |= ATTR_ATIME;
1840
1841 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1842 !(sattr->ia_valid & ATTR_MTIME_SET))
1843 sattr->ia_valid |= ATTR_MTIME;
1844}
1845
1846/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 */
Andy Adamson82be4172012-05-23 05:02:35 -04001849static int _nfs4_do_open(struct inode *dir,
1850 struct dentry *dentry,
1851 fmode_t fmode,
1852 int flags,
1853 struct iattr *sattr,
1854 struct rpc_cred *cred,
1855 struct nfs4_state **res,
1856 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 struct nfs4_state_owner *sp;
1859 struct nfs4_state *state = NULL;
1860 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001861 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001862 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001866 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1867 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1869 goto out_err;
1870 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001871 status = nfs4_recover_expired_lease(server);
1872 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001873 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001874 if (dentry->d_inode != NULL)
1875 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001876 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001877 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001878 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001879 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Andy Adamson82be4172012-05-23 05:02:35 -04001881 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1882 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1883 if (!opendata->f_attr.mdsthreshold)
1884 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001885 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001886 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001887 if (dentry->d_inode != NULL)
1888 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001889
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001890 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001892 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001894 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001895 status = PTR_ERR(state);
1896 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001897 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001898 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001899 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001900
1901 if (opendata->o_arg.open_flags & O_EXCL) {
1902 nfs4_exclusive_attrset(opendata, sattr);
1903
1904 nfs_fattr_init(opendata->o_res.f_attr);
1905 status = nfs4_do_setattr(state->inode, cred,
1906 opendata->o_res.f_attr, sattr,
1907 state);
1908 if (status == 0)
1909 nfs_setattr_update_inode(state->inode, sattr);
1910 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1911 }
Andy Adamson82be4172012-05-23 05:02:35 -04001912
1913 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1914 *ctx_th = opendata->f_attr.mdsthreshold;
1915 else
1916 kfree(opendata->f_attr.mdsthreshold);
1917 opendata->f_attr.mdsthreshold = NULL;
1918
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001919 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 *res = state;
1922 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001923err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001924 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001925 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001926err_put_state_owner:
1927 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 *res = NULL;
1930 return status;
1931}
1932
1933
Andy Adamson82be4172012-05-23 05:02:35 -04001934static struct nfs4_state *nfs4_do_open(struct inode *dir,
1935 struct dentry *dentry,
1936 fmode_t fmode,
1937 int flags,
1938 struct iattr *sattr,
1939 struct rpc_cred *cred,
1940 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 struct nfs4_exception exception = { };
1943 struct nfs4_state *res;
1944 int status;
1945
Trond Myklebust2d0dbc62012-06-08 10:58:09 -04001946 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001948 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1949 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (status == 0)
1951 break;
1952 /* NOTE: BAD_SEQID means the server and client disagree about the
1953 * book-keeping w.r.t. state-changing operations
1954 * (OPEN/CLOSE/LOCK/LOCKU...)
1955 * It is actually a sign of a bug on the client or on the server.
1956 *
1957 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001958 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 * have unhashed the old state_owner for us, and that we can
1960 * therefore safely retry using a new one. We should still warn
1961 * the user though...
1962 */
1963 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001964 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001965 " returned a bad sequence-id error!\n",
1966 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 exception.retry = 1;
1968 continue;
1969 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001970 /*
1971 * BAD_STATEID on OPEN means that the server cancelled our
1972 * state before it received the OPEN_CONFIRM.
1973 * Recover by retrying the request as per the discussion
1974 * on Page 181 of RFC3530.
1975 */
1976 if (status == -NFS4ERR_BAD_STATEID) {
1977 exception.retry = 1;
1978 continue;
1979 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001980 if (status == -EAGAIN) {
1981 /* We must have found a delegation */
1982 exception.retry = 1;
1983 continue;
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1986 status, &exception));
1987 } while (exception.retry);
1988 return res;
1989}
1990
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001991static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1992 struct nfs_fattr *fattr, struct iattr *sattr,
1993 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001995 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001997 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 .iap = sattr,
1999 .server = server,
2000 .bitmask = server->attr_bitmask,
2001 };
2002 struct nfs_setattrres res = {
2003 .fattr = fattr,
2004 .server = server,
2005 };
2006 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2008 .rpc_argp = &arg,
2009 .rpc_resp = &res,
2010 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002012 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002013 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Trond Myklebust0e574af2005-10-27 22:12:38 -04002015 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Trond Myklebust4fc87962012-03-08 17:42:01 -05002017 if (state != NULL) {
2018 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2019 current->files, current->tgid);
2020 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2021 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002022 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002023 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002024 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Bryan Schumaker7c513052011-03-24 17:12:24 +00002026 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002027 if (status == 0 && state != NULL)
2028 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002029 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002032static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2033 struct nfs_fattr *fattr, struct iattr *sattr,
2034 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002036 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002037 struct nfs4_exception exception = {
2038 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002039 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 int err;
2042 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002043 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2044 switch (err) {
2045 case -NFS4ERR_OPENMODE:
2046 if (state && !(state->state & FMODE_WRITE)) {
2047 err = -EBADF;
2048 if (sattr->ia_valid & ATTR_OPEN)
2049 err = -EACCES;
2050 goto out;
2051 }
2052 }
2053 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002055out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return err;
2057}
2058
2059struct nfs4_closedata {
2060 struct inode *inode;
2061 struct nfs4_state *state;
2062 struct nfs_closeargs arg;
2063 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002064 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002065 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002066 bool roc;
2067 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068};
2069
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002070static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002071{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002072 struct nfs4_closedata *calldata = data;
2073 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002074 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002075
Fred Isamanf7e89172011-01-06 11:36:32 +00002076 if (calldata->roc)
2077 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002078 nfs4_put_open_state(calldata->state);
2079 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002080 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002081 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002082 kfree(calldata);
2083}
2084
Trond Myklebust88069f72009-12-08 08:33:16 -05002085static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2086 fmode_t fmode)
2087{
2088 spin_lock(&state->owner->so_lock);
2089 if (!(fmode & FMODE_READ))
2090 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2091 if (!(fmode & FMODE_WRITE))
2092 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2093 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2094 spin_unlock(&state->owner->so_lock);
2095}
2096
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002097static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002099 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct nfs_server *server = NFS_SERVER(calldata->inode);
2102
Chuck Levera3ca5652012-03-01 17:00:40 -05002103 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002104 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2105 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* hmm. we are done with the inode, and in the process of freeing
2107 * the state_owner. we keep this around to process errors
2108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 switch (task->tk_status) {
2110 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002111 if (calldata->roc)
2112 pnfs_roc_set_barrier(state->inode,
2113 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002114 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002115 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002116 nfs4_close_clear_stateid_flags(state,
2117 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 break;
2119 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002120 case -NFS4ERR_OLD_STATEID:
2121 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002123 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002126 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2127 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002129 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002130 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002131 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002134static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002136 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002137 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002138 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002139
Chuck Levera3ca5652012-03-01 17:00:40 -05002140 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002141 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002142 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002143
Trond Myklebust88069f72009-12-08 08:33:16 -05002144 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2145 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002146 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002147 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002148 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002149 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002150 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2151 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2152 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002153 }
2154 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002155 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2156 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2157 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002158 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002159 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002160 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002161
2162 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002163 /* Note: exit _without_ calling nfs4_close_done */
2164 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002165 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002166 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002167
Fred Isamanf7e89172011-01-06 11:36:32 +00002168 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002169 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002170 if (calldata->roc &&
2171 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2172 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2173 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002174 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002175 }
2176 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002177
Trond Myklebust516a6af2005-10-27 22:12:41 -04002178 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002179 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002180 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002181 &calldata->arg.seq_args,
2182 &calldata->res.seq_res,
2183 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002184 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002186out:
2187 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002191 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002192 .rpc_call_done = nfs4_close_done,
2193 .rpc_release = nfs4_free_closedata,
2194};
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196/*
2197 * It is possible for data to be read/written from a mem-mapped file
2198 * after the sys_close call (which hits the vfs layer as a flush).
2199 * This means that we can't safely call nfsv4 close on a file until
2200 * the inode is cleared. This in turn means that we are not good
2201 * NFSv4 citizens - we do not indicate to the server to update the file's
2202 * share state even when we are done with one of the three share
2203 * stateid's in the inode.
2204 *
2205 * NOTE: Caller must be holding the sp->so_owner semaphore!
2206 */
Al Viro643168c2011-06-22 18:20:23 -04002207int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002209 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002211 struct nfs4_state_owner *sp = state->owner;
2212 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002213 struct rpc_message msg = {
2214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2215 .rpc_cred = state->owner->so_cred,
2216 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002217 struct rpc_task_setup task_setup_data = {
2218 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002219 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002221 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002222 .flags = RPC_TASK_ASYNC,
2223 };
Trond Myklebust95121352005-10-18 14:20:12 -07002224 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002226 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002228 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002229 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002230 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002232 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002233 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002235 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002236 if (calldata->arg.seqid == NULL)
2237 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002238 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002239 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002240 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002241 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002242 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002243 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002244 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002245
Trond Myklebust1174dd12010-12-21 10:52:24 -05002246 msg.rpc_argp = &calldata->arg;
2247 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002248 task_setup_data.callback_data = calldata;
2249 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002250 if (IS_ERR(task))
2251 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002252 status = 0;
2253 if (wait)
2254 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002255 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002256 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002257out_free_calldata:
2258 kfree(calldata);
2259out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002260 if (roc)
2261 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002262 nfs4_put_open_state(state);
2263 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002264 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Trond Myklebust2b484292010-09-17 10:56:51 -04002267static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002268nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct nfs4_state *state;
2271
Trond Myklebust565277f2007-10-15 18:17:53 -04002272 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002273 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2274 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002275 if (IS_ERR(state))
2276 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002277 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002278 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Trond Myklebust1185a552009-12-03 15:54:02 -05002281static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002282{
2283 if (ctx->state == NULL)
2284 return;
2285 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002286 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002287 else
Al Viro643168c2011-06-22 18:20:23 -04002288 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2292{
Benny Halevy43652ad2009-04-01 09:21:54 -04002293 struct nfs4_server_caps_arg args = {
2294 .fhandle = fhandle,
2295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 struct nfs4_server_caps_res res = {};
2297 struct rpc_message msg = {
2298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002299 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 .rpc_resp = &res,
2301 };
2302 int status;
2303
Bryan Schumaker7c513052011-03-24 17:12:24 +00002304 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (status == 0) {
2306 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002307 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2308 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2309 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2310 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2311 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2313 server->caps |= NFS_CAP_ACLS;
2314 if (res.has_links != 0)
2315 server->caps |= NFS_CAP_HARDLINKS;
2316 if (res.has_symlinks != 0)
2317 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002318 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2319 server->caps |= NFS_CAP_FILEID;
2320 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2321 server->caps |= NFS_CAP_MODE;
2322 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2323 server->caps |= NFS_CAP_NLINK;
2324 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2325 server->caps |= NFS_CAP_OWNER;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2327 server->caps |= NFS_CAP_OWNER_GROUP;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2329 server->caps |= NFS_CAP_ATIME;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2331 server->caps |= NFS_CAP_CTIME;
2332 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2333 server->caps |= NFS_CAP_MTIME;
2334
Trond Myklebusta65318b2009-03-11 14:10:28 -04002335 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2336 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2337 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002339 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return status;
2343}
2344
Trond Myklebust55a97592006-06-09 09:34:19 -04002345int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 struct nfs4_exception exception = { };
2348 int err;
2349 do {
2350 err = nfs4_handle_exception(server,
2351 _nfs4_server_capabilities(server, fhandle),
2352 &exception);
2353 } while (exception.retry);
2354 return err;
2355}
2356
2357static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2358 struct nfs_fsinfo *info)
2359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct nfs4_lookup_root_arg args = {
2361 .bitmask = nfs4_fattr_bitmap,
2362 };
2363 struct nfs4_lookup_res res = {
2364 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002365 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 .fh = fhandle,
2367 };
2368 struct rpc_message msg = {
2369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2370 .rpc_argp = &args,
2371 .rpc_resp = &res,
2372 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002373
Trond Myklebust0e574af2005-10-27 22:12:38 -04002374 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002375 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
2378static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2379 struct nfs_fsinfo *info)
2380{
2381 struct nfs4_exception exception = { };
2382 int err;
2383 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002384 err = _nfs4_lookup_root(server, fhandle, info);
2385 switch (err) {
2386 case 0:
2387 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002388 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002389 default:
2390 err = nfs4_handle_exception(server, err, &exception);
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002393out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return err;
2395}
2396
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002397static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2398 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2399{
2400 struct rpc_auth *auth;
2401 int ret;
2402
2403 auth = rpcauth_create(flavor, server->client);
2404 if (!auth) {
2405 ret = -EIO;
2406 goto out;
2407 }
2408 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002409out:
2410 return ret;
2411}
2412
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002413static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002414 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002416 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002417 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002419 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2420 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002421
2422 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002423 /* AUTH_UNIX is the default flavor if none was specified,
2424 * thus has already been tried. */
2425 if (flav_array[i] == RPC_AUTH_UNIX)
2426 continue;
2427
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002428 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002429 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002430 continue;
2431 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002432 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002433 /*
2434 * -EACCESS could mean that the user doesn't have correct permissions
2435 * to access the mount. It could also mean that we tried to mount
2436 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2437 * existing mount programs don't handle -EACCES very well so it should
2438 * be mapped to -EPERM instead.
2439 */
2440 if (status == -EACCES)
2441 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002442 return status;
2443}
2444
2445/*
2446 * get the file handle for the "/" directory on the server
2447 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002448int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2449 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002450{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002451 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002452 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002453 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2454 /*
2455 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2456 * by nfs4_map_errors() as this function exits.
2457 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002458 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (status == 0)
2460 status = nfs4_server_capabilities(server, fhandle);
2461 if (status == 0)
2462 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002463 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Bryan Schumakerbae36242012-05-10 15:07:31 -04002466static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2467 struct nfs_fsinfo *info)
2468{
2469 int error;
2470 struct nfs_fattr *fattr = info->fattr;
2471
2472 error = nfs4_server_capabilities(server, mntfh);
2473 if (error < 0) {
2474 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2475 return error;
2476 }
2477
2478 error = nfs4_proc_getattr(server, mntfh, fattr);
2479 if (error < 0) {
2480 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2481 return error;
2482 }
2483
2484 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2485 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2486 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2487
2488 return error;
2489}
2490
Manoj Naik6b97fd32006-06-09 09:34:29 -04002491/*
2492 * Get locations and (maybe) other attributes of a referral.
2493 * Note that we'll actually follow the referral later when
2494 * we detect fsid mismatch in inode revalidation
2495 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002496static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2497 const struct qstr *name, struct nfs_fattr *fattr,
2498 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002499{
2500 int status = -ENOMEM;
2501 struct page *page = NULL;
2502 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002503
2504 page = alloc_page(GFP_KERNEL);
2505 if (page == NULL)
2506 goto out;
2507 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2508 if (locations == NULL)
2509 goto out;
2510
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002511 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002512 if (status != 0)
2513 goto out;
2514 /* Make sure server returned a different fsid for the referral */
2515 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002516 dprintk("%s: server did not return a different fsid for"
2517 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002518 status = -EIO;
2519 goto out;
2520 }
Andy Adamson533eb462011-06-13 18:25:56 -04002521 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2522 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002523
Andy Adamson533eb462011-06-13 18:25:56 -04002524 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002525 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002526 memset(fhandle, 0, sizeof(struct nfs_fh));
2527out:
2528 if (page)
2529 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002530 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002531 return status;
2532}
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2535{
2536 struct nfs4_getattr_arg args = {
2537 .fh = fhandle,
2538 .bitmask = server->attr_bitmask,
2539 };
2540 struct nfs4_getattr_res res = {
2541 .fattr = fattr,
2542 .server = server,
2543 };
2544 struct rpc_message msg = {
2545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2546 .rpc_argp = &args,
2547 .rpc_resp = &res,
2548 };
2549
Trond Myklebust0e574af2005-10-27 22:12:38 -04002550 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002551 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2555{
2556 struct nfs4_exception exception = { };
2557 int err;
2558 do {
2559 err = nfs4_handle_exception(server,
2560 _nfs4_proc_getattr(server, fhandle, fattr),
2561 &exception);
2562 } while (exception.retry);
2563 return err;
2564}
2565
2566/*
2567 * The file is not closed if it is opened due to the a request to change
2568 * the size of the file. The open call will not be needed once the
2569 * VFS layer lookup-intents are implemented.
2570 *
2571 * Close is called when the inode is destroyed.
2572 * If we haven't opened the file for O_WRONLY, we
2573 * need to in the size_change case to obtain a stateid.
2574 *
2575 * Got race?
2576 * Because OPEN is always done by name in nfsv4, it is
2577 * possible that we opened a different file by the same
2578 * name. We can recognize this race condition, but we
2579 * can't do anything about it besides returning an error.
2580 *
2581 * This will be fixed with VFS changes (lookup-intent).
2582 */
2583static int
2584nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2585 struct iattr *sattr)
2586{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002587 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002588 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002589 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 int status;
2591
Benny Halevy8a1636c2010-07-14 15:43:57 -04002592 if (pnfs_ld_layoutret_on_setattr(inode))
2593 pnfs_return_layout(inode);
2594
Trond Myklebust0e574af2005-10-27 22:12:38 -04002595 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Andy Adamson26699402012-05-30 16:12:24 -04002597 /* Deal with open(O_TRUNC) */
2598 if (sattr->ia_valid & ATTR_OPEN)
2599 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2600
2601 /* Optimization: if the end result is no change, don't RPC */
2602 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2603 return 0;
2604
Trond Myklebustd5308382005-11-04 15:33:38 -05002605 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002606 if (sattr->ia_valid & ATTR_FILE) {
2607 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002608
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002609 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002610 if (ctx) {
2611 cred = ctx->cred;
2612 state = ctx->state;
2613 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002614 }
2615
2616 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002617 if (status == 0)
2618 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return status;
2620}
2621
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002622static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2623 const struct qstr *name, struct nfs_fh *fhandle,
2624 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002625{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002626 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002627 int status;
2628 struct nfs4_lookup_arg args = {
2629 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002630 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002631 .name = name,
2632 };
2633 struct nfs4_lookup_res res = {
2634 .server = server,
2635 .fattr = fattr,
2636 .fh = fhandle,
2637 };
2638 struct rpc_message msg = {
2639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2640 .rpc_argp = &args,
2641 .rpc_resp = &res,
2642 };
2643
2644 nfs_fattr_init(fattr);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002647 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 dprintk("NFS reply lookup: %d\n", status);
2649 return status;
2650}
2651
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002652static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002653{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002654 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002655 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002656 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2657 fattr->nlink = 2;
2658}
2659
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002660static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2661 struct qstr *name, struct nfs_fh *fhandle,
2662 struct nfs_fattr *fattr)
2663{
2664 struct nfs4_exception exception = { };
2665 struct rpc_clnt *client = *clnt;
2666 int err;
2667 do {
2668 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2669 switch (err) {
2670 case -NFS4ERR_BADNAME:
2671 err = -ENOENT;
2672 goto out;
2673 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002674 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002675 goto out;
2676 case -NFS4ERR_WRONGSEC:
2677 err = -EPERM;
2678 if (client != *clnt)
2679 goto out;
2680
2681 client = nfs4_create_sec_client(client, dir, name);
2682 if (IS_ERR(client))
2683 return PTR_ERR(client);
2684
2685 exception.retry = 1;
2686 break;
2687 default:
2688 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2689 }
2690 } while (exception.retry);
2691
2692out:
2693 if (err == 0)
2694 *clnt = client;
2695 else if (client != *clnt)
2696 rpc_shutdown_client(client);
2697
2698 return err;
2699}
2700
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002701static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002702 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002704 int status;
2705 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002706
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002707 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2708 if (client != NFS_CLIENT(dir)) {
2709 rpc_shutdown_client(client);
2710 nfs_fixup_secinfo_attributes(fattr);
2711 }
2712 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002715struct rpc_clnt *
2716nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2717 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2718{
2719 int status;
2720 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2721
2722 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2723 if (status < 0) {
2724 rpc_shutdown_client(client);
2725 return ERR_PTR(status);
2726 }
2727 return client;
2728}
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2731{
Trond Myklebust76b32992007-08-10 17:45:11 -04002732 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 struct nfs4_accessargs args = {
2734 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002735 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002737 struct nfs4_accessres res = {
2738 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002739 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct rpc_message msg = {
2741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2742 .rpc_argp = &args,
2743 .rpc_resp = &res,
2744 .rpc_cred = entry->cred,
2745 };
2746 int mode = entry->mask;
2747 int status;
2748
2749 /*
2750 * Determine which access bits we want to ask for...
2751 */
2752 if (mode & MAY_READ)
2753 args.access |= NFS4_ACCESS_READ;
2754 if (S_ISDIR(inode->i_mode)) {
2755 if (mode & MAY_WRITE)
2756 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2757 if (mode & MAY_EXEC)
2758 args.access |= NFS4_ACCESS_LOOKUP;
2759 } else {
2760 if (mode & MAY_WRITE)
2761 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2762 if (mode & MAY_EXEC)
2763 args.access |= NFS4_ACCESS_EXECUTE;
2764 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002765
2766 res.fattr = nfs_alloc_fattr();
2767 if (res.fattr == NULL)
2768 return -ENOMEM;
2769
Bryan Schumaker7c513052011-03-24 17:12:24 +00002770 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (!status) {
2772 entry->mask = 0;
2773 if (res.access & NFS4_ACCESS_READ)
2774 entry->mask |= MAY_READ;
2775 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2776 entry->mask |= MAY_WRITE;
2777 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2778 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002779 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002781 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return status;
2783}
2784
2785static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2786{
2787 struct nfs4_exception exception = { };
2788 int err;
2789 do {
2790 err = nfs4_handle_exception(NFS_SERVER(inode),
2791 _nfs4_proc_access(inode, entry),
2792 &exception);
2793 } while (exception.retry);
2794 return err;
2795}
2796
2797/*
2798 * TODO: For the time being, we don't try to get any attributes
2799 * along with any of the zero-copy operations READ, READDIR,
2800 * READLINK, WRITE.
2801 *
2802 * In the case of the first three, we want to put the GETATTR
2803 * after the read-type operation -- this is because it is hard
2804 * to predict the length of a GETATTR response in v4, and thus
2805 * align the READ data correctly. This means that the GETATTR
2806 * may end up partially falling into the page cache, and we should
2807 * shift it into the 'tail' of the xdr_buf before processing.
2808 * To do this efficiently, we need to know the total length
2809 * of data received, which doesn't seem to be available outside
2810 * of the RPC layer.
2811 *
2812 * In the case of WRITE, we also want to put the GETATTR after
2813 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002814 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 *
2816 * Both of these changes to the XDR layer would in fact be quite
2817 * minor, but I decided to leave them for a subsequent patch.
2818 */
2819static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2820 unsigned int pgbase, unsigned int pglen)
2821{
2822 struct nfs4_readlink args = {
2823 .fh = NFS_FH(inode),
2824 .pgbase = pgbase,
2825 .pglen = pglen,
2826 .pages = &page,
2827 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002828 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2831 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002832 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 };
2834
Bryan Schumaker7c513052011-03-24 17:12:24 +00002835 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
2838static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2839 unsigned int pgbase, unsigned int pglen)
2840{
2841 struct nfs4_exception exception = { };
2842 int err;
2843 do {
2844 err = nfs4_handle_exception(NFS_SERVER(inode),
2845 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2846 &exception);
2847 } while (exception.retry);
2848 return err;
2849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002852 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854static int
2855nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002858 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 int status = 0;
2861
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002862 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2863 if (IS_ERR(ctx))
2864 return PTR_ERR(ctx);
2865
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002866 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002867 state = nfs4_do_open(dir, dentry, ctx->mode,
2868 flags, sattr, ctx->cred,
2869 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002870 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (IS_ERR(state)) {
2872 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002875 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002876 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002877 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002879 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return status;
2881}
2882
2883static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2884{
Trond Myklebust16e42952005-10-27 22:12:44 -04002885 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002886 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002888 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002890 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002891 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2895 .rpc_argp = &args,
2896 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002898 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002899
Bryan Schumaker7c513052011-03-24 17:12:24 +00002900 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002901 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002902 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return status;
2904}
2905
2906static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2907{
2908 struct nfs4_exception exception = { };
2909 int err;
2910 do {
2911 err = nfs4_handle_exception(NFS_SERVER(dir),
2912 _nfs4_proc_remove(dir, name),
2913 &exception);
2914 } while (exception.retry);
2915 return err;
2916}
2917
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002918static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002920 struct nfs_server *server = NFS_SERVER(dir);
2921 struct nfs_removeargs *args = msg->rpc_argp;
2922 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002924 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002926 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002929static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2930{
2931 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2932 &data->args.seq_args,
2933 &data->res.seq_res,
2934 task))
2935 return;
2936 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002939static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002941 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2942
Trond Myklebust14516c32010-07-31 14:29:06 -04002943 if (!nfs4_sequence_done(task, &res->seq_res))
2944 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002945 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002946 return 0;
2947 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002948 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Jeff Laytond3d41522010-09-17 17:31:57 -04002951static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2952{
2953 struct nfs_server *server = NFS_SERVER(dir);
2954 struct nfs_renameargs *arg = msg->rpc_argp;
2955 struct nfs_renameres *res = msg->rpc_resp;
2956
2957 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002958 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002959 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002960}
2961
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002962static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2963{
2964 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2965 &data->args.seq_args,
2966 &data->res.seq_res,
2967 task))
2968 return;
2969 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002970}
2971
2972static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2973 struct inode *new_dir)
2974{
2975 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2976
2977 if (!nfs4_sequence_done(task, &res->seq_res))
2978 return 0;
2979 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2980 return 0;
2981
2982 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002983 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002984 return 1;
2985}
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2988 struct inode *new_dir, struct qstr *new_name)
2989{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002990 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002991 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .old_dir = NFS_FH(old_dir),
2993 .new_dir = NFS_FH(new_dir),
2994 .old_name = old_name,
2995 .new_name = new_name,
2996 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002997 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002998 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3002 .rpc_argp = &arg,
3003 .rpc_resp = &res,
3004 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003005 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Bryan Schumaker7c513052011-03-24 17:12:24 +00003007 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (!status) {
3009 update_changeattr(old_dir, &res.old_cinfo);
3010 update_changeattr(new_dir, &res.new_cinfo);
3011 }
3012 return status;
3013}
3014
3015static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3016 struct inode *new_dir, struct qstr *new_name)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020 do {
3021 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3022 _nfs4_proc_rename(old_dir, old_name,
3023 new_dir, new_name),
3024 &exception);
3025 } while (exception.retry);
3026 return err;
3027}
3028
3029static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3030{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003031 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct nfs4_link_arg arg = {
3033 .fh = NFS_FH(inode),
3034 .dir_fh = NFS_FH(dir),
3035 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003036 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003038 struct nfs4_link_res res = {
3039 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 struct rpc_message msg = {
3042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3043 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003044 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003046 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Trond Myklebust136f2622010-04-16 16:22:49 -04003048 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003049 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003050 goto out;
3051
Bryan Schumaker7c513052011-03-24 17:12:24 +00003052 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003053 if (!status) {
3054 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003055 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003056 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003057out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003058 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return status;
3060}
3061
3062static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3063{
3064 struct nfs4_exception exception = { };
3065 int err;
3066 do {
3067 err = nfs4_handle_exception(NFS_SERVER(inode),
3068 _nfs4_proc_link(inode, dir, name),
3069 &exception);
3070 } while (exception.retry);
3071 return err;
3072}
3073
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003074struct nfs4_createdata {
3075 struct rpc_message msg;
3076 struct nfs4_create_arg arg;
3077 struct nfs4_create_res res;
3078 struct nfs_fh fh;
3079 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003080};
3081
3082static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3083 struct qstr *name, struct iattr *sattr, u32 ftype)
3084{
3085 struct nfs4_createdata *data;
3086
3087 data = kzalloc(sizeof(*data), GFP_KERNEL);
3088 if (data != NULL) {
3089 struct nfs_server *server = NFS_SERVER(dir);
3090
3091 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3092 data->msg.rpc_argp = &data->arg;
3093 data->msg.rpc_resp = &data->res;
3094 data->arg.dir_fh = NFS_FH(dir);
3095 data->arg.server = server;
3096 data->arg.name = name;
3097 data->arg.attrs = sattr;
3098 data->arg.ftype = ftype;
3099 data->arg.bitmask = server->attr_bitmask;
3100 data->res.server = server;
3101 data->res.fh = &data->fh;
3102 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003104 }
3105 return data;
3106}
3107
3108static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3109{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003110 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003111 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 if (status == 0) {
3113 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003114 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3115 }
3116 return status;
3117}
3118
3119static void nfs4_free_createdata(struct nfs4_createdata *data)
3120{
3121 kfree(data);
3122}
3123
Chuck Lever4f390c12006-08-22 20:06:22 -04003124static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003125 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 struct nfs4_createdata *data;
3128 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Chuck Lever94a6d752006-08-22 20:06:23 -04003130 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003132
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003133 status = -ENOMEM;
3134 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3135 if (data == NULL)
3136 goto out;
3137
3138 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3139 data->arg.u.symlink.pages = &page;
3140 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003142 status = nfs4_do_create(dir, dentry, data);
3143
3144 nfs4_free_createdata(data);
3145out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return status;
3147}
3148
Chuck Lever4f390c12006-08-22 20:06:22 -04003149static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003150 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 struct nfs4_exception exception = { };
3153 int err;
3154 do {
3155 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003156 _nfs4_proc_symlink(dir, dentry, page,
3157 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3164 struct iattr *sattr)
3165{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003166 struct nfs4_createdata *data;
3167 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3170 if (data == NULL)
3171 goto out;
3172
3173 status = nfs4_do_create(dir, dentry, data);
3174
3175 nfs4_free_createdata(data);
3176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return status;
3178}
3179
3180static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3181 struct iattr *sattr)
3182{
3183 struct nfs4_exception exception = { };
3184 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003185
3186 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 do {
3188 err = nfs4_handle_exception(NFS_SERVER(dir),
3189 _nfs4_proc_mkdir(dir, dentry, sattr),
3190 &exception);
3191 } while (exception.retry);
3192 return err;
3193}
3194
3195static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003196 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct inode *dir = dentry->d_inode;
3199 struct nfs4_readdir_arg args = {
3200 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003201 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 .pgbase = 0,
3203 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003204 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003205 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 };
3207 struct nfs4_readdir_res res;
3208 struct rpc_message msg = {
3209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3210 .rpc_argp = &args,
3211 .rpc_resp = &res,
3212 .rpc_cred = cred,
3213 };
3214 int status;
3215
Harvey Harrison3110ff82008-05-02 13:42:44 -07003216 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003217 dentry->d_parent->d_name.name,
3218 dentry->d_name.name,
3219 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3221 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003222 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003223 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003225 status += args.pgbase;
3226 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003227
3228 nfs_invalidate_atime(dir);
3229
Harvey Harrison3110ff82008-05-02 13:42:44 -07003230 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return status;
3232}
3233
3234static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003235 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 struct nfs4_exception exception = { };
3238 int err;
3239 do {
3240 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3241 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003242 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 &exception);
3244 } while (exception.retry);
3245 return err;
3246}
3247
3248static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3249 struct iattr *sattr, dev_t rdev)
3250{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251 struct nfs4_createdata *data;
3252 int mode = sattr->ia_mode;
3253 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3256 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003257
3258 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3259 if (data == NULL)
3260 goto out;
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003263 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003265 data->arg.ftype = NF4BLK;
3266 data->arg.u.device.specdata1 = MAJOR(rdev);
3267 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
3269 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003270 data->arg.ftype = NF4CHR;
3271 data->arg.u.device.specdata1 = MAJOR(rdev);
3272 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003275 status = nfs4_do_create(dir, dentry, data);
3276
3277 nfs4_free_createdata(data);
3278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return status;
3280}
3281
3282static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3283 struct iattr *sattr, dev_t rdev)
3284{
3285 struct nfs4_exception exception = { };
3286 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003287
3288 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 do {
3290 err = nfs4_handle_exception(NFS_SERVER(dir),
3291 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3292 &exception);
3293 } while (exception.retry);
3294 return err;
3295}
3296
3297static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3298 struct nfs_fsstat *fsstat)
3299{
3300 struct nfs4_statfs_arg args = {
3301 .fh = fhandle,
3302 .bitmask = server->attr_bitmask,
3303 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003304 struct nfs4_statfs_res res = {
3305 .fsstat = fsstat,
3306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3309 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003310 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 };
3312
Trond Myklebust0e574af2005-10-27 22:12:38 -04003313 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003314 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3318{
3319 struct nfs4_exception exception = { };
3320 int err;
3321 do {
3322 err = nfs4_handle_exception(server,
3323 _nfs4_proc_statfs(server, fhandle, fsstat),
3324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
3329static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3330 struct nfs_fsinfo *fsinfo)
3331{
3332 struct nfs4_fsinfo_arg args = {
3333 .fh = fhandle,
3334 .bitmask = server->attr_bitmask,
3335 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003336 struct nfs4_fsinfo_res res = {
3337 .fsinfo = fsinfo,
3338 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct rpc_message msg = {
3340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3341 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003342 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 };
3344
Bryan Schumaker7c513052011-03-24 17:12:24 +00003345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
3348static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3349{
3350 struct nfs4_exception exception = { };
3351 int err;
3352
3353 do {
3354 err = nfs4_handle_exception(server,
3355 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3356 &exception);
3357 } while (exception.retry);
3358 return err;
3359}
3360
3361static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3362{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003363 int error;
3364
Trond Myklebust0e574af2005-10-27 22:12:38 -04003365 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003366 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3367 if (error == 0)
3368 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3369
3370 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
3373static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3374 struct nfs_pathconf *pathconf)
3375{
3376 struct nfs4_pathconf_arg args = {
3377 .fh = fhandle,
3378 .bitmask = server->attr_bitmask,
3379 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003380 struct nfs4_pathconf_res res = {
3381 .pathconf = pathconf,
3382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3385 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003386 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 };
3388
3389 /* None of the pathconf attributes are mandatory to implement */
3390 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3391 memset(pathconf, 0, sizeof(*pathconf));
3392 return 0;
3393 }
3394
Trond Myklebust0e574af2005-10-27 22:12:38 -04003395 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003396 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3400 struct nfs_pathconf *pathconf)
3401{
3402 struct nfs4_exception exception = { };
3403 int err;
3404
3405 do {
3406 err = nfs4_handle_exception(server,
3407 _nfs4_proc_pathconf(server, fhandle, pathconf),
3408 &exception);
3409 } while (exception.retry);
3410 return err;
3411}
3412
Benny Halevyd20581a2011-05-22 19:52:03 +03003413void __nfs4_read_done_cb(struct nfs_read_data *data)
3414{
Fred Isamancd841602012-04-20 14:47:44 -04003415 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003416}
3417
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003418static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Fred Isamancd841602012-04-20 14:47:44 -04003420 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003422 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003423 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003424 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003426
Benny Halevyd20581a2011-05-22 19:52:03 +03003427 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003429 renew_lease(server, data->timestamp);
3430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003433static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3434{
3435
3436 dprintk("--> %s\n", __func__);
3437
3438 if (!nfs4_sequence_done(task, &data->res.seq_res))
3439 return -EAGAIN;
3440
Benny Halevyd20581a2011-05-22 19:52:03 +03003441 return data->read_done_cb ? data->read_done_cb(task, data) :
3442 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003443}
3444
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003445static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003448 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003449 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003450 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003453static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3454{
Fred Isamancd841602012-04-20 14:47:44 -04003455 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003456 &data->args.seq_args,
3457 &data->res.seq_res,
3458 task))
3459 return;
3460 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
Fred Isamanb029bc92011-03-03 15:13:42 +00003463static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
Fred Isamancd841602012-04-20 14:47:44 -04003465 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003467 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003468 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003469 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003471 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003473 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003474 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Fred Isamanb029bc92011-03-03 15:13:42 +00003478static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3479{
3480 if (!nfs4_sequence_done(task, &data->res.seq_res))
3481 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003482 return data->write_done_cb ? data->write_done_cb(task, data) :
3483 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003484}
3485
Trond Myklebust5a37f852012-04-28 14:55:16 -04003486static
3487bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003488{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003489 const struct nfs_pgio_header *hdr = data->header;
3490
3491 /* Don't request attributes for pNFS or O_DIRECT writes */
3492 if (data->ds_clp != NULL || hdr->dreq != NULL)
3493 return false;
3494 /* Otherwise, request attributes if and only if we don't hold
3495 * a delegation
3496 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003497 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003498}
Fred Isamana69aef12011-03-03 15:13:47 +00003499
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003500static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
Fred Isamancd841602012-04-20 14:47:44 -04003502 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003503
Trond Myklebust5a37f852012-04-28 14:55:16 -04003504 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003505 data->args.bitmask = NULL;
3506 data->res.fattr = NULL;
3507 } else
3508 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003509
Fred Isamanb029bc92011-03-03 15:13:42 +00003510 if (!data->write_done_cb)
3511 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003512 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 data->timestamp = jiffies;
3514
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003515 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003516 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003519static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3520{
Fred Isamancd841602012-04-20 14:47:44 -04003521 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003522 &data->args.seq_args,
3523 &data->res.seq_res,
3524 task))
3525 return;
3526 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Fred Isaman0b7c0152012-04-20 14:47:39 -04003529static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3532 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003533 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return;
3536 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003537}
3538
Fred Isaman0b7c0152012-04-20 14:47:39 -04003539static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
3541 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003542
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003543 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003544 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003545 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
Fred Isaman0b7c0152012-04-20 14:47:39 -04003550static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003551{
3552 if (!nfs4_sequence_done(task, &data->res.seq_res))
3553 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003554 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003555}
3556
Fred Isaman0b7c0152012-04-20 14:47:39 -04003557static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003559 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003560
Fred Isaman0b7c0152012-04-20 14:47:39 -04003561 if (data->commit_done_cb == NULL)
3562 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003563 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003565 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003568struct nfs4_renewdata {
3569 struct nfs_client *client;
3570 unsigned long timestamp;
3571};
3572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573/*
3574 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3575 * standalone procedure for queueing an asynchronous RENEW.
3576 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003577static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003578{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003579 struct nfs4_renewdata *data = calldata;
3580 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003581
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003582 if (atomic_read(&clp->cl_count) > 1)
3583 nfs4_schedule_state_renewal(clp);
3584 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003585 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003586}
3587
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003588static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003590 struct nfs4_renewdata *data = calldata;
3591 struct nfs_client *clp = data->client;
3592 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
3594 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003595 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003596 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3597 return;
3598 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003599 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003600 return;
3601 }
3602 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003604 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003607static const struct rpc_call_ops nfs4_renew_ops = {
3608 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003609 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003610};
3611
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003612static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 struct rpc_message msg = {
3615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3616 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003617 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003619 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003621 if (renew_flags == 0)
3622 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003623 if (!atomic_inc_not_zero(&clp->cl_count))
3624 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003625 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003626 if (data == NULL)
3627 return -ENOMEM;
3628 data->client = clp;
3629 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003631 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003634static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 struct rpc_message msg = {
3637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3638 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003639 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 };
3641 unsigned long now = jiffies;
3642 int status;
3643
3644 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3645 if (status < 0)
3646 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003647 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return 0;
3649}
3650
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003651static inline int nfs4_server_supports_acls(struct nfs_server *server)
3652{
3653 return (server->caps & NFS_CAP_ACLS)
3654 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3655 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3656}
3657
3658/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3659 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3660 * the stack.
3661 */
3662#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3663
Neil Hormane9e3d722011-03-04 19:26:03 -05003664static int buf_to_pages_noslab(const void *buf, size_t buflen,
3665 struct page **pages, unsigned int *pgbase)
3666{
3667 struct page *newpage, **spages;
3668 int rc = 0;
3669 size_t len;
3670 spages = pages;
3671
3672 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003673 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003674 newpage = alloc_page(GFP_KERNEL);
3675
3676 if (newpage == NULL)
3677 goto unwind;
3678 memcpy(page_address(newpage), buf, len);
3679 buf += len;
3680 buflen -= len;
3681 *pages++ = newpage;
3682 rc++;
3683 } while (buflen != 0);
3684
3685 return rc;
3686
3687unwind:
3688 for(; rc > 0; rc--)
3689 __free_page(spages[rc-1]);
3690 return -ENOMEM;
3691}
3692
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003693struct nfs4_cached_acl {
3694 int cached;
3695 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003696 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003697};
3698
3699static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003700{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003701 struct nfs_inode *nfsi = NFS_I(inode);
3702
3703 spin_lock(&inode->i_lock);
3704 kfree(nfsi->nfs4_acl);
3705 nfsi->nfs4_acl = acl;
3706 spin_unlock(&inode->i_lock);
3707}
3708
3709static void nfs4_zap_acl_attr(struct inode *inode)
3710{
3711 nfs4_set_cached_acl(inode, NULL);
3712}
3713
3714static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3715{
3716 struct nfs_inode *nfsi = NFS_I(inode);
3717 struct nfs4_cached_acl *acl;
3718 int ret = -ENOENT;
3719
3720 spin_lock(&inode->i_lock);
3721 acl = nfsi->nfs4_acl;
3722 if (acl == NULL)
3723 goto out;
3724 if (buf == NULL) /* user is just asking for length */
3725 goto out_len;
3726 if (acl->cached == 0)
3727 goto out;
3728 ret = -ERANGE; /* see getxattr(2) man page */
3729 if (acl->len > buflen)
3730 goto out;
3731 memcpy(buf, acl->data, acl->len);
3732out_len:
3733 ret = acl->len;
3734out:
3735 spin_unlock(&inode->i_lock);
3736 return ret;
3737}
3738
Sachin Prabhu5794d212012-04-17 14:36:40 +01003739static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740{
3741 struct nfs4_cached_acl *acl;
3742
Sachin Prabhu5794d212012-04-17 14:36:40 +01003743 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3745 if (acl == NULL)
3746 goto out;
3747 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003748 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003749 } else {
3750 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3751 if (acl == NULL)
3752 goto out;
3753 acl->cached = 0;
3754 }
3755 acl->len = acl_len;
3756out:
3757 nfs4_set_cached_acl(inode, acl);
3758}
3759
Andy Adamsonbf118a32011-12-07 11:55:27 -05003760/*
3761 * The getxattr API returns the required buffer length when called with a
3762 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3763 * the required buf. On a NULL buf, we send a page of data to the server
3764 * guessing that the ACL request can be serviced by a page. If so, we cache
3765 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3766 * the cache. If not so, we throw away the page, and cache the required
3767 * length. The next getxattr call will then produce another round trip to
3768 * the server, this time with the input buf of the required size.
3769 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003770static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003771{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003772 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003773 struct nfs_getaclargs args = {
3774 .fh = NFS_FH(inode),
3775 .acl_pages = pages,
3776 .acl_len = buflen,
3777 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003778 struct nfs_getaclres res = {
3779 .acl_len = buflen,
3780 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003781 struct rpc_message msg = {
3782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3783 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003784 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003785 };
Chuck Lever56d08fe2012-07-11 16:30:32 -04003786 int ret = -ENOMEM, npages, i;
3787 size_t acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003788
Andy Adamsonbf118a32011-12-07 11:55:27 -05003789 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3790 /* As long as we're doing a round trip to the server anyway,
3791 * let's be prepared for a page of acl data. */
3792 if (npages == 0)
3793 npages = 1;
3794
Sachin Prabhu5a006892012-04-17 14:35:39 +01003795 /* Add an extra page to handle the bitmap returned */
3796 npages++;
3797
Andy Adamsonbf118a32011-12-07 11:55:27 -05003798 for (i = 0; i < npages; i++) {
3799 pages[i] = alloc_page(GFP_KERNEL);
3800 if (!pages[i])
3801 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003802 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003803
3804 /* for decoding across pages */
3805 res.acl_scratch = alloc_page(GFP_KERNEL);
3806 if (!res.acl_scratch)
3807 goto out_free;
3808
Andy Adamsonbf118a32011-12-07 11:55:27 -05003809 args.acl_len = npages * PAGE_SIZE;
3810 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003811
Andy Adamsonbf118a32011-12-07 11:55:27 -05003812 /* Let decode_getfacl know not to fail if the ACL data is larger than
3813 * the page we send as a guess */
3814 if (buf == NULL)
3815 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003816
Peng Taode040be2012-01-10 22:42:47 +08003817 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003818 __func__, buf, buflen, npages, args.acl_len);
3819 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3820 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003821 if (ret)
3822 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003823
3824 acl_len = res.acl_len - res.acl_data_offset;
3825 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003826 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003827 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003828 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003829 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003830 if (buf) {
3831 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003832 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003833 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003834 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003835 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003836 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003837 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003838out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003839 for (i = 0; i < npages; i++)
3840 if (pages[i])
3841 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003842 if (res.acl_scratch)
3843 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003844 return ret;
3845}
3846
Trond Myklebust16b42892006-08-24 12:27:15 -04003847static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3848{
3849 struct nfs4_exception exception = { };
3850 ssize_t ret;
3851 do {
3852 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3853 if (ret >= 0)
3854 break;
3855 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3856 } while (exception.retry);
3857 return ret;
3858}
3859
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3861{
3862 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003863 int ret;
3864
3865 if (!nfs4_server_supports_acls(server))
3866 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867 ret = nfs_revalidate_inode(server, inode);
3868 if (ret < 0)
3869 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003870 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3871 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003872 ret = nfs4_read_cached_acl(inode, buf, buflen);
3873 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003874 /* -ENOENT is returned if there is no ACL or if there is an ACL
3875 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003876 return ret;
3877 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003878}
3879
Trond Myklebust16b42892006-08-24 12:27:15 -04003880static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003881{
3882 struct nfs_server *server = NFS_SERVER(inode);
3883 struct page *pages[NFS4ACL_MAXPAGES];
3884 struct nfs_setaclargs arg = {
3885 .fh = NFS_FH(inode),
3886 .acl_pages = pages,
3887 .acl_len = buflen,
3888 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003889 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003890 struct rpc_message msg = {
3891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3892 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003893 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003894 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003895 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003896
3897 if (!nfs4_server_supports_acls(server))
3898 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003899 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3900 if (i < 0)
3901 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003902 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003903 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003904
3905 /*
3906 * Free each page after tx, so the only ref left is
3907 * held by the network stack
3908 */
3909 for (; i > 0; i--)
3910 put_page(pages[i-1]);
3911
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003912 /*
3913 * Acl update can result in inode attribute update.
3914 * so mark the attribute cache invalid.
3915 */
3916 spin_lock(&inode->i_lock);
3917 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3918 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003919 nfs_access_zap_cache(inode);
3920 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003921 return ret;
3922}
3923
Trond Myklebust16b42892006-08-24 12:27:15 -04003924static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3925{
3926 struct nfs4_exception exception = { };
3927 int err;
3928 do {
3929 err = nfs4_handle_exception(NFS_SERVER(inode),
3930 __nfs4_proc_set_acl(inode, buf, buflen),
3931 &exception);
3932 } while (exception.retry);
3933 return err;
3934}
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003937nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003939 struct nfs_client *clp = server->nfs_client;
3940
3941 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 return 0;
3943 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003944 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003945 case -NFS4ERR_ADMIN_REVOKED:
3946 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003947 if (state == NULL)
3948 break;
3949 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003950 case -NFS4ERR_OPENMODE:
3951 if (state == NULL)
3952 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003953 nfs4_schedule_stateid_recovery(server, state);
3954 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003955 case -NFS4ERR_EXPIRED:
3956 if (state != NULL)
3957 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003959 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003960 nfs4_schedule_lease_recovery(clp);
3961 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003962#if defined(CONFIG_NFS_V4_1)
3963 case -NFS4ERR_BADSESSION:
3964 case -NFS4ERR_BADSLOT:
3965 case -NFS4ERR_BAD_HIGH_SLOT:
3966 case -NFS4ERR_DEADSESSION:
3967 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3968 case -NFS4ERR_SEQ_FALSE_RETRY:
3969 case -NFS4ERR_SEQ_MISORDERED:
3970 dprintk("%s ERROR %d, Reset session\n", __func__,
3971 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003972 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003973 task->tk_status = 0;
3974 return -EAGAIN;
3975#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003977 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003978 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003979 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3981 task->tk_status = 0;
3982 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003983 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 case -NFS4ERR_OLD_STATEID:
3985 task->tk_status = 0;
3986 return -EAGAIN;
3987 }
3988 task->tk_status = nfs4_map_errors(task->tk_status);
3989 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003990wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003991 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003992 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3993 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3994 task->tk_status = 0;
3995 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
Chuck Leverf0920752012-05-21 22:45:41 -04003998static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3999 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004000{
4001 __be32 verf[2];
4002
Chuck Lever2c820d92012-05-21 22:45:33 -04004003 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4004 /* An impossible timestamp guarantees this value
4005 * will never match a generated boot time. */
4006 verf[0] = 0;
4007 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4008 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004009 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4010 verf[0] = (__be32)nn->boot_time.tv_sec;
4011 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004012 }
Chuck Levercd937102012-03-02 17:14:31 -05004013 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4014}
4015
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004016/**
4017 * nfs4_proc_setclientid - Negotiate client ID
4018 * @clp: state data structure
4019 * @program: RPC program for NFSv4 callback service
4020 * @port: IP port number for NFS4 callback service
4021 * @cred: RPC credential to use for this call
4022 * @res: where to place the result
4023 *
4024 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4025 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004026int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4027 unsigned short port, struct rpc_cred *cred,
4028 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
4030 nfs4_verifier sc_verifier;
4031 struct nfs4_setclientid setclientid = {
4032 .sc_verifier = &sc_verifier,
4033 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004034 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 };
4036 struct rpc_message msg = {
4037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4038 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004039 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004040 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004042 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Chuck Leverde734832012-07-11 16:30:50 -04004044 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004045 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Leverde734832012-07-11 16:30:50 -04004046 rcu_read_lock();
4047 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
4048 sizeof(setclientid.sc_name), "%s/%s %s",
4049 clp->cl_ipaddr,
4050 rpc_peeraddr2str(clp->cl_rpcclient,
4051 RPC_DISPLAY_ADDR),
4052 rpc_peeraddr2str(clp->cl_rpcclient,
4053 RPC_DISPLAY_PROTO));
4054 /* cb_client4 */
4055 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004056 sizeof(setclientid.sc_netid),
4057 rpc_peeraddr2str(clp->cl_rpcclient,
4058 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004059 rcu_read_unlock();
4060 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004061 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 clp->cl_ipaddr, port >> 8, port & 255);
4063
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004064 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4065 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4066 setclientid.sc_name_len, setclientid.sc_name);
4067 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4068 dprintk("NFS reply setclientid: %d\n", status);
4069 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070}
4071
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004072/**
4073 * nfs4_proc_setclientid_confirm - Confirm client ID
4074 * @clp: state data structure
4075 * @res: result of a previous SETCLIENTID
4076 * @cred: RPC credential to use for this call
4077 *
4078 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4079 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004080int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004081 struct nfs4_setclientid_res *arg,
4082 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
4084 struct nfs_fsinfo fsinfo;
4085 struct rpc_message msg = {
4086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004087 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004089 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 };
4091 unsigned long now;
4092 int status;
4093
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004094 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4095 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4096 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004098 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 if (status == 0) {
4100 spin_lock(&clp->cl_lock);
4101 clp->cl_lease_time = fsinfo.lease_time * HZ;
4102 clp->cl_last_renewal = now;
4103 spin_unlock(&clp->cl_lock);
4104 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004105 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return status;
4107}
4108
Trond Myklebustfe650402006-01-03 09:55:18 +01004109struct nfs4_delegreturndata {
4110 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004111 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004112 struct nfs_fh fh;
4113 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004114 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004115 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004116 int rpc_status;
4117};
4118
Trond Myklebustfe650402006-01-03 09:55:18 +01004119static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4120{
4121 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004122
Trond Myklebust14516c32010-07-31 14:29:06 -04004123 if (!nfs4_sequence_done(task, &data->res.seq_res))
4124 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004125
Ricardo Labiaga79708862009-12-07 09:23:21 -05004126 switch (task->tk_status) {
4127 case -NFS4ERR_STALE_STATEID:
4128 case -NFS4ERR_EXPIRED:
4129 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004130 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004131 break;
4132 default:
4133 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4134 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004135 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004136 return;
4137 }
4138 }
4139 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004140}
4141
4142static void nfs4_delegreturn_release(void *calldata)
4143{
Trond Myklebustfe650402006-01-03 09:55:18 +01004144 kfree(calldata);
4145}
4146
Andy Adamson938e1012009-04-01 09:22:28 -04004147#if defined(CONFIG_NFS_V4_1)
4148static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4149{
4150 struct nfs4_delegreturndata *d_data;
4151
4152 d_data = (struct nfs4_delegreturndata *)data;
4153
Trond Myklebust035168ab2010-06-16 09:52:26 -04004154 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004155 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004156 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004157 return;
4158 rpc_call_start(task);
4159}
4160#endif /* CONFIG_NFS_V4_1 */
4161
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004162static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004163#if defined(CONFIG_NFS_V4_1)
4164 .rpc_call_prepare = nfs4_delegreturn_prepare,
4165#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004166 .rpc_call_done = nfs4_delegreturn_done,
4167 .rpc_release = nfs4_delegreturn_release,
4168};
4169
Trond Myklebuste6f81072008-01-24 18:14:34 -05004170static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004171{
4172 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004173 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004174 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004175 struct rpc_message msg = {
4176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4177 .rpc_cred = cred,
4178 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004179 struct rpc_task_setup task_setup_data = {
4180 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004181 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004182 .callback_ops = &nfs4_delegreturn_ops,
4183 .flags = RPC_TASK_ASYNC,
4184 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004185 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004186
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004187 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004188 if (data == NULL)
4189 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004190 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004191 data->args.fhandle = &data->fh;
4192 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004193 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004194 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004195 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004196 data->res.fattr = &data->fattr;
4197 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004198 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004199 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004200 data->rpc_status = 0;
4201
Trond Myklebustc970aa82007-07-14 15:39:59 -04004202 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004203 msg.rpc_argp = &data->args;
4204 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004205 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004206 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004207 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004208 if (!issync)
4209 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004210 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004211 if (status != 0)
4212 goto out;
4213 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004214 if (status == 0)
4215 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4216 else
4217 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004218out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004219 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004220 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
Trond Myklebuste6f81072008-01-24 18:14:34 -05004223int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
4225 struct nfs_server *server = NFS_SERVER(inode);
4226 struct nfs4_exception exception = { };
4227 int err;
4228 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004229 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 switch (err) {
4231 case -NFS4ERR_STALE_STATEID:
4232 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 case 0:
4234 return 0;
4235 }
4236 err = nfs4_handle_exception(server, err, &exception);
4237 } while (exception.retry);
4238 return err;
4239}
4240
4241#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4242#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4243
4244/*
4245 * sleep, with exponential backoff, and retry the LOCK operation.
4246 */
4247static unsigned long
4248nfs4_set_lock_task_retry(unsigned long timeout)
4249{
Jeff Laytond3103102011-12-01 22:44:39 +01004250 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 timeout <<= 1;
4252 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4253 return NFS4_LOCK_MAXTIMEOUT;
4254 return timeout;
4255}
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4258{
4259 struct inode *inode = state->inode;
4260 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004261 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004262 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004264 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004266 struct nfs_lockt_res res = {
4267 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 };
4269 struct rpc_message msg = {
4270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4271 .rpc_argp = &arg,
4272 .rpc_resp = &res,
4273 .rpc_cred = state->owner->so_cred,
4274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 struct nfs4_lock_state *lsp;
4276 int status;
4277
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004278 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004279 status = nfs4_set_lock_state(state, request);
4280 if (status != 0)
4281 goto out;
4282 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004283 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004284 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004285 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004286 switch (status) {
4287 case 0:
4288 request->fl_type = F_UNLCK;
4289 break;
4290 case -NFS4ERR_DENIED:
4291 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004293 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004294out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return status;
4296}
4297
4298static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4299{
4300 struct nfs4_exception exception = { };
4301 int err;
4302
4303 do {
4304 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4305 _nfs4_proc_getlk(state, cmd, request),
4306 &exception);
4307 } while (exception.retry);
4308 return err;
4309}
4310
4311static int do_vfs_lock(struct file *file, struct file_lock *fl)
4312{
4313 int res = 0;
4314 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4315 case FL_POSIX:
4316 res = posix_lock_file_wait(file, fl);
4317 break;
4318 case FL_FLOCK:
4319 res = flock_lock_file_wait(file, fl);
4320 break;
4321 default:
4322 BUG();
4323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 return res;
4325}
4326
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004327struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004328 struct nfs_locku_args arg;
4329 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004330 struct nfs4_lock_state *lsp;
4331 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004332 struct file_lock fl;
4333 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004334 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004335};
4336
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004337static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4338 struct nfs_open_context *ctx,
4339 struct nfs4_lock_state *lsp,
4340 struct nfs_seqid *seqid)
4341{
4342 struct nfs4_unlockdata *p;
4343 struct inode *inode = lsp->ls_state->inode;
4344
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004345 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004346 if (p == NULL)
4347 return NULL;
4348 p->arg.fh = NFS_FH(inode);
4349 p->arg.fl = &p->fl;
4350 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004351 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004352 p->arg.stateid = &lsp->ls_stateid;
4353 p->lsp = lsp;
4354 atomic_inc(&lsp->ls_count);
4355 /* Ensure we don't close file until we're done freeing locks! */
4356 p->ctx = get_nfs_open_context(ctx);
4357 memcpy(&p->fl, fl, sizeof(p->fl));
4358 p->server = NFS_SERVER(inode);
4359 return p;
4360}
4361
Trond Myklebust06f814a2006-01-03 09:55:07 +01004362static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004363{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004364 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004365 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004366 nfs4_put_lock_state(calldata->lsp);
4367 put_nfs_open_context(calldata->ctx);
4368 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004369}
4370
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004371static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004372{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004373 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004374
Trond Myklebust14516c32010-07-31 14:29:06 -04004375 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4376 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004377 switch (task->tk_status) {
4378 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004379 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4380 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004381 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004382 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004383 case -NFS4ERR_BAD_STATEID:
4384 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004385 case -NFS4ERR_STALE_STATEID:
4386 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004387 break;
4388 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004389 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004390 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004392}
4393
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004394static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004395{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004396 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004398 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004399 return;
4400 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004401 /* Note: exit _without_ running nfs4_locku_done */
4402 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403 return;
4404 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004405 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004406 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004407 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004408 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004409 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004410 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004413static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004414 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004415 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004416 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004417};
4418
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004419static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4420 struct nfs_open_context *ctx,
4421 struct nfs4_lock_state *lsp,
4422 struct nfs_seqid *seqid)
4423{
4424 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004425 struct rpc_message msg = {
4426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4427 .rpc_cred = ctx->cred,
4428 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004429 struct rpc_task_setup task_setup_data = {
4430 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004431 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004432 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004433 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004434 .flags = RPC_TASK_ASYNC,
4435 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004436
Frank Filz137d6ac2007-07-09 15:32:29 -07004437 /* Ensure this is an unlock - when canceling a lock, the
4438 * canceled lock is passed in, and it won't be an unlock.
4439 */
4440 fl->fl_type = F_UNLCK;
4441
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4443 if (data == NULL) {
4444 nfs_free_seqid(seqid);
4445 return ERR_PTR(-ENOMEM);
4446 }
4447
Trond Myklebust9d12b212012-01-17 22:04:25 -05004448 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004449 msg.rpc_argp = &data->arg;
4450 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004451 task_setup_data.callback_data = data;
4452 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004453}
4454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4456{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004457 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004458 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004459 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004460 struct rpc_task *task;
4461 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004462 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Trond Myklebust9b073572006-06-29 16:38:34 -04004464 status = nfs4_set_lock_state(state, request);
4465 /* Unlock _before_ we do the RPC call */
4466 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004467 down_read(&nfsi->rwsem);
4468 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4469 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004470 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004471 }
4472 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004473 if (status != 0)
4474 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004475 /* Is this a delegated lock? */
4476 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004477 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004478 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004479 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004480 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004481 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004482 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004483 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004484 status = PTR_ERR(task);
4485 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004486 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004487 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004488 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004489out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004490 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004491 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492}
4493
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004494struct nfs4_lockdata {
4495 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004496 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004497 struct nfs4_lock_state *lsp;
4498 struct nfs_open_context *ctx;
4499 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004500 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004501 int rpc_status;
4502 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004503 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004504};
4505
4506static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004507 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4508 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509{
4510 struct nfs4_lockdata *p;
4511 struct inode *inode = lsp->ls_state->inode;
4512 struct nfs_server *server = NFS_SERVER(inode);
4513
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004514 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515 if (p == NULL)
4516 return NULL;
4517
4518 p->arg.fh = NFS_FH(inode);
4519 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004520 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004521 if (p->arg.open_seqid == NULL)
4522 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004523 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004525 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004526 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004527 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004528 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004529 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004530 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004531 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004532 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533 atomic_inc(&lsp->ls_count);
4534 p->ctx = get_nfs_open_context(ctx);
4535 memcpy(&p->fl, fl, sizeof(p->fl));
4536 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004537out_free_seqid:
4538 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004539out_free:
4540 kfree(p);
4541 return NULL;
4542}
4543
4544static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4545{
4546 struct nfs4_lockdata *data = calldata;
4547 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Harvey Harrison3110ff82008-05-02 13:42:44 -07004549 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004550 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4551 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004552 /* Do we need to do an open_to_lock_owner? */
4553 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004554 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4555 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004556 data->arg.open_stateid = &state->stateid;
4557 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004558 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004559 } else
4560 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004561 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004562 if (nfs4_setup_sequence(data->server,
4563 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004564 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004565 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004566 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004567 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568}
4569
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004570static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4571{
4572 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4573 nfs4_lock_prepare(task, calldata);
4574}
4575
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004576static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4577{
4578 struct nfs4_lockdata *data = calldata;
4579
Harvey Harrison3110ff82008-05-02 13:42:44 -07004580 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004581
Trond Myklebust14516c32010-07-31 14:29:06 -04004582 if (!nfs4_sequence_done(task, &data->res.seq_res))
4583 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004584
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004586 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004587 if (data->rpc_status == 0)
4588 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4589 else
4590 goto out;
4591 }
4592 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004593 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004595 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004596 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004598 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004599}
4600
4601static void nfs4_lock_release(void *calldata)
4602{
4603 struct nfs4_lockdata *data = calldata;
4604
Harvey Harrison3110ff82008-05-02 13:42:44 -07004605 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004606 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004607 if (data->cancelled != 0) {
4608 struct rpc_task *task;
4609 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4610 data->arg.lock_seqid);
4611 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004612 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004613 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614 } else
4615 nfs_free_seqid(data->arg.lock_seqid);
4616 nfs4_put_lock_state(data->lsp);
4617 put_nfs_open_context(data->ctx);
4618 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004619 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004620}
4621
4622static const struct rpc_call_ops nfs4_lock_ops = {
4623 .rpc_call_prepare = nfs4_lock_prepare,
4624 .rpc_call_done = nfs4_lock_done,
4625 .rpc_release = nfs4_lock_release,
4626};
4627
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004628static const struct rpc_call_ops nfs4_recover_lock_ops = {
4629 .rpc_call_prepare = nfs4_recover_lock_prepare,
4630 .rpc_call_done = nfs4_lock_done,
4631 .rpc_release = nfs4_lock_release,
4632};
4633
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004634static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4635{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004636 switch (error) {
4637 case -NFS4ERR_ADMIN_REVOKED:
4638 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004639 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004640 if (new_lock_owner != 0 ||
4641 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004642 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004643 break;
4644 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004645 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004646 case -NFS4ERR_EXPIRED:
4647 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004648 };
4649}
4650
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004651static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652{
4653 struct nfs4_lockdata *data;
4654 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004655 struct rpc_message msg = {
4656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4657 .rpc_cred = state->owner->so_cred,
4658 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004659 struct rpc_task_setup task_setup_data = {
4660 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004661 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004662 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004663 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004664 .flags = RPC_TASK_ASYNC,
4665 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666 int ret;
4667
Harvey Harrison3110ff82008-05-02 13:42:44 -07004668 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004669 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004670 fl->fl_u.nfs4_fl.owner,
4671 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672 if (data == NULL)
4673 return -ENOMEM;
4674 if (IS_SETLKW(cmd))
4675 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004676 if (recovery_type > NFS_LOCK_NEW) {
4677 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004678 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004679 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4680 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004681 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004682 msg.rpc_argp = &data->arg;
4683 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004684 task_setup_data.callback_data = data;
4685 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004686 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004687 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688 ret = nfs4_wait_for_completion_rpc_task(task);
4689 if (ret == 0) {
4690 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004691 if (ret)
4692 nfs4_handle_setlk_error(data->server, data->lsp,
4693 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004694 } else
4695 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004696 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004697 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004698 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699}
4700
4701static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4702{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004703 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004704 struct nfs4_exception exception = {
4705 .inode = state->inode,
4706 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004707 int err;
4708
4709 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004710 /* Cache the lock if possible... */
4711 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4712 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004713 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004714 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004715 break;
4716 nfs4_handle_exception(server, err, &exception);
4717 } while (exception.retry);
4718 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
4721static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4722{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004723 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004724 struct nfs4_exception exception = {
4725 .inode = state->inode,
4726 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004727 int err;
4728
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004729 err = nfs4_set_lock_state(state, request);
4730 if (err != 0)
4731 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004732 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004733 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4734 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004735 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004736 switch (err) {
4737 default:
4738 goto out;
4739 case -NFS4ERR_GRACE:
4740 case -NFS4ERR_DELAY:
4741 nfs4_handle_exception(server, err, &exception);
4742 err = 0;
4743 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004744 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004745out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004746 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747}
4748
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004749#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004750/**
4751 * nfs41_check_expired_locks - possibly free a lock stateid
4752 *
4753 * @state: NFSv4 state for an inode
4754 *
4755 * Returns NFS_OK if recovery for this stateid is now finished.
4756 * Otherwise a negative NFS4ERR value is returned.
4757 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004758static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004759{
Chuck Levereb64cf92012-07-11 16:30:05 -04004760 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004761 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004762 struct nfs_server *server = NFS_SERVER(state->inode);
4763
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004764 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4765 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4766 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4767 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004768 /* Free the stateid unless the server
4769 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004770 if (status != -NFS4ERR_BAD_STATEID)
4771 nfs41_free_stateid(server,
4772 &lsp->ls_stateid);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004773 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4774 ret = status;
4775 }
4776 }
4777 };
4778
4779 return ret;
4780}
4781
4782static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4783{
4784 int status = NFS_OK;
4785
4786 if (test_bit(LK_STATE_IN_USE, &state->flags))
4787 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004788 if (status != NFS_OK)
4789 status = nfs4_lock_expired(state, request);
4790 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004791}
4792#endif
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4795{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004796 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004797 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004798 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004800 if ((fl_flags & FL_POSIX) &&
4801 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4802 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004803 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004804 status = nfs4_set_lock_state(state, request);
4805 if (status != 0)
4806 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004807 request->fl_flags |= FL_ACCESS;
4808 status = do_vfs_lock(request->fl_file, request);
4809 if (status < 0)
4810 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004811 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004812 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004813 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004814 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004815 request->fl_flags = fl_flags & ~FL_SLEEP;
4816 status = do_vfs_lock(request->fl_file, request);
4817 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004818 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004819 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004820 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004821 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004822 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004823 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004824 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004825 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4826 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004827out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004828 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004829out:
4830 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 return status;
4832}
4833
4834static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4835{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004836 struct nfs4_exception exception = {
4837 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004838 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004839 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 int err;
4841
4842 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004843 err = _nfs4_proc_setlk(state, cmd, request);
4844 if (err == -NFS4ERR_DENIED)
4845 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004847 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 } while (exception.retry);
4849 return err;
4850}
4851
4852static int
4853nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4854{
4855 struct nfs_open_context *ctx;
4856 struct nfs4_state *state;
4857 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4858 int status;
4859
4860 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004861 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 state = ctx->state;
4863
4864 if (request->fl_start < 0 || request->fl_end < 0)
4865 return -EINVAL;
4866
Trond Myklebustd9531262009-07-21 19:22:38 -04004867 if (IS_GETLK(cmd)) {
4868 if (state != NULL)
4869 return nfs4_proc_getlk(state, F_GETLK, request);
4870 return 0;
4871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4874 return -EINVAL;
4875
Trond Myklebustd9531262009-07-21 19:22:38 -04004876 if (request->fl_type == F_UNLCK) {
4877 if (state != NULL)
4878 return nfs4_proc_unlck(state, cmd, request);
4879 return 0;
4880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Trond Myklebustd9531262009-07-21 19:22:38 -04004882 if (state == NULL)
4883 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004884 /*
4885 * Don't rely on the VFS having checked the file open mode,
4886 * since it won't do this for flock() locks.
4887 */
4888 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4889 case F_RDLCK:
4890 if (!(filp->f_mode & FMODE_READ))
4891 return -EBADF;
4892 break;
4893 case F_WRLCK:
4894 if (!(filp->f_mode & FMODE_WRITE))
4895 return -EBADF;
4896 }
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 do {
4899 status = nfs4_proc_setlk(state, cmd, request);
4900 if ((status != -EAGAIN) || IS_SETLK(cmd))
4901 break;
4902 timeout = nfs4_set_lock_task_retry(timeout);
4903 status = -ERESTARTSYS;
4904 if (signalled())
4905 break;
4906 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return status;
4908}
4909
Trond Myklebust888e6942005-11-04 15:38:11 -05004910int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4911{
4912 struct nfs_server *server = NFS_SERVER(state->inode);
4913 struct nfs4_exception exception = { };
4914 int err;
4915
4916 err = nfs4_set_lock_state(state, fl);
4917 if (err != 0)
4918 goto out;
4919 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004920 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004921 switch (err) {
4922 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004923 printk(KERN_ERR "NFS: %s: unhandled error "
4924 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004925 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004926 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004927 goto out;
4928 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004929 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004930 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004931 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004932 nfs4_schedule_lease_recovery(server->nfs_client);
4933 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004934 case -NFS4ERR_BADSESSION:
4935 case -NFS4ERR_BADSLOT:
4936 case -NFS4ERR_BAD_HIGH_SLOT:
4937 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4938 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004939 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004940 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004941 case -ERESTARTSYS:
4942 /*
4943 * The show must go on: exit, but mark the
4944 * stateid as needing recovery.
4945 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004946 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004947 case -NFS4ERR_ADMIN_REVOKED:
4948 case -NFS4ERR_BAD_STATEID:
4949 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004950 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004951 err = 0;
4952 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004953 case -EKEYEXPIRED:
4954 /*
4955 * User RPCSEC_GSS context has expired.
4956 * We cannot recover this stateid now, so
4957 * skip it and allow recovery thread to
4958 * proceed.
4959 */
4960 err = 0;
4961 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004962 case -ENOMEM:
4963 case -NFS4ERR_DENIED:
4964 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4965 err = 0;
4966 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004967 case -NFS4ERR_DELAY:
4968 break;
4969 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004970 err = nfs4_handle_exception(server, err, &exception);
4971 } while (exception.retry);
4972out:
4973 return err;
4974}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004975
Trond Myklebustcf470c32012-03-07 13:49:12 -05004976struct nfs_release_lockowner_data {
4977 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004978 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004979 struct nfs_release_lockowner_args args;
4980};
4981
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004982static void nfs4_release_lockowner_release(void *calldata)
4983{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004984 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004985 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004986 kfree(calldata);
4987}
4988
Trond Myklebust17280172012-03-11 13:11:00 -04004989static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004990 .rpc_release = nfs4_release_lockowner_release,
4991};
4992
Trond Myklebustcf470c32012-03-07 13:49:12 -05004993int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004994{
4995 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004996 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004997 struct rpc_message msg = {
4998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4999 };
5000
5001 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005002 return -EINVAL;
5003 data = kmalloc(sizeof(*data), GFP_NOFS);
5004 if (!data)
5005 return -ENOMEM;
5006 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005007 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005008 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5009 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5010 data->args.lock_owner.s_dev = server->s_dev;
5011 msg.rpc_argp = &data->args;
5012 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5013 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005014}
5015
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005016#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5017
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005018static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5019 const void *buf, size_t buflen,
5020 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005021{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005022 if (strcmp(key, "") != 0)
5023 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005024
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005025 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005026}
5027
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005028static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5029 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005030{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005031 if (strcmp(key, "") != 0)
5032 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005033
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005034 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005035}
5036
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005037static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5038 size_t list_len, const char *name,
5039 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005040{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005041 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005042
J. Bruce Fields096455a2006-03-20 23:23:42 -05005043 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5044 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005045
5046 if (list && len <= list_len)
5047 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005048 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005049}
5050
Andy Adamson533eb462011-06-13 18:25:56 -04005051/*
5052 * nfs_fhget will use either the mounted_on_fileid or the fileid
5053 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005054static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5055{
Andy Adamson533eb462011-06-13 18:25:56 -04005056 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5057 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5058 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005059 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005060 return;
5061
5062 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005063 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005064 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5065 fattr->nlink = 2;
5066}
5067
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005068static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5069 const struct qstr *name,
5070 struct nfs4_fs_locations *fs_locations,
5071 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005072{
5073 struct nfs_server *server = NFS_SERVER(dir);
5074 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005075 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005076 };
5077 struct nfs4_fs_locations_arg args = {
5078 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005079 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005080 .page = page,
5081 .bitmask = bitmask,
5082 };
Benny Halevy22958462009-04-01 09:22:02 -04005083 struct nfs4_fs_locations_res res = {
5084 .fs_locations = fs_locations,
5085 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005086 struct rpc_message msg = {
5087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5088 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005089 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005090 };
5091 int status;
5092
Harvey Harrison3110ff82008-05-02 13:42:44 -07005093 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005094
5095 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5096 * is not supported */
5097 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5098 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5099 else
5100 bitmask[0] |= FATTR4_WORD0_FILEID;
5101
Trond Myklebustc228fd32007-01-13 02:28:11 -05005102 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005103 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005104 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005105 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005106 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005107 return status;
5108}
5109
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005110int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5111 const struct qstr *name,
5112 struct nfs4_fs_locations *fs_locations,
5113 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005114{
5115 struct nfs4_exception exception = { };
5116 int err;
5117 do {
5118 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005119 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005120 &exception);
5121 } while (exception.retry);
5122 return err;
5123}
5124
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005125static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5126{
5127 int status;
5128 struct nfs4_secinfo_arg args = {
5129 .dir_fh = NFS_FH(dir),
5130 .name = name,
5131 };
5132 struct nfs4_secinfo_res res = {
5133 .flavors = flavors,
5134 };
5135 struct rpc_message msg = {
5136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5137 .rpc_argp = &args,
5138 .rpc_resp = &res,
5139 };
5140
5141 dprintk("NFS call secinfo %s\n", name->name);
5142 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5143 dprintk("NFS reply secinfo: %d\n", status);
5144 return status;
5145}
5146
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005147int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5148 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005149{
5150 struct nfs4_exception exception = { };
5151 int err;
5152 do {
5153 err = nfs4_handle_exception(NFS_SERVER(dir),
5154 _nfs4_proc_secinfo(dir, name, flavors),
5155 &exception);
5156 } while (exception.retry);
5157 return err;
5158}
5159
Andy Adamson557134a2009-04-01 09:21:53 -04005160#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005161/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005162 * Check the exchange flags returned by the server for invalid flags, having
5163 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5164 * DS flags set.
5165 */
5166static int nfs4_check_cl_exchange_flags(u32 flags)
5167{
5168 if (flags & ~EXCHGID4_FLAG_MASK_R)
5169 goto out_inval;
5170 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5171 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5172 goto out_inval;
5173 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5174 goto out_inval;
5175 return NFS_OK;
5176out_inval:
5177 return -NFS4ERR_INVAL;
5178}
5179
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005180static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005181nfs41_same_server_scope(struct nfs41_server_scope *a,
5182 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005183{
5184 if (a->server_scope_sz == b->server_scope_sz &&
5185 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5186 return true;
5187
5188 return false;
5189}
5190
Andy Adamson357f54d2010-12-14 10:11:57 -05005191/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005192 * nfs4_proc_bind_conn_to_session()
5193 *
5194 * The 4.1 client currently uses the same TCP connection for the
5195 * fore and backchannel.
5196 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005197int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005198{
5199 int status;
5200 struct nfs41_bind_conn_to_session_res res;
5201 struct rpc_message msg = {
5202 .rpc_proc =
5203 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5204 .rpc_argp = clp,
5205 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005206 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005207 };
5208
5209 dprintk("--> %s\n", __func__);
5210 BUG_ON(clp == NULL);
5211
5212 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5213 if (unlikely(res.session == NULL)) {
5214 status = -ENOMEM;
5215 goto out;
5216 }
5217
5218 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5219 if (status == 0) {
5220 if (memcmp(res.session->sess_id.data,
5221 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5222 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5223 status = -EIO;
5224 goto out_session;
5225 }
5226 if (res.dir != NFS4_CDFS4_BOTH) {
5227 dprintk("NFS: %s: Unexpected direction from server\n",
5228 __func__);
5229 status = -EIO;
5230 goto out_session;
5231 }
5232 if (res.use_conn_in_rdma_mode) {
5233 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5234 __func__);
5235 status = -EIO;
5236 goto out_session;
5237 }
5238 }
5239out_session:
5240 kfree(res.session);
5241out:
5242 dprintk("<-- %s status= %d\n", __func__, status);
5243 return status;
5244}
5245
5246/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005247 * nfs4_proc_exchange_id()
5248 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005249 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5250 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005251 * Since the clientid has expired, all compounds using sessions
5252 * associated with the stale clientid will be returning
5253 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5254 * be in some phase of session reset.
5255 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005256int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005257{
5258 nfs4_verifier verifier;
5259 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005260 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005261 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005262 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005263 };
5264 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005265 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005266 };
5267 int status;
5268 struct rpc_message msg = {
5269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5270 .rpc_argp = &args,
5271 .rpc_resp = &res,
5272 .rpc_cred = cred,
5273 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005274
Chuck Leverf0920752012-05-21 22:45:41 -04005275 nfs4_init_boot_verifier(clp, &verifier);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005276 args.id_len = scnprintf(args.id, sizeof(args.id),
Chuck Leverde734832012-07-11 16:30:50 -04005277 "%s/%s",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005278 clp->cl_ipaddr,
Chuck Leverde734832012-07-11 16:30:50 -04005279 clp->cl_rpcclient->cl_nodename);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005280 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5281 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5282 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005283
Chuck Leveracdeb692012-05-21 22:46:16 -04005284 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005285 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005286 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005287 status = -ENOMEM;
5288 goto out;
5289 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005290
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005291 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005292 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005293 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005294 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005295 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005296 }
5297
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005298 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005299 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005300 status = -ENOMEM;
5301 goto out_server_scope;
5302 }
5303
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005304 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005305 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005306 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005307
Chuck Lever177313f2012-05-21 22:44:58 -04005308 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005309 clp->cl_clientid = res.clientid;
5310 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5311 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5312 clp->cl_seqid = res.seqid;
5313
Chuck Leveracdeb692012-05-21 22:46:16 -04005314 kfree(clp->cl_serverowner);
5315 clp->cl_serverowner = res.server_owner;
5316 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005317
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005318 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005319 kfree(clp->cl_implid);
5320 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005321
Chuck Lever177313f2012-05-21 22:44:58 -04005322 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005323 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005324 res.server_scope)) {
5325 dprintk("%s: server_scope mismatch detected\n",
5326 __func__);
5327 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005328 kfree(clp->cl_serverscope);
5329 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005330 }
5331
Chuck Lever177313f2012-05-21 22:44:58 -04005332 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005333 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005334 goto out;
5335 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005336 } else
5337 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005338
Chuck Leveracdeb692012-05-21 22:46:16 -04005339out_server_owner:
5340 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005341out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005342 kfree(res.server_scope);
5343out:
Chuck Lever177313f2012-05-21 22:44:58 -04005344 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005345 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005346 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005347 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005348 clp->cl_implid->date.seconds,
5349 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005350 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005351 return status;
5352}
5353
Trond Myklebust66245532012-05-25 17:18:09 -04005354static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5355 struct rpc_cred *cred)
5356{
5357 struct rpc_message msg = {
5358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5359 .rpc_argp = clp,
5360 .rpc_cred = cred,
5361 };
5362 int status;
5363
5364 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5365 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005366 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005367 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5368 return status;
5369}
5370
5371static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5372 struct rpc_cred *cred)
5373{
5374 unsigned int loop;
5375 int ret;
5376
5377 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5378 ret = _nfs4_proc_destroy_clientid(clp, cred);
5379 switch (ret) {
5380 case -NFS4ERR_DELAY:
5381 case -NFS4ERR_CLIENTID_BUSY:
5382 ssleep(1);
5383 break;
5384 default:
5385 return ret;
5386 }
5387 }
5388 return 0;
5389}
5390
5391int nfs4_destroy_clientid(struct nfs_client *clp)
5392{
5393 struct rpc_cred *cred;
5394 int ret = 0;
5395
5396 if (clp->cl_mvops->minor_version < 1)
5397 goto out;
5398 if (clp->cl_exchange_flags == 0)
5399 goto out;
5400 cred = nfs4_get_exchange_id_cred(clp);
5401 ret = nfs4_proc_destroy_clientid(clp, cred);
5402 if (cred)
5403 put_rpccred(cred);
5404 switch (ret) {
5405 case 0:
5406 case -NFS4ERR_STALE_CLIENTID:
5407 clp->cl_exchange_flags = 0;
5408 }
5409out:
5410 return ret;
5411}
5412
Andy Adamson2050f0c2009-04-01 09:22:30 -04005413struct nfs4_get_lease_time_data {
5414 struct nfs4_get_lease_time_args *args;
5415 struct nfs4_get_lease_time_res *res;
5416 struct nfs_client *clp;
5417};
5418
5419static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5420 void *calldata)
5421{
5422 int ret;
5423 struct nfs4_get_lease_time_data *data =
5424 (struct nfs4_get_lease_time_data *)calldata;
5425
5426 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005427 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005428 /* just setup sequence, do not trigger session recovery
5429 since we're invoked within one */
5430 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005431 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005432 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005433
5434 BUG_ON(ret == -EAGAIN);
5435 rpc_call_start(task);
5436 dprintk("<-- %s\n", __func__);
5437}
5438
5439/*
5440 * Called from nfs4_state_manager thread for session setup, so don't recover
5441 * from sequence operation or clientid errors.
5442 */
5443static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5444{
5445 struct nfs4_get_lease_time_data *data =
5446 (struct nfs4_get_lease_time_data *)calldata;
5447
5448 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005449 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5450 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005451 switch (task->tk_status) {
5452 case -NFS4ERR_DELAY:
5453 case -NFS4ERR_GRACE:
5454 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5455 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5456 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005457 /* fall through */
5458 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005459 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005460 return;
5461 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005462 dprintk("<-- %s\n", __func__);
5463}
5464
Trond Myklebust17280172012-03-11 13:11:00 -04005465static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005466 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5467 .rpc_call_done = nfs4_get_lease_time_done,
5468};
5469
5470int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5471{
5472 struct rpc_task *task;
5473 struct nfs4_get_lease_time_args args;
5474 struct nfs4_get_lease_time_res res = {
5475 .lr_fsinfo = fsinfo,
5476 };
5477 struct nfs4_get_lease_time_data data = {
5478 .args = &args,
5479 .res = &res,
5480 .clp = clp,
5481 };
5482 struct rpc_message msg = {
5483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5484 .rpc_argp = &args,
5485 .rpc_resp = &res,
5486 };
5487 struct rpc_task_setup task_setup = {
5488 .rpc_client = clp->cl_rpcclient,
5489 .rpc_message = &msg,
5490 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005491 .callback_data = &data,
5492 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005493 };
5494 int status;
5495
Trond Myklebust9d12b212012-01-17 22:04:25 -05005496 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005497 dprintk("--> %s\n", __func__);
5498 task = rpc_run_task(&task_setup);
5499
5500 if (IS_ERR(task))
5501 status = PTR_ERR(task);
5502 else {
5503 status = task->tk_status;
5504 rpc_put_task(task);
5505 }
5506 dprintk("<-- %s return %d\n", __func__, status);
5507
5508 return status;
5509}
5510
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005511static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5512{
5513 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5514}
5515
5516static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5517 struct nfs4_slot *new,
5518 u32 max_slots,
5519 u32 ivalue)
5520{
5521 struct nfs4_slot *old = NULL;
5522 u32 i;
5523
5524 spin_lock(&tbl->slot_tbl_lock);
5525 if (new) {
5526 old = tbl->slots;
5527 tbl->slots = new;
5528 tbl->max_slots = max_slots;
5529 }
5530 tbl->highest_used_slotid = -1; /* no slot is currently used */
5531 for (i = 0; i < tbl->max_slots; i++)
5532 tbl->slots[i].seq_nr = ivalue;
5533 spin_unlock(&tbl->slot_tbl_lock);
5534 kfree(old);
5535}
5536
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005537/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005538 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005539 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005540static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5541 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005542{
Andy Adamson104aeba2010-01-14 17:45:10 -05005543 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005544 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005545
Andy Adamson104aeba2010-01-14 17:45:10 -05005546 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5547 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005548
Andy Adamson104aeba2010-01-14 17:45:10 -05005549 /* Does the newly negotiated max_reqs match the existing slot table? */
5550 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005551 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005552 if (!new)
5553 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005554 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005555 ret = 0;
5556
5557 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005558 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5559 tbl, tbl->slots, tbl->max_slots);
5560out:
5561 dprintk("<-- %s: return %d\n", __func__, ret);
5562 return ret;
5563}
5564
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005565/* Destroy the slot table */
5566static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5567{
5568 if (session->fc_slot_table.slots != NULL) {
5569 kfree(session->fc_slot_table.slots);
5570 session->fc_slot_table.slots = NULL;
5571 }
5572 if (session->bc_slot_table.slots != NULL) {
5573 kfree(session->bc_slot_table.slots);
5574 session->bc_slot_table.slots = NULL;
5575 }
5576 return;
5577}
5578
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005579/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005580 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005581 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005582static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005583{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005584 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005585 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005586
Andy Adamsonaacd5532011-11-09 13:58:21 -05005587 dprintk("--> %s\n", __func__);
5588 /* Fore channel */
5589 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005590 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5591 if (status) /* -ENOMEM */
5592 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005593 /* Back channel */
5594 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005595 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5596 if (status && tbl->slots == NULL)
5597 /* Fore and back channel share a connection so get
5598 * both slot tables or neither */
5599 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005600 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005601}
5602
5603struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5604{
5605 struct nfs4_session *session;
5606 struct nfs4_slot_table *tbl;
5607
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005608 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005609 if (!session)
5610 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005611
Andy Adamson557134a2009-04-01 09:21:53 -04005612 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005613 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005614 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005615 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005616 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005617
5618 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005619 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005620 spin_lock_init(&tbl->slot_tbl_lock);
5621 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005622 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005623
Trond Myklebust1055d762010-06-16 09:52:27 -04005624 session->session_state = 1<<NFS4_SESSION_INITING;
5625
Andy Adamson557134a2009-04-01 09:21:53 -04005626 session->clp = clp;
5627 return session;
5628}
5629
5630void nfs4_destroy_session(struct nfs4_session *session)
5631{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005632 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005633 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005634
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005635 cred = nfs4_get_exchange_id_cred(session->clp);
5636 nfs4_proc_destroy_session(session, cred);
5637 if (cred)
5638 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005639
5640 rcu_read_lock();
5641 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5642 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005643 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005644 __func__, xprt);
5645 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005646 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005647 kfree(session);
5648}
5649
Andy Adamsonfc931582009-04-01 09:22:31 -04005650/*
5651 * Initialize the values to be used by the client in CREATE_SESSION
5652 * If nfs4_init_session set the fore channel request and response sizes,
5653 * use them.
5654 *
5655 * Set the back channel max_resp_sz_cached to zero to force the client to
5656 * always set csa_cachethis to FALSE because the current implementation
5657 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5658 */
5659static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5660{
5661 struct nfs4_session *session = args->client->cl_session;
5662 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5663 mxresp_sz = session->fc_attrs.max_resp_sz;
5664
5665 if (mxrqst_sz == 0)
5666 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5667 if (mxresp_sz == 0)
5668 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5669 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005670 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5671 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005672 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005673 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005674
5675 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005676 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005677 __func__,
5678 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005679 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005680
5681 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005682 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5683 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5684 args->bc_attrs.max_resp_sz_cached = 0;
5685 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5686 args->bc_attrs.max_reqs = 1;
5687
5688 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5689 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5690 __func__,
5691 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5692 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5693 args->bc_attrs.max_reqs);
5694}
5695
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005696static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005697{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005698 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5699 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5700
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005701 if (rcvd->max_resp_sz > sent->max_resp_sz)
5702 return -EINVAL;
5703 /*
5704 * Our requested max_ops is the minimum we need; we're not
5705 * prepared to break up compounds into smaller pieces than that.
5706 * So, no point even trying to continue if the server won't
5707 * cooperate:
5708 */
5709 if (rcvd->max_ops < sent->max_ops)
5710 return -EINVAL;
5711 if (rcvd->max_reqs == 0)
5712 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005713 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5714 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005715 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005716}
5717
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005718static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5719{
5720 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5721 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005722
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005723 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5724 return -EINVAL;
5725 if (rcvd->max_resp_sz < sent->max_resp_sz)
5726 return -EINVAL;
5727 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5728 return -EINVAL;
5729 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005730 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005731 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005732 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005733 return -EINVAL;
5734 return 0;
5735}
Andy Adamson8d353012009-04-01 09:22:32 -04005736
Andy Adamson8d353012009-04-01 09:22:32 -04005737static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5738 struct nfs4_session *session)
5739{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005740 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005741
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005742 ret = nfs4_verify_fore_channel_attrs(args, session);
5743 if (ret)
5744 return ret;
5745 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005746}
5747
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005748static int _nfs4_proc_create_session(struct nfs_client *clp,
5749 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005750{
5751 struct nfs4_session *session = clp->cl_session;
5752 struct nfs41_create_session_args args = {
5753 .client = clp,
5754 .cb_program = NFS4_CALLBACK,
5755 };
5756 struct nfs41_create_session_res res = {
5757 .client = clp,
5758 };
5759 struct rpc_message msg = {
5760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5761 .rpc_argp = &args,
5762 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005763 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005764 };
5765 int status;
5766
5767 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005768 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005769
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005770 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005771
Andy Adamson8d353012009-04-01 09:22:32 -04005772 if (!status)
5773 /* Verify the session's negotiated channel_attrs values */
5774 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005775 if (!status) {
5776 /* Increment the clientid slot sequence id */
5777 clp->cl_seqid++;
5778 }
5779
5780 return status;
5781}
5782
5783/*
5784 * Issues a CREATE_SESSION operation to the server.
5785 * It is the responsibility of the caller to verify the session is
5786 * expired before calling this routine.
5787 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005788int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005789{
5790 int status;
5791 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005792 struct nfs4_session *session = clp->cl_session;
5793
5794 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5795
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005796 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005797 if (status)
5798 goto out;
5799
Andy Adamsonaacd5532011-11-09 13:58:21 -05005800 /* Init or reset the session slot tables */
5801 status = nfs4_setup_session_slot_tables(session);
5802 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005803 if (status)
5804 goto out;
5805
5806 ptr = (unsigned *)&session->sess_id.data[0];
5807 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5808 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005809out:
5810 dprintk("<-- %s\n", __func__);
5811 return status;
5812}
5813
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005814/*
5815 * Issue the over-the-wire RPC DESTROY_SESSION.
5816 * The caller must serialize access to this routine.
5817 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005818int nfs4_proc_destroy_session(struct nfs4_session *session,
5819 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005820{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005821 struct rpc_message msg = {
5822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5823 .rpc_argp = session,
5824 .rpc_cred = cred,
5825 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005826 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005827
5828 dprintk("--> nfs4_proc_destroy_session\n");
5829
5830 /* session is still being setup */
5831 if (session->clp->cl_cons_state != NFS_CS_READY)
5832 return status;
5833
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005834 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005835
5836 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005837 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005838 "Session has been destroyed regardless...\n", status);
5839
5840 dprintk("<-- nfs4_proc_destroy_session\n");
5841 return status;
5842}
5843
Trond Myklebust7b38c362012-05-23 13:23:31 -04005844/*
5845 * With sessions, the client is not marked ready until after a
5846 * successful EXCHANGE_ID and CREATE_SESSION.
5847 *
5848 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5849 * other versions of NFS can be tried.
5850 */
5851static int nfs41_check_session_ready(struct nfs_client *clp)
5852{
5853 int ret;
5854
5855 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5856 ret = nfs4_client_recover_expired_lease(clp);
5857 if (ret)
5858 return ret;
5859 }
5860 if (clp->cl_cons_state < NFS_CS_READY)
5861 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005862 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005863 return 0;
5864}
5865
Trond Myklebustfccba802009-07-21 16:48:07 -04005866int nfs4_init_session(struct nfs_server *server)
5867{
5868 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005869 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005870 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005871
5872 if (!nfs4_has_session(clp))
5873 return 0;
5874
Trond Myklebust1055d762010-06-16 09:52:27 -04005875 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005876 spin_lock(&clp->cl_lock);
5877 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005878
Trond Myklebust7b38c362012-05-23 13:23:31 -04005879 rsize = server->rsize;
5880 if (rsize == 0)
5881 rsize = NFS_MAX_FILE_IO_SIZE;
5882 wsize = server->wsize;
5883 if (wsize == 0)
5884 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005885
Trond Myklebust7b38c362012-05-23 13:23:31 -04005886 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5887 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5888 }
5889 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005890
Trond Myklebust7b38c362012-05-23 13:23:31 -04005891 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005892}
5893
Trond Myklebust7b38c362012-05-23 13:23:31 -04005894int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005895{
5896 struct nfs4_session *session = clp->cl_session;
5897 int ret;
5898
Trond Myklebust7b38c362012-05-23 13:23:31 -04005899 spin_lock(&clp->cl_lock);
5900 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5901 /*
5902 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5903 * DS lease to be equal to the MDS lease.
5904 */
5905 clp->cl_lease_time = lease_time;
5906 clp->cl_last_renewal = jiffies;
5907 }
5908 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005909
Trond Myklebust7b38c362012-05-23 13:23:31 -04005910 ret = nfs41_check_session_ready(clp);
5911 if (ret)
5912 return ret;
5913 /* Test for the DS role */
5914 if (!is_ds_client(clp))
5915 return -ENODEV;
5916 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005917}
5918EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5919
5920
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005921/*
5922 * Renew the cl_session lease.
5923 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005924struct nfs4_sequence_data {
5925 struct nfs_client *clp;
5926 struct nfs4_sequence_args args;
5927 struct nfs4_sequence_res res;
5928};
5929
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005930static void nfs41_sequence_release(void *data)
5931{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005932 struct nfs4_sequence_data *calldata = data;
5933 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005934
Alexandros Batsakis71358402010-02-05 03:45:05 -08005935 if (atomic_read(&clp->cl_count) > 1)
5936 nfs4_schedule_state_renewal(clp);
5937 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005938 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005939}
5940
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005941static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5942{
5943 switch(task->tk_status) {
5944 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005945 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5946 return -EAGAIN;
5947 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005948 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005949 }
5950 return 0;
5951}
5952
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005953static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005954{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005955 struct nfs4_sequence_data *calldata = data;
5956 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005957
Trond Myklebust14516c32010-07-31 14:29:06 -04005958 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5959 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005960
5961 if (task->tk_status < 0) {
5962 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005963 if (atomic_read(&clp->cl_count) == 1)
5964 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005965
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005966 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5967 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005968 return;
5969 }
5970 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005971 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005972out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005973 dprintk("<-- %s\n", __func__);
5974}
5975
5976static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5977{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005978 struct nfs4_sequence_data *calldata = data;
5979 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005980 struct nfs4_sequence_args *args;
5981 struct nfs4_sequence_res *res;
5982
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005983 args = task->tk_msg.rpc_argp;
5984 res = task->tk_msg.rpc_resp;
5985
Trond Myklebust9d12b212012-01-17 22:04:25 -05005986 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005987 return;
5988 rpc_call_start(task);
5989}
5990
5991static const struct rpc_call_ops nfs41_sequence_ops = {
5992 .rpc_call_done = nfs41_sequence_call_done,
5993 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005994 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005995};
5996
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005997static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005998{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005999 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006000 struct rpc_message msg = {
6001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6002 .rpc_cred = cred,
6003 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006004 struct rpc_task_setup task_setup_data = {
6005 .rpc_client = clp->cl_rpcclient,
6006 .rpc_message = &msg,
6007 .callback_ops = &nfs41_sequence_ops,
6008 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6009 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006010
Alexandros Batsakis71358402010-02-05 03:45:05 -08006011 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006012 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006013 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006014 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006015 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006016 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006017 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006018 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006019 msg.rpc_argp = &calldata->args;
6020 msg.rpc_resp = &calldata->res;
6021 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006022 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006023
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006024 return rpc_run_task(&task_setup_data);
6025}
6026
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006027static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006028{
6029 struct rpc_task *task;
6030 int ret = 0;
6031
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006032 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6033 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006034 task = _nfs41_proc_sequence(clp, cred);
6035 if (IS_ERR(task))
6036 ret = PTR_ERR(task);
6037 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006038 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006039 dprintk("<-- %s status=%d\n", __func__, ret);
6040 return ret;
6041}
6042
6043static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6044{
6045 struct rpc_task *task;
6046 int ret;
6047
6048 task = _nfs41_proc_sequence(clp, cred);
6049 if (IS_ERR(task)) {
6050 ret = PTR_ERR(task);
6051 goto out;
6052 }
6053 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006054 if (!ret) {
6055 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6056
6057 if (task->tk_status == 0)
6058 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006059 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006060 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006061 rpc_put_task(task);
6062out:
6063 dprintk("<-- %s status=%d\n", __func__, ret);
6064 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006065}
6066
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006067struct nfs4_reclaim_complete_data {
6068 struct nfs_client *clp;
6069 struct nfs41_reclaim_complete_args arg;
6070 struct nfs41_reclaim_complete_res res;
6071};
6072
6073static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6074{
6075 struct nfs4_reclaim_complete_data *calldata = data;
6076
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006077 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04006078 if (nfs41_setup_sequence(calldata->clp->cl_session,
6079 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006080 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006081 return;
6082
6083 rpc_call_start(task);
6084}
6085
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006086static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6087{
6088 switch(task->tk_status) {
6089 case 0:
6090 case -NFS4ERR_COMPLETE_ALREADY:
6091 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6092 break;
6093 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006094 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006095 /* fall through */
6096 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006097 return -EAGAIN;
6098 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006099 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006100 }
6101 return 0;
6102}
6103
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006104static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6105{
6106 struct nfs4_reclaim_complete_data *calldata = data;
6107 struct nfs_client *clp = calldata->clp;
6108 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6109
6110 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006111 if (!nfs41_sequence_done(task, res))
6112 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006113
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006114 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6115 rpc_restart_call_prepare(task);
6116 return;
6117 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006118 dprintk("<-- %s\n", __func__);
6119}
6120
6121static void nfs4_free_reclaim_complete_data(void *data)
6122{
6123 struct nfs4_reclaim_complete_data *calldata = data;
6124
6125 kfree(calldata);
6126}
6127
6128static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6129 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6130 .rpc_call_done = nfs4_reclaim_complete_done,
6131 .rpc_release = nfs4_free_reclaim_complete_data,
6132};
6133
6134/*
6135 * Issue a global reclaim complete.
6136 */
6137static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6138{
6139 struct nfs4_reclaim_complete_data *calldata;
6140 struct rpc_task *task;
6141 struct rpc_message msg = {
6142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6143 };
6144 struct rpc_task_setup task_setup_data = {
6145 .rpc_client = clp->cl_rpcclient,
6146 .rpc_message = &msg,
6147 .callback_ops = &nfs4_reclaim_complete_call_ops,
6148 .flags = RPC_TASK_ASYNC,
6149 };
6150 int status = -ENOMEM;
6151
6152 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006153 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006154 if (calldata == NULL)
6155 goto out;
6156 calldata->clp = clp;
6157 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006158
Trond Myklebust9d12b212012-01-17 22:04:25 -05006159 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006160 msg.rpc_argp = &calldata->arg;
6161 msg.rpc_resp = &calldata->res;
6162 task_setup_data.callback_data = calldata;
6163 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006164 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006165 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006166 goto out;
6167 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006168 status = nfs4_wait_for_completion_rpc_task(task);
6169 if (status == 0)
6170 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006171 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006172 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006173out:
6174 dprintk("<-- %s status=%d\n", __func__, status);
6175 return status;
6176}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006177
6178static void
6179nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6180{
6181 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006182 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006183
6184 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006185 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6186 * right now covering the LAYOUTGET we are about to send.
6187 * However, that is not so catastrophic, and there seems
6188 * to be no way to prevent it completely.
6189 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006190 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006191 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006192 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006193 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6194 NFS_I(lgp->args.inode)->layout,
6195 lgp->args.ctx->state)) {
6196 rpc_exit(task, NFS4_OK);
6197 return;
6198 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006199 rpc_call_start(task);
6200}
6201
6202static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6203{
6204 struct nfs4_layoutget *lgp = calldata;
6205 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6206
6207 dprintk("--> %s\n", __func__);
6208
6209 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6210 return;
6211
6212 switch (task->tk_status) {
6213 case 0:
6214 break;
6215 case -NFS4ERR_LAYOUTTRYLATER:
6216 case -NFS4ERR_RECALLCONFLICT:
6217 task->tk_status = -NFS4ERR_DELAY;
6218 /* Fall through */
6219 default:
6220 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6221 rpc_restart_call_prepare(task);
6222 return;
6223 }
6224 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006225 dprintk("<-- %s\n", __func__);
6226}
6227
6228static void nfs4_layoutget_release(void *calldata)
6229{
6230 struct nfs4_layoutget *lgp = calldata;
6231
6232 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006233 put_nfs_open_context(lgp->args.ctx);
6234 kfree(calldata);
6235 dprintk("<-- %s\n", __func__);
6236}
6237
6238static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6239 .rpc_call_prepare = nfs4_layoutget_prepare,
6240 .rpc_call_done = nfs4_layoutget_done,
6241 .rpc_release = nfs4_layoutget_release,
6242};
6243
6244int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6245{
6246 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6247 struct rpc_task *task;
6248 struct rpc_message msg = {
6249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6250 .rpc_argp = &lgp->args,
6251 .rpc_resp = &lgp->res,
6252 };
6253 struct rpc_task_setup task_setup_data = {
6254 .rpc_client = server->client,
6255 .rpc_message = &msg,
6256 .callback_ops = &nfs4_layoutget_call_ops,
6257 .callback_data = lgp,
6258 .flags = RPC_TASK_ASYNC,
6259 };
6260 int status = 0;
6261
6262 dprintk("--> %s\n", __func__);
6263
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006264 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006265 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006266 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006267 task = rpc_run_task(&task_setup_data);
6268 if (IS_ERR(task))
6269 return PTR_ERR(task);
6270 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006271 if (status == 0)
6272 status = task->tk_status;
6273 if (status == 0)
6274 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006275 rpc_put_task(task);
6276 dprintk("<-- %s status=%d\n", __func__, status);
6277 return status;
6278}
6279
Benny Halevycbe82602011-05-22 19:52:37 +03006280static void
6281nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6282{
6283 struct nfs4_layoutreturn *lrp = calldata;
6284
6285 dprintk("--> %s\n", __func__);
6286 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006287 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006288 return;
6289 rpc_call_start(task);
6290}
6291
6292static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6293{
6294 struct nfs4_layoutreturn *lrp = calldata;
6295 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006296 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006297
6298 dprintk("--> %s\n", __func__);
6299
6300 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6301 return;
6302
6303 server = NFS_SERVER(lrp->args.inode);
6304 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006305 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006306 return;
6307 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006308 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006309 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006310 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006311 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006312 } else
6313 BUG_ON(!list_empty(&lo->plh_segs));
6314 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006315 lo->plh_block_lgets--;
6316 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006317 dprintk("<-- %s\n", __func__);
6318}
6319
6320static void nfs4_layoutreturn_release(void *calldata)
6321{
6322 struct nfs4_layoutreturn *lrp = calldata;
6323
6324 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006325 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006326 kfree(calldata);
6327 dprintk("<-- %s\n", __func__);
6328}
6329
6330static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6331 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6332 .rpc_call_done = nfs4_layoutreturn_done,
6333 .rpc_release = nfs4_layoutreturn_release,
6334};
6335
6336int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6337{
6338 struct rpc_task *task;
6339 struct rpc_message msg = {
6340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6341 .rpc_argp = &lrp->args,
6342 .rpc_resp = &lrp->res,
6343 };
6344 struct rpc_task_setup task_setup_data = {
6345 .rpc_client = lrp->clp->cl_rpcclient,
6346 .rpc_message = &msg,
6347 .callback_ops = &nfs4_layoutreturn_call_ops,
6348 .callback_data = lrp,
6349 };
6350 int status;
6351
6352 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006353 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006354 task = rpc_run_task(&task_setup_data);
6355 if (IS_ERR(task))
6356 return PTR_ERR(task);
6357 status = task->tk_status;
6358 dprintk("<-- %s status=%d\n", __func__, status);
6359 rpc_put_task(task);
6360 return status;
6361}
6362
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006363/*
6364 * Retrieve the list of Data Server devices from the MDS.
6365 */
6366static int _nfs4_getdevicelist(struct nfs_server *server,
6367 const struct nfs_fh *fh,
6368 struct pnfs_devicelist *devlist)
6369{
6370 struct nfs4_getdevicelist_args args = {
6371 .fh = fh,
6372 .layoutclass = server->pnfs_curr_ld->id,
6373 };
6374 struct nfs4_getdevicelist_res res = {
6375 .devlist = devlist,
6376 };
6377 struct rpc_message msg = {
6378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6379 .rpc_argp = &args,
6380 .rpc_resp = &res,
6381 };
6382 int status;
6383
6384 dprintk("--> %s\n", __func__);
6385 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6386 &res.seq_res, 0);
6387 dprintk("<-- %s status=%d\n", __func__, status);
6388 return status;
6389}
6390
6391int nfs4_proc_getdevicelist(struct nfs_server *server,
6392 const struct nfs_fh *fh,
6393 struct pnfs_devicelist *devlist)
6394{
6395 struct nfs4_exception exception = { };
6396 int err;
6397
6398 do {
6399 err = nfs4_handle_exception(server,
6400 _nfs4_getdevicelist(server, fh, devlist),
6401 &exception);
6402 } while (exception.retry);
6403
6404 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6405 err, devlist->num_devs);
6406
6407 return err;
6408}
6409EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6410
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006411static int
6412_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6413{
6414 struct nfs4_getdeviceinfo_args args = {
6415 .pdev = pdev,
6416 };
6417 struct nfs4_getdeviceinfo_res res = {
6418 .pdev = pdev,
6419 };
6420 struct rpc_message msg = {
6421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6422 .rpc_argp = &args,
6423 .rpc_resp = &res,
6424 };
6425 int status;
6426
6427 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006428 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006429 dprintk("<-- %s status=%d\n", __func__, status);
6430
6431 return status;
6432}
6433
6434int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6435{
6436 struct nfs4_exception exception = { };
6437 int err;
6438
6439 do {
6440 err = nfs4_handle_exception(server,
6441 _nfs4_proc_getdeviceinfo(server, pdev),
6442 &exception);
6443 } while (exception.retry);
6444 return err;
6445}
6446EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6447
Andy Adamson863a3c62011-03-23 13:27:54 +00006448static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6449{
6450 struct nfs4_layoutcommit_data *data = calldata;
6451 struct nfs_server *server = NFS_SERVER(data->args.inode);
6452
6453 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006454 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006455 return;
6456 rpc_call_start(task);
6457}
6458
6459static void
6460nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6461{
6462 struct nfs4_layoutcommit_data *data = calldata;
6463 struct nfs_server *server = NFS_SERVER(data->args.inode);
6464
6465 if (!nfs4_sequence_done(task, &data->res.seq_res))
6466 return;
6467
6468 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006469 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6470 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6471 case -NFS4ERR_BADLAYOUT: /* no layout */
6472 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006473 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006474 break;
6475 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006476 nfs_post_op_update_inode_force_wcc(data->args.inode,
6477 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006478 break;
6479 default:
6480 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6481 rpc_restart_call_prepare(task);
6482 return;
6483 }
6484 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006485}
6486
6487static void nfs4_layoutcommit_release(void *calldata)
6488{
6489 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006490 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006491 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006492
Andy Adamsondb29c082011-07-30 20:52:38 -04006493 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006494 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006495 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6496 list_del_init(&lseg->pls_lc_list);
6497 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6498 &lseg->pls_flags))
6499 put_lseg(lseg);
6500 }
Peng Tao92407e72011-10-23 20:21:17 -07006501
6502 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6503 smp_mb__after_clear_bit();
6504 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6505
Andy Adamson863a3c62011-03-23 13:27:54 +00006506 put_rpccred(data->cred);
6507 kfree(data);
6508}
6509
6510static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6511 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6512 .rpc_call_done = nfs4_layoutcommit_done,
6513 .rpc_release = nfs4_layoutcommit_release,
6514};
6515
6516int
Andy Adamsonef311532011-03-12 02:58:10 -05006517nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006518{
6519 struct rpc_message msg = {
6520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6521 .rpc_argp = &data->args,
6522 .rpc_resp = &data->res,
6523 .rpc_cred = data->cred,
6524 };
6525 struct rpc_task_setup task_setup_data = {
6526 .task = &data->task,
6527 .rpc_client = NFS_CLIENT(data->args.inode),
6528 .rpc_message = &msg,
6529 .callback_ops = &nfs4_layoutcommit_ops,
6530 .callback_data = data,
6531 .flags = RPC_TASK_ASYNC,
6532 };
6533 struct rpc_task *task;
6534 int status = 0;
6535
6536 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6537 "lbw: %llu inode %lu\n",
6538 data->task.tk_pid, sync,
6539 data->args.lastbytewritten,
6540 data->args.inode->i_ino);
6541
Trond Myklebust9d12b212012-01-17 22:04:25 -05006542 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006543 task = rpc_run_task(&task_setup_data);
6544 if (IS_ERR(task))
6545 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006546 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006547 goto out;
6548 status = nfs4_wait_for_completion_rpc_task(task);
6549 if (status != 0)
6550 goto out;
6551 status = task->tk_status;
6552out:
6553 dprintk("%s: status %d\n", __func__, status);
6554 rpc_put_task(task);
6555 return status;
6556}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006557
6558static int
6559_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6560 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6561{
6562 struct nfs41_secinfo_no_name_args args = {
6563 .style = SECINFO_STYLE_CURRENT_FH,
6564 };
6565 struct nfs4_secinfo_res res = {
6566 .flavors = flavors,
6567 };
6568 struct rpc_message msg = {
6569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6570 .rpc_argp = &args,
6571 .rpc_resp = &res,
6572 };
6573 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6574}
6575
6576static int
6577nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6578 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6579{
6580 struct nfs4_exception exception = { };
6581 int err;
6582 do {
6583 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6584 switch (err) {
6585 case 0:
6586 case -NFS4ERR_WRONGSEC:
6587 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006588 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006589 default:
6590 err = nfs4_handle_exception(server, err, &exception);
6591 }
6592 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006593out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006594 return err;
6595}
6596
6597static int
6598nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6599 struct nfs_fsinfo *info)
6600{
6601 int err;
6602 struct page *page;
6603 rpc_authflavor_t flavor;
6604 struct nfs4_secinfo_flavors *flavors;
6605
6606 page = alloc_page(GFP_KERNEL);
6607 if (!page) {
6608 err = -ENOMEM;
6609 goto out;
6610 }
6611
6612 flavors = page_address(page);
6613 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6614
6615 /*
6616 * Fall back on "guess and check" method if
6617 * the server doesn't support SECINFO_NO_NAME
6618 */
6619 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6620 err = nfs4_find_root_sec(server, fhandle, info);
6621 goto out_freepage;
6622 }
6623 if (err)
6624 goto out_freepage;
6625
6626 flavor = nfs_find_best_sec(flavors);
6627 if (err == 0)
6628 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6629
6630out_freepage:
6631 put_page(page);
6632 if (err == -EACCES)
6633 return -EPERM;
6634out:
6635 return err;
6636}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006637
6638static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006639{
6640 int status;
6641 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006642 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006643 };
6644 struct nfs41_test_stateid_res res;
6645 struct rpc_message msg = {
6646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6647 .rpc_argp = &args,
6648 .rpc_resp = &res,
6649 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006650
Chuck Lever38527b12012-07-11 16:30:23 -04006651 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006652 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006653 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006654 if (status != NFS_OK) {
6655 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006656 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006657 }
6658 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006659 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006660}
6661
Chuck Lever38527b12012-07-11 16:30:23 -04006662/**
6663 * nfs41_test_stateid - perform a TEST_STATEID operation
6664 *
6665 * @server: server / transport on which to perform the operation
6666 * @stateid: state ID to test
6667 *
6668 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6669 * Otherwise a negative NFS4ERR value is returned if the operation
6670 * failed or the state ID is not currently valid.
6671 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006672static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006673{
6674 struct nfs4_exception exception = { };
6675 int err;
6676 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006677 err = _nfs41_test_stateid(server, stateid);
6678 if (err != -NFS4ERR_DELAY)
6679 break;
6680 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006681 } while (exception.retry);
6682 return err;
6683}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006684
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006685static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006686{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006687 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006688 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006689 };
6690 struct nfs41_free_stateid_res res;
6691 struct rpc_message msg = {
6692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6693 .rpc_argp = &args,
6694 .rpc_resp = &res,
6695 };
Chuck Lever38527b12012-07-11 16:30:23 -04006696 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006697
Chuck Lever38527b12012-07-11 16:30:23 -04006698 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006699 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006700 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006701 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006702 dprintk("NFS reply free_stateid: %d\n", status);
6703 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006704}
6705
Chuck Lever38527b12012-07-11 16:30:23 -04006706/**
6707 * nfs41_free_stateid - perform a FREE_STATEID operation
6708 *
6709 * @server: server / transport on which to perform the operation
6710 * @stateid: state ID to release
6711 *
6712 * Returns NFS_OK if the server freed "stateid". Otherwise a
6713 * negative NFS4ERR value is returned.
6714 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006715static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006716{
6717 struct nfs4_exception exception = { };
6718 int err;
6719 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006720 err = _nfs4_free_stateid(server, stateid);
6721 if (err != -NFS4ERR_DELAY)
6722 break;
6723 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006724 } while (exception.retry);
6725 return err;
6726}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006727
6728static bool nfs41_match_stateid(const nfs4_stateid *s1,
6729 const nfs4_stateid *s2)
6730{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006731 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006732 return false;
6733
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006734 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006735 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006736 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006737 return true;
6738
6739 return false;
6740}
6741
Andy Adamson557134a2009-04-01 09:21:53 -04006742#endif /* CONFIG_NFS_V4_1 */
6743
Trond Myklebust36281ca2012-03-04 18:13:56 -05006744static bool nfs4_match_stateid(const nfs4_stateid *s1,
6745 const nfs4_stateid *s2)
6746{
Trond Myklebustf597c532012-03-04 18:13:56 -05006747 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006748}
6749
6750
Trond Myklebust17280172012-03-11 13:11:00 -04006751static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006752 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006753 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 .recover_open = nfs4_open_reclaim,
6755 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006756 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006757 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758};
6759
Andy Adamson591d71c2009-04-01 09:22:47 -04006760#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006761static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006762 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6763 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6764 .recover_open = nfs4_open_reclaim,
6765 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006766 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006767 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006768 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006769};
6770#endif /* CONFIG_NFS_V4_1 */
6771
Trond Myklebust17280172012-03-11 13:11:00 -04006772static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006773 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006774 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 .recover_open = nfs4_open_expired,
6776 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006777 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006778 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779};
6780
Andy Adamson591d71c2009-04-01 09:22:47 -04006781#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006782static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006783 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6784 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006785 .recover_open = nfs41_open_expired,
6786 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006787 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006788 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006789};
6790#endif /* CONFIG_NFS_V4_1 */
6791
Trond Myklebust17280172012-03-11 13:11:00 -04006792static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006793 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006794 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006795 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006796};
6797
6798#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006799static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006800 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006801 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006802 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006803};
6804#endif
6805
Trond Myklebust97dc1352010-06-16 09:52:26 -04006806static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6807 .minor_version = 0,
6808 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006809 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006810 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006811 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6812 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6813 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006814};
6815
6816#if defined(CONFIG_NFS_V4_1)
6817static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6818 .minor_version = 1,
6819 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006820 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006821 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006822 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6823 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6824 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006825};
6826#endif
6827
Trond Myklebust97dc1352010-06-16 09:52:26 -04006828const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6829 [0] = &nfs_v4_0_minor_ops,
6830#if defined(CONFIG_NFS_V4_1)
6831 [1] = &nfs_v4_1_minor_ops,
6832#endif
6833};
6834
Bryan Schumaker73a79702012-07-16 16:39:12 -04006835const struct inode_operations nfs4_dir_inode_operations = {
6836 .create = nfs_create,
6837 .lookup = nfs_lookup,
6838 .atomic_open = nfs_atomic_open,
6839 .link = nfs_link,
6840 .unlink = nfs_unlink,
6841 .symlink = nfs_symlink,
6842 .mkdir = nfs_mkdir,
6843 .rmdir = nfs_rmdir,
6844 .mknod = nfs_mknod,
6845 .rename = nfs_rename,
6846 .permission = nfs_permission,
6847 .getattr = nfs_getattr,
6848 .setattr = nfs_setattr,
6849 .getxattr = generic_getxattr,
6850 .setxattr = generic_setxattr,
6851 .listxattr = generic_listxattr,
6852 .removexattr = generic_removexattr,
6853};
6854
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006855static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006856 .permission = nfs_permission,
6857 .getattr = nfs_getattr,
6858 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006859 .getxattr = generic_getxattr,
6860 .setxattr = generic_setxattr,
6861 .listxattr = generic_listxattr,
6862 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006863};
6864
David Howells509de812006-08-22 20:06:11 -04006865const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 .version = 4, /* protocol version */
6867 .dentry_ops = &nfs4_dentry_operations,
6868 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006869 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006870 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006872 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 .getattr = nfs4_proc_getattr,
6874 .setattr = nfs4_proc_setattr,
6875 .lookup = nfs4_proc_lookup,
6876 .access = nfs4_proc_access,
6877 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 .create = nfs4_proc_create,
6879 .remove = nfs4_proc_remove,
6880 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006881 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 .unlink_done = nfs4_proc_unlink_done,
6883 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006884 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006885 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006886 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 .link = nfs4_proc_link,
6888 .symlink = nfs4_proc_symlink,
6889 .mkdir = nfs4_proc_mkdir,
6890 .rmdir = nfs4_proc_remove,
6891 .readdir = nfs4_proc_readdir,
6892 .mknod = nfs4_proc_mknod,
6893 .statfs = nfs4_proc_statfs,
6894 .fsinfo = nfs4_proc_fsinfo,
6895 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006896 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 .decode_dirent = nfs4_decode_dirent,
6898 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006899 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006900 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006901 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006903 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006904 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006905 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006907 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006908 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006910 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006911 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006912 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006913 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006914 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006915 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006916 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04006917 .free_client = nfs4_free_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918};
6919
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006920static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6921 .prefix = XATTR_NAME_NFSV4_ACL,
6922 .list = nfs4_xattr_list_nfs4_acl,
6923 .get = nfs4_xattr_get_nfs4_acl,
6924 .set = nfs4_xattr_set_nfs4_acl,
6925};
6926
6927const struct xattr_handler *nfs4_xattr_handlers[] = {
6928 &nfs4_xattr_nfs4_acl_handler,
6929 NULL
6930};
6931
Trond Myklebustef159e92012-02-06 19:50:40 -05006932module_param(max_session_slots, ushort, 0644);
6933MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6934 "requests the client will negotiate");
6935
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936/*
6937 * Local variables:
6938 * c-basic-offset: 8
6939 * End:
6940 */