blob: 68438aa4f08ede77f5aa375bac36981374655bb2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400107 case -NFS4ERR_ACCESS:
108 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700111 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/*
118 * This is our standard bitmap for GETATTR requests.
119 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400120const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 FATTR4_WORD0_TYPE
122 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_SIZE
124 | FATTR4_WORD0_FSID
125 | FATTR4_WORD0_FILEID,
126 FATTR4_WORD1_MODE
127 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
135};
136
Trond Myklebust1549210f2012-06-05 09:16:47 -0400137static const u32 nfs4_pnfs_open_bitmap[3] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
153};
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155const u32 nfs4_statfs_bitmap[2] = {
156 FATTR4_WORD0_FILES_AVAIL
157 | FATTR4_WORD0_FILES_FREE
158 | FATTR4_WORD0_FILES_TOTAL,
159 FATTR4_WORD1_SPACE_AVAIL
160 | FATTR4_WORD1_SPACE_FREE
161 | FATTR4_WORD1_SPACE_TOTAL
162};
163
Trond Myklebust4ce79712005-06-22 17:16:21 +0000164const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 FATTR4_WORD0_MAXLINK
166 | FATTR4_WORD0_MAXNAME,
167 0
168};
169
Fred Isamandae100c2011-07-30 20:52:37 -0400170const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 | FATTR4_WORD0_MAXREAD
172 | FATTR4_WORD0_MAXWRITE
173 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700174 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400175 | FATTR4_WORD1_FS_LAYOUT_TYPES,
176 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Manoj Naik830b8e32006-06-09 09:34:25 -0400179const u32 nfs4_fs_locations_bitmap[2] = {
180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID
185 | FATTR4_WORD0_FS_LOCATIONS,
186 FATTR4_WORD1_MODE
187 | FATTR4_WORD1_NUMLINKS
188 | FATTR4_WORD1_OWNER
189 | FATTR4_WORD1_OWNER_GROUP
190 | FATTR4_WORD1_RAWDEV
191 | FATTR4_WORD1_SPACE_USED
192 | FATTR4_WORD1_TIME_ACCESS
193 | FATTR4_WORD1_TIME_METADATA
194 | FATTR4_WORD1_TIME_MODIFY
195 | FATTR4_WORD1_MOUNTED_ON_FILEID
196};
197
Al Virobc4785c2006-10-19 23:28:51 -0700198static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct nfs4_readdir_arg *readdir)
200{
Al Viro0dbb4c62006-10-19 23:28:49 -0700201 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 BUG_ON(readdir->count < 80);
204 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000205 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
207 return;
208 }
209
210 readdir->cookie = 0;
211 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
212 if (cookie == 2)
213 return;
214
215 /*
216 * NFSv4 servers do not return entries for '.' and '..'
217 * Therefore, we fake these entries here. We let '.'
218 * have cookie 0 and '..' have cookie 1. Note that
219 * when talking to the server, we always send cookie 0
220 * instead of 1 or 2.
221 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800222 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (cookie == 0) {
225 *p++ = xdr_one; /* next */
226 *p++ = xdr_zero; /* cookie, first word */
227 *p++ = xdr_one; /* cookie, second word */
228 *p++ = xdr_one; /* entry len */
229 memcpy(p, ".\0\0\0", 4); /* entry */
230 p++;
231 *p++ = xdr_one; /* bitmap length */
232 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
233 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400234 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 *p++ = xdr_one; /* next */
238 *p++ = xdr_zero; /* cookie, first word */
239 *p++ = xdr_two; /* cookie, second word */
240 *p++ = xdr_two; /* entry len */
241 memcpy(p, "..\0\0", 4); /* entry */
242 p++;
243 *p++ = xdr_one; /* bitmap length */
244 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
245 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400246 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 readdir->pgbase = (char *)p - (char *)start;
249 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800250 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Trond Myklebust65de8722008-12-23 15:21:44 -0500253static int nfs4_wait_clnt_recover(struct nfs_client *clp)
254{
255 int res;
256
257 might_sleep();
258
Trond Myklebuste005e802008-12-23 15:21:48 -0500259 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400260 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400261 if (res)
262 return res;
263
264 if (clp->cl_cons_state < 0)
265 return clp->cl_cons_state;
266 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500267}
268
269static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
270{
271 int res = 0;
272
273 might_sleep();
274
275 if (*timeout <= 0)
276 *timeout = NFS4_POLL_RETRY_MIN;
277 if (*timeout > NFS4_POLL_RETRY_MAX)
278 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100279 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500280 if (fatal_signal_pending(current))
281 res = -ERESTARTSYS;
282 *timeout <<= 1;
283 return res;
284}
285
286/* This is the error handling routine for processes that are allowed
287 * to sleep.
288 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800289static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500290{
291 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500292 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500293 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 int ret = errorcode;
295
296 exception->retry = 0;
297 switch(errorcode) {
298 case 0:
299 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500300 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400301 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400302 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500303 exception->retry = 1;
304 return 0;
305 }
306 if (state == NULL)
307 break;
308 nfs4_schedule_stateid_recovery(server, state);
309 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500310 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500311 case -NFS4ERR_ADMIN_REVOKED:
312 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500313 if (state == NULL)
314 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400315 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500316 nfs4_schedule_stateid_recovery(server, state);
317 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400318 case -NFS4ERR_EXPIRED:
319 if (state != NULL)
320 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500321 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500322 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500323 nfs4_schedule_lease_recovery(clp);
324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400326 case -NFS4ERR_BADSESSION:
327 case -NFS4ERR_BADSLOT:
328 case -NFS4ERR_BAD_HIGH_SLOT:
329 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
330 case -NFS4ERR_DEADSESSION:
331 case -NFS4ERR_SEQ_FALSE_RETRY:
332 case -NFS4ERR_SEQ_MISORDERED:
333 dprintk("%s ERROR: %d Reset session\n", __func__,
334 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400335 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400336 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500337 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500338#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500340 if (exception->timeout > HZ) {
341 /* We have retried a decent amount, time to
342 * fail
343 */
344 ret = -EBUSY;
345 break;
346 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500347 case -NFS4ERR_GRACE:
348 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500349 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500350 ret = nfs4_delay(server->client, &exception->timeout);
351 if (ret != 0)
352 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400353 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500354 case -NFS4ERR_OLD_STATEID:
355 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800356 break;
357 case -NFS4ERR_BADOWNER:
358 /* The following works around a Linux server bug! */
359 case -NFS4ERR_BADNAME:
360 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
361 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
362 exception->retry = 1;
363 printk(KERN_WARNING "NFS: v4 server %s "
364 "does not accept raw "
365 "uid/gids. "
366 "Reenabling the idmapper.\n",
367 server->nfs_client->cl_hostname);
368 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500369 }
370 /* We failed to handle the error */
371 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500372wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500373 ret = nfs4_wait_clnt_recover(clp);
374 if (ret == 0)
375 exception->retry = 1;
376 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500377}
378
379
Trond Myklebust452e9352010-07-31 14:29:06 -0400380static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_lock(&clp->cl_lock);
383 if (time_before(clp->cl_last_renewal,timestamp))
384 clp->cl_last_renewal = timestamp;
385 spin_unlock(&clp->cl_lock);
386}
387
Trond Myklebust452e9352010-07-31 14:29:06 -0400388static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
389{
390 do_renew_lease(server->nfs_client, timestamp);
391}
392
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400393#if defined(CONFIG_NFS_V4_1)
394
Benny Halevy510b8172009-04-01 09:22:14 -0400395/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400396 * nfs4_free_slot - free a slot and efficiently update slot table.
397 *
398 * freeing a slot is trivially done by clearing its respective bit
399 * in the bitmap.
400 * If the freed slotid equals highest_used_slotid we want to update it
401 * so that the server would be able to size down the slot table if needed,
402 * otherwise we know that the highest_used_slotid is still in use.
403 * When updating highest_used_slotid there may be "holes" in the bitmap
404 * so we need to scan down from highest_used_slotid to 0 looking for the now
405 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500406 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 *
408 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400409 */
410static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500411nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400412{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500413 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400414 /* clear used bit in bitmap */
415 __clear_bit(slotid, tbl->used_slots);
416
417 /* update highest_used_slotid when it is freed */
418 if (slotid == tbl->highest_used_slotid) {
419 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500420 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400421 tbl->highest_used_slotid = slotid;
422 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500423 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400424 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500425 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
426 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427}
428
Trond Myklebust961a8282012-01-17 22:57:37 -0500429bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
430{
431 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
432 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400433}
434
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435/*
Andy Adamson42acd022011-01-06 02:04:34 +0000436 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437 */
Andy Adamson42acd022011-01-06 02:04:34 +0000438static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800439{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400440 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500441 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
442 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800443 return;
444 }
445
Trond Myklebust45d43c22012-02-06 19:38:51 -0500446 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800447 return;
448
Andy Adamson42acd022011-01-06 02:04:34 +0000449 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
450 complete(&ses->fc_slot_table.complete);
451}
452
453/*
454 * Signal state manager thread if session back channel is drained
455 */
456void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
457{
458 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500459 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000460 return;
461 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
462 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800463}
464
Trond Myklebustd185a332010-06-16 09:52:25 -0400465static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400466{
467 struct nfs4_slot_table *tbl;
468
Trond Myklebustd185a332010-06-16 09:52:25 -0400469 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400470 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400471 /* just wake up the next guy waiting since
472 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500473 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500474 return;
Andy Adamson13615872009-04-01 09:22:17 -0400475 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500476
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500477 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500478 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000479 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500480 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400481 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400482}
483
Trond Myklebust14516c32010-07-31 14:29:06 -0400484static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485{
486 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400488
489 /*
490 * sr_status remains 1 if an RPC level error occurred. The server
491 * may or may not have processed the sequence operation..
492 * Proceed as if the server received and processed the sequence
493 * operation.
494 */
495 if (res->sr_status == 1)
496 res->sr_status = NFS_OK;
497
Bryan Schumaker468f8612011-04-18 15:57:32 -0400498 /* don't increment the sequence number if the task wasn't sent */
499 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400500 goto out;
501
Andy Adamson691daf32009-12-04 15:55:39 -0500502 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400503 switch (res->sr_status) {
504 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400505 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400506 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400507 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400509 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500510 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500511 if (res->sr_status_flags != 0)
512 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 break;
514 case -NFS4ERR_DELAY:
515 /* The server detected a resend of the RPC call and
516 * returned NFS4ERR_DELAY as per Section 2.10.6.2
517 * of RFC5661.
518 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200519 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400520 __func__,
521 res->sr_slot - res->sr_session->fc_slot_table.slots,
522 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 goto out_retry;
524 default:
525 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400526 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527 }
528out:
529 /* The session may be reset by one of the error handlers. */
530 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400531 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400532 return 1;
533out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400534 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400535 goto out;
536 rpc_delay(task, NFS4_POLL_RETRY_MAX);
537 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400538}
539
Trond Myklebust14516c32010-07-31 14:29:06 -0400540static int nfs4_sequence_done(struct rpc_task *task,
541 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400542{
Trond Myklebust14516c32010-07-31 14:29:06 -0400543 if (res->sr_session == NULL)
544 return 1;
545 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400546}
547
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400548/*
Benny Halevy510b8172009-04-01 09:22:14 -0400549 * nfs4_find_slot - efficiently look for a free slot
550 *
551 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
552 * If found, we mark the slot as used, update the highest_used_slotid,
553 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500554 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 *
556 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400557 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500558static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800559nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400560{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500561 u32 slotid;
562 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400563
Trond Myklebust45d43c22012-02-06 19:38:51 -0500564 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400565 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
566 tbl->max_slots);
567 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
568 if (slotid >= tbl->max_slots)
569 goto out;
570 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500571 if (slotid > tbl->highest_used_slotid ||
572 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400573 tbl->highest_used_slotid = slotid;
574 ret_id = slotid;
575out:
576 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
577 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400578 return ret_id;
579}
580
Trond Myklebust9d12b212012-01-17 22:04:25 -0500581static void nfs41_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
583{
584 args->sa_session = NULL;
585 args->sa_cache_this = 0;
586 if (cache_reply)
587 args->sa_cache_this = 1;
588 res->sr_session = NULL;
589 res->sr_slot = NULL;
590}
591
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000592int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *args,
594 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595 struct rpc_task *task)
596{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400597 struct nfs4_slot *slot;
598 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500599 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600
601 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400603 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 return 0;
605
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606 tbl = &session->fc_slot_table;
607
608 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400609 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800610 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500611 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500612 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400613 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500614 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500615 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400616 }
617
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800618 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
619 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
620 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
621 spin_unlock(&tbl->slot_tbl_lock);
622 dprintk("%s enforce FIFO order\n", __func__);
623 return -EAGAIN;
624 }
625
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800626 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500627 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400628 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
629 spin_unlock(&tbl->slot_tbl_lock);
630 dprintk("<-- %s: no free slots\n", __func__);
631 return -EAGAIN;
632 }
633 spin_unlock(&tbl->slot_tbl_lock);
634
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800635 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400636 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400637 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400638 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400639
640 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
641
Benny Halevy99fe60d2009-04-01 09:22:29 -0400642 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400643 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400645 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400646 /*
647 * sr_status is only set in decode_sequence, and so will remain
648 * set to 1 if an rpc level failure occurs.
649 */
650 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651 return 0;
652}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000653EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654
Trond Myklebust035168a2010-06-16 09:52:26 -0400655int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 struct nfs4_sequence_args *args,
657 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 struct rpc_task *task)
659{
Trond Myklebust035168a2010-06-16 09:52:26 -0400660 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 int ret = 0;
662
Trond Myklebust9d12b212012-01-17 22:04:25 -0500663 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400665
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200666 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400667 __func__, session->clp, session, res->sr_slot ?
668 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400669
Trond Myklebust9d12b212012-01-17 22:04:25 -0500670 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671out:
672 dprintk("<-- %s status=%d\n", __func__, ret);
673 return ret;
674}
675
676struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400677 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678 struct nfs4_sequence_args *seq_args;
679 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680};
681
682static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
683{
684 struct nfs41_call_sync_data *data = calldata;
685
Trond Myklebust035168a2010-06-16 09:52:26 -0400686 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
687
688 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500689 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 return;
691 rpc_call_start(task);
692}
693
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
695{
696 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
697 nfs41_call_sync_prepare(task, calldata);
698}
699
Andy Adamson69ab40c2009-04-01 09:22:19 -0400700static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
701{
702 struct nfs41_call_sync_data *data = calldata;
703
Trond Myklebust14516c32010-07-31 14:29:06 -0400704 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400705}
706
Trond Myklebust17280172012-03-11 13:11:00 -0400707static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400709 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710};
711
Trond Myklebust17280172012-03-11 13:11:00 -0400712static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800713 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
714 .rpc_call_done = nfs41_call_sync_done,
715};
716
Bryan Schumaker7c513052011-03-24 17:12:24 +0000717static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
718 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
721 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800722 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723{
724 int ret;
725 struct rpc_task *task;
726 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400727 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 .seq_args = args,
729 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730 };
731 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 .rpc_message = msg,
734 .callback_ops = &nfs41_call_sync_ops,
735 .callback_data = &data
736 };
737
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800738 if (privileged)
739 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 task = rpc_run_task(&task_setup);
741 if (IS_ERR(task))
742 ret = PTR_ERR(task);
743 else {
744 ret = task->tk_status;
745 rpc_put_task(task);
746 }
747 return ret;
748}
749
Bryan Schumaker7c513052011-03-24 17:12:24 +0000750int _nfs4_call_sync_session(struct rpc_clnt *clnt,
751 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400752 struct rpc_message *msg,
753 struct nfs4_sequence_args *args,
754 struct nfs4_sequence_res *res,
755 int cache_reply)
756{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500757 nfs41_init_sequence(args, res, cache_reply);
758 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400759}
760
Trond Myklebustdf896452010-06-16 09:52:26 -0400761#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500762static inline
763void nfs41_init_sequence(struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res, int cache_reply)
765{
766}
767
Trond Myklebust14516c32010-07-31 14:29:06 -0400768static int nfs4_sequence_done(struct rpc_task *task,
769 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400770{
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400772}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400773#endif /* CONFIG_NFS_V4_1 */
774
Bryan Schumaker7c513052011-03-24 17:12:24 +0000775int _nfs4_call_sync(struct rpc_clnt *clnt,
776 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400777 struct rpc_message *msg,
778 struct nfs4_sequence_args *args,
779 struct nfs4_sequence_res *res,
780 int cache_reply)
781{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500782 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000783 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400784}
785
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000786static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000787int nfs4_call_sync(struct rpc_clnt *clnt,
788 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000789 struct rpc_message *msg,
790 struct nfs4_sequence_args *args,
791 struct nfs4_sequence_res *res,
792 int cache_reply)
793{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000794 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
795 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000796}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400797
Trond Myklebust38478b22006-05-25 01:40:57 -0400798static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Trond Myklebust38478b22006-05-25 01:40:57 -0400800 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Trond Myklebust38478b22006-05-25 01:40:57 -0400802 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400803 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700804 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400805 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700806 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 struct nfs_openargs o_arg;
813 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100814 struct nfs_open_confirmargs c_arg;
815 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500816 struct nfs4_string owner_name;
817 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400820 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100824 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400825 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100826 int rpc_status;
827 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828};
829
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400830
831static void nfs4_init_opendata_res(struct nfs4_opendata *p)
832{
833 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400834 p->o_res.seqid = p->o_arg.seqid;
835 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836 p->o_res.server = p->o_arg.server;
837 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500838 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400839}
840
Al Viro82a2c1b2011-06-22 18:30:55 -0400841static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400843 const struct iattr *attrs,
844 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845{
Al Viro82a2c1b2011-06-22 18:30:55 -0400846 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 struct inode *dir = parent->d_inode;
848 struct nfs_server *server = NFS_SERVER(dir);
849 struct nfs4_opendata *p;
850
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400851 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 if (p == NULL)
853 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400854 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 if (p->o_arg.seqid == NULL)
856 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 nfs_sb_active(dentry->d_sb);
858 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 p->dir = parent;
860 p->owner = sp;
861 atomic_inc(&sp->so_count);
862 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863 p->o_arg.open_flags = flags;
864 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700865 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
866 * will return permission denied for all bits until close */
867 if (!(flags & O_EXCL)) {
868 /* ask server to check for all possible rights as results
869 * are cached */
870 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
871 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
872 p->o_res.access_request = p->o_arg.access;
873 }
David Howells7539bba2006-08-22 20:06:09 -0400874 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400875 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
876 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400877 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100878 p->o_arg.server = server;
879 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400880 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500882 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500883 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400884
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 p->o_arg.u.attrs = &p->attrs;
886 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500887
888 verf[0] = jiffies;
889 verf[1] = current->pid;
890 memcpy(p->o_arg.u.verifier.data, verf,
891 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100892 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100893 p->c_arg.fh = &p->o_res.fh;
894 p->c_arg.stateid = &p->o_res.stateid;
895 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400896 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400897 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100898 return p;
899err_free:
900 kfree(p);
901err:
902 dput(parent);
903 return NULL;
904}
905
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400906static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100907{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400908 struct nfs4_opendata *p = container_of(kref,
909 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400910 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400911
912 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400913 if (p->state != NULL)
914 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400915 nfs4_put_state_owner(p->owner);
916 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400917 dput(p->dentry);
918 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500919 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400920 kfree(p);
921}
922
923static void nfs4_opendata_put(struct nfs4_opendata *p)
924{
925 if (p != NULL)
926 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100927}
928
Trond Myklebust06f814a2006-01-03 09:55:07 +0100929static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
930{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100931 int ret;
932
Trond Myklebust06f814a2006-01-03 09:55:07 +0100933 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100934 return ret;
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938{
939 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940
Trond Myklebust536e43d2012-01-17 22:04:26 -0500941 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 goto out;
943 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500945 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
946 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400947 break;
948 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500949 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
950 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400951 break;
952 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500953 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
954 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400955 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400957 return ret;
958}
959
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500962 if (delegation == NULL)
963 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500966 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400967 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500968 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 return 1;
970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100973{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100975 case FMODE_WRITE:
976 state->n_wronly++;
977 break;
978 case FMODE_READ:
979 state->n_rdonly++;
980 break;
981 case FMODE_READ|FMODE_WRITE:
982 state->n_rdwr++;
983 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100985}
986
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400988{
989 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500990 nfs4_stateid_copy(&state->stateid, stateid);
991 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400993 case FMODE_READ:
994 set_bit(NFS_O_RDONLY_STATE, &state->flags);
995 break;
996 case FMODE_WRITE:
997 set_bit(NFS_O_WRONLY_STATE, &state->flags);
998 break;
999 case FMODE_READ|FMODE_WRITE:
1000 set_bit(NFS_O_RDWR_STATE, &state->flags);
1001 }
1002}
1003
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001005{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001006 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001008 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009}
1010
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 /*
1014 * Protect the call to nfs4_state_set_mode_locked and
1015 * serialise the stateid update
1016 */
1017 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001018 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001019 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001020 set_bit(NFS_DELEGATED_STATE, &state->flags);
1021 }
1022 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001024 write_sequnlock(&state->seqlock);
1025 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001027 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001031{
1032 struct nfs_inode *nfsi = NFS_I(state->inode);
1033 struct nfs_delegation *deleg_cur;
1034 int ret = 0;
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001037
1038 rcu_read_lock();
1039 deleg_cur = rcu_dereference(nfsi->delegation);
1040 if (deleg_cur == NULL)
1041 goto no_delegation;
1042
1043 spin_lock(&deleg_cur->lock);
1044 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001046 goto no_delegation_unlock;
1047
1048 if (delegation == NULL)
1049 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001050 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001051 goto no_delegation_unlock;
1052
Trond Myklebustb7391f42008-12-23 15:21:52 -05001053 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001055 ret = 1;
1056no_delegation_unlock:
1057 spin_unlock(&deleg_cur->lock);
1058no_delegation:
1059 rcu_read_unlock();
1060
1061 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001063 ret = 1;
1064 }
1065
1066 return ret;
1067}
1068
1069
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071{
1072 struct nfs_delegation *delegation;
1073
1074 rcu_read_lock();
1075 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 rcu_read_unlock();
1078 return;
1079 }
1080 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001081 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082}
1083
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085{
1086 struct nfs4_state *state = opendata->state;
1087 struct nfs_inode *nfsi = NFS_I(state->inode);
1088 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001089 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001090 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001091 nfs4_stateid stateid;
1092 int ret = -EAGAIN;
1093
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001096 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001097 if (can_open_cached(state, fmode, open_mode)) {
1098 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 goto out_return_state;
1101 }
1102 spin_unlock(&state->owner->so_lock);
1103 }
Trond Myklebust34310432008-12-23 15:21:38 -05001104 rcu_read_lock();
1105 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001106 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001107 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001109 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001111 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001113 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001114 if (ret != 0)
1115 goto out;
1116 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001117
1118 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001120 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122out:
1123 return ERR_PTR(ret);
1124out_return_state:
1125 atomic_inc(&state->count);
1126 return state;
1127}
1128
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1130{
1131 struct inode *inode;
1132 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001133 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135
Trond Myklebustaac00a82007-07-05 19:02:21 -04001136 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001137 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001138 goto out;
1139 }
1140
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001141 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001143 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001146 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147 goto err;
1148 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149 state = nfs4_get_open_state(inode, data->owner);
1150 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001151 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001152 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001153 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001154 int delegation_flags = 0;
1155
Trond Myklebust003707c2007-07-05 18:07:55 -04001156 rcu_read_lock();
1157 delegation = rcu_dereference(NFS_I(inode)->delegation);
1158 if (delegation)
1159 delegation_flags = delegation->flags;
1160 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001161 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1162 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1163 "returning a delegation for "
1164 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001165 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001166 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001167 nfs_inode_set_delegation(state->inode,
1168 data->owner->so_cred,
1169 &data->o_res);
1170 else
1171 nfs_inode_reclaim_delegation(state->inode,
1172 data->owner->so_cred,
1173 &data->o_res);
1174 }
Trond Myklebust34310432008-12-23 15:21:38 -05001175
1176 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001178 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001179out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001180 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001181err_put_inode:
1182 iput(inode);
1183err:
1184 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001185}
1186
Trond Myklebust864472e2006-01-03 09:55:15 +01001187static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1188{
1189 struct nfs_inode *nfsi = NFS_I(state->inode);
1190 struct nfs_open_context *ctx;
1191
1192 spin_lock(&state->inode->i_lock);
1193 list_for_each_entry(ctx, &nfsi->open_files, list) {
1194 if (ctx->state != state)
1195 continue;
1196 get_nfs_open_context(ctx);
1197 spin_unlock(&state->inode->i_lock);
1198 return ctx;
1199 }
1200 spin_unlock(&state->inode->i_lock);
1201 return ERR_PTR(-ENOENT);
1202}
1203
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001204static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1205{
1206 struct nfs4_opendata *opendata;
1207
Al Viro3d4ff432011-06-22 18:40:12 -04001208 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001209 if (opendata == NULL)
1210 return ERR_PTR(-ENOMEM);
1211 opendata->state = state;
1212 atomic_inc(&state->count);
1213 return opendata;
1214}
1215
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001216static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001217{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 int ret;
1220
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001221 opendata->o_arg.open_flags = 0;
1222 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001223 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1224 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1225 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001226 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 if (ret != 0)
1228 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001230 if (IS_ERR(newstate))
1231 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001232 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 return 0;
1235}
1236
1237static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1238{
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 int ret;
1241
1242 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001243 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 smp_rmb();
1245 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001246 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001248 if (ret != 0)
1249 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001250 if (newstate != state)
1251 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001252 }
1253 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001254 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001256 if (ret != 0)
1257 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001258 if (newstate != state)
1259 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 }
1261 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001262 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001263 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001264 if (ret != 0)
1265 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001266 if (newstate != state)
1267 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 /*
1270 * We may have performed cached opens for all three recoveries.
1271 * Check if we need to update the current stateid.
1272 */
1273 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001274 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001275 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001276 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001277 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001278 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001279 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 return 0;
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/*
1284 * OPEN_RECLAIM:
1285 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001287static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001289 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001290 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int status;
1293
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001294 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1295 if (IS_ERR(opendata))
1296 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1298 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001299 rcu_read_lock();
1300 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001301 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001302 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001303 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 opendata->o_arg.u.delegation_type = delegation_type;
1305 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001306 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return status;
1308}
1309
Trond Myklebust539cd032007-06-05 11:46:42 -04001310static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct nfs_server *server = NFS_SERVER(state->inode);
1313 struct nfs4_exception exception = { };
1314 int err;
1315 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001316 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001317 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001318 break;
1319 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 } while (exception.retry);
1321 return err;
1322}
1323
Trond Myklebust864472e2006-01-03 09:55:15 +01001324static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1325{
1326 struct nfs_open_context *ctx;
1327 int ret;
1328
1329 ctx = nfs4_state_find_open_context(state);
1330 if (IS_ERR(ctx))
1331 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001332 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 put_nfs_open_context(ctx);
1334 return ret;
1335}
1336
Trond Myklebust13437e12007-07-06 15:10:43 -04001337static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001339 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001342 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1343 if (IS_ERR(opendata))
1344 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001345 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001346 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001348 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Trond Myklebust13437e12007-07-06 15:10:43 -04001352int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001355 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int err;
1357 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001358 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 switch (err) {
1360 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001361 case -ENOENT:
1362 case -ESTALE:
1363 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001364 case -NFS4ERR_BADSESSION:
1365 case -NFS4ERR_BADSLOT:
1366 case -NFS4ERR_BAD_HIGH_SLOT:
1367 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1368 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001369 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 case -NFS4ERR_STALE_CLIENTID:
1372 case -NFS4ERR_STALE_STATEID:
1373 case -NFS4ERR_EXPIRED:
1374 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001375 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001376 goto out;
1377 case -ERESTARTSYS:
1378 /*
1379 * The show must go on: exit, but mark the
1380 * stateid as needing recovery.
1381 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001382 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001383 case -NFS4ERR_ADMIN_REVOKED:
1384 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001385 nfs_inode_find_state_and_recover(state->inode,
1386 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001387 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001388 case -EKEYEXPIRED:
1389 /*
1390 * User RPCSEC_GSS context has expired.
1391 * We cannot recover this stateid now, so
1392 * skip it and allow recovery thread to
1393 * proceed.
1394 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001395 case -ENOMEM:
1396 err = 0;
1397 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 err = nfs4_handle_exception(server, err, &exception);
1400 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001401out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return err;
1403}
1404
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1406{
1407 struct nfs4_opendata *data = calldata;
1408
1409 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001410 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001411 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001412 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001413 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001414 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001415 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416}
1417
1418static void nfs4_open_confirm_release(void *calldata)
1419{
1420 struct nfs4_opendata *data = calldata;
1421 struct nfs4_state *state = NULL;
1422
1423 /* If this request hasn't been cancelled, do nothing */
1424 if (data->cancelled == 0)
1425 goto out_free;
1426 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001427 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001428 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001429 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001430 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001431 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001432out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001433 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001434}
1435
1436static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001437 .rpc_call_done = nfs4_open_confirm_done,
1438 .rpc_release = nfs4_open_confirm_release,
1439};
1440
1441/*
1442 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1443 */
1444static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1445{
1446 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1447 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001448 struct rpc_message msg = {
1449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1450 .rpc_argp = &data->c_arg,
1451 .rpc_resp = &data->c_res,
1452 .rpc_cred = data->owner->so_cred,
1453 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001454 struct rpc_task_setup task_setup_data = {
1455 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001457 .callback_ops = &nfs4_open_confirm_ops,
1458 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001459 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001460 .flags = RPC_TASK_ASYNC,
1461 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int status;
1463
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001464 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001465 data->rpc_done = 0;
1466 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001467 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001468 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001469 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001470 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001471 status = nfs4_wait_for_completion_rpc_task(task);
1472 if (status != 0) {
1473 data->cancelled = 1;
1474 smp_wmb();
1475 } else
1476 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001477 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return status;
1479}
1480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483 struct nfs4_opendata *data = calldata;
1484 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001485
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1487 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001488 /*
1489 * Check if we still need to send an OPEN call, or if we can use
1490 * a delegation instead.
1491 */
1492 if (data->state != NULL) {
1493 struct nfs_delegation *delegation;
1494
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001496 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001497 rcu_read_lock();
1498 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001499 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1500 can_open_delegated(delegation, data->o_arg.fmode))
1501 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001502 rcu_read_unlock();
1503 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001504 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001505 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001506 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001507 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001508 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1509 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001510 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001511 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001512 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001513 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001514 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001515 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001516 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001517unlock_no_action:
1518 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001519out_no_action:
1520 task->tk_action = NULL;
1521
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001524static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1525{
1526 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1527 nfs4_open_prepare(task, calldata);
1528}
1529
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530static void nfs4_open_done(struct rpc_task *task, void *calldata)
1531{
1532 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001535
Trond Myklebust14516c32010-07-31 14:29:06 -04001536 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1537 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001538
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001540 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1541 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001542 case S_IFREG:
1543 break;
1544 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001546 break;
1547 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001549 break;
1550 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001552 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001553 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001554 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001555 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1556 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001557 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001558 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001560
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001561static void nfs4_open_release(void *calldata)
1562{
1563 struct nfs4_opendata *data = calldata;
1564 struct nfs4_state *state = NULL;
1565
1566 /* If this request hasn't been cancelled, do nothing */
1567 if (data->cancelled == 0)
1568 goto out_free;
1569 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001570 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001571 goto out_free;
1572 /* In case we need an open_confirm, no cleanup! */
1573 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1574 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001575 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001576 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001577 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001578out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001579 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001580}
1581
1582static const struct rpc_call_ops nfs4_open_ops = {
1583 .rpc_call_prepare = nfs4_open_prepare,
1584 .rpc_call_done = nfs4_open_done,
1585 .rpc_release = nfs4_open_release,
1586};
1587
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001588static const struct rpc_call_ops nfs4_recover_open_ops = {
1589 .rpc_call_prepare = nfs4_recover_open_prepare,
1590 .rpc_call_done = nfs4_open_done,
1591 .rpc_release = nfs4_open_release,
1592};
1593
1594static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595{
1596 struct inode *dir = data->dir->d_inode;
1597 struct nfs_server *server = NFS_SERVER(dir);
1598 struct nfs_openargs *o_arg = &data->o_arg;
1599 struct nfs_openres *o_res = &data->o_res;
1600 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001601 struct rpc_message msg = {
1602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1603 .rpc_argp = o_arg,
1604 .rpc_resp = o_res,
1605 .rpc_cred = data->owner->so_cred,
1606 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001607 struct rpc_task_setup task_setup_data = {
1608 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001609 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001610 .callback_ops = &nfs4_open_ops,
1611 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001612 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001613 .flags = RPC_TASK_ASYNC,
1614 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001615 int status;
1616
Trond Myklebust9d12b212012-01-17 22:04:25 -05001617 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001618 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001619 data->rpc_done = 0;
1620 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001621 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001622 if (isrecover)
1623 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001624 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001625 if (IS_ERR(task))
1626 return PTR_ERR(task);
1627 status = nfs4_wait_for_completion_rpc_task(task);
1628 if (status != 0) {
1629 data->cancelled = 1;
1630 smp_wmb();
1631 } else
1632 status = data->rpc_status;
1633 rpc_put_task(task);
1634
1635 return status;
1636}
1637
1638static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1639{
1640 struct inode *dir = data->dir->d_inode;
1641 struct nfs_openres *o_res = &data->o_res;
1642 int status;
1643
1644 status = nfs4_run_open_task(data, 1);
1645 if (status != 0 || !data->rpc_done)
1646 return status;
1647
Trond Myklebust6926afd2012-01-07 13:22:46 -05001648 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1649
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001650 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1651 status = _nfs4_proc_open_confirm(data);
1652 if (status != 0)
1653 return status;
1654 }
1655
1656 return status;
1657}
1658
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001659static int nfs4_opendata_access(struct rpc_cred *cred,
1660 struct nfs4_opendata *opendata,
1661 struct nfs4_state *state, fmode_t fmode)
1662{
1663 struct nfs_access_entry cache;
1664 u32 mask;
1665
1666 /* access call failed or for some reason the server doesn't
1667 * support any access modes -- defer access call until later */
1668 if (opendata->o_res.access_supported == 0)
1669 return 0;
1670
1671 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001672 /* don't check MAY_WRITE - a newly created file may not have
1673 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001674 if (fmode & FMODE_READ)
1675 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001676 if (fmode & FMODE_EXEC)
1677 mask |= MAY_EXEC;
1678
1679 cache.cred = cred;
1680 cache.jiffies = jiffies;
1681 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1682 nfs_access_add_cache(state->inode, &cache);
1683
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001684 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001685 return 0;
1686
1687 /* even though OPEN succeeded, access is denied. Close the file */
1688 nfs4_close_state(state, fmode);
1689 return -NFS4ERR_ACCESS;
1690}
1691
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001692/*
1693 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1694 */
1695static int _nfs4_proc_open(struct nfs4_opendata *data)
1696{
1697 struct inode *dir = data->dir->d_inode;
1698 struct nfs_server *server = NFS_SERVER(dir);
1699 struct nfs_openargs *o_arg = &data->o_arg;
1700 struct nfs_openres *o_res = &data->o_res;
1701 int status;
1702
1703 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001704 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001706 if (status != 0) {
1707 if (status == -NFS4ERR_BADNAME &&
1708 !(o_arg->open_flags & O_CREAT))
1709 return -ENOENT;
1710 return status;
1711 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712
Trond Myklebust6926afd2012-01-07 13:22:46 -05001713 nfs_fattr_map_and_free_names(server, &data->f_attr);
1714
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001715 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001716 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001717 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1718 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001720 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001722 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001725 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Andy Adamsond83217c2011-03-01 01:34:17 +00001729static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001730{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001731 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001732 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001733
Trond Myklebusta78cb572009-08-09 15:06:19 -04001734 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001735 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001736 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001737 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001738 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1739 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001740 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001741 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001742 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001743 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001744 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001745}
1746
Andy Adamsond83217c2011-03-01 01:34:17 +00001747static int nfs4_recover_expired_lease(struct nfs_server *server)
1748{
1749 return nfs4_client_recover_expired_lease(server->nfs_client);
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752/*
1753 * OPEN_EXPIRED:
1754 * reclaim state on the server after a network partition.
1755 * Assumes caller holds the appropriate lock
1756 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001757static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001759 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001760 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001762 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1763 if (IS_ERR(opendata))
1764 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001765 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001766 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001767 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001768 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001772static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001773{
Trond Myklebust539cd032007-06-05 11:46:42 -04001774 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001775 struct nfs4_exception exception = { };
1776 int err;
1777
1778 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001779 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001780 switch (err) {
1781 default:
1782 goto out;
1783 case -NFS4ERR_GRACE:
1784 case -NFS4ERR_DELAY:
1785 nfs4_handle_exception(server, err, &exception);
1786 err = 0;
1787 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001788 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001789out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001790 return err;
1791}
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1794{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001796 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Trond Myklebust864472e2006-01-03 09:55:15 +01001798 ctx = nfs4_state_find_open_context(state);
1799 if (IS_ERR(ctx))
1800 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001801 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001802 put_nfs_open_context(ctx);
1803 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001806#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001807static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001808{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001809 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001810 nfs4_stateid *stateid = &state->stateid;
1811 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001812
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001813 /* If a state reset has been done, test_stateid is unneeded */
1814 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1815 return;
1816
1817 status = nfs41_test_stateid(server, stateid);
1818 if (status != NFS_OK) {
1819 /* Free the stateid unless the server explicitly
1820 * informs us the stateid is unrecognized. */
1821 if (status != -NFS4ERR_BAD_STATEID)
1822 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001823 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001824
Bryan Schumaker69388672012-09-26 15:25:52 -04001825 write_seqlock(&state->seqlock);
1826 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1827 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001828 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1829 }
1830}
1831
1832/**
1833 * nfs41_check_open_stateid - possibly free an open stateid
1834 *
1835 * @state: NFSv4 state for an inode
1836 *
1837 * Returns NFS_OK if recovery for this stateid is now finished.
1838 * Otherwise a negative NFS4ERR value is returned.
1839 */
1840static int nfs41_check_open_stateid(struct nfs4_state *state)
1841{
1842 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001843 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001844 int status;
1845
1846 /* If a state reset has been done, test_stateid is unneeded */
1847 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1848 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1849 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1850 return -NFS4ERR_BAD_STATEID;
1851
1852 status = nfs41_test_stateid(server, stateid);
1853 if (status != NFS_OK) {
1854 /* Free the stateid unless the server explicitly
1855 * informs us the stateid is unrecognized. */
1856 if (status != -NFS4ERR_BAD_STATEID)
1857 nfs41_free_stateid(server, stateid);
1858
1859 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1860 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1861 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001862 }
1863 return status;
1864}
1865
1866static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1867{
Chuck Levereb64cf92012-07-11 16:30:05 -04001868 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001869
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001870 nfs41_clear_delegation_stateid(state);
1871 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001872 if (status != NFS_OK)
1873 status = nfs4_open_expired(sp, state);
1874 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001875}
1876#endif
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001879 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1880 * fields corresponding to attributes that were used to store the verifier.
1881 * Make sure we clobber those fields in the later setattr call
1882 */
1883static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1884{
1885 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1886 !(sattr->ia_valid & ATTR_ATIME_SET))
1887 sattr->ia_valid |= ATTR_ATIME;
1888
1889 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1890 !(sattr->ia_valid & ATTR_MTIME_SET))
1891 sattr->ia_valid |= ATTR_MTIME;
1892}
1893
1894/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001895 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 */
Andy Adamson82be4172012-05-23 05:02:35 -04001897static int _nfs4_do_open(struct inode *dir,
1898 struct dentry *dentry,
1899 fmode_t fmode,
1900 int flags,
1901 struct iattr *sattr,
1902 struct rpc_cred *cred,
1903 struct nfs4_state **res,
1904 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct nfs4_state_owner *sp;
1907 struct nfs4_state *state = NULL;
1908 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001909 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001910 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001914 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1915 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1917 goto out_err;
1918 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001919 status = nfs4_recover_expired_lease(server);
1920 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001921 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001922 if (dentry->d_inode != NULL)
1923 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001924 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001925 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001926 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001927 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Andy Adamson82be4172012-05-23 05:02:35 -04001929 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1930 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1931 if (!opendata->f_attr.mdsthreshold)
1932 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001933 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001934 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001935 if (dentry->d_inode != NULL)
1936 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001937
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001938 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001940 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001942 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001943 status = PTR_ERR(state);
1944 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001945 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001946 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001947 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001948
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001949 status = nfs4_opendata_access(cred, opendata, state, fmode);
1950 if (status != 0)
1951 goto err_opendata_put;
1952
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001953 if (opendata->o_arg.open_flags & O_EXCL) {
1954 nfs4_exclusive_attrset(opendata, sattr);
1955
1956 nfs_fattr_init(opendata->o_res.f_attr);
1957 status = nfs4_do_setattr(state->inode, cred,
1958 opendata->o_res.f_attr, sattr,
1959 state);
1960 if (status == 0)
1961 nfs_setattr_update_inode(state->inode, sattr);
1962 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1963 }
Andy Adamson82be4172012-05-23 05:02:35 -04001964
1965 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1966 *ctx_th = opendata->f_attr.mdsthreshold;
1967 else
1968 kfree(opendata->f_attr.mdsthreshold);
1969 opendata->f_attr.mdsthreshold = NULL;
1970
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001971 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 *res = state;
1974 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001975err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001976 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001977 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001978err_put_state_owner:
1979 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 *res = NULL;
1982 return status;
1983}
1984
1985
Andy Adamson82be4172012-05-23 05:02:35 -04001986static struct nfs4_state *nfs4_do_open(struct inode *dir,
1987 struct dentry *dentry,
1988 fmode_t fmode,
1989 int flags,
1990 struct iattr *sattr,
1991 struct rpc_cred *cred,
1992 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct nfs4_exception exception = { };
1995 struct nfs4_state *res;
1996 int status;
1997
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001998 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002000 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2001 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (status == 0)
2003 break;
2004 /* NOTE: BAD_SEQID means the server and client disagree about the
2005 * book-keeping w.r.t. state-changing operations
2006 * (OPEN/CLOSE/LOCK/LOCKU...)
2007 * It is actually a sign of a bug on the client or on the server.
2008 *
2009 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002010 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 * have unhashed the old state_owner for us, and that we can
2012 * therefore safely retry using a new one. We should still warn
2013 * the user though...
2014 */
2015 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002016 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002017 " returned a bad sequence-id error!\n",
2018 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 exception.retry = 1;
2020 continue;
2021 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002022 /*
2023 * BAD_STATEID on OPEN means that the server cancelled our
2024 * state before it received the OPEN_CONFIRM.
2025 * Recover by retrying the request as per the discussion
2026 * on Page 181 of RFC3530.
2027 */
2028 if (status == -NFS4ERR_BAD_STATEID) {
2029 exception.retry = 1;
2030 continue;
2031 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002032 if (status == -EAGAIN) {
2033 /* We must have found a delegation */
2034 exception.retry = 1;
2035 continue;
2036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2038 status, &exception));
2039 } while (exception.retry);
2040 return res;
2041}
2042
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002043static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2044 struct nfs_fattr *fattr, struct iattr *sattr,
2045 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002047 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002049 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 .iap = sattr,
2051 .server = server,
2052 .bitmask = server->attr_bitmask,
2053 };
2054 struct nfs_setattrres res = {
2055 .fattr = fattr,
2056 .server = server,
2057 };
2058 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2060 .rpc_argp = &arg,
2061 .rpc_resp = &res,
2062 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002064 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002065 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Trond Myklebust0e574af2005-10-27 22:12:38 -04002067 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Trond Myklebust4fc87962012-03-08 17:42:01 -05002069 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002070 struct nfs_lockowner lockowner = {
2071 .l_owner = current->files,
2072 .l_pid = current->tgid,
2073 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002074 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002075 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002076 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2077 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002078 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002079 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002080 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Bryan Schumaker7c513052011-03-24 17:12:24 +00002082 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002083 if (status == 0 && state != NULL)
2084 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002085 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002088static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2089 struct nfs_fattr *fattr, struct iattr *sattr,
2090 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002092 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002093 struct nfs4_exception exception = {
2094 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002095 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002096 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 int err;
2098 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002099 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2100 switch (err) {
2101 case -NFS4ERR_OPENMODE:
2102 if (state && !(state->state & FMODE_WRITE)) {
2103 err = -EBADF;
2104 if (sattr->ia_valid & ATTR_OPEN)
2105 err = -EACCES;
2106 goto out;
2107 }
2108 }
2109 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002111out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return err;
2113}
2114
2115struct nfs4_closedata {
2116 struct inode *inode;
2117 struct nfs4_state *state;
2118 struct nfs_closeargs arg;
2119 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002120 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002121 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002122 bool roc;
2123 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124};
2125
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002126static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002127{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002128 struct nfs4_closedata *calldata = data;
2129 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002130 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002131
Fred Isamanf7e89172011-01-06 11:36:32 +00002132 if (calldata->roc)
2133 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002134 nfs4_put_open_state(calldata->state);
2135 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002136 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002137 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002138 kfree(calldata);
2139}
2140
Trond Myklebust88069f72009-12-08 08:33:16 -05002141static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2142 fmode_t fmode)
2143{
2144 spin_lock(&state->owner->so_lock);
2145 if (!(fmode & FMODE_READ))
2146 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2147 if (!(fmode & FMODE_WRITE))
2148 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2149 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2150 spin_unlock(&state->owner->so_lock);
2151}
2152
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002153static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002155 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct nfs_server *server = NFS_SERVER(calldata->inode);
2158
Chuck Levera3ca5652012-03-01 17:00:40 -05002159 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002160 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /* hmm. we are done with the inode, and in the process of freeing
2163 * the state_owner. we keep this around to process errors
2164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 switch (task->tk_status) {
2166 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002167 if (calldata->roc)
2168 pnfs_roc_set_barrier(state->inode,
2169 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002170 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002171 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002172 nfs4_close_clear_stateid_flags(state,
2173 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 break;
2175 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002176 case -NFS4ERR_OLD_STATEID:
2177 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002179 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002182 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2183 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002185 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002186 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002187 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002190static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002192 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002193 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002194 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002195 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002196
Chuck Levera3ca5652012-03-01 17:00:40 -05002197 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002198 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002199 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002200
Trond Myklebust88069f72009-12-08 08:33:16 -05002201 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2202 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002203 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002204 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002205 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002206 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002207 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2208 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2209 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002210 }
2211 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002212 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2213 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2214 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002215 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002216 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002217 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002218
2219 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002220 /* Note: exit _without_ calling nfs4_close_done */
2221 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002222 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002223 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002224
Fred Isamanf7e89172011-01-06 11:36:32 +00002225 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002226 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002227 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002228 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002229 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002230 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002231
Trond Myklebust516a6af2005-10-27 22:12:41 -04002232 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002233 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002234 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002235 &calldata->arg.seq_args,
2236 &calldata->res.seq_res,
2237 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002238 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002239 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002240out:
2241 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002244static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002245 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002246 .rpc_call_done = nfs4_close_done,
2247 .rpc_release = nfs4_free_closedata,
2248};
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250/*
2251 * It is possible for data to be read/written from a mem-mapped file
2252 * after the sys_close call (which hits the vfs layer as a flush).
2253 * This means that we can't safely call nfsv4 close on a file until
2254 * the inode is cleared. This in turn means that we are not good
2255 * NFSv4 citizens - we do not indicate to the server to update the file's
2256 * share state even when we are done with one of the three share
2257 * stateid's in the inode.
2258 *
2259 * NOTE: Caller must be holding the sp->so_owner semaphore!
2260 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002261int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002263 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002265 struct nfs4_state_owner *sp = state->owner;
2266 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002267 struct rpc_message msg = {
2268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2269 .rpc_cred = state->owner->so_cred,
2270 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002271 struct rpc_task_setup task_setup_data = {
2272 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002273 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002274 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002275 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002276 .flags = RPC_TASK_ASYNC,
2277 };
Trond Myklebust95121352005-10-18 14:20:12 -07002278 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002280 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002282 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002283 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002284 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002286 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002287 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002289 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002290 if (calldata->arg.seqid == NULL)
2291 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002292 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002293 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002294 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002295 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002296 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002297 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002298 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002299
Trond Myklebust1174dd12010-12-21 10:52:24 -05002300 msg.rpc_argp = &calldata->arg;
2301 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002302 task_setup_data.callback_data = calldata;
2303 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002304 if (IS_ERR(task))
2305 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002306 status = 0;
2307 if (wait)
2308 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002309 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002310 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002311out_free_calldata:
2312 kfree(calldata);
2313out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002314 nfs4_put_open_state(state);
2315 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002316 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Trond Myklebust2b484292010-09-17 10:56:51 -04002319static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002320nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct nfs4_state *state;
2323
Trond Myklebust565277f2007-10-15 18:17:53 -04002324 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002325 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2326 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002327 if (IS_ERR(state))
2328 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002329 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002330 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Trond Myklebust1185a552009-12-03 15:54:02 -05002333static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002334{
2335 if (ctx->state == NULL)
2336 return;
2337 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002338 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002339 else
Al Viro643168c2011-06-22 18:20:23 -04002340 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2344{
Benny Halevy43652ad2009-04-01 09:21:54 -04002345 struct nfs4_server_caps_arg args = {
2346 .fhandle = fhandle,
2347 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 struct nfs4_server_caps_res res = {};
2349 struct rpc_message msg = {
2350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002351 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 .rpc_resp = &res,
2353 };
2354 int status;
2355
Bryan Schumaker7c513052011-03-24 17:12:24 +00002356 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (status == 0) {
2358 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002359 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2360 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2361 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2362 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2363 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2365 server->caps |= NFS_CAP_ACLS;
2366 if (res.has_links != 0)
2367 server->caps |= NFS_CAP_HARDLINKS;
2368 if (res.has_symlinks != 0)
2369 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002370 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2371 server->caps |= NFS_CAP_FILEID;
2372 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2373 server->caps |= NFS_CAP_MODE;
2374 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2375 server->caps |= NFS_CAP_NLINK;
2376 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2377 server->caps |= NFS_CAP_OWNER;
2378 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2379 server->caps |= NFS_CAP_OWNER_GROUP;
2380 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2381 server->caps |= NFS_CAP_ATIME;
2382 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2383 server->caps |= NFS_CAP_CTIME;
2384 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2385 server->caps |= NFS_CAP_MTIME;
2386
Trond Myklebusta65318b2009-03-11 14:10:28 -04002387 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2388 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2389 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002391 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return status;
2395}
2396
Trond Myklebust55a97592006-06-09 09:34:19 -04002397int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
2399 struct nfs4_exception exception = { };
2400 int err;
2401 do {
2402 err = nfs4_handle_exception(server,
2403 _nfs4_server_capabilities(server, fhandle),
2404 &exception);
2405 } while (exception.retry);
2406 return err;
2407}
2408
2409static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2410 struct nfs_fsinfo *info)
2411{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 struct nfs4_lookup_root_arg args = {
2413 .bitmask = nfs4_fattr_bitmap,
2414 };
2415 struct nfs4_lookup_res res = {
2416 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002417 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 .fh = fhandle,
2419 };
2420 struct rpc_message msg = {
2421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2422 .rpc_argp = &args,
2423 .rpc_resp = &res,
2424 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002425
Trond Myklebust0e574af2005-10-27 22:12:38 -04002426 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002427 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
2430static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2431 struct nfs_fsinfo *info)
2432{
2433 struct nfs4_exception exception = { };
2434 int err;
2435 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002436 err = _nfs4_lookup_root(server, fhandle, info);
2437 switch (err) {
2438 case 0:
2439 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002440 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002441 default:
2442 err = nfs4_handle_exception(server, err, &exception);
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002445out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return err;
2447}
2448
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002449static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2450 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2451{
2452 struct rpc_auth *auth;
2453 int ret;
2454
2455 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002456 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002457 ret = -EIO;
2458 goto out;
2459 }
2460 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002461out:
2462 return ret;
2463}
2464
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002465static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002466 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002468 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002469 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002471 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2472 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002473
2474 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002475 /* AUTH_UNIX is the default flavor if none was specified,
2476 * thus has already been tried. */
2477 if (flav_array[i] == RPC_AUTH_UNIX)
2478 continue;
2479
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002480 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002481 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002482 continue;
2483 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002484 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002485 /*
2486 * -EACCESS could mean that the user doesn't have correct permissions
2487 * to access the mount. It could also mean that we tried to mount
2488 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2489 * existing mount programs don't handle -EACCES very well so it should
2490 * be mapped to -EPERM instead.
2491 */
2492 if (status == -EACCES)
2493 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002494 return status;
2495}
2496
2497/*
2498 * get the file handle for the "/" directory on the server
2499 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002500int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2501 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002502{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002503 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002504 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002505 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2506 /*
2507 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2508 * by nfs4_map_errors() as this function exits.
2509 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002510 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (status == 0)
2512 status = nfs4_server_capabilities(server, fhandle);
2513 if (status == 0)
2514 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002515 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
Bryan Schumakerbae36242012-05-10 15:07:31 -04002518static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2519 struct nfs_fsinfo *info)
2520{
2521 int error;
2522 struct nfs_fattr *fattr = info->fattr;
2523
2524 error = nfs4_server_capabilities(server, mntfh);
2525 if (error < 0) {
2526 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2527 return error;
2528 }
2529
2530 error = nfs4_proc_getattr(server, mntfh, fattr);
2531 if (error < 0) {
2532 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2533 return error;
2534 }
2535
2536 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2537 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2538 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2539
2540 return error;
2541}
2542
Manoj Naik6b97fd32006-06-09 09:34:29 -04002543/*
2544 * Get locations and (maybe) other attributes of a referral.
2545 * Note that we'll actually follow the referral later when
2546 * we detect fsid mismatch in inode revalidation
2547 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002548static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2549 const struct qstr *name, struct nfs_fattr *fattr,
2550 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002551{
2552 int status = -ENOMEM;
2553 struct page *page = NULL;
2554 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002555
2556 page = alloc_page(GFP_KERNEL);
2557 if (page == NULL)
2558 goto out;
2559 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2560 if (locations == NULL)
2561 goto out;
2562
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002563 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002564 if (status != 0)
2565 goto out;
2566 /* Make sure server returned a different fsid for the referral */
2567 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002568 dprintk("%s: server did not return a different fsid for"
2569 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002570 status = -EIO;
2571 goto out;
2572 }
Andy Adamson533eb462011-06-13 18:25:56 -04002573 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2574 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002575
Andy Adamson533eb462011-06-13 18:25:56 -04002576 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002577 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002578 memset(fhandle, 0, sizeof(struct nfs_fh));
2579out:
2580 if (page)
2581 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002582 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002583 return status;
2584}
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2587{
2588 struct nfs4_getattr_arg args = {
2589 .fh = fhandle,
2590 .bitmask = server->attr_bitmask,
2591 };
2592 struct nfs4_getattr_res res = {
2593 .fattr = fattr,
2594 .server = server,
2595 };
2596 struct rpc_message msg = {
2597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2598 .rpc_argp = &args,
2599 .rpc_resp = &res,
2600 };
2601
Trond Myklebust0e574af2005-10-27 22:12:38 -04002602 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002603 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
2606static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2607{
2608 struct nfs4_exception exception = { };
2609 int err;
2610 do {
2611 err = nfs4_handle_exception(server,
2612 _nfs4_proc_getattr(server, fhandle, fattr),
2613 &exception);
2614 } while (exception.retry);
2615 return err;
2616}
2617
2618/*
2619 * The file is not closed if it is opened due to the a request to change
2620 * the size of the file. The open call will not be needed once the
2621 * VFS layer lookup-intents are implemented.
2622 *
2623 * Close is called when the inode is destroyed.
2624 * If we haven't opened the file for O_WRONLY, we
2625 * need to in the size_change case to obtain a stateid.
2626 *
2627 * Got race?
2628 * Because OPEN is always done by name in nfsv4, it is
2629 * possible that we opened a different file by the same
2630 * name. We can recognize this race condition, but we
2631 * can't do anything about it besides returning an error.
2632 *
2633 * This will be fixed with VFS changes (lookup-intent).
2634 */
2635static int
2636nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2637 struct iattr *sattr)
2638{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002639 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002640 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002641 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 int status;
2643
Benny Halevy8a1636c2010-07-14 15:43:57 -04002644 if (pnfs_ld_layoutret_on_setattr(inode))
2645 pnfs_return_layout(inode);
2646
Trond Myklebust0e574af2005-10-27 22:12:38 -04002647 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Andy Adamson26699402012-05-30 16:12:24 -04002649 /* Deal with open(O_TRUNC) */
2650 if (sattr->ia_valid & ATTR_OPEN)
2651 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2652
2653 /* Optimization: if the end result is no change, don't RPC */
2654 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2655 return 0;
2656
Trond Myklebustd5308382005-11-04 15:33:38 -05002657 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002658 if (sattr->ia_valid & ATTR_FILE) {
2659 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002660
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002661 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002662 if (ctx) {
2663 cred = ctx->cred;
2664 state = ctx->state;
2665 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002666 }
2667
2668 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002669 if (status == 0)
2670 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return status;
2672}
2673
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002674static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2675 const struct qstr *name, struct nfs_fh *fhandle,
2676 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002677{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002678 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002679 int status;
2680 struct nfs4_lookup_arg args = {
2681 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002682 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002683 .name = name,
2684 };
2685 struct nfs4_lookup_res res = {
2686 .server = server,
2687 .fattr = fattr,
2688 .fh = fhandle,
2689 };
2690 struct rpc_message msg = {
2691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2692 .rpc_argp = &args,
2693 .rpc_resp = &res,
2694 };
2695
2696 nfs_fattr_init(fattr);
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002699 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 dprintk("NFS reply lookup: %d\n", status);
2701 return status;
2702}
2703
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002704static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002705{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002706 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002707 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002708 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2709 fattr->nlink = 2;
2710}
2711
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002712static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2713 struct qstr *name, struct nfs_fh *fhandle,
2714 struct nfs_fattr *fattr)
2715{
2716 struct nfs4_exception exception = { };
2717 struct rpc_clnt *client = *clnt;
2718 int err;
2719 do {
2720 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2721 switch (err) {
2722 case -NFS4ERR_BADNAME:
2723 err = -ENOENT;
2724 goto out;
2725 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002726 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002727 goto out;
2728 case -NFS4ERR_WRONGSEC:
2729 err = -EPERM;
2730 if (client != *clnt)
2731 goto out;
2732
2733 client = nfs4_create_sec_client(client, dir, name);
2734 if (IS_ERR(client))
2735 return PTR_ERR(client);
2736
2737 exception.retry = 1;
2738 break;
2739 default:
2740 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2741 }
2742 } while (exception.retry);
2743
2744out:
2745 if (err == 0)
2746 *clnt = client;
2747 else if (client != *clnt)
2748 rpc_shutdown_client(client);
2749
2750 return err;
2751}
2752
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002753static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002754 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002756 int status;
2757 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002758
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002759 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2760 if (client != NFS_CLIENT(dir)) {
2761 rpc_shutdown_client(client);
2762 nfs_fixup_secinfo_attributes(fattr);
2763 }
2764 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002767struct rpc_clnt *
2768nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2769 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2770{
2771 int status;
2772 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2773
2774 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2775 if (status < 0) {
2776 rpc_shutdown_client(client);
2777 return ERR_PTR(status);
2778 }
2779 return client;
2780}
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2783{
Trond Myklebust76b32992007-08-10 17:45:11 -04002784 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 struct nfs4_accessargs args = {
2786 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002787 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002789 struct nfs4_accessres res = {
2790 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002791 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 struct rpc_message msg = {
2793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2794 .rpc_argp = &args,
2795 .rpc_resp = &res,
2796 .rpc_cred = entry->cred,
2797 };
2798 int mode = entry->mask;
2799 int status;
2800
2801 /*
2802 * Determine which access bits we want to ask for...
2803 */
2804 if (mode & MAY_READ)
2805 args.access |= NFS4_ACCESS_READ;
2806 if (S_ISDIR(inode->i_mode)) {
2807 if (mode & MAY_WRITE)
2808 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2809 if (mode & MAY_EXEC)
2810 args.access |= NFS4_ACCESS_LOOKUP;
2811 } else {
2812 if (mode & MAY_WRITE)
2813 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2814 if (mode & MAY_EXEC)
2815 args.access |= NFS4_ACCESS_EXECUTE;
2816 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002817
2818 res.fattr = nfs_alloc_fattr();
2819 if (res.fattr == NULL)
2820 return -ENOMEM;
2821
Bryan Schumaker7c513052011-03-24 17:12:24 +00002822 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002824 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002825 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002827 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return status;
2829}
2830
2831static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2832{
2833 struct nfs4_exception exception = { };
2834 int err;
2835 do {
2836 err = nfs4_handle_exception(NFS_SERVER(inode),
2837 _nfs4_proc_access(inode, entry),
2838 &exception);
2839 } while (exception.retry);
2840 return err;
2841}
2842
2843/*
2844 * TODO: For the time being, we don't try to get any attributes
2845 * along with any of the zero-copy operations READ, READDIR,
2846 * READLINK, WRITE.
2847 *
2848 * In the case of the first three, we want to put the GETATTR
2849 * after the read-type operation -- this is because it is hard
2850 * to predict the length of a GETATTR response in v4, and thus
2851 * align the READ data correctly. This means that the GETATTR
2852 * may end up partially falling into the page cache, and we should
2853 * shift it into the 'tail' of the xdr_buf before processing.
2854 * To do this efficiently, we need to know the total length
2855 * of data received, which doesn't seem to be available outside
2856 * of the RPC layer.
2857 *
2858 * In the case of WRITE, we also want to put the GETATTR after
2859 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002860 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 *
2862 * Both of these changes to the XDR layer would in fact be quite
2863 * minor, but I decided to leave them for a subsequent patch.
2864 */
2865static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2866 unsigned int pgbase, unsigned int pglen)
2867{
2868 struct nfs4_readlink args = {
2869 .fh = NFS_FH(inode),
2870 .pgbase = pgbase,
2871 .pglen = pglen,
2872 .pages = &page,
2873 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002874 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2877 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002878 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 };
2880
Bryan Schumaker7c513052011-03-24 17:12:24 +00002881 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 -07002882}
2883
2884static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2885 unsigned int pgbase, unsigned int pglen)
2886{
2887 struct nfs4_exception exception = { };
2888 int err;
2889 do {
2890 err = nfs4_handle_exception(NFS_SERVER(inode),
2891 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2892 &exception);
2893 } while (exception.retry);
2894 return err;
2895}
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002898 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900static int
2901nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002902 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002904 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 int status = 0;
2907
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002908 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2909 if (IS_ERR(ctx))
2910 return PTR_ERR(ctx);
2911
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002912 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002913 state = nfs4_do_open(dir, dentry, ctx->mode,
2914 flags, sattr, ctx->cred,
2915 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002916 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (IS_ERR(state)) {
2918 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002921 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002922 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002923 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002925 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return status;
2927}
2928
2929static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2930{
Trond Myklebust16e42952005-10-27 22:12:44 -04002931 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002932 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002934 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002936 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002937 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002938 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2941 .rpc_argp = &args,
2942 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002944 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002945
Bryan Schumaker7c513052011-03-24 17:12:24 +00002946 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002947 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002948 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return status;
2950}
2951
2952static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2953{
2954 struct nfs4_exception exception = { };
2955 int err;
2956 do {
2957 err = nfs4_handle_exception(NFS_SERVER(dir),
2958 _nfs4_proc_remove(dir, name),
2959 &exception);
2960 } while (exception.retry);
2961 return err;
2962}
2963
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002964static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002966 struct nfs_server *server = NFS_SERVER(dir);
2967 struct nfs_removeargs *args = msg->rpc_argp;
2968 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002970 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002972 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002975static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2976{
2977 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2978 &data->args.seq_args,
2979 &data->res.seq_res,
2980 task))
2981 return;
2982 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002985static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002987 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2988
Trond Myklebust14516c32010-07-31 14:29:06 -04002989 if (!nfs4_sequence_done(task, &res->seq_res))
2990 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002991 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002992 return 0;
2993 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002994 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
Jeff Laytond3d41522010-09-17 17:31:57 -04002997static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2998{
2999 struct nfs_server *server = NFS_SERVER(dir);
3000 struct nfs_renameargs *arg = msg->rpc_argp;
3001 struct nfs_renameres *res = msg->rpc_resp;
3002
3003 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003004 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003005 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003006}
3007
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003008static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3009{
3010 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3011 &data->args.seq_args,
3012 &data->res.seq_res,
3013 task))
3014 return;
3015 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003016}
3017
3018static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3019 struct inode *new_dir)
3020{
3021 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3022
3023 if (!nfs4_sequence_done(task, &res->seq_res))
3024 return 0;
3025 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3026 return 0;
3027
3028 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003029 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003030 return 1;
3031}
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3034 struct inode *new_dir, struct qstr *new_name)
3035{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003036 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003037 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 .old_dir = NFS_FH(old_dir),
3039 .new_dir = NFS_FH(new_dir),
3040 .old_name = old_name,
3041 .new_name = new_name,
3042 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003043 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003044 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003045 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 struct rpc_message msg = {
3047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3048 .rpc_argp = &arg,
3049 .rpc_resp = &res,
3050 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003051 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Bryan Schumaker7c513052011-03-24 17:12:24 +00003053 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 if (!status) {
3055 update_changeattr(old_dir, &res.old_cinfo);
3056 update_changeattr(new_dir, &res.new_cinfo);
3057 }
3058 return status;
3059}
3060
3061static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3062 struct inode *new_dir, struct qstr *new_name)
3063{
3064 struct nfs4_exception exception = { };
3065 int err;
3066 do {
3067 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3068 _nfs4_proc_rename(old_dir, old_name,
3069 new_dir, new_name),
3070 &exception);
3071 } while (exception.retry);
3072 return err;
3073}
3074
3075static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3076{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003077 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 struct nfs4_link_arg arg = {
3079 .fh = NFS_FH(inode),
3080 .dir_fh = NFS_FH(dir),
3081 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003082 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003084 struct nfs4_link_res res = {
3085 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003086 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 struct rpc_message msg = {
3088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3089 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003090 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003092 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Trond Myklebust136f2622010-04-16 16:22:49 -04003094 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003095 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003096 goto out;
3097
Bryan Schumaker7c513052011-03-24 17:12:24 +00003098 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003099 if (!status) {
3100 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003101 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003102 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003103out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003104 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return status;
3106}
3107
3108static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3109{
3110 struct nfs4_exception exception = { };
3111 int err;
3112 do {
3113 err = nfs4_handle_exception(NFS_SERVER(inode),
3114 _nfs4_proc_link(inode, dir, name),
3115 &exception);
3116 } while (exception.retry);
3117 return err;
3118}
3119
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003120struct nfs4_createdata {
3121 struct rpc_message msg;
3122 struct nfs4_create_arg arg;
3123 struct nfs4_create_res res;
3124 struct nfs_fh fh;
3125 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003126};
3127
3128static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3129 struct qstr *name, struct iattr *sattr, u32 ftype)
3130{
3131 struct nfs4_createdata *data;
3132
3133 data = kzalloc(sizeof(*data), GFP_KERNEL);
3134 if (data != NULL) {
3135 struct nfs_server *server = NFS_SERVER(dir);
3136
3137 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3138 data->msg.rpc_argp = &data->arg;
3139 data->msg.rpc_resp = &data->res;
3140 data->arg.dir_fh = NFS_FH(dir);
3141 data->arg.server = server;
3142 data->arg.name = name;
3143 data->arg.attrs = sattr;
3144 data->arg.ftype = ftype;
3145 data->arg.bitmask = server->attr_bitmask;
3146 data->res.server = server;
3147 data->res.fh = &data->fh;
3148 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003149 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003150 }
3151 return data;
3152}
3153
3154static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3155{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003156 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003157 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003158 if (status == 0) {
3159 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003160 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3161 }
3162 return status;
3163}
3164
3165static void nfs4_free_createdata(struct nfs4_createdata *data)
3166{
3167 kfree(data);
3168}
3169
Chuck Lever4f390c12006-08-22 20:06:22 -04003170static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003171 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003173 struct nfs4_createdata *data;
3174 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Chuck Lever94a6d752006-08-22 20:06:23 -04003176 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003177 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003178
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003179 status = -ENOMEM;
3180 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3181 if (data == NULL)
3182 goto out;
3183
3184 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3185 data->arg.u.symlink.pages = &page;
3186 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003188 status = nfs4_do_create(dir, dentry, data);
3189
3190 nfs4_free_createdata(data);
3191out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return status;
3193}
3194
Chuck Lever4f390c12006-08-22 20:06:22 -04003195static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003196 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct nfs4_exception exception = { };
3199 int err;
3200 do {
3201 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003202 _nfs4_proc_symlink(dir, dentry, page,
3203 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 &exception);
3205 } while (exception.retry);
3206 return err;
3207}
3208
3209static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3210 struct iattr *sattr)
3211{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003212 struct nfs4_createdata *data;
3213 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003215 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3216 if (data == NULL)
3217 goto out;
3218
3219 status = nfs4_do_create(dir, dentry, data);
3220
3221 nfs4_free_createdata(data);
3222out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return status;
3224}
3225
3226static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3227 struct iattr *sattr)
3228{
3229 struct nfs4_exception exception = { };
3230 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003231
3232 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 do {
3234 err = nfs4_handle_exception(NFS_SERVER(dir),
3235 _nfs4_proc_mkdir(dir, dentry, sattr),
3236 &exception);
3237 } while (exception.retry);
3238 return err;
3239}
3240
3241static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003242 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
3244 struct inode *dir = dentry->d_inode;
3245 struct nfs4_readdir_arg args = {
3246 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003247 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 .pgbase = 0,
3249 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003250 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003251 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 };
3253 struct nfs4_readdir_res res;
3254 struct rpc_message msg = {
3255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3256 .rpc_argp = &args,
3257 .rpc_resp = &res,
3258 .rpc_cred = cred,
3259 };
3260 int status;
3261
Harvey Harrison3110ff82008-05-02 13:42:44 -07003262 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003263 dentry->d_parent->d_name.name,
3264 dentry->d_name.name,
3265 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003266 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003268 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 -05003269 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003270 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003271 status += args.pgbase;
3272 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003273
3274 nfs_invalidate_atime(dir);
3275
Harvey Harrison3110ff82008-05-02 13:42:44 -07003276 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return status;
3278}
3279
3280static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003281 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
3283 struct nfs4_exception exception = { };
3284 int err;
3285 do {
3286 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3287 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003288 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 &exception);
3290 } while (exception.retry);
3291 return err;
3292}
3293
3294static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3295 struct iattr *sattr, dev_t rdev)
3296{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003297 struct nfs4_createdata *data;
3298 int mode = sattr->ia_mode;
3299 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
3301 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3302 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003303
3304 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3305 if (data == NULL)
3306 goto out;
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003309 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003311 data->arg.ftype = NF4BLK;
3312 data->arg.u.device.specdata1 = MAJOR(rdev);
3313 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
3315 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003316 data->arg.ftype = NF4CHR;
3317 data->arg.u.device.specdata1 = MAJOR(rdev);
3318 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003321 status = nfs4_do_create(dir, dentry, data);
3322
3323 nfs4_free_createdata(data);
3324out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return status;
3326}
3327
3328static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3329 struct iattr *sattr, dev_t rdev)
3330{
3331 struct nfs4_exception exception = { };
3332 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003333
3334 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 do {
3336 err = nfs4_handle_exception(NFS_SERVER(dir),
3337 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3338 &exception);
3339 } while (exception.retry);
3340 return err;
3341}
3342
3343static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3344 struct nfs_fsstat *fsstat)
3345{
3346 struct nfs4_statfs_arg args = {
3347 .fh = fhandle,
3348 .bitmask = server->attr_bitmask,
3349 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003350 struct nfs4_statfs_res res = {
3351 .fsstat = fsstat,
3352 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 struct rpc_message msg = {
3354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3355 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003356 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 };
3358
Trond Myklebust0e574af2005-10-27 22:12:38 -04003359 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003360 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
3363static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3364{
3365 struct nfs4_exception exception = { };
3366 int err;
3367 do {
3368 err = nfs4_handle_exception(server,
3369 _nfs4_proc_statfs(server, fhandle, fsstat),
3370 &exception);
3371 } while (exception.retry);
3372 return err;
3373}
3374
3375static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3376 struct nfs_fsinfo *fsinfo)
3377{
3378 struct nfs4_fsinfo_arg args = {
3379 .fh = fhandle,
3380 .bitmask = server->attr_bitmask,
3381 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003382 struct nfs4_fsinfo_res res = {
3383 .fsinfo = fsinfo,
3384 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 struct rpc_message msg = {
3386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3387 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003388 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 };
3390
Bryan Schumaker7c513052011-03-24 17:12:24 +00003391 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
3394static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3395{
3396 struct nfs4_exception exception = { };
3397 int err;
3398
3399 do {
3400 err = nfs4_handle_exception(server,
3401 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3402 &exception);
3403 } while (exception.retry);
3404 return err;
3405}
3406
3407static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3408{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003409 int error;
3410
Trond Myklebust0e574af2005-10-27 22:12:38 -04003411 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003412 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003413 if (error == 0) {
3414 /* block layout checks this! */
3415 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003416 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003417 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003418
3419 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
3421
3422static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3423 struct nfs_pathconf *pathconf)
3424{
3425 struct nfs4_pathconf_arg args = {
3426 .fh = fhandle,
3427 .bitmask = server->attr_bitmask,
3428 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003429 struct nfs4_pathconf_res res = {
3430 .pathconf = pathconf,
3431 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3434 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003435 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 };
3437
3438 /* None of the pathconf attributes are mandatory to implement */
3439 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3440 memset(pathconf, 0, sizeof(*pathconf));
3441 return 0;
3442 }
3443
Trond Myklebust0e574af2005-10-27 22:12:38 -04003444 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003445 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
3447
3448static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3449 struct nfs_pathconf *pathconf)
3450{
3451 struct nfs4_exception exception = { };
3452 int err;
3453
3454 do {
3455 err = nfs4_handle_exception(server,
3456 _nfs4_proc_pathconf(server, fhandle, pathconf),
3457 &exception);
3458 } while (exception.retry);
3459 return err;
3460}
3461
Benny Halevyd20581a2011-05-22 19:52:03 +03003462void __nfs4_read_done_cb(struct nfs_read_data *data)
3463{
Fred Isamancd841602012-04-20 14:47:44 -04003464 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003465}
3466
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003467static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Fred Isamancd841602012-04-20 14:47:44 -04003469 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003471 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003472 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003473 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003475
Benny Halevyd20581a2011-05-22 19:52:03 +03003476 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003478 renew_lease(server, data->timestamp);
3479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480}
3481
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003482static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3483{
3484
3485 dprintk("--> %s\n", __func__);
3486
3487 if (!nfs4_sequence_done(task, &data->res.seq_res))
3488 return -EAGAIN;
3489
Benny Halevyd20581a2011-05-22 19:52:03 +03003490 return data->read_done_cb ? data->read_done_cb(task, data) :
3491 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003492}
3493
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003494static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003497 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003498 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003499 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500}
3501
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003502static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3503{
Fred Isamancd841602012-04-20 14:47:44 -04003504 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003505 &data->args.seq_args,
3506 &data->res.seq_res,
3507 task))
3508 return;
3509 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Fred Isamanb029bc92011-03-03 15:13:42 +00003512static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Fred Isamancd841602012-04-20 14:47:44 -04003514 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003516 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003517 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003518 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003520 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003522 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003523 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
Fred Isamanb029bc92011-03-03 15:13:42 +00003527static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3528{
3529 if (!nfs4_sequence_done(task, &data->res.seq_res))
3530 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003531 return data->write_done_cb ? data->write_done_cb(task, data) :
3532 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003533}
3534
Trond Myklebust5a37f852012-04-28 14:55:16 -04003535static
3536bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003537{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003538 const struct nfs_pgio_header *hdr = data->header;
3539
3540 /* Don't request attributes for pNFS or O_DIRECT writes */
3541 if (data->ds_clp != NULL || hdr->dreq != NULL)
3542 return false;
3543 /* Otherwise, request attributes if and only if we don't hold
3544 * a delegation
3545 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003546 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003547}
Fred Isamana69aef12011-03-03 15:13:47 +00003548
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003549static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
Fred Isamancd841602012-04-20 14:47:44 -04003551 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003552
Trond Myklebust5a37f852012-04-28 14:55:16 -04003553 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003554 data->args.bitmask = NULL;
3555 data->res.fattr = NULL;
3556 } else
3557 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003558
Fred Isamanb029bc92011-03-03 15:13:42 +00003559 if (!data->write_done_cb)
3560 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003561 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 data->timestamp = jiffies;
3563
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
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
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003568static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3569{
Fred Isamancd841602012-04-20 14:47:44 -04003570 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003571 &data->args.seq_args,
3572 &data->res.seq_res,
3573 task))
3574 return;
3575 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Fred Isaman0b7c0152012-04-20 14:47:39 -04003578static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3579{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3581 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003582 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return;
3585 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003586}
3587
Fred Isaman0b7c0152012-04-20 14:47:39 -04003588static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
3590 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003591
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003592 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003593 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003594 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
Fred Isaman0b7c0152012-04-20 14:47:39 -04003599static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003600{
3601 if (!nfs4_sequence_done(task, &data->res.seq_res))
3602 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003603 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003604}
3605
Fred Isaman0b7c0152012-04-20 14:47:39 -04003606static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003608 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003609
Fred Isaman0b7c0152012-04-20 14:47:39 -04003610 if (data->commit_done_cb == NULL)
3611 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003612 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003613 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003614 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003617struct nfs4_renewdata {
3618 struct nfs_client *client;
3619 unsigned long timestamp;
3620};
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622/*
3623 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3624 * standalone procedure for queueing an asynchronous RENEW.
3625 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003626static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003627{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003628 struct nfs4_renewdata *data = calldata;
3629 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003630
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003631 if (atomic_read(&clp->cl_count) > 1)
3632 nfs4_schedule_state_renewal(clp);
3633 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003634 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003635}
3636
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003637static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003639 struct nfs4_renewdata *data = calldata;
3640 struct nfs_client *clp = data->client;
3641 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
3643 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003644 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003645 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3646 return;
3647 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003648 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003649 return;
3650 }
3651 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003653 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003656static const struct rpc_call_ops nfs4_renew_ops = {
3657 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003658 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003659};
3660
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003661static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
3663 struct rpc_message msg = {
3664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3665 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003666 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003668 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003670 if (renew_flags == 0)
3671 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003672 if (!atomic_inc_not_zero(&clp->cl_count))
3673 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003674 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003675 if (data == NULL)
3676 return -ENOMEM;
3677 data->client = clp;
3678 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003680 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003683static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
3685 struct rpc_message msg = {
3686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3687 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003688 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 };
3690 unsigned long now = jiffies;
3691 int status;
3692
3693 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3694 if (status < 0)
3695 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003696 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return 0;
3698}
3699
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003700static inline int nfs4_server_supports_acls(struct nfs_server *server)
3701{
3702 return (server->caps & NFS_CAP_ACLS)
3703 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3704 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3705}
3706
Trond Myklebust21f498c2012-08-24 10:59:25 -04003707/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3708 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003709 * the stack.
3710 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003711#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003712
Neil Hormane9e3d722011-03-04 19:26:03 -05003713static int buf_to_pages_noslab(const void *buf, size_t buflen,
3714 struct page **pages, unsigned int *pgbase)
3715{
3716 struct page *newpage, **spages;
3717 int rc = 0;
3718 size_t len;
3719 spages = pages;
3720
3721 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003722 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003723 newpage = alloc_page(GFP_KERNEL);
3724
3725 if (newpage == NULL)
3726 goto unwind;
3727 memcpy(page_address(newpage), buf, len);
3728 buf += len;
3729 buflen -= len;
3730 *pages++ = newpage;
3731 rc++;
3732 } while (buflen != 0);
3733
3734 return rc;
3735
3736unwind:
3737 for(; rc > 0; rc--)
3738 __free_page(spages[rc-1]);
3739 return -ENOMEM;
3740}
3741
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003742struct nfs4_cached_acl {
3743 int cached;
3744 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003745 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003746};
3747
3748static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003749{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003750 struct nfs_inode *nfsi = NFS_I(inode);
3751
3752 spin_lock(&inode->i_lock);
3753 kfree(nfsi->nfs4_acl);
3754 nfsi->nfs4_acl = acl;
3755 spin_unlock(&inode->i_lock);
3756}
3757
3758static void nfs4_zap_acl_attr(struct inode *inode)
3759{
3760 nfs4_set_cached_acl(inode, NULL);
3761}
3762
3763static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3764{
3765 struct nfs_inode *nfsi = NFS_I(inode);
3766 struct nfs4_cached_acl *acl;
3767 int ret = -ENOENT;
3768
3769 spin_lock(&inode->i_lock);
3770 acl = nfsi->nfs4_acl;
3771 if (acl == NULL)
3772 goto out;
3773 if (buf == NULL) /* user is just asking for length */
3774 goto out_len;
3775 if (acl->cached == 0)
3776 goto out;
3777 ret = -ERANGE; /* see getxattr(2) man page */
3778 if (acl->len > buflen)
3779 goto out;
3780 memcpy(buf, acl->data, acl->len);
3781out_len:
3782 ret = acl->len;
3783out:
3784 spin_unlock(&inode->i_lock);
3785 return ret;
3786}
3787
Sachin Prabhu5794d212012-04-17 14:36:40 +01003788static 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 +00003789{
3790 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003791 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003792
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003793 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003794 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003795 if (acl == NULL)
3796 goto out;
3797 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003798 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003799 } else {
3800 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3801 if (acl == NULL)
3802 goto out;
3803 acl->cached = 0;
3804 }
3805 acl->len = acl_len;
3806out:
3807 nfs4_set_cached_acl(inode, acl);
3808}
3809
Andy Adamsonbf118a32011-12-07 11:55:27 -05003810/*
3811 * The getxattr API returns the required buffer length when called with a
3812 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3813 * the required buf. On a NULL buf, we send a page of data to the server
3814 * guessing that the ACL request can be serviced by a page. If so, we cache
3815 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3816 * the cache. If not so, we throw away the page, and cache the required
3817 * length. The next getxattr call will then produce another round trip to
3818 * the server, this time with the input buf of the required size.
3819 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003820static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003821{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003822 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003823 struct nfs_getaclargs args = {
3824 .fh = NFS_FH(inode),
3825 .acl_pages = pages,
3826 .acl_len = buflen,
3827 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003828 struct nfs_getaclres res = {
3829 .acl_len = buflen,
3830 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003831 struct rpc_message msg = {
3832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3833 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003834 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003835 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003836 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3837 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003838
Andy Adamsonbf118a32011-12-07 11:55:27 -05003839 /* As long as we're doing a round trip to the server anyway,
3840 * let's be prepared for a page of acl data. */
3841 if (npages == 0)
3842 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003843 if (npages > ARRAY_SIZE(pages))
3844 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003845
Andy Adamsonbf118a32011-12-07 11:55:27 -05003846 for (i = 0; i < npages; i++) {
3847 pages[i] = alloc_page(GFP_KERNEL);
3848 if (!pages[i])
3849 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003850 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003851
3852 /* for decoding across pages */
3853 res.acl_scratch = alloc_page(GFP_KERNEL);
3854 if (!res.acl_scratch)
3855 goto out_free;
3856
Andy Adamsonbf118a32011-12-07 11:55:27 -05003857 args.acl_len = npages * PAGE_SIZE;
3858 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003859
Peng Taode040be2012-01-10 22:42:47 +08003860 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003861 __func__, buf, buflen, npages, args.acl_len);
3862 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3863 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003864 if (ret)
3865 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003866
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003867 /* Handle the case where the passed-in buffer is too short */
3868 if (res.acl_flags & NFS4_ACL_TRUNC) {
3869 /* Did the user only issue a request for the acl length? */
3870 if (buf == NULL)
3871 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003872 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003873 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003874 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003875 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3876 if (buf)
3877 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3878out_ok:
3879 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003880out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003881 for (i = 0; i < npages; i++)
3882 if (pages[i])
3883 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003884 if (res.acl_scratch)
3885 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003886 return ret;
3887}
3888
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003889static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3890{
3891 struct nfs4_exception exception = { };
3892 ssize_t ret;
3893 do {
3894 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3895 if (ret >= 0)
3896 break;
3897 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3898 } while (exception.retry);
3899 return ret;
3900}
3901
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003902static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3903{
3904 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003905 int ret;
3906
3907 if (!nfs4_server_supports_acls(server))
3908 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003909 ret = nfs_revalidate_inode(server, inode);
3910 if (ret < 0)
3911 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003912 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3913 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003914 ret = nfs4_read_cached_acl(inode, buf, buflen);
3915 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003916 /* -ENOENT is returned if there is no ACL or if there is an ACL
3917 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003918 return ret;
3919 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003920}
3921
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003922static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003923{
3924 struct nfs_server *server = NFS_SERVER(inode);
3925 struct page *pages[NFS4ACL_MAXPAGES];
3926 struct nfs_setaclargs arg = {
3927 .fh = NFS_FH(inode),
3928 .acl_pages = pages,
3929 .acl_len = buflen,
3930 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003931 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003932 struct rpc_message msg = {
3933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3934 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003935 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003936 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003937 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003938 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003939
3940 if (!nfs4_server_supports_acls(server))
3941 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003942 if (npages > ARRAY_SIZE(pages))
3943 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003944 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3945 if (i < 0)
3946 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003947 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003948 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003949
3950 /*
3951 * Free each page after tx, so the only ref left is
3952 * held by the network stack
3953 */
3954 for (; i > 0; i--)
3955 put_page(pages[i-1]);
3956
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003957 /*
3958 * Acl update can result in inode attribute update.
3959 * so mark the attribute cache invalid.
3960 */
3961 spin_lock(&inode->i_lock);
3962 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3963 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003964 nfs_access_zap_cache(inode);
3965 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003966 return ret;
3967}
3968
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003969static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3970{
3971 struct nfs4_exception exception = { };
3972 int err;
3973 do {
3974 err = nfs4_handle_exception(NFS_SERVER(inode),
3975 __nfs4_proc_set_acl(inode, buf, buflen),
3976 &exception);
3977 } while (exception.retry);
3978 return err;
3979}
3980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003982nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003984 struct nfs_client *clp = server->nfs_client;
3985
3986 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 return 0;
3988 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003989 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003990 case -NFS4ERR_ADMIN_REVOKED:
3991 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003992 if (state == NULL)
3993 break;
3994 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003995 case -NFS4ERR_OPENMODE:
3996 if (state == NULL)
3997 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003998 nfs4_schedule_stateid_recovery(server, state);
3999 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004000 case -NFS4ERR_EXPIRED:
4001 if (state != NULL)
4002 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004004 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004005 nfs4_schedule_lease_recovery(clp);
4006 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004007#if defined(CONFIG_NFS_V4_1)
4008 case -NFS4ERR_BADSESSION:
4009 case -NFS4ERR_BADSLOT:
4010 case -NFS4ERR_BAD_HIGH_SLOT:
4011 case -NFS4ERR_DEADSESSION:
4012 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4013 case -NFS4ERR_SEQ_FALSE_RETRY:
4014 case -NFS4ERR_SEQ_MISORDERED:
4015 dprintk("%s ERROR %d, Reset session\n", __func__,
4016 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004017 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004018 task->tk_status = 0;
4019 return -EAGAIN;
4020#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004022 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004023 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004024 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4026 task->tk_status = 0;
4027 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004028 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 case -NFS4ERR_OLD_STATEID:
4030 task->tk_status = 0;
4031 return -EAGAIN;
4032 }
4033 task->tk_status = nfs4_map_errors(task->tk_status);
4034 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004035wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004036 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004037 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4038 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4039 task->tk_status = 0;
4040 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Chuck Leverf0920752012-05-21 22:45:41 -04004043static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4044 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004045{
4046 __be32 verf[2];
4047
Chuck Lever2c820d92012-05-21 22:45:33 -04004048 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4049 /* An impossible timestamp guarantees this value
4050 * will never match a generated boot time. */
4051 verf[0] = 0;
4052 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4053 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004054 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4055 verf[0] = (__be32)nn->boot_time.tv_sec;
4056 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004057 }
Chuck Levercd937102012-03-02 17:14:31 -05004058 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4059}
4060
Chuck Levere984a552012-09-14 17:24:21 -04004061static unsigned int
4062nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4063 char *buf, size_t len)
4064{
4065 unsigned int result;
4066
4067 rcu_read_lock();
4068 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4069 clp->cl_ipaddr,
4070 rpc_peeraddr2str(clp->cl_rpcclient,
4071 RPC_DISPLAY_ADDR),
4072 rpc_peeraddr2str(clp->cl_rpcclient,
4073 RPC_DISPLAY_PROTO));
4074 rcu_read_unlock();
4075 return result;
4076}
4077
4078static unsigned int
4079nfs4_init_uniform_client_string(const struct nfs_client *clp,
4080 char *buf, size_t len)
4081{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004082 char *nodename = clp->cl_rpcclient->cl_nodename;
4083
4084 if (nfs4_client_id_uniquifier[0] != '\0')
4085 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004086 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4087 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004088 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004089}
4090
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004091/**
4092 * nfs4_proc_setclientid - Negotiate client ID
4093 * @clp: state data structure
4094 * @program: RPC program for NFSv4 callback service
4095 * @port: IP port number for NFS4 callback service
4096 * @cred: RPC credential to use for this call
4097 * @res: where to place the result
4098 *
4099 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4100 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004101int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4102 unsigned short port, struct rpc_cred *cred,
4103 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 nfs4_verifier sc_verifier;
4106 struct nfs4_setclientid setclientid = {
4107 .sc_verifier = &sc_verifier,
4108 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004109 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 };
4111 struct rpc_message msg = {
4112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4113 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004114 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004115 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004117 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Chuck Leverde734832012-07-11 16:30:50 -04004119 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004120 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004121 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4122 setclientid.sc_name_len =
4123 nfs4_init_uniform_client_string(clp,
4124 setclientid.sc_name,
4125 sizeof(setclientid.sc_name));
4126 else
4127 setclientid.sc_name_len =
4128 nfs4_init_nonuniform_client_string(clp,
4129 setclientid.sc_name,
4130 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004131 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004132 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004133 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004134 sizeof(setclientid.sc_netid),
4135 rpc_peeraddr2str(clp->cl_rpcclient,
4136 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004137 rcu_read_unlock();
4138 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004139 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 clp->cl_ipaddr, port >> 8, port & 255);
4141
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004142 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4143 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4144 setclientid.sc_name_len, setclientid.sc_name);
4145 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4146 dprintk("NFS reply setclientid: %d\n", status);
4147 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148}
4149
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004150/**
4151 * nfs4_proc_setclientid_confirm - Confirm client ID
4152 * @clp: state data structure
4153 * @res: result of a previous SETCLIENTID
4154 * @cred: RPC credential to use for this call
4155 *
4156 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4157 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004158int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004159 struct nfs4_setclientid_res *arg,
4160 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
4162 struct nfs_fsinfo fsinfo;
4163 struct rpc_message msg = {
4164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004165 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004167 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 };
4169 unsigned long now;
4170 int status;
4171
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004172 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4173 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4174 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004176 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 if (status == 0) {
4178 spin_lock(&clp->cl_lock);
4179 clp->cl_lease_time = fsinfo.lease_time * HZ;
4180 clp->cl_last_renewal = now;
4181 spin_unlock(&clp->cl_lock);
4182 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004183 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return status;
4185}
4186
Trond Myklebustfe650402006-01-03 09:55:18 +01004187struct nfs4_delegreturndata {
4188 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004189 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004190 struct nfs_fh fh;
4191 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004192 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004193 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004194 int rpc_status;
4195};
4196
Trond Myklebustfe650402006-01-03 09:55:18 +01004197static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4198{
4199 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004200
Trond Myklebust14516c32010-07-31 14:29:06 -04004201 if (!nfs4_sequence_done(task, &data->res.seq_res))
4202 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004203
Ricardo Labiaga79708862009-12-07 09:23:21 -05004204 switch (task->tk_status) {
4205 case -NFS4ERR_STALE_STATEID:
4206 case -NFS4ERR_EXPIRED:
4207 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004208 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004209 break;
4210 default:
4211 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4212 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004213 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004214 return;
4215 }
4216 }
4217 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004218}
4219
4220static void nfs4_delegreturn_release(void *calldata)
4221{
Trond Myklebustfe650402006-01-03 09:55:18 +01004222 kfree(calldata);
4223}
4224
Andy Adamson938e1012009-04-01 09:22:28 -04004225#if defined(CONFIG_NFS_V4_1)
4226static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4227{
4228 struct nfs4_delegreturndata *d_data;
4229
4230 d_data = (struct nfs4_delegreturndata *)data;
4231
Trond Myklebust035168a2010-06-16 09:52:26 -04004232 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004233 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004234 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004235 return;
4236 rpc_call_start(task);
4237}
4238#endif /* CONFIG_NFS_V4_1 */
4239
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004240static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004241#if defined(CONFIG_NFS_V4_1)
4242 .rpc_call_prepare = nfs4_delegreturn_prepare,
4243#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004244 .rpc_call_done = nfs4_delegreturn_done,
4245 .rpc_release = nfs4_delegreturn_release,
4246};
4247
Trond Myklebuste6f81072008-01-24 18:14:34 -05004248static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004249{
4250 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004251 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004252 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004253 struct rpc_message msg = {
4254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4255 .rpc_cred = cred,
4256 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004257 struct rpc_task_setup task_setup_data = {
4258 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004259 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004260 .callback_ops = &nfs4_delegreturn_ops,
4261 .flags = RPC_TASK_ASYNC,
4262 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004263 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004264
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004265 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004266 if (data == NULL)
4267 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004268 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004269 data->args.fhandle = &data->fh;
4270 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004271 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004272 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004273 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004274 data->res.fattr = &data->fattr;
4275 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004276 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004277 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004278 data->rpc_status = 0;
4279
Trond Myklebustc970aa82007-07-14 15:39:59 -04004280 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004281 msg.rpc_argp = &data->args;
4282 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004283 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004284 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004285 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004286 if (!issync)
4287 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004288 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004289 if (status != 0)
4290 goto out;
4291 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004292 if (status == 0)
4293 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4294 else
4295 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004296out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004297 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004298 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
Trond Myklebuste6f81072008-01-24 18:14:34 -05004301int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
4303 struct nfs_server *server = NFS_SERVER(inode);
4304 struct nfs4_exception exception = { };
4305 int err;
4306 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004307 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 switch (err) {
4309 case -NFS4ERR_STALE_STATEID:
4310 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 case 0:
4312 return 0;
4313 }
4314 err = nfs4_handle_exception(server, err, &exception);
4315 } while (exception.retry);
4316 return err;
4317}
4318
4319#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4320#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4321
4322/*
4323 * sleep, with exponential backoff, and retry the LOCK operation.
4324 */
4325static unsigned long
4326nfs4_set_lock_task_retry(unsigned long timeout)
4327{
Jeff Laytond3103102011-12-01 22:44:39 +01004328 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 timeout <<= 1;
4330 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4331 return NFS4_LOCK_MAXTIMEOUT;
4332 return timeout;
4333}
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4336{
4337 struct inode *inode = state->inode;
4338 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004339 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004340 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004342 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004344 struct nfs_lockt_res res = {
4345 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 };
4347 struct rpc_message msg = {
4348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4349 .rpc_argp = &arg,
4350 .rpc_resp = &res,
4351 .rpc_cred = state->owner->so_cred,
4352 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 struct nfs4_lock_state *lsp;
4354 int status;
4355
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004356 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004357 status = nfs4_set_lock_state(state, request);
4358 if (status != 0)
4359 goto out;
4360 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004361 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004362 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004363 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004364 switch (status) {
4365 case 0:
4366 request->fl_type = F_UNLCK;
4367 break;
4368 case -NFS4ERR_DENIED:
4369 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004371 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004372out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 return status;
4374}
4375
4376static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4377{
4378 struct nfs4_exception exception = { };
4379 int err;
4380
4381 do {
4382 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4383 _nfs4_proc_getlk(state, cmd, request),
4384 &exception);
4385 } while (exception.retry);
4386 return err;
4387}
4388
4389static int do_vfs_lock(struct file *file, struct file_lock *fl)
4390{
4391 int res = 0;
4392 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4393 case FL_POSIX:
4394 res = posix_lock_file_wait(file, fl);
4395 break;
4396 case FL_FLOCK:
4397 res = flock_lock_file_wait(file, fl);
4398 break;
4399 default:
4400 BUG();
4401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 return res;
4403}
4404
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004405struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004406 struct nfs_locku_args arg;
4407 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004408 struct nfs4_lock_state *lsp;
4409 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004410 struct file_lock fl;
4411 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004412 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004413};
4414
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004415static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4416 struct nfs_open_context *ctx,
4417 struct nfs4_lock_state *lsp,
4418 struct nfs_seqid *seqid)
4419{
4420 struct nfs4_unlockdata *p;
4421 struct inode *inode = lsp->ls_state->inode;
4422
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004423 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004424 if (p == NULL)
4425 return NULL;
4426 p->arg.fh = NFS_FH(inode);
4427 p->arg.fl = &p->fl;
4428 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004429 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004430 p->arg.stateid = &lsp->ls_stateid;
4431 p->lsp = lsp;
4432 atomic_inc(&lsp->ls_count);
4433 /* Ensure we don't close file until we're done freeing locks! */
4434 p->ctx = get_nfs_open_context(ctx);
4435 memcpy(&p->fl, fl, sizeof(p->fl));
4436 p->server = NFS_SERVER(inode);
4437 return p;
4438}
4439
Trond Myklebust06f814a2006-01-03 09:55:07 +01004440static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004441{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004442 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004443 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004444 nfs4_put_lock_state(calldata->lsp);
4445 put_nfs_open_context(calldata->ctx);
4446 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004447}
4448
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004449static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004450{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004451 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004452
Trond Myklebust14516c32010-07-31 14:29:06 -04004453 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4454 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004455 switch (task->tk_status) {
4456 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004457 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4458 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004459 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004460 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004461 case -NFS4ERR_BAD_STATEID:
4462 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004463 case -NFS4ERR_STALE_STATEID:
4464 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004465 break;
4466 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004467 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004468 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004469 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004470}
4471
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004472static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004473{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004474 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004476 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004477 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004478 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004479 /* Note: exit _without_ running nfs4_locku_done */
4480 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004481 return;
4482 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004483 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004484 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004485 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004486 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004487 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004488 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489}
4490
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004491static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004492 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004493 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004494 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004495};
4496
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004497static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4498 struct nfs_open_context *ctx,
4499 struct nfs4_lock_state *lsp,
4500 struct nfs_seqid *seqid)
4501{
4502 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004503 struct rpc_message msg = {
4504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4505 .rpc_cred = ctx->cred,
4506 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004507 struct rpc_task_setup task_setup_data = {
4508 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004509 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004510 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004511 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004512 .flags = RPC_TASK_ASYNC,
4513 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514
Frank Filz137d6ac2007-07-09 15:32:29 -07004515 /* Ensure this is an unlock - when canceling a lock, the
4516 * canceled lock is passed in, and it won't be an unlock.
4517 */
4518 fl->fl_type = F_UNLCK;
4519
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4521 if (data == NULL) {
4522 nfs_free_seqid(seqid);
4523 return ERR_PTR(-ENOMEM);
4524 }
4525
Trond Myklebust9d12b212012-01-17 22:04:25 -05004526 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004527 msg.rpc_argp = &data->arg;
4528 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004529 task_setup_data.callback_data = data;
4530 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004531}
4532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4534{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004535 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004536 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004537 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004538 struct rpc_task *task;
4539 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004540 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Trond Myklebust9b073572006-06-29 16:38:34 -04004542 status = nfs4_set_lock_state(state, request);
4543 /* Unlock _before_ we do the RPC call */
4544 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004545 down_read(&nfsi->rwsem);
4546 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4547 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004548 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004549 }
4550 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004551 if (status != 0)
4552 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004553 /* Is this a delegated lock? */
4554 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004555 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004556 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004557 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004558 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004559 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004560 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004561 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562 status = PTR_ERR(task);
4563 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004564 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004565 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004566 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004567out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004568 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570}
4571
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004572struct nfs4_lockdata {
4573 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004574 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575 struct nfs4_lock_state *lsp;
4576 struct nfs_open_context *ctx;
4577 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004578 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579 int rpc_status;
4580 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004581 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582};
4583
4584static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004585 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4586 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004587{
4588 struct nfs4_lockdata *p;
4589 struct inode *inode = lsp->ls_state->inode;
4590 struct nfs_server *server = NFS_SERVER(inode);
4591
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004592 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593 if (p == NULL)
4594 return NULL;
4595
4596 p->arg.fh = NFS_FH(inode);
4597 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004598 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004599 if (p->arg.open_seqid == NULL)
4600 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004601 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004603 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004604 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004605 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004606 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004607 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004608 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004610 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004611 atomic_inc(&lsp->ls_count);
4612 p->ctx = get_nfs_open_context(ctx);
4613 memcpy(&p->fl, fl, sizeof(p->fl));
4614 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004615out_free_seqid:
4616 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004617out_free:
4618 kfree(p);
4619 return NULL;
4620}
4621
4622static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4623{
4624 struct nfs4_lockdata *data = calldata;
4625 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Harvey Harrison3110ff82008-05-02 13:42:44 -07004627 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004628 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4629 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004630 /* Do we need to do an open_to_lock_owner? */
4631 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004632 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4633 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004634 data->arg.open_stateid = &state->stateid;
4635 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004636 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004637 } else
4638 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004639 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004640 if (nfs4_setup_sequence(data->server,
4641 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004642 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004643 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004644 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004645 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646}
4647
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004648static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4649{
4650 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4651 nfs4_lock_prepare(task, calldata);
4652}
4653
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004654static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4655{
4656 struct nfs4_lockdata *data = calldata;
4657
Harvey Harrison3110ff82008-05-02 13:42:44 -07004658 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659
Trond Myklebust14516c32010-07-31 14:29:06 -04004660 if (!nfs4_sequence_done(task, &data->res.seq_res))
4661 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004662
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004663 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004664 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004665 if (data->rpc_status == 0)
4666 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4667 else
4668 goto out;
4669 }
4670 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004671 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004672 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004673 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004674 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004676 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004677}
4678
4679static void nfs4_lock_release(void *calldata)
4680{
4681 struct nfs4_lockdata *data = calldata;
4682
Harvey Harrison3110ff82008-05-02 13:42:44 -07004683 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004684 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004685 if (data->cancelled != 0) {
4686 struct rpc_task *task;
4687 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4688 data->arg.lock_seqid);
4689 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004690 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004691 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004692 } else
4693 nfs_free_seqid(data->arg.lock_seqid);
4694 nfs4_put_lock_state(data->lsp);
4695 put_nfs_open_context(data->ctx);
4696 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004697 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004698}
4699
4700static const struct rpc_call_ops nfs4_lock_ops = {
4701 .rpc_call_prepare = nfs4_lock_prepare,
4702 .rpc_call_done = nfs4_lock_done,
4703 .rpc_release = nfs4_lock_release,
4704};
4705
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004706static const struct rpc_call_ops nfs4_recover_lock_ops = {
4707 .rpc_call_prepare = nfs4_recover_lock_prepare,
4708 .rpc_call_done = nfs4_lock_done,
4709 .rpc_release = nfs4_lock_release,
4710};
4711
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004712static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4713{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004714 switch (error) {
4715 case -NFS4ERR_ADMIN_REVOKED:
4716 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004717 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004718 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004719 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004720 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004721 break;
4722 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004723 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004724 case -NFS4ERR_EXPIRED:
4725 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004726 };
4727}
4728
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004729static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730{
4731 struct nfs4_lockdata *data;
4732 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004733 struct rpc_message msg = {
4734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4735 .rpc_cred = state->owner->so_cred,
4736 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004737 struct rpc_task_setup task_setup_data = {
4738 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004739 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004740 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004741 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004742 .flags = RPC_TASK_ASYNC,
4743 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004744 int ret;
4745
Harvey Harrison3110ff82008-05-02 13:42:44 -07004746 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004747 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004748 fl->fl_u.nfs4_fl.owner,
4749 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750 if (data == NULL)
4751 return -ENOMEM;
4752 if (IS_SETLKW(cmd))
4753 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004754 if (recovery_type > NFS_LOCK_NEW) {
4755 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004756 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004757 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4758 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004759 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004760 msg.rpc_argp = &data->arg;
4761 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004762 task_setup_data.callback_data = data;
4763 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004764 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004765 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004766 ret = nfs4_wait_for_completion_rpc_task(task);
4767 if (ret == 0) {
4768 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004769 if (ret)
4770 nfs4_handle_setlk_error(data->server, data->lsp,
4771 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004772 } else
4773 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004774 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004775 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004776 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
4779static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4780{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004781 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004782 struct nfs4_exception exception = {
4783 .inode = state->inode,
4784 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004785 int err;
4786
4787 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004788 /* Cache the lock if possible... */
4789 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4790 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004791 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004792 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004793 break;
4794 nfs4_handle_exception(server, err, &exception);
4795 } while (exception.retry);
4796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
4799static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4800{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004801 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004802 struct nfs4_exception exception = {
4803 .inode = state->inode,
4804 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004805 int err;
4806
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004807 err = nfs4_set_lock_state(state, request);
4808 if (err != 0)
4809 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004810 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004811 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4812 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004813 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004814 switch (err) {
4815 default:
4816 goto out;
4817 case -NFS4ERR_GRACE:
4818 case -NFS4ERR_DELAY:
4819 nfs4_handle_exception(server, err, &exception);
4820 err = 0;
4821 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004822 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004823out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004824 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825}
4826
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004827#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004828/**
4829 * nfs41_check_expired_locks - possibly free a lock stateid
4830 *
4831 * @state: NFSv4 state for an inode
4832 *
4833 * Returns NFS_OK if recovery for this stateid is now finished.
4834 * Otherwise a negative NFS4ERR value is returned.
4835 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004836static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004837{
Chuck Levereb64cf92012-07-11 16:30:05 -04004838 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004839 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004840 struct nfs_server *server = NFS_SERVER(state->inode);
4841
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004842 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004843 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004844 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4845 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004846 /* Free the stateid unless the server
4847 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004848 if (status != -NFS4ERR_BAD_STATEID)
4849 nfs41_free_stateid(server,
4850 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004851 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004852 ret = status;
4853 }
4854 }
4855 };
4856
4857 return ret;
4858}
4859
4860static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4861{
4862 int status = NFS_OK;
4863
4864 if (test_bit(LK_STATE_IN_USE, &state->flags))
4865 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004866 if (status != NFS_OK)
4867 status = nfs4_lock_expired(state, request);
4868 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004869}
4870#endif
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4873{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004874 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004875 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004876 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004878 if ((fl_flags & FL_POSIX) &&
4879 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4880 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004881 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004882 status = nfs4_set_lock_state(state, request);
4883 if (status != 0)
4884 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004885 request->fl_flags |= FL_ACCESS;
4886 status = do_vfs_lock(request->fl_file, request);
4887 if (status < 0)
4888 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004889 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004890 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004891 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004892 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004893 request->fl_flags = fl_flags & ~FL_SLEEP;
4894 status = do_vfs_lock(request->fl_file, request);
4895 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004896 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004897 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004898 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004899 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004900 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004901 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004902 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004903 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4904 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004905out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004906 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004907out:
4908 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 return status;
4910}
4911
4912static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4913{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004914 struct nfs4_exception exception = {
4915 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004916 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004917 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 int err;
4919
4920 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004921 err = _nfs4_proc_setlk(state, cmd, request);
4922 if (err == -NFS4ERR_DENIED)
4923 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004925 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 } while (exception.retry);
4927 return err;
4928}
4929
4930static int
4931nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4932{
4933 struct nfs_open_context *ctx;
4934 struct nfs4_state *state;
4935 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4936 int status;
4937
4938 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004939 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 state = ctx->state;
4941
4942 if (request->fl_start < 0 || request->fl_end < 0)
4943 return -EINVAL;
4944
Trond Myklebustd9531262009-07-21 19:22:38 -04004945 if (IS_GETLK(cmd)) {
4946 if (state != NULL)
4947 return nfs4_proc_getlk(state, F_GETLK, request);
4948 return 0;
4949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
4951 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4952 return -EINVAL;
4953
Trond Myklebustd9531262009-07-21 19:22:38 -04004954 if (request->fl_type == F_UNLCK) {
4955 if (state != NULL)
4956 return nfs4_proc_unlck(state, cmd, request);
4957 return 0;
4958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Trond Myklebustd9531262009-07-21 19:22:38 -04004960 if (state == NULL)
4961 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004962 /*
4963 * Don't rely on the VFS having checked the file open mode,
4964 * since it won't do this for flock() locks.
4965 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004966 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004967 case F_RDLCK:
4968 if (!(filp->f_mode & FMODE_READ))
4969 return -EBADF;
4970 break;
4971 case F_WRLCK:
4972 if (!(filp->f_mode & FMODE_WRITE))
4973 return -EBADF;
4974 }
4975
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 do {
4977 status = nfs4_proc_setlk(state, cmd, request);
4978 if ((status != -EAGAIN) || IS_SETLK(cmd))
4979 break;
4980 timeout = nfs4_set_lock_task_retry(timeout);
4981 status = -ERESTARTSYS;
4982 if (signalled())
4983 break;
4984 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 return status;
4986}
4987
Trond Myklebust888e6942005-11-04 15:38:11 -05004988int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4989{
4990 struct nfs_server *server = NFS_SERVER(state->inode);
4991 struct nfs4_exception exception = { };
4992 int err;
4993
4994 err = nfs4_set_lock_state(state, fl);
4995 if (err != 0)
4996 goto out;
4997 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004998 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004999 switch (err) {
5000 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005001 printk(KERN_ERR "NFS: %s: unhandled error "
5002 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005003 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005004 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005005 goto out;
5006 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005007 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005008 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005009 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005010 nfs4_schedule_lease_recovery(server->nfs_client);
5011 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005012 case -NFS4ERR_BADSESSION:
5013 case -NFS4ERR_BADSLOT:
5014 case -NFS4ERR_BAD_HIGH_SLOT:
5015 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5016 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005017 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005018 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005019 case -ERESTARTSYS:
5020 /*
5021 * The show must go on: exit, but mark the
5022 * stateid as needing recovery.
5023 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005024 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005025 case -NFS4ERR_ADMIN_REVOKED:
5026 case -NFS4ERR_BAD_STATEID:
5027 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005028 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005029 err = 0;
5030 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005031 case -EKEYEXPIRED:
5032 /*
5033 * User RPCSEC_GSS context has expired.
5034 * We cannot recover this stateid now, so
5035 * skip it and allow recovery thread to
5036 * proceed.
5037 */
5038 err = 0;
5039 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005040 case -ENOMEM:
5041 case -NFS4ERR_DENIED:
5042 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5043 err = 0;
5044 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005045 case -NFS4ERR_DELAY:
5046 break;
5047 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005048 err = nfs4_handle_exception(server, err, &exception);
5049 } while (exception.retry);
5050out:
5051 return err;
5052}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005053
Trond Myklebustcf470c32012-03-07 13:49:12 -05005054struct nfs_release_lockowner_data {
5055 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005056 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005057 struct nfs_release_lockowner_args args;
5058};
5059
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005060static void nfs4_release_lockowner_release(void *calldata)
5061{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005062 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005063 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005064 kfree(calldata);
5065}
5066
Trond Myklebust17280172012-03-11 13:11:00 -04005067static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005068 .rpc_release = nfs4_release_lockowner_release,
5069};
5070
Trond Myklebustcf470c32012-03-07 13:49:12 -05005071int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005072{
5073 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005074 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005075 struct rpc_message msg = {
5076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5077 };
5078
5079 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005080 return -EINVAL;
5081 data = kmalloc(sizeof(*data), GFP_NOFS);
5082 if (!data)
5083 return -ENOMEM;
5084 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005085 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005086 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5087 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5088 data->args.lock_owner.s_dev = server->s_dev;
5089 msg.rpc_argp = &data->args;
5090 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5091 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005092}
5093
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005094#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5095
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005096static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5097 const void *buf, size_t buflen,
5098 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005099{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005100 if (strcmp(key, "") != 0)
5101 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005102
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005103 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005104}
5105
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005106static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5107 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005108{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005109 if (strcmp(key, "") != 0)
5110 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005111
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005112 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005113}
5114
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005115static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5116 size_t list_len, const char *name,
5117 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005118{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005119 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005120
J. Bruce Fields096455a2006-03-20 23:23:42 -05005121 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5122 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005123
5124 if (list && len <= list_len)
5125 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005126 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005127}
5128
Andy Adamson533eb462011-06-13 18:25:56 -04005129/*
5130 * nfs_fhget will use either the mounted_on_fileid or the fileid
5131 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005132static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5133{
Andy Adamson533eb462011-06-13 18:25:56 -04005134 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5135 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5136 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005137 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005138 return;
5139
5140 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005141 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005142 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5143 fattr->nlink = 2;
5144}
5145
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005146static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5147 const struct qstr *name,
5148 struct nfs4_fs_locations *fs_locations,
5149 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005150{
5151 struct nfs_server *server = NFS_SERVER(dir);
5152 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005153 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005154 };
5155 struct nfs4_fs_locations_arg args = {
5156 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005157 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005158 .page = page,
5159 .bitmask = bitmask,
5160 };
Benny Halevy22958462009-04-01 09:22:02 -04005161 struct nfs4_fs_locations_res res = {
5162 .fs_locations = fs_locations,
5163 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005164 struct rpc_message msg = {
5165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5166 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005167 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005168 };
5169 int status;
5170
Harvey Harrison3110ff82008-05-02 13:42:44 -07005171 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005172
5173 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5174 * is not supported */
5175 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5176 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5177 else
5178 bitmask[0] |= FATTR4_WORD0_FILEID;
5179
Trond Myklebustc228fd32007-01-13 02:28:11 -05005180 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005181 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005182 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005183 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005184 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005185 return status;
5186}
5187
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005188int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5189 const struct qstr *name,
5190 struct nfs4_fs_locations *fs_locations,
5191 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005192{
5193 struct nfs4_exception exception = { };
5194 int err;
5195 do {
5196 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005197 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005198 &exception);
5199 } while (exception.retry);
5200 return err;
5201}
5202
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005203static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5204{
5205 int status;
5206 struct nfs4_secinfo_arg args = {
5207 .dir_fh = NFS_FH(dir),
5208 .name = name,
5209 };
5210 struct nfs4_secinfo_res res = {
5211 .flavors = flavors,
5212 };
5213 struct rpc_message msg = {
5214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5215 .rpc_argp = &args,
5216 .rpc_resp = &res,
5217 };
5218
5219 dprintk("NFS call secinfo %s\n", name->name);
5220 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5221 dprintk("NFS reply secinfo: %d\n", status);
5222 return status;
5223}
5224
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005225int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5226 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005227{
5228 struct nfs4_exception exception = { };
5229 int err;
5230 do {
5231 err = nfs4_handle_exception(NFS_SERVER(dir),
5232 _nfs4_proc_secinfo(dir, name, flavors),
5233 &exception);
5234 } while (exception.retry);
5235 return err;
5236}
5237
Andy Adamson557134a2009-04-01 09:21:53 -04005238#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005239/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005240 * Check the exchange flags returned by the server for invalid flags, having
5241 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5242 * DS flags set.
5243 */
5244static int nfs4_check_cl_exchange_flags(u32 flags)
5245{
5246 if (flags & ~EXCHGID4_FLAG_MASK_R)
5247 goto out_inval;
5248 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5249 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5250 goto out_inval;
5251 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5252 goto out_inval;
5253 return NFS_OK;
5254out_inval:
5255 return -NFS4ERR_INVAL;
5256}
5257
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005258static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005259nfs41_same_server_scope(struct nfs41_server_scope *a,
5260 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005261{
5262 if (a->server_scope_sz == b->server_scope_sz &&
5263 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5264 return true;
5265
5266 return false;
5267}
5268
Andy Adamson357f54d2010-12-14 10:11:57 -05005269/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005270 * nfs4_proc_bind_conn_to_session()
5271 *
5272 * The 4.1 client currently uses the same TCP connection for the
5273 * fore and backchannel.
5274 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005275int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005276{
5277 int status;
5278 struct nfs41_bind_conn_to_session_res res;
5279 struct rpc_message msg = {
5280 .rpc_proc =
5281 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5282 .rpc_argp = clp,
5283 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005284 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005285 };
5286
5287 dprintk("--> %s\n", __func__);
5288 BUG_ON(clp == NULL);
5289
5290 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5291 if (unlikely(res.session == NULL)) {
5292 status = -ENOMEM;
5293 goto out;
5294 }
5295
5296 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5297 if (status == 0) {
5298 if (memcmp(res.session->sess_id.data,
5299 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5300 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5301 status = -EIO;
5302 goto out_session;
5303 }
5304 if (res.dir != NFS4_CDFS4_BOTH) {
5305 dprintk("NFS: %s: Unexpected direction from server\n",
5306 __func__);
5307 status = -EIO;
5308 goto out_session;
5309 }
5310 if (res.use_conn_in_rdma_mode) {
5311 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5312 __func__);
5313 status = -EIO;
5314 goto out_session;
5315 }
5316 }
5317out_session:
5318 kfree(res.session);
5319out:
5320 dprintk("<-- %s status= %d\n", __func__, status);
5321 return status;
5322}
5323
5324/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005325 * nfs4_proc_exchange_id()
5326 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005327 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5328 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005329 * Since the clientid has expired, all compounds using sessions
5330 * associated with the stale clientid will be returning
5331 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5332 * be in some phase of session reset.
5333 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005334int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005335{
5336 nfs4_verifier verifier;
5337 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005338 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005339 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005340 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005341 };
5342 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005343 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005344 };
5345 int status;
5346 struct rpc_message msg = {
5347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5348 .rpc_argp = &args,
5349 .rpc_resp = &res,
5350 .rpc_cred = cred,
5351 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005352
Chuck Leverf0920752012-05-21 22:45:41 -04005353 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005354 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5355 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005356 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5357 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5358 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005359
Chuck Leveracdeb692012-05-21 22:46:16 -04005360 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005361 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005362 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005363 status = -ENOMEM;
5364 goto out;
5365 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005366
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005367 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005368 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005369 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005370 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005371 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005372 }
5373
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005374 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005375 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005376 status = -ENOMEM;
5377 goto out_server_scope;
5378 }
5379
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005380 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005381 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005382 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005383
Chuck Lever177313f2012-05-21 22:44:58 -04005384 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005385 clp->cl_clientid = res.clientid;
5386 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5387 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5388 clp->cl_seqid = res.seqid;
5389
Chuck Leveracdeb692012-05-21 22:46:16 -04005390 kfree(clp->cl_serverowner);
5391 clp->cl_serverowner = res.server_owner;
5392 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005393
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005394 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005395 kfree(clp->cl_implid);
5396 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005397
Chuck Lever177313f2012-05-21 22:44:58 -04005398 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005399 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005400 res.server_scope)) {
5401 dprintk("%s: server_scope mismatch detected\n",
5402 __func__);
5403 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005404 kfree(clp->cl_serverscope);
5405 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005406 }
5407
Chuck Lever177313f2012-05-21 22:44:58 -04005408 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005409 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005410 goto out;
5411 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005412 } else
5413 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005414
Chuck Leveracdeb692012-05-21 22:46:16 -04005415out_server_owner:
5416 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005417out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005418 kfree(res.server_scope);
5419out:
Chuck Lever177313f2012-05-21 22:44:58 -04005420 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005421 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005422 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005423 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005424 clp->cl_implid->date.seconds,
5425 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005426 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005427 return status;
5428}
5429
Trond Myklebust66245532012-05-25 17:18:09 -04005430static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5431 struct rpc_cred *cred)
5432{
5433 struct rpc_message msg = {
5434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5435 .rpc_argp = clp,
5436 .rpc_cred = cred,
5437 };
5438 int status;
5439
5440 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5441 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005442 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005443 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5444 return status;
5445}
5446
5447static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5448 struct rpc_cred *cred)
5449{
5450 unsigned int loop;
5451 int ret;
5452
5453 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5454 ret = _nfs4_proc_destroy_clientid(clp, cred);
5455 switch (ret) {
5456 case -NFS4ERR_DELAY:
5457 case -NFS4ERR_CLIENTID_BUSY:
5458 ssleep(1);
5459 break;
5460 default:
5461 return ret;
5462 }
5463 }
5464 return 0;
5465}
5466
5467int nfs4_destroy_clientid(struct nfs_client *clp)
5468{
5469 struct rpc_cred *cred;
5470 int ret = 0;
5471
5472 if (clp->cl_mvops->minor_version < 1)
5473 goto out;
5474 if (clp->cl_exchange_flags == 0)
5475 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005476 if (clp->cl_preserve_clid)
5477 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005478 cred = nfs4_get_exchange_id_cred(clp);
5479 ret = nfs4_proc_destroy_clientid(clp, cred);
5480 if (cred)
5481 put_rpccred(cred);
5482 switch (ret) {
5483 case 0:
5484 case -NFS4ERR_STALE_CLIENTID:
5485 clp->cl_exchange_flags = 0;
5486 }
5487out:
5488 return ret;
5489}
5490
Andy Adamson2050f0c2009-04-01 09:22:30 -04005491struct nfs4_get_lease_time_data {
5492 struct nfs4_get_lease_time_args *args;
5493 struct nfs4_get_lease_time_res *res;
5494 struct nfs_client *clp;
5495};
5496
5497static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5498 void *calldata)
5499{
5500 int ret;
5501 struct nfs4_get_lease_time_data *data =
5502 (struct nfs4_get_lease_time_data *)calldata;
5503
5504 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005505 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005506 /* just setup sequence, do not trigger session recovery
5507 since we're invoked within one */
5508 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005509 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005510 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005511
5512 BUG_ON(ret == -EAGAIN);
5513 rpc_call_start(task);
5514 dprintk("<-- %s\n", __func__);
5515}
5516
5517/*
5518 * Called from nfs4_state_manager thread for session setup, so don't recover
5519 * from sequence operation or clientid errors.
5520 */
5521static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5522{
5523 struct nfs4_get_lease_time_data *data =
5524 (struct nfs4_get_lease_time_data *)calldata;
5525
5526 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005527 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5528 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005529 switch (task->tk_status) {
5530 case -NFS4ERR_DELAY:
5531 case -NFS4ERR_GRACE:
5532 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5533 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5534 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005535 /* fall through */
5536 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005537 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005538 return;
5539 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005540 dprintk("<-- %s\n", __func__);
5541}
5542
Trond Myklebust17280172012-03-11 13:11:00 -04005543static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005544 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5545 .rpc_call_done = nfs4_get_lease_time_done,
5546};
5547
5548int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5549{
5550 struct rpc_task *task;
5551 struct nfs4_get_lease_time_args args;
5552 struct nfs4_get_lease_time_res res = {
5553 .lr_fsinfo = fsinfo,
5554 };
5555 struct nfs4_get_lease_time_data data = {
5556 .args = &args,
5557 .res = &res,
5558 .clp = clp,
5559 };
5560 struct rpc_message msg = {
5561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5562 .rpc_argp = &args,
5563 .rpc_resp = &res,
5564 };
5565 struct rpc_task_setup task_setup = {
5566 .rpc_client = clp->cl_rpcclient,
5567 .rpc_message = &msg,
5568 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005569 .callback_data = &data,
5570 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005571 };
5572 int status;
5573
Trond Myklebust9d12b212012-01-17 22:04:25 -05005574 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005575 dprintk("--> %s\n", __func__);
5576 task = rpc_run_task(&task_setup);
5577
5578 if (IS_ERR(task))
5579 status = PTR_ERR(task);
5580 else {
5581 status = task->tk_status;
5582 rpc_put_task(task);
5583 }
5584 dprintk("<-- %s return %d\n", __func__, status);
5585
5586 return status;
5587}
5588
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005589static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5590{
5591 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5592}
5593
5594static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5595 struct nfs4_slot *new,
5596 u32 max_slots,
5597 u32 ivalue)
5598{
5599 struct nfs4_slot *old = NULL;
5600 u32 i;
5601
5602 spin_lock(&tbl->slot_tbl_lock);
5603 if (new) {
5604 old = tbl->slots;
5605 tbl->slots = new;
5606 tbl->max_slots = max_slots;
5607 }
5608 tbl->highest_used_slotid = -1; /* no slot is currently used */
5609 for (i = 0; i < tbl->max_slots; i++)
5610 tbl->slots[i].seq_nr = ivalue;
5611 spin_unlock(&tbl->slot_tbl_lock);
5612 kfree(old);
5613}
5614
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005615/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005616 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005617 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005618static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5619 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005620{
Andy Adamson104aeba2010-01-14 17:45:10 -05005621 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005622 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005623
Andy Adamson104aeba2010-01-14 17:45:10 -05005624 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5625 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005626
Andy Adamson104aeba2010-01-14 17:45:10 -05005627 /* Does the newly negotiated max_reqs match the existing slot table? */
5628 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005629 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005630 if (!new)
5631 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005632 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005633 ret = 0;
5634
5635 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005636 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5637 tbl, tbl->slots, tbl->max_slots);
5638out:
5639 dprintk("<-- %s: return %d\n", __func__, ret);
5640 return ret;
5641}
5642
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005643/* Destroy the slot table */
5644static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5645{
5646 if (session->fc_slot_table.slots != NULL) {
5647 kfree(session->fc_slot_table.slots);
5648 session->fc_slot_table.slots = NULL;
5649 }
5650 if (session->bc_slot_table.slots != NULL) {
5651 kfree(session->bc_slot_table.slots);
5652 session->bc_slot_table.slots = NULL;
5653 }
5654 return;
5655}
5656
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005657/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005658 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005659 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005660static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005661{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005662 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005663 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005664
Andy Adamsonaacd5532011-11-09 13:58:21 -05005665 dprintk("--> %s\n", __func__);
5666 /* Fore channel */
5667 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005668 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5669 if (status) /* -ENOMEM */
5670 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005671 /* Back channel */
5672 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005673 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5674 if (status && tbl->slots == NULL)
5675 /* Fore and back channel share a connection so get
5676 * both slot tables or neither */
5677 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005678 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005679}
5680
5681struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5682{
5683 struct nfs4_session *session;
5684 struct nfs4_slot_table *tbl;
5685
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005686 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005687 if (!session)
5688 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005689
Andy Adamson557134a2009-04-01 09:21:53 -04005690 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005691 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005692 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005693 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005694 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005695
5696 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005697 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005698 spin_lock_init(&tbl->slot_tbl_lock);
5699 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005700 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005701
Trond Myklebust1055d762010-06-16 09:52:27 -04005702 session->session_state = 1<<NFS4_SESSION_INITING;
5703
Andy Adamson557134a2009-04-01 09:21:53 -04005704 session->clp = clp;
5705 return session;
5706}
5707
5708void nfs4_destroy_session(struct nfs4_session *session)
5709{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005710 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005711 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005712
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005713 cred = nfs4_get_exchange_id_cred(session->clp);
5714 nfs4_proc_destroy_session(session, cred);
5715 if (cred)
5716 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005717
5718 rcu_read_lock();
5719 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5720 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005721 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005722 __func__, xprt);
5723 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005724 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005725 kfree(session);
5726}
5727
Andy Adamsonfc931582009-04-01 09:22:31 -04005728/*
5729 * Initialize the values to be used by the client in CREATE_SESSION
5730 * If nfs4_init_session set the fore channel request and response sizes,
5731 * use them.
5732 *
5733 * Set the back channel max_resp_sz_cached to zero to force the client to
5734 * always set csa_cachethis to FALSE because the current implementation
5735 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5736 */
5737static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5738{
5739 struct nfs4_session *session = args->client->cl_session;
5740 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5741 mxresp_sz = session->fc_attrs.max_resp_sz;
5742
5743 if (mxrqst_sz == 0)
5744 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5745 if (mxresp_sz == 0)
5746 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5747 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005748 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5749 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005750 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005751 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005752
5753 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005754 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005755 __func__,
5756 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005757 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005758
5759 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005760 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5761 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5762 args->bc_attrs.max_resp_sz_cached = 0;
5763 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5764 args->bc_attrs.max_reqs = 1;
5765
5766 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5767 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5768 __func__,
5769 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5770 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5771 args->bc_attrs.max_reqs);
5772}
5773
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005774static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005775{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005776 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5777 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5778
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005779 if (rcvd->max_resp_sz > sent->max_resp_sz)
5780 return -EINVAL;
5781 /*
5782 * Our requested max_ops is the minimum we need; we're not
5783 * prepared to break up compounds into smaller pieces than that.
5784 * So, no point even trying to continue if the server won't
5785 * cooperate:
5786 */
5787 if (rcvd->max_ops < sent->max_ops)
5788 return -EINVAL;
5789 if (rcvd->max_reqs == 0)
5790 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005791 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5792 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005793 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005794}
5795
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005796static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5797{
5798 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5799 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005800
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005801 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5802 return -EINVAL;
5803 if (rcvd->max_resp_sz < sent->max_resp_sz)
5804 return -EINVAL;
5805 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5806 return -EINVAL;
5807 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005808 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005809 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005810 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005811 return -EINVAL;
5812 return 0;
5813}
Andy Adamson8d353012009-04-01 09:22:32 -04005814
Andy Adamson8d353012009-04-01 09:22:32 -04005815static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5816 struct nfs4_session *session)
5817{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005818 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005819
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005820 ret = nfs4_verify_fore_channel_attrs(args, session);
5821 if (ret)
5822 return ret;
5823 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005824}
5825
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005826static int _nfs4_proc_create_session(struct nfs_client *clp,
5827 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005828{
5829 struct nfs4_session *session = clp->cl_session;
5830 struct nfs41_create_session_args args = {
5831 .client = clp,
5832 .cb_program = NFS4_CALLBACK,
5833 };
5834 struct nfs41_create_session_res res = {
5835 .client = clp,
5836 };
5837 struct rpc_message msg = {
5838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5839 .rpc_argp = &args,
5840 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005841 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005842 };
5843 int status;
5844
5845 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005846 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005847
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005848 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005849
Andy Adamson8d353012009-04-01 09:22:32 -04005850 if (!status)
5851 /* Verify the session's negotiated channel_attrs values */
5852 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005853 if (!status) {
5854 /* Increment the clientid slot sequence id */
5855 clp->cl_seqid++;
5856 }
5857
5858 return status;
5859}
5860
5861/*
5862 * Issues a CREATE_SESSION operation to the server.
5863 * It is the responsibility of the caller to verify the session is
5864 * expired before calling this routine.
5865 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005866int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005867{
5868 int status;
5869 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005870 struct nfs4_session *session = clp->cl_session;
5871
5872 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5873
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005874 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005875 if (status)
5876 goto out;
5877
Andy Adamsonaacd5532011-11-09 13:58:21 -05005878 /* Init or reset the session slot tables */
5879 status = nfs4_setup_session_slot_tables(session);
5880 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005881 if (status)
5882 goto out;
5883
5884 ptr = (unsigned *)&session->sess_id.data[0];
5885 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5886 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005887out:
5888 dprintk("<-- %s\n", __func__);
5889 return status;
5890}
5891
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005892/*
5893 * Issue the over-the-wire RPC DESTROY_SESSION.
5894 * The caller must serialize access to this routine.
5895 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005896int nfs4_proc_destroy_session(struct nfs4_session *session,
5897 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005898{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005899 struct rpc_message msg = {
5900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5901 .rpc_argp = session,
5902 .rpc_cred = cred,
5903 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005904 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005905
5906 dprintk("--> nfs4_proc_destroy_session\n");
5907
5908 /* session is still being setup */
5909 if (session->clp->cl_cons_state != NFS_CS_READY)
5910 return status;
5911
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005912 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005913
5914 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005915 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005916 "Session has been destroyed regardless...\n", status);
5917
5918 dprintk("<-- nfs4_proc_destroy_session\n");
5919 return status;
5920}
5921
Trond Myklebust7b38c362012-05-23 13:23:31 -04005922/*
5923 * With sessions, the client is not marked ready until after a
5924 * successful EXCHANGE_ID and CREATE_SESSION.
5925 *
5926 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5927 * other versions of NFS can be tried.
5928 */
5929static int nfs41_check_session_ready(struct nfs_client *clp)
5930{
5931 int ret;
5932
5933 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5934 ret = nfs4_client_recover_expired_lease(clp);
5935 if (ret)
5936 return ret;
5937 }
5938 if (clp->cl_cons_state < NFS_CS_READY)
5939 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005940 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005941 return 0;
5942}
5943
Trond Myklebustfccba802009-07-21 16:48:07 -04005944int nfs4_init_session(struct nfs_server *server)
5945{
5946 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005947 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005948 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005949
5950 if (!nfs4_has_session(clp))
5951 return 0;
5952
Trond Myklebust1055d762010-06-16 09:52:27 -04005953 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005954 spin_lock(&clp->cl_lock);
5955 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005956
Trond Myklebust7b38c362012-05-23 13:23:31 -04005957 rsize = server->rsize;
5958 if (rsize == 0)
5959 rsize = NFS_MAX_FILE_IO_SIZE;
5960 wsize = server->wsize;
5961 if (wsize == 0)
5962 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005963
Trond Myklebust7b38c362012-05-23 13:23:31 -04005964 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5965 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5966 }
5967 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005968
Trond Myklebust7b38c362012-05-23 13:23:31 -04005969 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005970}
5971
Trond Myklebust7b38c362012-05-23 13:23:31 -04005972int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005973{
5974 struct nfs4_session *session = clp->cl_session;
5975 int ret;
5976
Trond Myklebust7b38c362012-05-23 13:23:31 -04005977 spin_lock(&clp->cl_lock);
5978 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5979 /*
5980 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5981 * DS lease to be equal to the MDS lease.
5982 */
5983 clp->cl_lease_time = lease_time;
5984 clp->cl_last_renewal = jiffies;
5985 }
5986 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005987
Trond Myklebust7b38c362012-05-23 13:23:31 -04005988 ret = nfs41_check_session_ready(clp);
5989 if (ret)
5990 return ret;
5991 /* Test for the DS role */
5992 if (!is_ds_client(clp))
5993 return -ENODEV;
5994 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005995}
5996EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5997
5998
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005999/*
6000 * Renew the cl_session lease.
6001 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006002struct nfs4_sequence_data {
6003 struct nfs_client *clp;
6004 struct nfs4_sequence_args args;
6005 struct nfs4_sequence_res res;
6006};
6007
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006008static void nfs41_sequence_release(void *data)
6009{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006010 struct nfs4_sequence_data *calldata = data;
6011 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006012
Alexandros Batsakis71358402010-02-05 03:45:05 -08006013 if (atomic_read(&clp->cl_count) > 1)
6014 nfs4_schedule_state_renewal(clp);
6015 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006016 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006017}
6018
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006019static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6020{
6021 switch(task->tk_status) {
6022 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006023 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6024 return -EAGAIN;
6025 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006026 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006027 }
6028 return 0;
6029}
6030
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006031static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006032{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006033 struct nfs4_sequence_data *calldata = data;
6034 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006035
Trond Myklebust14516c32010-07-31 14:29:06 -04006036 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6037 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006038
6039 if (task->tk_status < 0) {
6040 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006041 if (atomic_read(&clp->cl_count) == 1)
6042 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006043
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006044 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6045 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006046 return;
6047 }
6048 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006049 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006050out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006051 dprintk("<-- %s\n", __func__);
6052}
6053
6054static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6055{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006056 struct nfs4_sequence_data *calldata = data;
6057 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006058 struct nfs4_sequence_args *args;
6059 struct nfs4_sequence_res *res;
6060
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006061 args = task->tk_msg.rpc_argp;
6062 res = task->tk_msg.rpc_resp;
6063
Trond Myklebust9d12b212012-01-17 22:04:25 -05006064 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006065 return;
6066 rpc_call_start(task);
6067}
6068
6069static const struct rpc_call_ops nfs41_sequence_ops = {
6070 .rpc_call_done = nfs41_sequence_call_done,
6071 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006072 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006073};
6074
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006075static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006076{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006077 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006078 struct rpc_message msg = {
6079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6080 .rpc_cred = cred,
6081 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006082 struct rpc_task_setup task_setup_data = {
6083 .rpc_client = clp->cl_rpcclient,
6084 .rpc_message = &msg,
6085 .callback_ops = &nfs41_sequence_ops,
6086 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6087 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006088
Alexandros Batsakis71358402010-02-05 03:45:05 -08006089 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006090 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006091 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006092 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006093 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006094 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006095 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006096 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006097 msg.rpc_argp = &calldata->args;
6098 msg.rpc_resp = &calldata->res;
6099 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006100 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006101
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006102 return rpc_run_task(&task_setup_data);
6103}
6104
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006105static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006106{
6107 struct rpc_task *task;
6108 int ret = 0;
6109
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006110 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6111 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006112 task = _nfs41_proc_sequence(clp, cred);
6113 if (IS_ERR(task))
6114 ret = PTR_ERR(task);
6115 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006116 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006117 dprintk("<-- %s status=%d\n", __func__, ret);
6118 return ret;
6119}
6120
6121static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6122{
6123 struct rpc_task *task;
6124 int ret;
6125
6126 task = _nfs41_proc_sequence(clp, cred);
6127 if (IS_ERR(task)) {
6128 ret = PTR_ERR(task);
6129 goto out;
6130 }
6131 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006132 if (!ret) {
6133 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6134
6135 if (task->tk_status == 0)
6136 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006137 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006138 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006139 rpc_put_task(task);
6140out:
6141 dprintk("<-- %s status=%d\n", __func__, ret);
6142 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006143}
6144
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006145struct nfs4_reclaim_complete_data {
6146 struct nfs_client *clp;
6147 struct nfs41_reclaim_complete_args arg;
6148 struct nfs41_reclaim_complete_res res;
6149};
6150
6151static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6152{
6153 struct nfs4_reclaim_complete_data *calldata = data;
6154
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006155 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006156 if (nfs41_setup_sequence(calldata->clp->cl_session,
6157 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006158 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006159 return;
6160
6161 rpc_call_start(task);
6162}
6163
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006164static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6165{
6166 switch(task->tk_status) {
6167 case 0:
6168 case -NFS4ERR_COMPLETE_ALREADY:
6169 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6170 break;
6171 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006172 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006173 /* fall through */
6174 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006175 return -EAGAIN;
6176 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006177 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006178 }
6179 return 0;
6180}
6181
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006182static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6183{
6184 struct nfs4_reclaim_complete_data *calldata = data;
6185 struct nfs_client *clp = calldata->clp;
6186 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6187
6188 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006189 if (!nfs41_sequence_done(task, res))
6190 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006191
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006192 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6193 rpc_restart_call_prepare(task);
6194 return;
6195 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006196 dprintk("<-- %s\n", __func__);
6197}
6198
6199static void nfs4_free_reclaim_complete_data(void *data)
6200{
6201 struct nfs4_reclaim_complete_data *calldata = data;
6202
6203 kfree(calldata);
6204}
6205
6206static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6207 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6208 .rpc_call_done = nfs4_reclaim_complete_done,
6209 .rpc_release = nfs4_free_reclaim_complete_data,
6210};
6211
6212/*
6213 * Issue a global reclaim complete.
6214 */
6215static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6216{
6217 struct nfs4_reclaim_complete_data *calldata;
6218 struct rpc_task *task;
6219 struct rpc_message msg = {
6220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6221 };
6222 struct rpc_task_setup task_setup_data = {
6223 .rpc_client = clp->cl_rpcclient,
6224 .rpc_message = &msg,
6225 .callback_ops = &nfs4_reclaim_complete_call_ops,
6226 .flags = RPC_TASK_ASYNC,
6227 };
6228 int status = -ENOMEM;
6229
6230 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006231 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006232 if (calldata == NULL)
6233 goto out;
6234 calldata->clp = clp;
6235 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006236
Trond Myklebust9d12b212012-01-17 22:04:25 -05006237 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006238 msg.rpc_argp = &calldata->arg;
6239 msg.rpc_resp = &calldata->res;
6240 task_setup_data.callback_data = calldata;
6241 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006242 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006243 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006244 goto out;
6245 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006246 status = nfs4_wait_for_completion_rpc_task(task);
6247 if (status == 0)
6248 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006249 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006250 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006251out:
6252 dprintk("<-- %s status=%d\n", __func__, status);
6253 return status;
6254}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006255
6256static void
6257nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6258{
6259 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006260 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006261
6262 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006263 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6264 * right now covering the LAYOUTGET we are about to send.
6265 * However, that is not so catastrophic, and there seems
6266 * to be no way to prevent it completely.
6267 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006268 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006269 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006270 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006271 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6272 NFS_I(lgp->args.inode)->layout,
6273 lgp->args.ctx->state)) {
6274 rpc_exit(task, NFS4_OK);
6275 return;
6276 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006277 rpc_call_start(task);
6278}
6279
6280static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6281{
6282 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006283 struct inode *inode = lgp->args.inode;
6284 struct nfs_server *server = NFS_SERVER(inode);
6285 struct pnfs_layout_hdr *lo;
6286 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006287
6288 dprintk("--> %s\n", __func__);
6289
6290 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006291 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006292
6293 switch (task->tk_status) {
6294 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006295 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006296 case -NFS4ERR_LAYOUTTRYLATER:
6297 case -NFS4ERR_RECALLCONFLICT:
6298 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006299 break;
6300 case -NFS4ERR_EXPIRED:
6301 case -NFS4ERR_BAD_STATEID:
6302 spin_lock(&inode->i_lock);
6303 lo = NFS_I(inode)->layout;
6304 if (!lo || list_empty(&lo->plh_segs)) {
6305 spin_unlock(&inode->i_lock);
6306 /* If the open stateid was bad, then recover it. */
6307 state = lgp->args.ctx->state;
6308 } else {
6309 LIST_HEAD(head);
6310
6311 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6312 spin_unlock(&inode->i_lock);
6313 /* Mark the bad layout state as invalid, then
6314 * retry using the open stateid. */
6315 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006316 }
6317 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006318 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6319 rpc_restart_call_prepare(task);
6320out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006321 dprintk("<-- %s\n", __func__);
6322}
6323
Idan Kedar85541162012-08-02 11:47:10 +03006324static size_t max_response_pages(struct nfs_server *server)
6325{
6326 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6327 return nfs_page_array_len(0, max_resp_sz);
6328}
6329
6330static void nfs4_free_pages(struct page **pages, size_t size)
6331{
6332 int i;
6333
6334 if (!pages)
6335 return;
6336
6337 for (i = 0; i < size; i++) {
6338 if (!pages[i])
6339 break;
6340 __free_page(pages[i]);
6341 }
6342 kfree(pages);
6343}
6344
6345static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6346{
6347 struct page **pages;
6348 int i;
6349
6350 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6351 if (!pages) {
6352 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6353 return NULL;
6354 }
6355
6356 for (i = 0; i < size; i++) {
6357 pages[i] = alloc_page(gfp_flags);
6358 if (!pages[i]) {
6359 dprintk("%s: failed to allocate page\n", __func__);
6360 nfs4_free_pages(pages, size);
6361 return NULL;
6362 }
6363 }
6364
6365 return pages;
6366}
6367
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006368static void nfs4_layoutget_release(void *calldata)
6369{
6370 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006371 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6372 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006373
6374 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006375 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006376 put_nfs_open_context(lgp->args.ctx);
6377 kfree(calldata);
6378 dprintk("<-- %s\n", __func__);
6379}
6380
6381static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6382 .rpc_call_prepare = nfs4_layoutget_prepare,
6383 .rpc_call_done = nfs4_layoutget_done,
6384 .rpc_release = nfs4_layoutget_release,
6385};
6386
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006387struct pnfs_layout_segment *
6388nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006389{
6390 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006391 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006392 struct rpc_task *task;
6393 struct rpc_message msg = {
6394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6395 .rpc_argp = &lgp->args,
6396 .rpc_resp = &lgp->res,
6397 };
6398 struct rpc_task_setup task_setup_data = {
6399 .rpc_client = server->client,
6400 .rpc_message = &msg,
6401 .callback_ops = &nfs4_layoutget_call_ops,
6402 .callback_data = lgp,
6403 .flags = RPC_TASK_ASYNC,
6404 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006405 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006406 int status = 0;
6407
6408 dprintk("--> %s\n", __func__);
6409
Idan Kedar85541162012-08-02 11:47:10 +03006410 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6411 if (!lgp->args.layout.pages) {
6412 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006413 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006414 }
6415 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6416
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006417 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006418 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006419 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006420 task = rpc_run_task(&task_setup_data);
6421 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006422 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006423 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006424 if (status == 0)
6425 status = task->tk_status;
6426 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006427 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006428 rpc_put_task(task);
6429 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006430 if (status)
6431 return ERR_PTR(status);
6432 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006433}
6434
Benny Halevycbe82602011-05-22 19:52:37 +03006435static void
6436nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6437{
6438 struct nfs4_layoutreturn *lrp = calldata;
6439
6440 dprintk("--> %s\n", __func__);
6441 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006442 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006443 return;
6444 rpc_call_start(task);
6445}
6446
6447static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6448{
6449 struct nfs4_layoutreturn *lrp = calldata;
6450 struct nfs_server *server;
6451
6452 dprintk("--> %s\n", __func__);
6453
6454 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6455 return;
6456
6457 server = NFS_SERVER(lrp->args.inode);
6458 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006459 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006460 return;
6461 }
Benny Halevycbe82602011-05-22 19:52:37 +03006462 dprintk("<-- %s\n", __func__);
6463}
6464
6465static void nfs4_layoutreturn_release(void *calldata)
6466{
6467 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006468 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006469
6470 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006471 spin_lock(&lo->plh_inode->i_lock);
6472 if (lrp->res.lrs_present)
6473 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6474 lo->plh_block_lgets--;
6475 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006476 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006477 kfree(calldata);
6478 dprintk("<-- %s\n", __func__);
6479}
6480
6481static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6482 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6483 .rpc_call_done = nfs4_layoutreturn_done,
6484 .rpc_release = nfs4_layoutreturn_release,
6485};
6486
6487int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6488{
6489 struct rpc_task *task;
6490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6492 .rpc_argp = &lrp->args,
6493 .rpc_resp = &lrp->res,
6494 };
6495 struct rpc_task_setup task_setup_data = {
6496 .rpc_client = lrp->clp->cl_rpcclient,
6497 .rpc_message = &msg,
6498 .callback_ops = &nfs4_layoutreturn_call_ops,
6499 .callback_data = lrp,
6500 };
6501 int status;
6502
6503 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006504 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006505 task = rpc_run_task(&task_setup_data);
6506 if (IS_ERR(task))
6507 return PTR_ERR(task);
6508 status = task->tk_status;
6509 dprintk("<-- %s status=%d\n", __func__, status);
6510 rpc_put_task(task);
6511 return status;
6512}
6513
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006514/*
6515 * Retrieve the list of Data Server devices from the MDS.
6516 */
6517static int _nfs4_getdevicelist(struct nfs_server *server,
6518 const struct nfs_fh *fh,
6519 struct pnfs_devicelist *devlist)
6520{
6521 struct nfs4_getdevicelist_args args = {
6522 .fh = fh,
6523 .layoutclass = server->pnfs_curr_ld->id,
6524 };
6525 struct nfs4_getdevicelist_res res = {
6526 .devlist = devlist,
6527 };
6528 struct rpc_message msg = {
6529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6530 .rpc_argp = &args,
6531 .rpc_resp = &res,
6532 };
6533 int status;
6534
6535 dprintk("--> %s\n", __func__);
6536 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6537 &res.seq_res, 0);
6538 dprintk("<-- %s status=%d\n", __func__, status);
6539 return status;
6540}
6541
6542int nfs4_proc_getdevicelist(struct nfs_server *server,
6543 const struct nfs_fh *fh,
6544 struct pnfs_devicelist *devlist)
6545{
6546 struct nfs4_exception exception = { };
6547 int err;
6548
6549 do {
6550 err = nfs4_handle_exception(server,
6551 _nfs4_getdevicelist(server, fh, devlist),
6552 &exception);
6553 } while (exception.retry);
6554
6555 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6556 err, devlist->num_devs);
6557
6558 return err;
6559}
6560EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6561
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006562static int
6563_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6564{
6565 struct nfs4_getdeviceinfo_args args = {
6566 .pdev = pdev,
6567 };
6568 struct nfs4_getdeviceinfo_res res = {
6569 .pdev = pdev,
6570 };
6571 struct rpc_message msg = {
6572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6573 .rpc_argp = &args,
6574 .rpc_resp = &res,
6575 };
6576 int status;
6577
6578 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006579 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006580 dprintk("<-- %s status=%d\n", __func__, status);
6581
6582 return status;
6583}
6584
6585int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6586{
6587 struct nfs4_exception exception = { };
6588 int err;
6589
6590 do {
6591 err = nfs4_handle_exception(server,
6592 _nfs4_proc_getdeviceinfo(server, pdev),
6593 &exception);
6594 } while (exception.retry);
6595 return err;
6596}
6597EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6598
Andy Adamson863a3c62011-03-23 13:27:54 +00006599static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6600{
6601 struct nfs4_layoutcommit_data *data = calldata;
6602 struct nfs_server *server = NFS_SERVER(data->args.inode);
6603
6604 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006605 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006606 return;
6607 rpc_call_start(task);
6608}
6609
6610static void
6611nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6612{
6613 struct nfs4_layoutcommit_data *data = calldata;
6614 struct nfs_server *server = NFS_SERVER(data->args.inode);
6615
6616 if (!nfs4_sequence_done(task, &data->res.seq_res))
6617 return;
6618
6619 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006620 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6621 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6622 case -NFS4ERR_BADLAYOUT: /* no layout */
6623 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006624 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006625 break;
6626 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006627 nfs_post_op_update_inode_force_wcc(data->args.inode,
6628 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006629 break;
6630 default:
6631 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6632 rpc_restart_call_prepare(task);
6633 return;
6634 }
6635 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006636}
6637
6638static void nfs4_layoutcommit_release(void *calldata)
6639{
6640 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006641 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006642 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006643
Andy Adamsondb29c082011-07-30 20:52:38 -04006644 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006645 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006646 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6647 list_del_init(&lseg->pls_lc_list);
6648 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6649 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006650 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006651 }
Peng Tao92407e72011-10-23 20:21:17 -07006652
6653 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6654 smp_mb__after_clear_bit();
6655 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6656
Andy Adamson863a3c62011-03-23 13:27:54 +00006657 put_rpccred(data->cred);
6658 kfree(data);
6659}
6660
6661static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6662 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6663 .rpc_call_done = nfs4_layoutcommit_done,
6664 .rpc_release = nfs4_layoutcommit_release,
6665};
6666
6667int
Andy Adamsonef311532011-03-12 02:58:10 -05006668nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006669{
6670 struct rpc_message msg = {
6671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6672 .rpc_argp = &data->args,
6673 .rpc_resp = &data->res,
6674 .rpc_cred = data->cred,
6675 };
6676 struct rpc_task_setup task_setup_data = {
6677 .task = &data->task,
6678 .rpc_client = NFS_CLIENT(data->args.inode),
6679 .rpc_message = &msg,
6680 .callback_ops = &nfs4_layoutcommit_ops,
6681 .callback_data = data,
6682 .flags = RPC_TASK_ASYNC,
6683 };
6684 struct rpc_task *task;
6685 int status = 0;
6686
6687 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6688 "lbw: %llu inode %lu\n",
6689 data->task.tk_pid, sync,
6690 data->args.lastbytewritten,
6691 data->args.inode->i_ino);
6692
Trond Myklebust9d12b212012-01-17 22:04:25 -05006693 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006694 task = rpc_run_task(&task_setup_data);
6695 if (IS_ERR(task))
6696 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006697 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006698 goto out;
6699 status = nfs4_wait_for_completion_rpc_task(task);
6700 if (status != 0)
6701 goto out;
6702 status = task->tk_status;
6703out:
6704 dprintk("%s: status %d\n", __func__, status);
6705 rpc_put_task(task);
6706 return status;
6707}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006708
6709static int
6710_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6711 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6712{
6713 struct nfs41_secinfo_no_name_args args = {
6714 .style = SECINFO_STYLE_CURRENT_FH,
6715 };
6716 struct nfs4_secinfo_res res = {
6717 .flavors = flavors,
6718 };
6719 struct rpc_message msg = {
6720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6721 .rpc_argp = &args,
6722 .rpc_resp = &res,
6723 };
6724 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6725}
6726
6727static int
6728nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6729 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6730{
6731 struct nfs4_exception exception = { };
6732 int err;
6733 do {
6734 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6735 switch (err) {
6736 case 0:
6737 case -NFS4ERR_WRONGSEC:
6738 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006739 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006740 default:
6741 err = nfs4_handle_exception(server, err, &exception);
6742 }
6743 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006744out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006745 return err;
6746}
6747
6748static int
6749nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6750 struct nfs_fsinfo *info)
6751{
6752 int err;
6753 struct page *page;
6754 rpc_authflavor_t flavor;
6755 struct nfs4_secinfo_flavors *flavors;
6756
6757 page = alloc_page(GFP_KERNEL);
6758 if (!page) {
6759 err = -ENOMEM;
6760 goto out;
6761 }
6762
6763 flavors = page_address(page);
6764 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6765
6766 /*
6767 * Fall back on "guess and check" method if
6768 * the server doesn't support SECINFO_NO_NAME
6769 */
6770 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6771 err = nfs4_find_root_sec(server, fhandle, info);
6772 goto out_freepage;
6773 }
6774 if (err)
6775 goto out_freepage;
6776
6777 flavor = nfs_find_best_sec(flavors);
6778 if (err == 0)
6779 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6780
6781out_freepage:
6782 put_page(page);
6783 if (err == -EACCES)
6784 return -EPERM;
6785out:
6786 return err;
6787}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006788
6789static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006790{
6791 int status;
6792 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006793 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006794 };
6795 struct nfs41_test_stateid_res res;
6796 struct rpc_message msg = {
6797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6798 .rpc_argp = &args,
6799 .rpc_resp = &res,
6800 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006801
Chuck Lever38527b12012-07-11 16:30:23 -04006802 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006803 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006804 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006805 if (status != NFS_OK) {
6806 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006807 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006808 }
6809 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006810 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006811}
6812
Chuck Lever38527b12012-07-11 16:30:23 -04006813/**
6814 * nfs41_test_stateid - perform a TEST_STATEID operation
6815 *
6816 * @server: server / transport on which to perform the operation
6817 * @stateid: state ID to test
6818 *
6819 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6820 * Otherwise a negative NFS4ERR value is returned if the operation
6821 * failed or the state ID is not currently valid.
6822 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006823static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006824{
6825 struct nfs4_exception exception = { };
6826 int err;
6827 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006828 err = _nfs41_test_stateid(server, stateid);
6829 if (err != -NFS4ERR_DELAY)
6830 break;
6831 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006832 } while (exception.retry);
6833 return err;
6834}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006835
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006836static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006837{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006838 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006839 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006840 };
6841 struct nfs41_free_stateid_res res;
6842 struct rpc_message msg = {
6843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6844 .rpc_argp = &args,
6845 .rpc_resp = &res,
6846 };
Chuck Lever38527b12012-07-11 16:30:23 -04006847 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006848
Chuck Lever38527b12012-07-11 16:30:23 -04006849 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006850 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006851 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006852 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006853 dprintk("NFS reply free_stateid: %d\n", status);
6854 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006855}
6856
Chuck Lever38527b12012-07-11 16:30:23 -04006857/**
6858 * nfs41_free_stateid - perform a FREE_STATEID operation
6859 *
6860 * @server: server / transport on which to perform the operation
6861 * @stateid: state ID to release
6862 *
6863 * Returns NFS_OK if the server freed "stateid". Otherwise a
6864 * negative NFS4ERR value is returned.
6865 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006866static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006867{
6868 struct nfs4_exception exception = { };
6869 int err;
6870 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006871 err = _nfs4_free_stateid(server, stateid);
6872 if (err != -NFS4ERR_DELAY)
6873 break;
6874 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006875 } while (exception.retry);
6876 return err;
6877}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006878
6879static bool nfs41_match_stateid(const nfs4_stateid *s1,
6880 const nfs4_stateid *s2)
6881{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006882 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006883 return false;
6884
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006885 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006886 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006887 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006888 return true;
6889
6890 return false;
6891}
6892
Andy Adamson557134a2009-04-01 09:21:53 -04006893#endif /* CONFIG_NFS_V4_1 */
6894
Trond Myklebust36281ca2012-03-04 18:13:56 -05006895static bool nfs4_match_stateid(const nfs4_stateid *s1,
6896 const nfs4_stateid *s2)
6897{
Trond Myklebustf597c532012-03-04 18:13:56 -05006898 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006899}
6900
6901
Trond Myklebust17280172012-03-11 13:11:00 -04006902static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006903 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006904 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 .recover_open = nfs4_open_reclaim,
6906 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006907 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006908 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006909 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910};
6911
Andy Adamson591d71c2009-04-01 09:22:47 -04006912#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006913static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006914 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6915 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6916 .recover_open = nfs4_open_reclaim,
6917 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006918 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006919 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006920 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006921 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006922};
6923#endif /* CONFIG_NFS_V4_1 */
6924
Trond Myklebust17280172012-03-11 13:11:00 -04006925static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006926 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006927 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 .recover_open = nfs4_open_expired,
6929 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006930 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006931 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932};
6933
Andy Adamson591d71c2009-04-01 09:22:47 -04006934#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006935static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006936 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6937 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006938 .recover_open = nfs41_open_expired,
6939 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006940 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006941 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006942};
6943#endif /* CONFIG_NFS_V4_1 */
6944
Trond Myklebust17280172012-03-11 13:11:00 -04006945static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006946 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006947 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006948 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006949};
6950
6951#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006952static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006953 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006954 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006955 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006956};
6957#endif
6958
Trond Myklebust97dc1352010-06-16 09:52:26 -04006959static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6960 .minor_version = 0,
6961 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006962 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006963 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006964 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6965 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6966 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006967};
6968
6969#if defined(CONFIG_NFS_V4_1)
6970static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6971 .minor_version = 1,
6972 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006973 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006974 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006975 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6976 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6977 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006978};
6979#endif
6980
Trond Myklebust97dc1352010-06-16 09:52:26 -04006981const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6982 [0] = &nfs_v4_0_minor_ops,
6983#if defined(CONFIG_NFS_V4_1)
6984 [1] = &nfs_v4_1_minor_ops,
6985#endif
6986};
6987
Bryan Schumaker73a79702012-07-16 16:39:12 -04006988const struct inode_operations nfs4_dir_inode_operations = {
6989 .create = nfs_create,
6990 .lookup = nfs_lookup,
6991 .atomic_open = nfs_atomic_open,
6992 .link = nfs_link,
6993 .unlink = nfs_unlink,
6994 .symlink = nfs_symlink,
6995 .mkdir = nfs_mkdir,
6996 .rmdir = nfs_rmdir,
6997 .mknod = nfs_mknod,
6998 .rename = nfs_rename,
6999 .permission = nfs_permission,
7000 .getattr = nfs_getattr,
7001 .setattr = nfs_setattr,
7002 .getxattr = generic_getxattr,
7003 .setxattr = generic_setxattr,
7004 .listxattr = generic_listxattr,
7005 .removexattr = generic_removexattr,
7006};
7007
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007008static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007009 .permission = nfs_permission,
7010 .getattr = nfs_getattr,
7011 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007012 .getxattr = generic_getxattr,
7013 .setxattr = generic_setxattr,
7014 .listxattr = generic_listxattr,
7015 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007016};
7017
David Howells509de812006-08-22 20:06:11 -04007018const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 .version = 4, /* protocol version */
7020 .dentry_ops = &nfs4_dentry_operations,
7021 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007022 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007023 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007025 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007026 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 .getattr = nfs4_proc_getattr,
7028 .setattr = nfs4_proc_setattr,
7029 .lookup = nfs4_proc_lookup,
7030 .access = nfs4_proc_access,
7031 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 .create = nfs4_proc_create,
7033 .remove = nfs4_proc_remove,
7034 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007035 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 .unlink_done = nfs4_proc_unlink_done,
7037 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007038 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007039 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007040 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 .link = nfs4_proc_link,
7042 .symlink = nfs4_proc_symlink,
7043 .mkdir = nfs4_proc_mkdir,
7044 .rmdir = nfs4_proc_remove,
7045 .readdir = nfs4_proc_readdir,
7046 .mknod = nfs4_proc_mknod,
7047 .statfs = nfs4_proc_statfs,
7048 .fsinfo = nfs4_proc_fsinfo,
7049 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007050 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 .decode_dirent = nfs4_decode_dirent,
7052 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007053 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007054 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007055 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007057 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007058 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007059 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007061 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007062 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007064 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007065 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007066 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007067 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007068 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007069 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007070 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007071 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007072 .create_server = nfs4_create_server,
7073 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074};
7075
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007076static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7077 .prefix = XATTR_NAME_NFSV4_ACL,
7078 .list = nfs4_xattr_list_nfs4_acl,
7079 .get = nfs4_xattr_get_nfs4_acl,
7080 .set = nfs4_xattr_set_nfs4_acl,
7081};
7082
7083const struct xattr_handler *nfs4_xattr_handlers[] = {
7084 &nfs4_xattr_nfs4_acl_handler,
7085 NULL
7086};
7087
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088/*
7089 * Local variables:
7090 * c-basic-offset: 8
7091 * End:
7092 */