blob: e9e4d6393f1b3848487964481ef7632a17bce89e [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
Andy Adamsone23008e2012-10-02 21:07:32 -0400155static const u32 nfs4_open_noattr_bitmap[3] = {
156 FATTR4_WORD0_TYPE
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID,
159};
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161const u32 nfs4_statfs_bitmap[2] = {
162 FATTR4_WORD0_FILES_AVAIL
163 | FATTR4_WORD0_FILES_FREE
164 | FATTR4_WORD0_FILES_TOTAL,
165 FATTR4_WORD1_SPACE_AVAIL
166 | FATTR4_WORD1_SPACE_FREE
167 | FATTR4_WORD1_SPACE_TOTAL
168};
169
Trond Myklebust4ce79712005-06-22 17:16:21 +0000170const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 FATTR4_WORD0_MAXLINK
172 | FATTR4_WORD0_MAXNAME,
173 0
174};
175
Fred Isamandae100c2011-07-30 20:52:37 -0400176const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 | FATTR4_WORD0_MAXREAD
178 | FATTR4_WORD0_MAXWRITE
179 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700180 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400181 | FATTR4_WORD1_FS_LAYOUT_TYPES,
182 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Manoj Naik830b8e32006-06-09 09:34:25 -0400185const u32 nfs4_fs_locations_bitmap[2] = {
186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID
191 | FATTR4_WORD0_FS_LOCATIONS,
192 FATTR4_WORD1_MODE
193 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER
195 | FATTR4_WORD1_OWNER_GROUP
196 | FATTR4_WORD1_RAWDEV
197 | FATTR4_WORD1_SPACE_USED
198 | FATTR4_WORD1_TIME_ACCESS
199 | FATTR4_WORD1_TIME_METADATA
200 | FATTR4_WORD1_TIME_MODIFY
201 | FATTR4_WORD1_MOUNTED_ON_FILEID
202};
203
Al Virobc4785c2006-10-19 23:28:51 -0700204static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct nfs4_readdir_arg *readdir)
206{
Al Viro0dbb4c62006-10-19 23:28:49 -0700207 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000210 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
212 return;
213 }
214
215 readdir->cookie = 0;
216 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
217 if (cookie == 2)
218 return;
219
220 /*
221 * NFSv4 servers do not return entries for '.' and '..'
222 * Therefore, we fake these entries here. We let '.'
223 * have cookie 0 and '..' have cookie 1. Note that
224 * when talking to the server, we always send cookie 0
225 * instead of 1 or 2.
226 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800227 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (cookie == 0) {
230 *p++ = xdr_one; /* next */
231 *p++ = xdr_zero; /* cookie, first word */
232 *p++ = xdr_one; /* cookie, second word */
233 *p++ = xdr_one; /* entry len */
234 memcpy(p, ".\0\0\0", 4); /* entry */
235 p++;
236 *p++ = xdr_one; /* bitmap length */
237 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
238 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400239 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
242 *p++ = xdr_one; /* next */
243 *p++ = xdr_zero; /* cookie, first word */
244 *p++ = xdr_two; /* cookie, second word */
245 *p++ = xdr_two; /* entry len */
246 memcpy(p, "..\0\0", 4); /* entry */
247 p++;
248 *p++ = xdr_one; /* bitmap length */
249 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
250 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400251 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 readdir->pgbase = (char *)p - (char *)start;
254 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800255 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Trond Myklebust65de8722008-12-23 15:21:44 -0500258static int nfs4_wait_clnt_recover(struct nfs_client *clp)
259{
260 int res;
261
262 might_sleep();
263
Trond Myklebuste005e802008-12-23 15:21:48 -0500264 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400265 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400266 if (res)
267 return res;
268
269 if (clp->cl_cons_state < 0)
270 return clp->cl_cons_state;
271 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500272}
273
274static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
275{
276 int res = 0;
277
278 might_sleep();
279
280 if (*timeout <= 0)
281 *timeout = NFS4_POLL_RETRY_MIN;
282 if (*timeout > NFS4_POLL_RETRY_MAX)
283 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100284 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 if (fatal_signal_pending(current))
286 res = -ERESTARTSYS;
287 *timeout <<= 1;
288 return res;
289}
290
291/* This is the error handling routine for processes that are allowed
292 * to sleep.
293 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800294static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500295{
296 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500297 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500298 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500299 int ret = errorcode;
300
301 exception->retry = 0;
302 switch(errorcode) {
303 case 0:
304 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500305 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400306 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400307 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500308 exception->retry = 1;
309 return 0;
310 }
311 if (state == NULL)
312 break;
313 nfs4_schedule_stateid_recovery(server, state);
314 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500315 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500316 case -NFS4ERR_ADMIN_REVOKED:
317 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500318 if (state == NULL)
319 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400320 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500321 nfs4_schedule_stateid_recovery(server, state);
322 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400323 case -NFS4ERR_EXPIRED:
324 if (state != NULL)
325 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500327 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500328 nfs4_schedule_lease_recovery(clp);
329 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500330#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400331 case -NFS4ERR_BADSESSION:
332 case -NFS4ERR_BADSLOT:
333 case -NFS4ERR_BAD_HIGH_SLOT:
334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
335 case -NFS4ERR_DEADSESSION:
336 case -NFS4ERR_SEQ_FALSE_RETRY:
337 case -NFS4ERR_SEQ_MISORDERED:
338 dprintk("%s ERROR: %d Reset session\n", __func__,
339 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400340 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400341 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500342#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500343 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500344 if (exception->timeout > HZ) {
345 /* We have retried a decent amount, time to
346 * fail
347 */
348 ret = -EBUSY;
349 break;
350 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500351 case -NFS4ERR_GRACE:
352 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500353 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500354 ret = nfs4_delay(server->client, &exception->timeout);
355 if (ret != 0)
356 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400357 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500358 case -NFS4ERR_OLD_STATEID:
359 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800360 break;
361 case -NFS4ERR_BADOWNER:
362 /* The following works around a Linux server bug! */
363 case -NFS4ERR_BADNAME:
364 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
365 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
366 exception->retry = 1;
367 printk(KERN_WARNING "NFS: v4 server %s "
368 "does not accept raw "
369 "uid/gids. "
370 "Reenabling the idmapper.\n",
371 server->nfs_client->cl_hostname);
372 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500373 }
374 /* We failed to handle the error */
375 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500377 ret = nfs4_wait_clnt_recover(clp);
378 if (ret == 0)
379 exception->retry = 1;
380 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500381}
382
383
Trond Myklebust452e9352010-07-31 14:29:06 -0400384static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock(&clp->cl_lock);
387 if (time_before(clp->cl_last_renewal,timestamp))
388 clp->cl_last_renewal = timestamp;
389 spin_unlock(&clp->cl_lock);
390}
391
Trond Myklebust452e9352010-07-31 14:29:06 -0400392static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
393{
394 do_renew_lease(server->nfs_client, timestamp);
395}
396
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400397#if defined(CONFIG_NFS_V4_1)
398
Benny Halevy510b8172009-04-01 09:22:14 -0400399/*
Trond Myklebust87dda672012-11-20 19:49:20 -0500400 * nfs4_shrink_slot_table - free retired slots from the slot table
401 */
402static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
403{
404 struct nfs4_slot **p;
405 if (newsize >= tbl->max_slots)
406 return;
407
408 p = &tbl->slots;
409 while (newsize--)
410 p = &(*p)->next;
411 while (*p) {
412 struct nfs4_slot *slot = *p;
413
414 *p = slot->next;
415 kfree(slot);
416 tbl->max_slots--;
417 }
418}
419
420/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400421 * nfs4_free_slot - free a slot and efficiently update slot table.
422 *
423 * freeing a slot is trivially done by clearing its respective bit
424 * in the bitmap.
425 * If the freed slotid equals highest_used_slotid we want to update it
426 * so that the server would be able to size down the slot table if needed,
427 * otherwise we know that the highest_used_slotid is still in use.
428 * When updating highest_used_slotid there may be "holes" in the bitmap
429 * so we need to scan down from highest_used_slotid to 0 looking for the now
430 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500431 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500432 *
433 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400434 */
435static void
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500436nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400437{
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500438 u32 slotid = slot->slot_nr;
439
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400440 /* clear used bit in bitmap */
441 __clear_bit(slotid, tbl->used_slots);
442
443 /* update highest_used_slotid when it is freed */
444 if (slotid == tbl->highest_used_slotid) {
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500445 u32 new_max = find_last_bit(tbl->used_slots, slotid);
446 if (new_max < slotid)
447 tbl->highest_used_slotid = new_max;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400448 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500449 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400450 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500451 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
452 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400453}
454
Trond Myklebust961a8282012-01-17 22:57:37 -0500455bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
456{
457 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
458 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400459}
460
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800461/*
Andy Adamson42acd022011-01-06 02:04:34 +0000462 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800463 */
Andy Adamson42acd022011-01-06 02:04:34 +0000464static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800465{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400466 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500467 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
468 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800469 return;
470 }
471
Trond Myklebust45d43c22012-02-06 19:38:51 -0500472 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800473 return;
474
Andy Adamson42acd022011-01-06 02:04:34 +0000475 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
476 complete(&ses->fc_slot_table.complete);
477}
478
479/*
480 * Signal state manager thread if session back channel is drained
481 */
482void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
483{
484 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500485 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000486 return;
487 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
488 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800489}
490
Trond Myklebustd185a332010-06-16 09:52:25 -0400491static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400492{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500493 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400494 struct nfs4_slot_table *tbl;
495
Benny Halevydfb4f3092010-09-24 09:17:01 -0400496 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400497 /* just wake up the next guy waiting since
498 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500499 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500500 return;
Andy Adamson13615872009-04-01 09:22:17 -0400501 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500502 tbl = res->sr_slot->table;
503 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500504
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500505 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500506 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500507 nfs4_check_drain_fc_complete(session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500508 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400509 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400510}
511
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500512/* Update the client's idea of target_highest_slotid */
513static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
514 u32 target_highest_slotid)
515{
Trond Myklebust97e548a2012-11-20 14:45:48 -0500516 unsigned int max_slotid, i;
517
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500518 if (tbl->target_highest_slotid == target_highest_slotid)
519 return;
520 tbl->target_highest_slotid = target_highest_slotid;
521 tbl->generation++;
Trond Myklebust97e548a2012-11-20 14:45:48 -0500522
Trond Myklebust87dda672012-11-20 19:49:20 -0500523 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
Trond Myklebust97e548a2012-11-20 14:45:48 -0500524 for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
525 rpc_wake_up_next(&tbl->slot_tbl_waitq);
526 tbl->max_slotid = max_slotid;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500527}
528
Trond Myklebustce008c42012-11-20 15:16:30 -0500529void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
530 u32 target_highest_slotid)
531{
532 spin_lock(&tbl->slot_tbl_lock);
533 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
534 spin_unlock(&tbl->slot_tbl_lock);
535}
536
Trond Myklebustda0507b2012-11-20 18:10:30 -0500537static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
538 u32 highest_slotid)
539{
Trond Myklebustda0507b2012-11-20 18:10:30 -0500540 if (tbl->server_highest_slotid == highest_slotid)
541 return;
542 if (tbl->highest_used_slotid > highest_slotid)
543 return;
Trond Myklebust87dda672012-11-20 19:49:20 -0500544 /* Deallocate slots */
545 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
Trond Myklebustda0507b2012-11-20 18:10:30 -0500546 tbl->server_highest_slotid = highest_slotid;
547}
548
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500549static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
550 struct nfs4_slot *slot,
551 struct nfs4_sequence_res *res)
552{
553 spin_lock(&tbl->slot_tbl_lock);
554 if (tbl->generation != slot->generation)
555 goto out;
Trond Myklebustda0507b2012-11-20 18:10:30 -0500556 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500557 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
558out:
559 spin_unlock(&tbl->slot_tbl_lock);
560}
561
Trond Myklebust14516c32010-07-31 14:29:06 -0400562static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400563{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500564 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500565 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400566 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400567 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400568
569 /*
570 * sr_status remains 1 if an RPC level error occurred. The server
571 * may or may not have processed the sequence operation..
572 * Proceed as if the server received and processed the sequence
573 * operation.
574 */
575 if (res->sr_status == 1)
576 res->sr_status = NFS_OK;
577
Bryan Schumaker468f8612011-04-18 15:57:32 -0400578 /* don't increment the sequence number if the task wasn't sent */
579 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400580 goto out;
581
Trond Myklebust933602e2012-11-16 12:12:38 -0500582 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500583 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500584
Andy Adamson691daf32009-12-04 15:55:39 -0500585 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400586 switch (res->sr_status) {
587 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400588 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500589 ++slot->seq_nr;
590 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500591 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400592 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500593 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500594 if (res->sr_status_flags != 0)
595 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500596 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400597 break;
598 case -NFS4ERR_DELAY:
599 /* The server detected a resend of the RPC call and
600 * returned NFS4ERR_DELAY as per Section 2.10.6.2
601 * of RFC5661.
602 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500603 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400604 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500605 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500606 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400607 goto out_retry;
608 default:
609 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500610 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400611 }
612out:
613 /* The session may be reset by one of the error handlers. */
614 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400615 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400616 return 1;
617out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400618 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400619 goto out;
620 rpc_delay(task, NFS4_POLL_RETRY_MAX);
621 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400622}
623
Trond Myklebust14516c32010-07-31 14:29:06 -0400624static int nfs4_sequence_done(struct rpc_task *task,
625 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400626{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500627 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400628 return 1;
629 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400630}
631
Trond Myklebust87dda672012-11-20 19:49:20 -0500632static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
633 u32 slotid, u32 seq_init, gfp_t gfp_mask)
634{
635 struct nfs4_slot *slot;
636
637 slot = kzalloc(sizeof(*slot), gfp_mask);
638 if (slot) {
639 slot->table = tbl;
640 slot->slot_nr = slotid;
641 slot->seq_nr = seq_init;
642 }
643 return slot;
644}
645
646static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
647 u32 slotid, u32 seq_init, gfp_t gfp_mask)
648{
649 struct nfs4_slot **p, *slot;
650
651 p = &tbl->slots;
652 for (;;) {
653 if (*p == NULL) {
654 *p = nfs4_new_slot(tbl, tbl->max_slots,
655 seq_init, gfp_mask);
656 if (*p == NULL)
657 break;
658 tbl->max_slots++;
659 }
660 slot = *p;
661 if (slot->slot_nr == slotid)
662 return slot;
663 p = &slot->next;
664 }
Trond Myklebust69d206b2012-11-22 13:21:02 -0500665 return ERR_PTR(-ENOMEM);
Trond Myklebust87dda672012-11-20 19:49:20 -0500666}
667
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400668/*
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500669 * nfs4_alloc_slot - efficiently look for a free slot
Benny Halevy510b8172009-04-01 09:22:14 -0400670 *
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500671 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
Benny Halevy510b8172009-04-01 09:22:14 -0400672 * If found, we mark the slot as used, update the highest_used_slotid,
673 * and respectively set up the sequence operation args.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400674 *
675 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400676 */
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500677static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400678{
Trond Myklebust69d206b2012-11-22 13:21:02 -0500679 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500680 u32 slotid;
Benny Halevy510b8172009-04-01 09:22:14 -0400681
Trond Myklebust45d43c22012-02-06 19:38:51 -0500682 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400683 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Trond Myklebust87dda672012-11-20 19:49:20 -0500684 tbl->max_slotid + 1);
Trond Myklebust97e548a2012-11-20 14:45:48 -0500685 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
686 if (slotid > tbl->max_slotid)
Benny Halevy510b8172009-04-01 09:22:14 -0400687 goto out;
Trond Myklebust87dda672012-11-20 19:49:20 -0500688 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500689 if (IS_ERR(ret))
Trond Myklebust87dda672012-11-20 19:49:20 -0500690 goto out;
Benny Halevy510b8172009-04-01 09:22:14 -0400691 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500692 if (slotid > tbl->highest_used_slotid ||
693 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400694 tbl->highest_used_slotid = slotid;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500695 ret->renewal_time = jiffies;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500696 ret->generation = tbl->generation;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500697
Benny Halevy510b8172009-04-01 09:22:14 -0400698out:
699 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500700 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Trond Myklebust69d206b2012-11-22 13:21:02 -0500701 !IS_ERR(ret) ? ret->slot_nr : -1);
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500702 return ret;
Benny Halevy510b8172009-04-01 09:22:14 -0400703}
704
Trond Myklebust9d12b212012-01-17 22:04:25 -0500705static void nfs41_init_sequence(struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res, int cache_reply)
707{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500708 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500709 args->sa_cache_this = 0;
710 if (cache_reply)
711 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500712 res->sr_slot = NULL;
713}
714
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000715int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716 struct nfs4_sequence_args *args,
717 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400718 struct rpc_task *task)
719{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400720 struct nfs4_slot *slot;
721 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400722
723 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400724 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726 return 0;
727
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400728 tbl = &session->fc_slot_table;
729
Trond Myklebust69d206b2012-11-22 13:21:02 -0500730 task->tk_timeout = 0;
731
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400732 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400733 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800734 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500735 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500736 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400737 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500738 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500739 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400740 }
741
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800742 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
743 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
744 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
745 spin_unlock(&tbl->slot_tbl_lock);
746 dprintk("%s enforce FIFO order\n", __func__);
747 return -EAGAIN;
748 }
749
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500750 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500751 if (IS_ERR(slot)) {
752 /* If out of memory, try again in 1/4 second */
753 if (slot == ERR_PTR(-ENOMEM))
754 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400755 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
756 spin_unlock(&tbl->slot_tbl_lock);
757 dprintk("<-- %s: no free slots\n", __func__);
758 return -EAGAIN;
759 }
760 spin_unlock(&tbl->slot_tbl_lock);
761
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800762 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500763
764 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400765
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500766 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
767 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400768
Benny Halevydfb4f3092010-09-24 09:17:01 -0400769 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400770 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400771 /*
772 * sr_status is only set in decode_sequence, and so will remain
773 * set to 1 if an rpc level failure occurs.
774 */
775 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400776 return 0;
777}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000778EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400779
Trond Myklebust035168a2010-06-16 09:52:26 -0400780int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400781 struct nfs4_sequence_args *args,
782 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400783 struct rpc_task *task)
784{
Trond Myklebust035168a2010-06-16 09:52:26 -0400785 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400786 int ret = 0;
787
Trond Myklebust9d12b212012-01-17 22:04:25 -0500788 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400789 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400790
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500791 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400792 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500793 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400794
Trond Myklebust9d12b212012-01-17 22:04:25 -0500795 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400796out:
797 dprintk("<-- %s status=%d\n", __func__, ret);
798 return ret;
799}
800
801struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400802 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400803 struct nfs4_sequence_args *seq_args;
804 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400805};
806
807static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
808{
809 struct nfs41_call_sync_data *data = calldata;
810
Trond Myklebust035168a2010-06-16 09:52:26 -0400811 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
812
813 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500814 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400815 return;
816 rpc_call_start(task);
817}
818
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800819static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
820{
821 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
822 nfs41_call_sync_prepare(task, calldata);
823}
824
Andy Adamson69ab40c2009-04-01 09:22:19 -0400825static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
826{
827 struct nfs41_call_sync_data *data = calldata;
828
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400830}
831
Trond Myklebust17280172012-03-11 13:11:00 -0400832static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400833 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400834 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400835};
836
Trond Myklebust17280172012-03-11 13:11:00 -0400837static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800838 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
839 .rpc_call_done = nfs41_call_sync_done,
840};
841
Bryan Schumaker7c513052011-03-24 17:12:24 +0000842static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
843 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400844 struct rpc_message *msg,
845 struct nfs4_sequence_args *args,
846 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800847 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400848{
849 int ret;
850 struct rpc_task *task;
851 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400852 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400853 .seq_args = args,
854 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400855 };
856 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000857 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400858 .rpc_message = msg,
859 .callback_ops = &nfs41_call_sync_ops,
860 .callback_data = &data
861 };
862
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800863 if (privileged)
864 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400865 task = rpc_run_task(&task_setup);
866 if (IS_ERR(task))
867 ret = PTR_ERR(task);
868 else {
869 ret = task->tk_status;
870 rpc_put_task(task);
871 }
872 return ret;
873}
874
Bryan Schumaker7c513052011-03-24 17:12:24 +0000875int _nfs4_call_sync_session(struct rpc_clnt *clnt,
876 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400877 struct rpc_message *msg,
878 struct nfs4_sequence_args *args,
879 struct nfs4_sequence_res *res,
880 int cache_reply)
881{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500882 nfs41_init_sequence(args, res, cache_reply);
883 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400884}
885
Trond Myklebustdf896452010-06-16 09:52:26 -0400886#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500887static inline
888void nfs41_init_sequence(struct nfs4_sequence_args *args,
889 struct nfs4_sequence_res *res, int cache_reply)
890{
891}
892
Trond Myklebust14516c32010-07-31 14:29:06 -0400893static int nfs4_sequence_done(struct rpc_task *task,
894 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400895{
Trond Myklebust14516c32010-07-31 14:29:06 -0400896 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400897}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400898#endif /* CONFIG_NFS_V4_1 */
899
Bryan Schumaker7c513052011-03-24 17:12:24 +0000900int _nfs4_call_sync(struct rpc_clnt *clnt,
901 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400902 struct rpc_message *msg,
903 struct nfs4_sequence_args *args,
904 struct nfs4_sequence_res *res,
905 int cache_reply)
906{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500907 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000908 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400909}
910
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000911static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000912int nfs4_call_sync(struct rpc_clnt *clnt,
913 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000914 struct rpc_message *msg,
915 struct nfs4_sequence_args *args,
916 struct nfs4_sequence_res *res,
917 int cache_reply)
918{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000919 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
920 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000921}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400922
Trond Myklebust38478b22006-05-25 01:40:57 -0400923static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Trond Myklebust38478b22006-05-25 01:40:57 -0400925 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Trond Myklebust38478b22006-05-25 01:40:57 -0400927 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400928 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700929 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400930 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700931 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400932 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100935struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400936 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100937 struct nfs_openargs o_arg;
938 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100939 struct nfs_open_confirmargs c_arg;
940 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500941 struct nfs4_string owner_name;
942 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100943 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100944 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400945 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100946 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400947 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100948 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100949 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400950 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100951 int rpc_status;
952 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100953};
954
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400955
956static void nfs4_init_opendata_res(struct nfs4_opendata *p)
957{
958 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400959 p->o_res.seqid = p->o_arg.seqid;
960 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400961 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400962 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400963 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500964 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400965}
966
Al Viro82a2c1b2011-06-22 18:30:55 -0400967static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400969 const struct iattr *attrs,
970 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971{
Al Viro82a2c1b2011-06-22 18:30:55 -0400972 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100973 struct inode *dir = parent->d_inode;
974 struct nfs_server *server = NFS_SERVER(dir);
975 struct nfs4_opendata *p;
976
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400977 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100978 if (p == NULL)
979 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400980 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100981 if (p->o_arg.seqid == NULL)
982 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400983 nfs_sb_active(dentry->d_sb);
984 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100985 p->dir = parent;
986 p->owner = sp;
987 atomic_inc(&sp->so_count);
988 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 p->o_arg.open_flags = flags;
990 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700991 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
992 * will return permission denied for all bits until close */
993 if (!(flags & O_EXCL)) {
994 /* ask server to check for all possible rights as results
995 * are cached */
996 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
997 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700998 }
David Howells7539bba2006-08-22 20:06:09 -0400999 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001000 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1001 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001002 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 p->o_arg.server = server;
1004 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001005 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001007 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -05001008 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001009
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 p->o_arg.u.attrs = &p->attrs;
1011 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001012
1013 verf[0] = jiffies;
1014 verf[1] = current->pid;
1015 memcpy(p->o_arg.u.verifier.data, verf,
1016 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001017 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001018 p->c_arg.fh = &p->o_res.fh;
1019 p->c_arg.stateid = &p->o_res.stateid;
1020 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001021 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001022 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001023 return p;
1024err_free:
1025 kfree(p);
1026err:
1027 dput(parent);
1028 return NULL;
1029}
1030
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001031static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001032{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001033 struct nfs4_opendata *p = container_of(kref,
1034 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001035 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001036
1037 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038 if (p->state != NULL)
1039 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001040 nfs4_put_state_owner(p->owner);
1041 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001042 dput(p->dentry);
1043 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001044 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001045 kfree(p);
1046}
1047
1048static void nfs4_opendata_put(struct nfs4_opendata *p)
1049{
1050 if (p != NULL)
1051 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052}
1053
Trond Myklebust06f814a2006-01-03 09:55:07 +01001054static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1055{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001056 int ret;
1057
Trond Myklebust06f814a2006-01-03 09:55:07 +01001058 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001059 return ret;
1060}
1061
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001063{
1064 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065
Trond Myklebust536e43d2012-01-17 22:04:26 -05001066 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067 goto out;
1068 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001069 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001070 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1071 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001072 break;
1073 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001074 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1075 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001076 break;
1077 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001078 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1079 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001080 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001082 return ret;
1083}
1084
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001087 if (delegation == NULL)
1088 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001090 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001091 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001092 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001093 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 return 1;
1095}
1096
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001097static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001098{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001099 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001100 case FMODE_WRITE:
1101 state->n_wronly++;
1102 break;
1103 case FMODE_READ:
1104 state->n_rdonly++;
1105 break;
1106 case FMODE_READ|FMODE_WRITE:
1107 state->n_rdwr++;
1108 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001110}
1111
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001113{
1114 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001115 nfs4_stateid_copy(&state->stateid, stateid);
1116 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001117 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001118 case FMODE_READ:
1119 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1120 break;
1121 case FMODE_WRITE:
1122 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1123 break;
1124 case FMODE_READ|FMODE_WRITE:
1125 set_bit(NFS_O_RDWR_STATE, &state->flags);
1126 }
1127}
1128
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001129static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001130{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001131 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001133 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001134}
1135
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001136static 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 -07001137{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001138 /*
1139 * Protect the call to nfs4_state_set_mode_locked and
1140 * serialise the stateid update
1141 */
1142 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001143 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001144 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001145 set_bit(NFS_DELEGATED_STATE, &state->flags);
1146 }
1147 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001149 write_sequnlock(&state->seqlock);
1150 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001152 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155static 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 -05001156{
1157 struct nfs_inode *nfsi = NFS_I(state->inode);
1158 struct nfs_delegation *deleg_cur;
1159 int ret = 0;
1160
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001161 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001162
1163 rcu_read_lock();
1164 deleg_cur = rcu_dereference(nfsi->delegation);
1165 if (deleg_cur == NULL)
1166 goto no_delegation;
1167
1168 spin_lock(&deleg_cur->lock);
1169 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001171 goto no_delegation_unlock;
1172
1173 if (delegation == NULL)
1174 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001175 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001176 goto no_delegation_unlock;
1177
Trond Myklebustb7391f42008-12-23 15:21:52 -05001178 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001180 ret = 1;
1181no_delegation_unlock:
1182 spin_unlock(&deleg_cur->lock);
1183no_delegation:
1184 rcu_read_unlock();
1185
1186 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001188 ret = 1;
1189 }
1190
1191 return ret;
1192}
1193
1194
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001196{
1197 struct nfs_delegation *delegation;
1198
1199 rcu_read_lock();
1200 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001201 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001202 rcu_read_unlock();
1203 return;
1204 }
1205 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001206 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001207}
1208
Trond Myklebust6ee41262007-07-08 14:11:36 -04001209static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001210{
1211 struct nfs4_state *state = opendata->state;
1212 struct nfs_inode *nfsi = NFS_I(state->inode);
1213 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001214 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001216 nfs4_stateid stateid;
1217 int ret = -EAGAIN;
1218
Trond Myklebustaac00a82007-07-05 19:02:21 -04001219 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001220 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001221 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222 if (can_open_cached(state, fmode, open_mode)) {
1223 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001224 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001225 goto out_return_state;
1226 }
1227 spin_unlock(&state->owner->so_lock);
1228 }
Trond Myklebust34310432008-12-23 15:21:38 -05001229 rcu_read_lock();
1230 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001231 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001232 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001233 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001234 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001235 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001236 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001237 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001238 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001239 if (ret != 0)
1240 goto out;
1241 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001242
1243 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001244 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001245 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001246 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001247out:
1248 return ERR_PTR(ret);
1249out_return_state:
1250 atomic_inc(&state->count);
1251 return state;
1252}
1253
Andy Adamsone23008e2012-10-02 21:07:32 -04001254static void
1255nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1256{
1257 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1258 struct nfs_delegation *delegation;
1259 int delegation_flags = 0;
1260
1261 rcu_read_lock();
1262 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1263 if (delegation)
1264 delegation_flags = delegation->flags;
1265 rcu_read_unlock();
1266 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1267 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1268 "returning a delegation for "
1269 "OPEN(CLAIM_DELEGATE_CUR)\n",
1270 clp->cl_hostname);
1271 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1272 nfs_inode_set_delegation(state->inode,
1273 data->owner->so_cred,
1274 &data->o_res);
1275 else
1276 nfs_inode_reclaim_delegation(state->inode,
1277 data->owner->so_cred,
1278 &data->o_res);
1279}
1280
1281/*
1282 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1283 * and update the nfs4_state.
1284 */
1285static struct nfs4_state *
1286_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1287{
1288 struct inode *inode = data->state->inode;
1289 struct nfs4_state *state = data->state;
1290 int ret;
1291
1292 if (!data->rpc_done) {
1293 ret = data->rpc_status;
1294 goto err;
1295 }
1296
1297 ret = -ESTALE;
1298 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1299 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1300 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1301 goto err;
1302
1303 ret = -ENOMEM;
1304 state = nfs4_get_open_state(inode, data->owner);
1305 if (state == NULL)
1306 goto err;
1307
1308 ret = nfs_refresh_inode(inode, &data->f_attr);
1309 if (ret)
1310 goto err;
1311
1312 if (data->o_res.delegation_type != 0)
1313 nfs4_opendata_check_deleg(data, state);
1314 update_open_stateid(state, &data->o_res.stateid, NULL,
1315 data->o_arg.fmode);
1316
1317 return state;
1318err:
1319 return ERR_PTR(ret);
1320
1321}
1322
1323static struct nfs4_state *
1324_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001325{
1326 struct inode *inode;
1327 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001328 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001329
Trond Myklebustaac00a82007-07-05 19:02:21 -04001330 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001331 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001332 goto out;
1333 }
1334
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001335 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001336 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001337 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001338 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001339 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001340 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001341 goto err;
1342 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001343 state = nfs4_get_open_state(inode, data->owner);
1344 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001345 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001346 if (data->o_res.delegation_type != 0)
1347 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001348 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001349 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001350 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001351out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001352 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001353err_put_inode:
1354 iput(inode);
1355err:
1356 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357}
1358
Andy Adamsone23008e2012-10-02 21:07:32 -04001359static struct nfs4_state *
1360nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1361{
1362 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1363 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1364 return _nfs4_opendata_to_nfs4_state(data);
1365}
1366
Trond Myklebust864472e2006-01-03 09:55:15 +01001367static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1368{
1369 struct nfs_inode *nfsi = NFS_I(state->inode);
1370 struct nfs_open_context *ctx;
1371
1372 spin_lock(&state->inode->i_lock);
1373 list_for_each_entry(ctx, &nfsi->open_files, list) {
1374 if (ctx->state != state)
1375 continue;
1376 get_nfs_open_context(ctx);
1377 spin_unlock(&state->inode->i_lock);
1378 return ctx;
1379 }
1380 spin_unlock(&state->inode->i_lock);
1381 return ERR_PTR(-ENOENT);
1382}
1383
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001384static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1385{
1386 struct nfs4_opendata *opendata;
1387
Al Viro3d4ff432011-06-22 18:40:12 -04001388 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001389 if (opendata == NULL)
1390 return ERR_PTR(-ENOMEM);
1391 opendata->state = state;
1392 atomic_inc(&state->count);
1393 return opendata;
1394}
1395
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001396static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001397{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001398 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001399 int ret;
1400
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001401 opendata->o_arg.open_flags = 0;
1402 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001403 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1404 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1405 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001406 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001407 if (ret != 0)
1408 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001409 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001410 if (IS_ERR(newstate))
1411 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001412 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001413 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001414 return 0;
1415}
1416
1417static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1418{
Trond Myklebust864472e2006-01-03 09:55:15 +01001419 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001420 int ret;
1421
1422 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001423 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001424 smp_rmb();
1425 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001426 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001427 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001428 if (ret != 0)
1429 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001430 if (newstate != state)
1431 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001432 }
1433 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001434 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001435 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001436 if (ret != 0)
1437 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001438 if (newstate != state)
1439 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001440 }
1441 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001442 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001443 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001444 if (ret != 0)
1445 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001446 if (newstate != state)
1447 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001448 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001449 /*
1450 * We may have performed cached opens for all three recoveries.
1451 * Check if we need to update the current stateid.
1452 */
1453 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001454 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001455 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001456 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001457 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001458 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001459 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001460 return 0;
1461}
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463/*
1464 * OPEN_RECLAIM:
1465 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001467static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001469 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001470 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001471 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int status;
1473
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001474 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1475 if (IS_ERR(opendata))
1476 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001477 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1478 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001479 rcu_read_lock();
1480 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001481 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001482 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001483 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001484 opendata->o_arg.u.delegation_type = delegation_type;
1485 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001486 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return status;
1488}
1489
Trond Myklebust539cd032007-06-05 11:46:42 -04001490static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct nfs_server *server = NFS_SERVER(state->inode);
1493 struct nfs4_exception exception = { };
1494 int err;
1495 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001496 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001497 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001498 break;
1499 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 } while (exception.retry);
1501 return err;
1502}
1503
Trond Myklebust864472e2006-01-03 09:55:15 +01001504static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1505{
1506 struct nfs_open_context *ctx;
1507 int ret;
1508
1509 ctx = nfs4_state_find_open_context(state);
1510 if (IS_ERR(ctx))
1511 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001512 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001513 put_nfs_open_context(ctx);
1514 return ret;
1515}
1516
Trond Myklebust13437e12007-07-06 15:10:43 -04001517static 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 -07001518{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001519 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001520 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001522 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1523 if (IS_ERR(opendata))
1524 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001525 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001526 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001527 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001528 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001529 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Trond Myklebust13437e12007-07-06 15:10:43 -04001532int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001535 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int err;
1537 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001538 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 switch (err) {
1540 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001541 case -ENOENT:
1542 case -ESTALE:
1543 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001544 case -NFS4ERR_BADSESSION:
1545 case -NFS4ERR_BADSLOT:
1546 case -NFS4ERR_BAD_HIGH_SLOT:
1547 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1548 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001549 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 case -NFS4ERR_STALE_CLIENTID:
1552 case -NFS4ERR_STALE_STATEID:
1553 case -NFS4ERR_EXPIRED:
1554 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001555 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001556 goto out;
1557 case -ERESTARTSYS:
1558 /*
1559 * The show must go on: exit, but mark the
1560 * stateid as needing recovery.
1561 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001562 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001563 case -NFS4ERR_ADMIN_REVOKED:
1564 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001565 nfs_inode_find_state_and_recover(state->inode,
1566 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001567 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001568 case -EKEYEXPIRED:
1569 /*
1570 * User RPCSEC_GSS context has expired.
1571 * We cannot recover this stateid now, so
1572 * skip it and allow recovery thread to
1573 * proceed.
1574 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001575 case -ENOMEM:
1576 err = 0;
1577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579 err = nfs4_handle_exception(server, err, &exception);
1580 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001581out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return err;
1583}
1584
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001585static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1586{
1587 struct nfs4_opendata *data = calldata;
1588
1589 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001590 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001591 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001592 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001593 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001594 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001595 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001596}
1597
1598static void nfs4_open_confirm_release(void *calldata)
1599{
1600 struct nfs4_opendata *data = calldata;
1601 struct nfs4_state *state = NULL;
1602
1603 /* If this request hasn't been cancelled, do nothing */
1604 if (data->cancelled == 0)
1605 goto out_free;
1606 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001607 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001608 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001609 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001610 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001611 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001612out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001613 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001614}
1615
1616static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001617 .rpc_call_done = nfs4_open_confirm_done,
1618 .rpc_release = nfs4_open_confirm_release,
1619};
1620
1621/*
1622 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1623 */
1624static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1625{
1626 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1627 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001628 struct rpc_message msg = {
1629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1630 .rpc_argp = &data->c_arg,
1631 .rpc_resp = &data->c_res,
1632 .rpc_cred = data->owner->so_cred,
1633 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001634 struct rpc_task_setup task_setup_data = {
1635 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001636 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001637 .callback_ops = &nfs4_open_confirm_ops,
1638 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001639 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001640 .flags = RPC_TASK_ASYNC,
1641 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int status;
1643
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001644 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001645 data->rpc_done = 0;
1646 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001647 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001648 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001649 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001650 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001651 status = nfs4_wait_for_completion_rpc_task(task);
1652 if (status != 0) {
1653 data->cancelled = 1;
1654 smp_wmb();
1655 } else
1656 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001657 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return status;
1659}
1660
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001663 struct nfs4_opendata *data = calldata;
1664 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001665
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1667 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668 /*
1669 * Check if we still need to send an OPEN call, or if we can use
1670 * a delegation instead.
1671 */
1672 if (data->state != NULL) {
1673 struct nfs_delegation *delegation;
1674
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001675 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001676 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001677 rcu_read_lock();
1678 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001679 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1680 can_open_delegated(delegation, data->o_arg.fmode))
1681 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682 rcu_read_unlock();
1683 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001684 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001685 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001686 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001687 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001688 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001689 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1690 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001691 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001692 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001693 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001694 &data->o_res.seq_res,
1695 task) != 0)
1696 nfs_release_seqid(data->o_arg.seqid);
1697 else
1698 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001699 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001700unlock_no_action:
1701 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001702out_no_action:
1703 task->tk_action = NULL;
1704
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001707static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1708{
1709 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1710 nfs4_open_prepare(task, calldata);
1711}
1712
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001713static void nfs4_open_done(struct rpc_task *task, void *calldata)
1714{
1715 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001717 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001718
Trond Myklebust14516c32010-07-31 14:29:06 -04001719 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1720 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001721
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001722 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001723 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1724 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001725 case S_IFREG:
1726 break;
1727 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001728 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001729 break;
1730 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001731 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001732 break;
1733 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001734 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001735 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001736 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001737 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001738 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1739 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001740 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001741 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001742}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001743
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001744static void nfs4_open_release(void *calldata)
1745{
1746 struct nfs4_opendata *data = calldata;
1747 struct nfs4_state *state = NULL;
1748
1749 /* If this request hasn't been cancelled, do nothing */
1750 if (data->cancelled == 0)
1751 goto out_free;
1752 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001753 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 goto out_free;
1755 /* In case we need an open_confirm, no cleanup! */
1756 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1757 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001758 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001759 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001760 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001762 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001763}
1764
1765static const struct rpc_call_ops nfs4_open_ops = {
1766 .rpc_call_prepare = nfs4_open_prepare,
1767 .rpc_call_done = nfs4_open_done,
1768 .rpc_release = nfs4_open_release,
1769};
1770
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001771static const struct rpc_call_ops nfs4_recover_open_ops = {
1772 .rpc_call_prepare = nfs4_recover_open_prepare,
1773 .rpc_call_done = nfs4_open_done,
1774 .rpc_release = nfs4_open_release,
1775};
1776
1777static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778{
1779 struct inode *dir = data->dir->d_inode;
1780 struct nfs_server *server = NFS_SERVER(dir);
1781 struct nfs_openargs *o_arg = &data->o_arg;
1782 struct nfs_openres *o_res = &data->o_res;
1783 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001784 struct rpc_message msg = {
1785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1786 .rpc_argp = o_arg,
1787 .rpc_resp = o_res,
1788 .rpc_cred = data->owner->so_cred,
1789 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001790 struct rpc_task_setup task_setup_data = {
1791 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001792 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001793 .callback_ops = &nfs4_open_ops,
1794 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001795 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001796 .flags = RPC_TASK_ASYNC,
1797 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001798 int status;
1799
Trond Myklebust9d12b212012-01-17 22:04:25 -05001800 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001801 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001802 data->rpc_done = 0;
1803 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001804 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001805 if (isrecover)
1806 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001807 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001808 if (IS_ERR(task))
1809 return PTR_ERR(task);
1810 status = nfs4_wait_for_completion_rpc_task(task);
1811 if (status != 0) {
1812 data->cancelled = 1;
1813 smp_wmb();
1814 } else
1815 status = data->rpc_status;
1816 rpc_put_task(task);
1817
1818 return status;
1819}
1820
1821static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1822{
1823 struct inode *dir = data->dir->d_inode;
1824 struct nfs_openres *o_res = &data->o_res;
1825 int status;
1826
1827 status = nfs4_run_open_task(data, 1);
1828 if (status != 0 || !data->rpc_done)
1829 return status;
1830
Trond Myklebust6926afd2012-01-07 13:22:46 -05001831 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1832
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001833 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1834 status = _nfs4_proc_open_confirm(data);
1835 if (status != 0)
1836 return status;
1837 }
1838
1839 return status;
1840}
1841
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001842static int nfs4_opendata_access(struct rpc_cred *cred,
1843 struct nfs4_opendata *opendata,
1844 struct nfs4_state *state, fmode_t fmode)
1845{
1846 struct nfs_access_entry cache;
1847 u32 mask;
1848
1849 /* access call failed or for some reason the server doesn't
1850 * support any access modes -- defer access call until later */
1851 if (opendata->o_res.access_supported == 0)
1852 return 0;
1853
1854 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001855 /* don't check MAY_WRITE - a newly created file may not have
1856 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001857 if (fmode & FMODE_READ)
1858 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001859 if (fmode & FMODE_EXEC)
1860 mask |= MAY_EXEC;
1861
1862 cache.cred = cred;
1863 cache.jiffies = jiffies;
1864 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1865 nfs_access_add_cache(state->inode, &cache);
1866
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001867 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001868 return 0;
1869
1870 /* even though OPEN succeeded, access is denied. Close the file */
1871 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001872 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001873}
1874
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001875/*
1876 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1877 */
1878static int _nfs4_proc_open(struct nfs4_opendata *data)
1879{
1880 struct inode *dir = data->dir->d_inode;
1881 struct nfs_server *server = NFS_SERVER(dir);
1882 struct nfs_openargs *o_arg = &data->o_arg;
1883 struct nfs_openres *o_res = &data->o_res;
1884 int status;
1885
1886 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001887 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001888 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001889 if (status != 0) {
1890 if (status == -NFS4ERR_BADNAME &&
1891 !(o_arg->open_flags & O_CREAT))
1892 return -ENOENT;
1893 return status;
1894 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001895
Trond Myklebust6926afd2012-01-07 13:22:46 -05001896 nfs_fattr_map_and_free_names(server, &data->f_attr);
1897
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001898 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001899 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001900 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1901 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001903 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001905 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001908 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Andy Adamsond83217c2011-03-01 01:34:17 +00001912static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001913{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001914 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001915 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001916
Trond Myklebusta78cb572009-08-09 15:06:19 -04001917 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001918 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001919 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001920 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001921 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1922 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001923 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001924 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001925 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001926 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001927 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001928}
1929
Andy Adamsond83217c2011-03-01 01:34:17 +00001930static int nfs4_recover_expired_lease(struct nfs_server *server)
1931{
1932 return nfs4_client_recover_expired_lease(server->nfs_client);
1933}
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935/*
1936 * OPEN_EXPIRED:
1937 * reclaim state on the server after a network partition.
1938 * Assumes caller holds the appropriate lock
1939 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001940static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001942 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001943 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001945 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1946 if (IS_ERR(opendata))
1947 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001948 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001949 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001950 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001951 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001955static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001956{
Trond Myklebust539cd032007-06-05 11:46:42 -04001957 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001958 struct nfs4_exception exception = { };
1959 int err;
1960
1961 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001962 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001963 switch (err) {
1964 default:
1965 goto out;
1966 case -NFS4ERR_GRACE:
1967 case -NFS4ERR_DELAY:
1968 nfs4_handle_exception(server, err, &exception);
1969 err = 0;
1970 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001971 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001972out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001973 return err;
1974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1977{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001979 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Trond Myklebust864472e2006-01-03 09:55:15 +01001981 ctx = nfs4_state_find_open_context(state);
1982 if (IS_ERR(ctx))
1983 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001984 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001985 put_nfs_open_context(ctx);
1986 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001989#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001990static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001991{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001992 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001993 nfs4_stateid *stateid = &state->stateid;
1994 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001995
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001996 /* If a state reset has been done, test_stateid is unneeded */
1997 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1998 return;
1999
2000 status = nfs41_test_stateid(server, stateid);
2001 if (status != NFS_OK) {
2002 /* Free the stateid unless the server explicitly
2003 * informs us the stateid is unrecognized. */
2004 if (status != -NFS4ERR_BAD_STATEID)
2005 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04002006 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002007
Bryan Schumaker69388672012-09-26 15:25:52 -04002008 write_seqlock(&state->seqlock);
2009 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2010 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002011 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2012 }
2013}
2014
2015/**
2016 * nfs41_check_open_stateid - possibly free an open stateid
2017 *
2018 * @state: NFSv4 state for an inode
2019 *
2020 * Returns NFS_OK if recovery for this stateid is now finished.
2021 * Otherwise a negative NFS4ERR value is returned.
2022 */
2023static int nfs41_check_open_stateid(struct nfs4_state *state)
2024{
2025 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002026 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002027 int status;
2028
2029 /* If a state reset has been done, test_stateid is unneeded */
2030 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2031 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2032 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2033 return -NFS4ERR_BAD_STATEID;
2034
2035 status = nfs41_test_stateid(server, stateid);
2036 if (status != NFS_OK) {
2037 /* Free the stateid unless the server explicitly
2038 * informs us the stateid is unrecognized. */
2039 if (status != -NFS4ERR_BAD_STATEID)
2040 nfs41_free_stateid(server, stateid);
2041
2042 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2043 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2044 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002045 }
2046 return status;
2047}
2048
2049static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2050{
Chuck Levereb64cf92012-07-11 16:30:05 -04002051 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002052
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002053 nfs41_clear_delegation_stateid(state);
2054 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002055 if (status != NFS_OK)
2056 status = nfs4_open_expired(sp, state);
2057 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002058}
2059#endif
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002062 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2063 * fields corresponding to attributes that were used to store the verifier.
2064 * Make sure we clobber those fields in the later setattr call
2065 */
2066static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2067{
2068 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2069 !(sattr->ia_valid & ATTR_ATIME_SET))
2070 sattr->ia_valid |= ATTR_ATIME;
2071
2072 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2073 !(sattr->ia_valid & ATTR_MTIME_SET))
2074 sattr->ia_valid |= ATTR_MTIME;
2075}
2076
2077/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002078 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 */
Andy Adamson82be4172012-05-23 05:02:35 -04002080static int _nfs4_do_open(struct inode *dir,
2081 struct dentry *dentry,
2082 fmode_t fmode,
2083 int flags,
2084 struct iattr *sattr,
2085 struct rpc_cred *cred,
2086 struct nfs4_state **res,
2087 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 struct nfs4_state_owner *sp;
2090 struct nfs4_state *state = NULL;
2091 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002092 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002093 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002097 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2098 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2100 goto out_err;
2101 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002102 status = nfs4_recover_expired_lease(server);
2103 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002104 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002105 if (dentry->d_inode != NULL)
2106 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002107 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04002108 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002109 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002110 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Andy Adamson82be4172012-05-23 05:02:35 -04002112 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2113 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2114 if (!opendata->f_attr.mdsthreshold)
2115 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002116 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002117 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002118 if (dentry->d_inode != NULL)
2119 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002120
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002121 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002123 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002125 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002126 status = PTR_ERR(state);
2127 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002128 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002129 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002130 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002131
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002132 status = nfs4_opendata_access(cred, opendata, state, fmode);
2133 if (status != 0)
2134 goto err_opendata_put;
2135
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002136 if (opendata->o_arg.open_flags & O_EXCL) {
2137 nfs4_exclusive_attrset(opendata, sattr);
2138
2139 nfs_fattr_init(opendata->o_res.f_attr);
2140 status = nfs4_do_setattr(state->inode, cred,
2141 opendata->o_res.f_attr, sattr,
2142 state);
2143 if (status == 0)
2144 nfs_setattr_update_inode(state->inode, sattr);
2145 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2146 }
Andy Adamson82be4172012-05-23 05:02:35 -04002147
2148 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2149 *ctx_th = opendata->f_attr.mdsthreshold;
2150 else
2151 kfree(opendata->f_attr.mdsthreshold);
2152 opendata->f_attr.mdsthreshold = NULL;
2153
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002154 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 *res = state;
2157 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002158err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002159 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002160 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002161err_put_state_owner:
2162 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 *res = NULL;
2165 return status;
2166}
2167
2168
Andy Adamson82be4172012-05-23 05:02:35 -04002169static struct nfs4_state *nfs4_do_open(struct inode *dir,
2170 struct dentry *dentry,
2171 fmode_t fmode,
2172 int flags,
2173 struct iattr *sattr,
2174 struct rpc_cred *cred,
2175 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 struct nfs4_exception exception = { };
2178 struct nfs4_state *res;
2179 int status;
2180
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002181 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002183 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2184 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (status == 0)
2186 break;
2187 /* NOTE: BAD_SEQID means the server and client disagree about the
2188 * book-keeping w.r.t. state-changing operations
2189 * (OPEN/CLOSE/LOCK/LOCKU...)
2190 * It is actually a sign of a bug on the client or on the server.
2191 *
2192 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002193 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 * have unhashed the old state_owner for us, and that we can
2195 * therefore safely retry using a new one. We should still warn
2196 * the user though...
2197 */
2198 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002199 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002200 " returned a bad sequence-id error!\n",
2201 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 exception.retry = 1;
2203 continue;
2204 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002205 /*
2206 * BAD_STATEID on OPEN means that the server cancelled our
2207 * state before it received the OPEN_CONFIRM.
2208 * Recover by retrying the request as per the discussion
2209 * on Page 181 of RFC3530.
2210 */
2211 if (status == -NFS4ERR_BAD_STATEID) {
2212 exception.retry = 1;
2213 continue;
2214 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002215 if (status == -EAGAIN) {
2216 /* We must have found a delegation */
2217 exception.retry = 1;
2218 continue;
2219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2221 status, &exception));
2222 } while (exception.retry);
2223 return res;
2224}
2225
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002226static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2227 struct nfs_fattr *fattr, struct iattr *sattr,
2228 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002230 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002232 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 .iap = sattr,
2234 .server = server,
2235 .bitmask = server->attr_bitmask,
2236 };
2237 struct nfs_setattrres res = {
2238 .fattr = fattr,
2239 .server = server,
2240 };
2241 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2243 .rpc_argp = &arg,
2244 .rpc_resp = &res,
2245 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002247 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002248 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Trond Myklebust0e574af2005-10-27 22:12:38 -04002250 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Trond Myklebust4fc87962012-03-08 17:42:01 -05002252 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002253 struct nfs_lockowner lockowner = {
2254 .l_owner = current->files,
2255 .l_pid = current->tgid,
2256 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002257 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002258 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002259 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2260 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002261 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002262 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002263 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Bryan Schumaker7c513052011-03-24 17:12:24 +00002265 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002266 if (status == 0 && state != NULL)
2267 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002268 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002271static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2272 struct nfs_fattr *fattr, struct iattr *sattr,
2273 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002275 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002276 struct nfs4_exception exception = {
2277 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002278 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002279 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 int err;
2281 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002282 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2283 switch (err) {
2284 case -NFS4ERR_OPENMODE:
2285 if (state && !(state->state & FMODE_WRITE)) {
2286 err = -EBADF;
2287 if (sattr->ia_valid & ATTR_OPEN)
2288 err = -EACCES;
2289 goto out;
2290 }
2291 }
2292 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002294out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return err;
2296}
2297
2298struct nfs4_closedata {
2299 struct inode *inode;
2300 struct nfs4_state *state;
2301 struct nfs_closeargs arg;
2302 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002303 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002304 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002305 bool roc;
2306 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307};
2308
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002309static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002310{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002311 struct nfs4_closedata *calldata = data;
2312 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002313 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002314
Fred Isamanf7e89172011-01-06 11:36:32 +00002315 if (calldata->roc)
2316 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002317 nfs4_put_open_state(calldata->state);
2318 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002319 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002320 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002321 kfree(calldata);
2322}
2323
Trond Myklebust88069f72009-12-08 08:33:16 -05002324static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2325 fmode_t fmode)
2326{
2327 spin_lock(&state->owner->so_lock);
2328 if (!(fmode & FMODE_READ))
2329 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2330 if (!(fmode & FMODE_WRITE))
2331 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2332 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2333 spin_unlock(&state->owner->so_lock);
2334}
2335
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002336static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002338 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct nfs_server *server = NFS_SERVER(calldata->inode);
2341
Chuck Levera3ca5652012-03-01 17:00:40 -05002342 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002343 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /* hmm. we are done with the inode, and in the process of freeing
2346 * the state_owner. we keep this around to process errors
2347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 switch (task->tk_status) {
2349 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002350 if (calldata->roc)
2351 pnfs_roc_set_barrier(state->inode,
2352 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002353 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002354 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002355 nfs4_close_clear_stateid_flags(state,
2356 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 break;
2358 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002359 case -NFS4ERR_OLD_STATEID:
2360 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002362 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002365 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2366 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002368 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002369 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002370 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002373static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002375 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002376 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002377 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002378 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002379
Chuck Levera3ca5652012-03-01 17:00:40 -05002380 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002381 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002382 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002383
Trond Myklebust88069f72009-12-08 08:33:16 -05002384 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2385 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002386 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002387 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002388 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002389 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002390 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2391 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2392 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002393 }
2394 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002395 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2396 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2397 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002398 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002399 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002400 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002401
2402 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002403 /* Note: exit _without_ calling nfs4_close_done */
2404 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002405 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002406 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002407
Fred Isamanf7e89172011-01-06 11:36:32 +00002408 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002409 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002410 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002411 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002412 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002413 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002414
Trond Myklebust516a6af2005-10-27 22:12:41 -04002415 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002416 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002417 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002418 &calldata->arg.seq_args,
2419 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002420 task) != 0)
2421 nfs_release_seqid(calldata->arg.seqid);
2422 else
2423 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002424out:
2425 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002428static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002429 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002430 .rpc_call_done = nfs4_close_done,
2431 .rpc_release = nfs4_free_closedata,
2432};
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434/*
2435 * It is possible for data to be read/written from a mem-mapped file
2436 * after the sys_close call (which hits the vfs layer as a flush).
2437 * This means that we can't safely call nfsv4 close on a file until
2438 * the inode is cleared. This in turn means that we are not good
2439 * NFSv4 citizens - we do not indicate to the server to update the file's
2440 * share state even when we are done with one of the three share
2441 * stateid's in the inode.
2442 *
2443 * NOTE: Caller must be holding the sp->so_owner semaphore!
2444 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002445int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002447 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002449 struct nfs4_state_owner *sp = state->owner;
2450 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002451 struct rpc_message msg = {
2452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2453 .rpc_cred = state->owner->so_cred,
2454 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002455 struct rpc_task_setup task_setup_data = {
2456 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002457 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002458 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002459 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002460 .flags = RPC_TASK_ASYNC,
2461 };
Trond Myklebust95121352005-10-18 14:20:12 -07002462 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002464 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002466 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002467 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002468 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002470 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002471 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002473 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002474 if (calldata->arg.seqid == NULL)
2475 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002476 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002477 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002478 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002479 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002480 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002481 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002482 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002483
Trond Myklebust1174dd12010-12-21 10:52:24 -05002484 msg.rpc_argp = &calldata->arg;
2485 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002486 task_setup_data.callback_data = calldata;
2487 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002488 if (IS_ERR(task))
2489 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002490 status = 0;
2491 if (wait)
2492 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002493 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002494 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002495out_free_calldata:
2496 kfree(calldata);
2497out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002498 nfs4_put_open_state(state);
2499 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002500 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Trond Myklebust2b484292010-09-17 10:56:51 -04002503static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002504nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 struct nfs4_state *state;
2507
Trond Myklebust565277f2007-10-15 18:17:53 -04002508 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002509 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2510 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002511 if (IS_ERR(state))
2512 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002513 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002514 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
Trond Myklebust1185a552009-12-03 15:54:02 -05002517static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002518{
2519 if (ctx->state == NULL)
2520 return;
2521 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002522 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002523 else
Al Viro643168c2011-06-22 18:20:23 -04002524 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2528{
Benny Halevy43652ad2009-04-01 09:21:54 -04002529 struct nfs4_server_caps_arg args = {
2530 .fhandle = fhandle,
2531 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 struct nfs4_server_caps_res res = {};
2533 struct rpc_message msg = {
2534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002535 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 .rpc_resp = &res,
2537 };
2538 int status;
2539
Bryan Schumaker7c513052011-03-24 17:12:24 +00002540 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (status == 0) {
2542 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002543 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2544 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2545 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2546 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2547 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2549 server->caps |= NFS_CAP_ACLS;
2550 if (res.has_links != 0)
2551 server->caps |= NFS_CAP_HARDLINKS;
2552 if (res.has_symlinks != 0)
2553 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002554 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2555 server->caps |= NFS_CAP_FILEID;
2556 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2557 server->caps |= NFS_CAP_MODE;
2558 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2559 server->caps |= NFS_CAP_NLINK;
2560 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2561 server->caps |= NFS_CAP_OWNER;
2562 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2563 server->caps |= NFS_CAP_OWNER_GROUP;
2564 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2565 server->caps |= NFS_CAP_ATIME;
2566 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2567 server->caps |= NFS_CAP_CTIME;
2568 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2569 server->caps |= NFS_CAP_MTIME;
2570
Trond Myklebusta65318b2009-03-11 14:10:28 -04002571 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2572 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2573 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002575 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return status;
2579}
2580
Trond Myklebust55a97592006-06-09 09:34:19 -04002581int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
2583 struct nfs4_exception exception = { };
2584 int err;
2585 do {
2586 err = nfs4_handle_exception(server,
2587 _nfs4_server_capabilities(server, fhandle),
2588 &exception);
2589 } while (exception.retry);
2590 return err;
2591}
2592
2593static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2594 struct nfs_fsinfo *info)
2595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 struct nfs4_lookup_root_arg args = {
2597 .bitmask = nfs4_fattr_bitmap,
2598 };
2599 struct nfs4_lookup_res res = {
2600 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002601 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 .fh = fhandle,
2603 };
2604 struct rpc_message msg = {
2605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2606 .rpc_argp = &args,
2607 .rpc_resp = &res,
2608 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002609
Trond Myklebust0e574af2005-10-27 22:12:38 -04002610 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002611 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
2614static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2615 struct nfs_fsinfo *info)
2616{
2617 struct nfs4_exception exception = { };
2618 int err;
2619 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002620 err = _nfs4_lookup_root(server, fhandle, info);
2621 switch (err) {
2622 case 0:
2623 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002624 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002625 default:
2626 err = nfs4_handle_exception(server, err, &exception);
2627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002629out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return err;
2631}
2632
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002633static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2634 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2635{
2636 struct rpc_auth *auth;
2637 int ret;
2638
2639 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002640 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002641 ret = -EIO;
2642 goto out;
2643 }
2644 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002645out:
2646 return ret;
2647}
2648
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002649static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002650 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002652 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002653 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002655 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002656 if (len < 0)
2657 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002658
2659 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002660 /* AUTH_UNIX is the default flavor if none was specified,
2661 * thus has already been tried. */
2662 if (flav_array[i] == RPC_AUTH_UNIX)
2663 continue;
2664
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002665 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002666 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002667 continue;
2668 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002669 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002670 /*
2671 * -EACCESS could mean that the user doesn't have correct permissions
2672 * to access the mount. It could also mean that we tried to mount
2673 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2674 * existing mount programs don't handle -EACCES very well so it should
2675 * be mapped to -EPERM instead.
2676 */
2677 if (status == -EACCES)
2678 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002679 return status;
2680}
2681
2682/*
2683 * get the file handle for the "/" directory on the server
2684 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002685int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2686 struct nfs_fsinfo *info)
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002687{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002688 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002689 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002690 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2691 /*
2692 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2693 * by nfs4_map_errors() as this function exits.
2694 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002695 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (status == 0)
2697 status = nfs4_server_capabilities(server, fhandle);
2698 if (status == 0)
2699 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002700 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Bryan Schumakerbae36242012-05-10 15:07:31 -04002703static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2704 struct nfs_fsinfo *info)
2705{
2706 int error;
2707 struct nfs_fattr *fattr = info->fattr;
2708
2709 error = nfs4_server_capabilities(server, mntfh);
2710 if (error < 0) {
2711 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2712 return error;
2713 }
2714
2715 error = nfs4_proc_getattr(server, mntfh, fattr);
2716 if (error < 0) {
2717 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2718 return error;
2719 }
2720
2721 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2722 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2723 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2724
2725 return error;
2726}
2727
Manoj Naik6b97fd32006-06-09 09:34:29 -04002728/*
2729 * Get locations and (maybe) other attributes of a referral.
2730 * Note that we'll actually follow the referral later when
2731 * we detect fsid mismatch in inode revalidation
2732 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002733static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2734 const struct qstr *name, struct nfs_fattr *fattr,
2735 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002736{
2737 int status = -ENOMEM;
2738 struct page *page = NULL;
2739 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002740
2741 page = alloc_page(GFP_KERNEL);
2742 if (page == NULL)
2743 goto out;
2744 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2745 if (locations == NULL)
2746 goto out;
2747
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002748 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002749 if (status != 0)
2750 goto out;
2751 /* Make sure server returned a different fsid for the referral */
2752 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002753 dprintk("%s: server did not return a different fsid for"
2754 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002755 status = -EIO;
2756 goto out;
2757 }
Andy Adamson533eb462011-06-13 18:25:56 -04002758 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2759 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002760
Andy Adamson533eb462011-06-13 18:25:56 -04002761 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002762 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002763 memset(fhandle, 0, sizeof(struct nfs_fh));
2764out:
2765 if (page)
2766 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002767 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002768 return status;
2769}
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2772{
2773 struct nfs4_getattr_arg args = {
2774 .fh = fhandle,
2775 .bitmask = server->attr_bitmask,
2776 };
2777 struct nfs4_getattr_res res = {
2778 .fattr = fattr,
2779 .server = server,
2780 };
2781 struct rpc_message msg = {
2782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2783 .rpc_argp = &args,
2784 .rpc_resp = &res,
2785 };
2786
Trond Myklebust0e574af2005-10-27 22:12:38 -04002787 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002788 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
2791static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2792{
2793 struct nfs4_exception exception = { };
2794 int err;
2795 do {
2796 err = nfs4_handle_exception(server,
2797 _nfs4_proc_getattr(server, fhandle, fattr),
2798 &exception);
2799 } while (exception.retry);
2800 return err;
2801}
2802
2803/*
2804 * The file is not closed if it is opened due to the a request to change
2805 * the size of the file. The open call will not be needed once the
2806 * VFS layer lookup-intents are implemented.
2807 *
2808 * Close is called when the inode is destroyed.
2809 * If we haven't opened the file for O_WRONLY, we
2810 * need to in the size_change case to obtain a stateid.
2811 *
2812 * Got race?
2813 * Because OPEN is always done by name in nfsv4, it is
2814 * possible that we opened a different file by the same
2815 * name. We can recognize this race condition, but we
2816 * can't do anything about it besides returning an error.
2817 *
2818 * This will be fixed with VFS changes (lookup-intent).
2819 */
2820static int
2821nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2822 struct iattr *sattr)
2823{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002824 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002825 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002826 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 int status;
2828
Benny Halevy8a1636c2010-07-14 15:43:57 -04002829 if (pnfs_ld_layoutret_on_setattr(inode))
2830 pnfs_return_layout(inode);
2831
Trond Myklebust0e574af2005-10-27 22:12:38 -04002832 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Andy Adamson26699402012-05-30 16:12:24 -04002834 /* Deal with open(O_TRUNC) */
2835 if (sattr->ia_valid & ATTR_OPEN)
2836 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2837
2838 /* Optimization: if the end result is no change, don't RPC */
2839 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2840 return 0;
2841
Trond Myklebustd5308382005-11-04 15:33:38 -05002842 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002843 if (sattr->ia_valid & ATTR_FILE) {
2844 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002845
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002846 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002847 if (ctx) {
2848 cred = ctx->cred;
2849 state = ctx->state;
2850 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002851 }
2852
2853 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002854 if (status == 0)
2855 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return status;
2857}
2858
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002859static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2860 const struct qstr *name, struct nfs_fh *fhandle,
2861 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002862{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002863 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002864 int status;
2865 struct nfs4_lookup_arg args = {
2866 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002867 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002868 .name = name,
2869 };
2870 struct nfs4_lookup_res res = {
2871 .server = server,
2872 .fattr = fattr,
2873 .fh = fhandle,
2874 };
2875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2877 .rpc_argp = &args,
2878 .rpc_resp = &res,
2879 };
2880
2881 nfs_fattr_init(fattr);
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002884 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 dprintk("NFS reply lookup: %d\n", status);
2886 return status;
2887}
2888
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002889static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002890{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002891 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002892 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002893 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2894 fattr->nlink = 2;
2895}
2896
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002897static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2898 struct qstr *name, struct nfs_fh *fhandle,
2899 struct nfs_fattr *fattr)
2900{
2901 struct nfs4_exception exception = { };
2902 struct rpc_clnt *client = *clnt;
2903 int err;
2904 do {
2905 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2906 switch (err) {
2907 case -NFS4ERR_BADNAME:
2908 err = -ENOENT;
2909 goto out;
2910 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002911 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002912 goto out;
2913 case -NFS4ERR_WRONGSEC:
2914 err = -EPERM;
2915 if (client != *clnt)
2916 goto out;
2917
2918 client = nfs4_create_sec_client(client, dir, name);
2919 if (IS_ERR(client))
2920 return PTR_ERR(client);
2921
2922 exception.retry = 1;
2923 break;
2924 default:
2925 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2926 }
2927 } while (exception.retry);
2928
2929out:
2930 if (err == 0)
2931 *clnt = client;
2932 else if (client != *clnt)
2933 rpc_shutdown_client(client);
2934
2935 return err;
2936}
2937
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002938static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002939 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002941 int status;
2942 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002943
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002944 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2945 if (client != NFS_CLIENT(dir)) {
2946 rpc_shutdown_client(client);
2947 nfs_fixup_secinfo_attributes(fattr);
2948 }
2949 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002952struct rpc_clnt *
2953nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2954 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2955{
2956 int status;
2957 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2958
2959 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2960 if (status < 0) {
2961 rpc_shutdown_client(client);
2962 return ERR_PTR(status);
2963 }
2964 return client;
2965}
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2968{
Trond Myklebust76b32992007-08-10 17:45:11 -04002969 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 struct nfs4_accessargs args = {
2971 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002972 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002974 struct nfs4_accessres res = {
2975 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002976 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct rpc_message msg = {
2978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2979 .rpc_argp = &args,
2980 .rpc_resp = &res,
2981 .rpc_cred = entry->cred,
2982 };
2983 int mode = entry->mask;
2984 int status;
2985
2986 /*
2987 * Determine which access bits we want to ask for...
2988 */
2989 if (mode & MAY_READ)
2990 args.access |= NFS4_ACCESS_READ;
2991 if (S_ISDIR(inode->i_mode)) {
2992 if (mode & MAY_WRITE)
2993 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2994 if (mode & MAY_EXEC)
2995 args.access |= NFS4_ACCESS_LOOKUP;
2996 } else {
2997 if (mode & MAY_WRITE)
2998 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2999 if (mode & MAY_EXEC)
3000 args.access |= NFS4_ACCESS_EXECUTE;
3001 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003002
3003 res.fattr = nfs_alloc_fattr();
3004 if (res.fattr == NULL)
3005 return -ENOMEM;
3006
Bryan Schumaker7c513052011-03-24 17:12:24 +00003007 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003009 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003010 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003012 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return status;
3014}
3015
3016static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020 do {
3021 err = nfs4_handle_exception(NFS_SERVER(inode),
3022 _nfs4_proc_access(inode, entry),
3023 &exception);
3024 } while (exception.retry);
3025 return err;
3026}
3027
3028/*
3029 * TODO: For the time being, we don't try to get any attributes
3030 * along with any of the zero-copy operations READ, READDIR,
3031 * READLINK, WRITE.
3032 *
3033 * In the case of the first three, we want to put the GETATTR
3034 * after the read-type operation -- this is because it is hard
3035 * to predict the length of a GETATTR response in v4, and thus
3036 * align the READ data correctly. This means that the GETATTR
3037 * may end up partially falling into the page cache, and we should
3038 * shift it into the 'tail' of the xdr_buf before processing.
3039 * To do this efficiently, we need to know the total length
3040 * of data received, which doesn't seem to be available outside
3041 * of the RPC layer.
3042 *
3043 * In the case of WRITE, we also want to put the GETATTR after
3044 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003045 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 *
3047 * Both of these changes to the XDR layer would in fact be quite
3048 * minor, but I decided to leave them for a subsequent patch.
3049 */
3050static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3051 unsigned int pgbase, unsigned int pglen)
3052{
3053 struct nfs4_readlink args = {
3054 .fh = NFS_FH(inode),
3055 .pgbase = pgbase,
3056 .pglen = pglen,
3057 .pages = &page,
3058 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003059 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 struct rpc_message msg = {
3061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3062 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003063 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 };
3065
Bryan Schumaker7c513052011-03-24 17:12:24 +00003066 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 -07003067}
3068
3069static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3070 unsigned int pgbase, unsigned int pglen)
3071{
3072 struct nfs4_exception exception = { };
3073 int err;
3074 do {
3075 err = nfs4_handle_exception(NFS_SERVER(inode),
3076 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3077 &exception);
3078 } while (exception.retry);
3079 return err;
3080}
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003083 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085static int
3086nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003087 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003089 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 int status = 0;
3092
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003093 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3094 if (IS_ERR(ctx))
3095 return PTR_ERR(ctx);
3096
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003097 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003098 state = nfs4_do_open(dir, dentry, ctx->mode,
3099 flags, sattr, ctx->cred,
3100 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003101 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (IS_ERR(state)) {
3103 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003104 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003106 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003107 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003108 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003110 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return status;
3112}
3113
3114static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3115{
Trond Myklebust16e42952005-10-27 22:12:44 -04003116 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003117 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003119 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003121 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003122 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003123 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3126 .rpc_argp = &args,
3127 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003129 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003130
Bryan Schumaker7c513052011-03-24 17:12:24 +00003131 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003132 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003133 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return status;
3135}
3136
3137static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3138{
3139 struct nfs4_exception exception = { };
3140 int err;
3141 do {
3142 err = nfs4_handle_exception(NFS_SERVER(dir),
3143 _nfs4_proc_remove(dir, name),
3144 &exception);
3145 } while (exception.retry);
3146 return err;
3147}
3148
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003149static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003151 struct nfs_server *server = NFS_SERVER(dir);
3152 struct nfs_removeargs *args = msg->rpc_argp;
3153 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003155 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003157 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003160static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3161{
3162 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3163 &data->args.seq_args,
3164 &data->res.seq_res,
3165 task))
3166 return;
3167 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003170static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003172 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3173
Trond Myklebust14516c32010-07-31 14:29:06 -04003174 if (!nfs4_sequence_done(task, &res->seq_res))
3175 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003176 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003177 return 0;
3178 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003179 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Jeff Laytond3d41522010-09-17 17:31:57 -04003182static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3183{
3184 struct nfs_server *server = NFS_SERVER(dir);
3185 struct nfs_renameargs *arg = msg->rpc_argp;
3186 struct nfs_renameres *res = msg->rpc_resp;
3187
3188 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003189 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003190 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003191}
3192
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003193static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3194{
3195 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3196 &data->args.seq_args,
3197 &data->res.seq_res,
3198 task))
3199 return;
3200 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003201}
3202
3203static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3204 struct inode *new_dir)
3205{
3206 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3207
3208 if (!nfs4_sequence_done(task, &res->seq_res))
3209 return 0;
3210 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3211 return 0;
3212
3213 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003214 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003215 return 1;
3216}
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3219 struct inode *new_dir, struct qstr *new_name)
3220{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003221 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003222 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 .old_dir = NFS_FH(old_dir),
3224 .new_dir = NFS_FH(new_dir),
3225 .old_name = old_name,
3226 .new_name = new_name,
3227 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003228 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003229 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003230 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3233 .rpc_argp = &arg,
3234 .rpc_resp = &res,
3235 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003236 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Bryan Schumaker7c513052011-03-24 17:12:24 +00003238 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 if (!status) {
3240 update_changeattr(old_dir, &res.old_cinfo);
3241 update_changeattr(new_dir, &res.new_cinfo);
3242 }
3243 return status;
3244}
3245
3246static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3247 struct inode *new_dir, struct qstr *new_name)
3248{
3249 struct nfs4_exception exception = { };
3250 int err;
3251 do {
3252 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3253 _nfs4_proc_rename(old_dir, old_name,
3254 new_dir, new_name),
3255 &exception);
3256 } while (exception.retry);
3257 return err;
3258}
3259
3260static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3261{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003262 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 struct nfs4_link_arg arg = {
3264 .fh = NFS_FH(inode),
3265 .dir_fh = NFS_FH(dir),
3266 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003267 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003269 struct nfs4_link_res res = {
3270 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003271 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct rpc_message msg = {
3273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3274 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003275 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003277 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Trond Myklebust136f2622010-04-16 16:22:49 -04003279 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003280 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003281 goto out;
3282
Bryan Schumaker7c513052011-03-24 17:12:24 +00003283 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003284 if (!status) {
3285 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003286 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003287 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003288out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003289 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return status;
3291}
3292
3293static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3294{
3295 struct nfs4_exception exception = { };
3296 int err;
3297 do {
3298 err = nfs4_handle_exception(NFS_SERVER(inode),
3299 _nfs4_proc_link(inode, dir, name),
3300 &exception);
3301 } while (exception.retry);
3302 return err;
3303}
3304
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003305struct nfs4_createdata {
3306 struct rpc_message msg;
3307 struct nfs4_create_arg arg;
3308 struct nfs4_create_res res;
3309 struct nfs_fh fh;
3310 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003311};
3312
3313static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3314 struct qstr *name, struct iattr *sattr, u32 ftype)
3315{
3316 struct nfs4_createdata *data;
3317
3318 data = kzalloc(sizeof(*data), GFP_KERNEL);
3319 if (data != NULL) {
3320 struct nfs_server *server = NFS_SERVER(dir);
3321
3322 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3323 data->msg.rpc_argp = &data->arg;
3324 data->msg.rpc_resp = &data->res;
3325 data->arg.dir_fh = NFS_FH(dir);
3326 data->arg.server = server;
3327 data->arg.name = name;
3328 data->arg.attrs = sattr;
3329 data->arg.ftype = ftype;
3330 data->arg.bitmask = server->attr_bitmask;
3331 data->res.server = server;
3332 data->res.fh = &data->fh;
3333 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003334 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003335 }
3336 return data;
3337}
3338
3339static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3340{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003341 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003342 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003343 if (status == 0) {
3344 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003345 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3346 }
3347 return status;
3348}
3349
3350static void nfs4_free_createdata(struct nfs4_createdata *data)
3351{
3352 kfree(data);
3353}
3354
Chuck Lever4f390c12006-08-22 20:06:22 -04003355static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003356 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003358 struct nfs4_createdata *data;
3359 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Chuck Lever94a6d752006-08-22 20:06:23 -04003361 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003362 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003363
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003364 status = -ENOMEM;
3365 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3366 if (data == NULL)
3367 goto out;
3368
3369 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3370 data->arg.u.symlink.pages = &page;
3371 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003373 status = nfs4_do_create(dir, dentry, data);
3374
3375 nfs4_free_createdata(data);
3376out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return status;
3378}
3379
Chuck Lever4f390c12006-08-22 20:06:22 -04003380static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003381 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
3383 struct nfs4_exception exception = { };
3384 int err;
3385 do {
3386 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003387 _nfs4_proc_symlink(dir, dentry, page,
3388 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 &exception);
3390 } while (exception.retry);
3391 return err;
3392}
3393
3394static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3395 struct iattr *sattr)
3396{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003397 struct nfs4_createdata *data;
3398 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003400 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3401 if (data == NULL)
3402 goto out;
3403
3404 status = nfs4_do_create(dir, dentry, data);
3405
3406 nfs4_free_createdata(data);
3407out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return status;
3409}
3410
3411static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3412 struct iattr *sattr)
3413{
3414 struct nfs4_exception exception = { };
3415 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003416
3417 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 do {
3419 err = nfs4_handle_exception(NFS_SERVER(dir),
3420 _nfs4_proc_mkdir(dir, dentry, sattr),
3421 &exception);
3422 } while (exception.retry);
3423 return err;
3424}
3425
3426static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003427 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
3429 struct inode *dir = dentry->d_inode;
3430 struct nfs4_readdir_arg args = {
3431 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003432 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 .pgbase = 0,
3434 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003435 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003436 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 };
3438 struct nfs4_readdir_res res;
3439 struct rpc_message msg = {
3440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3441 .rpc_argp = &args,
3442 .rpc_resp = &res,
3443 .rpc_cred = cred,
3444 };
3445 int status;
3446
Harvey Harrison3110ff82008-05-02 13:42:44 -07003447 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003448 dentry->d_parent->d_name.name,
3449 dentry->d_name.name,
3450 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003451 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003453 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 -05003454 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003455 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003456 status += args.pgbase;
3457 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003458
3459 nfs_invalidate_atime(dir);
3460
Harvey Harrison3110ff82008-05-02 13:42:44 -07003461 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return status;
3463}
3464
3465static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003466 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
3468 struct nfs4_exception exception = { };
3469 int err;
3470 do {
3471 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3472 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003473 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 &exception);
3475 } while (exception.retry);
3476 return err;
3477}
3478
3479static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3480 struct iattr *sattr, dev_t rdev)
3481{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003482 struct nfs4_createdata *data;
3483 int mode = sattr->ia_mode;
3484 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003486 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3487 if (data == NULL)
3488 goto out;
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003491 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003493 data->arg.ftype = NF4BLK;
3494 data->arg.u.device.specdata1 = MAJOR(rdev);
3495 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003498 data->arg.ftype = NF4CHR;
3499 data->arg.u.device.specdata1 = MAJOR(rdev);
3500 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003501 } else if (!S_ISSOCK(mode)) {
3502 status = -EINVAL;
3503 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003506 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003507out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003508 nfs4_free_createdata(data);
3509out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return status;
3511}
3512
3513static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3514 struct iattr *sattr, dev_t rdev)
3515{
3516 struct nfs4_exception exception = { };
3517 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003518
3519 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 do {
3521 err = nfs4_handle_exception(NFS_SERVER(dir),
3522 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3523 &exception);
3524 } while (exception.retry);
3525 return err;
3526}
3527
3528static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3529 struct nfs_fsstat *fsstat)
3530{
3531 struct nfs4_statfs_arg args = {
3532 .fh = fhandle,
3533 .bitmask = server->attr_bitmask,
3534 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003535 struct nfs4_statfs_res res = {
3536 .fsstat = fsstat,
3537 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3540 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003541 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 };
3543
Trond Myklebust0e574af2005-10-27 22:12:38 -04003544 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003545 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
3548static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3549{
3550 struct nfs4_exception exception = { };
3551 int err;
3552 do {
3553 err = nfs4_handle_exception(server,
3554 _nfs4_proc_statfs(server, fhandle, fsstat),
3555 &exception);
3556 } while (exception.retry);
3557 return err;
3558}
3559
3560static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3561 struct nfs_fsinfo *fsinfo)
3562{
3563 struct nfs4_fsinfo_arg args = {
3564 .fh = fhandle,
3565 .bitmask = server->attr_bitmask,
3566 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003567 struct nfs4_fsinfo_res res = {
3568 .fsinfo = fsinfo,
3569 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 struct rpc_message msg = {
3571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3572 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003573 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 };
3575
Bryan Schumaker7c513052011-03-24 17:12:24 +00003576 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
3579static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3580{
3581 struct nfs4_exception exception = { };
3582 int err;
3583
3584 do {
3585 err = nfs4_handle_exception(server,
3586 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3587 &exception);
3588 } while (exception.retry);
3589 return err;
3590}
3591
3592static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3593{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003594 int error;
3595
Trond Myklebust0e574af2005-10-27 22:12:38 -04003596 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003597 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003598 if (error == 0) {
3599 /* block layout checks this! */
3600 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003601 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003602 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003603
3604 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
3607static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3608 struct nfs_pathconf *pathconf)
3609{
3610 struct nfs4_pathconf_arg args = {
3611 .fh = fhandle,
3612 .bitmask = server->attr_bitmask,
3613 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003614 struct nfs4_pathconf_res res = {
3615 .pathconf = pathconf,
3616 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 struct rpc_message msg = {
3618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3619 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003620 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 };
3622
3623 /* None of the pathconf attributes are mandatory to implement */
3624 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3625 memset(pathconf, 0, sizeof(*pathconf));
3626 return 0;
3627 }
3628
Trond Myklebust0e574af2005-10-27 22:12:38 -04003629 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003630 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
3633static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3634 struct nfs_pathconf *pathconf)
3635{
3636 struct nfs4_exception exception = { };
3637 int err;
3638
3639 do {
3640 err = nfs4_handle_exception(server,
3641 _nfs4_proc_pathconf(server, fhandle, pathconf),
3642 &exception);
3643 } while (exception.retry);
3644 return err;
3645}
3646
Benny Halevyd20581a2011-05-22 19:52:03 +03003647void __nfs4_read_done_cb(struct nfs_read_data *data)
3648{
Fred Isamancd841602012-04-20 14:47:44 -04003649 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003650}
3651
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003652static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Fred Isamancd841602012-04-20 14:47:44 -04003654 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003656 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003657 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003658 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003660
Benny Halevyd20581a2011-05-22 19:52:03 +03003661 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003663 renew_lease(server, data->timestamp);
3664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
3666
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003667static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3668{
3669
3670 dprintk("--> %s\n", __func__);
3671
3672 if (!nfs4_sequence_done(task, &data->res.seq_res))
3673 return -EAGAIN;
3674
Benny Halevyd20581a2011-05-22 19:52:03 +03003675 return data->read_done_cb ? data->read_done_cb(task, data) :
3676 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003677}
3678
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003679static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003682 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003683 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003684 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685}
3686
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003687static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3688{
Fred Isamancd841602012-04-20 14:47:44 -04003689 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003690 &data->args.seq_args,
3691 &data->res.seq_res,
3692 task))
3693 return;
3694 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695}
3696
Fred Isamanb029bc92011-03-03 15:13:42 +00003697static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
Fred Isamancd841602012-04-20 14:47:44 -04003699 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003701 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003702 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003703 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003705 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003707 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003708 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
3711
Fred Isamanb029bc92011-03-03 15:13:42 +00003712static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3713{
3714 if (!nfs4_sequence_done(task, &data->res.seq_res))
3715 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003716 return data->write_done_cb ? data->write_done_cb(task, data) :
3717 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003718}
3719
Trond Myklebust5a37f852012-04-28 14:55:16 -04003720static
3721bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003722{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003723 const struct nfs_pgio_header *hdr = data->header;
3724
3725 /* Don't request attributes for pNFS or O_DIRECT writes */
3726 if (data->ds_clp != NULL || hdr->dreq != NULL)
3727 return false;
3728 /* Otherwise, request attributes if and only if we don't hold
3729 * a delegation
3730 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003731 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003732}
Fred Isamana69aef12011-03-03 15:13:47 +00003733
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003734static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
Fred Isamancd841602012-04-20 14:47:44 -04003736 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003737
Trond Myklebust5a37f852012-04-28 14:55:16 -04003738 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003739 data->args.bitmask = NULL;
3740 data->res.fattr = NULL;
3741 } else
3742 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003743
Fred Isamanb029bc92011-03-03 15:13:42 +00003744 if (!data->write_done_cb)
3745 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003746 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 data->timestamp = jiffies;
3748
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003749 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003750 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003753static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3754{
Fred Isamancd841602012-04-20 14:47:44 -04003755 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003756 &data->args.seq_args,
3757 &data->res.seq_res,
3758 task))
3759 return;
3760 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761}
3762
Fred Isaman0b7c0152012-04-20 14:47:39 -04003763static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3764{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3766 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003767 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return;
3770 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003771}
3772
Fred Isaman0b7c0152012-04-20 14:47:39 -04003773static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
3775 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003776
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003777 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003778 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003779 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
Fred Isaman0b7c0152012-04-20 14:47:39 -04003784static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003785{
3786 if (!nfs4_sequence_done(task, &data->res.seq_res))
3787 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003788 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003789}
3790
Fred Isaman0b7c0152012-04-20 14:47:39 -04003791static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003793 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003794
Fred Isaman0b7c0152012-04-20 14:47:39 -04003795 if (data->commit_done_cb == NULL)
3796 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003797 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003798 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003799 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800}
3801
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003802struct nfs4_renewdata {
3803 struct nfs_client *client;
3804 unsigned long timestamp;
3805};
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807/*
3808 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3809 * standalone procedure for queueing an asynchronous RENEW.
3810 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003811static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003812{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003813 struct nfs4_renewdata *data = calldata;
3814 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003815
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003816 if (atomic_read(&clp->cl_count) > 1)
3817 nfs4_schedule_state_renewal(clp);
3818 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003819 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003820}
3821
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003822static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003824 struct nfs4_renewdata *data = calldata;
3825 struct nfs_client *clp = data->client;
3826 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
3828 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003829 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003830 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3831 return;
3832 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003833 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003834 return;
3835 }
3836 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003838 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
3840
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003841static const struct rpc_call_ops nfs4_renew_ops = {
3842 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003843 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003844};
3845
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003846static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3850 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003851 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003853 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003855 if (renew_flags == 0)
3856 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003857 if (!atomic_inc_not_zero(&clp->cl_count))
3858 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003859 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003860 if (data == NULL)
3861 return -ENOMEM;
3862 data->client = clp;
3863 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003865 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866}
3867
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003868static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869{
3870 struct rpc_message msg = {
3871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3872 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003873 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 };
3875 unsigned long now = jiffies;
3876 int status;
3877
3878 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3879 if (status < 0)
3880 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003881 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 return 0;
3883}
3884
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003885static inline int nfs4_server_supports_acls(struct nfs_server *server)
3886{
3887 return (server->caps & NFS_CAP_ACLS)
3888 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3889 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3890}
3891
Trond Myklebust21f498c2012-08-24 10:59:25 -04003892/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3893 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003894 * the stack.
3895 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003896#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003897
Neil Hormane9e3d722011-03-04 19:26:03 -05003898static int buf_to_pages_noslab(const void *buf, size_t buflen,
3899 struct page **pages, unsigned int *pgbase)
3900{
3901 struct page *newpage, **spages;
3902 int rc = 0;
3903 size_t len;
3904 spages = pages;
3905
3906 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003907 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003908 newpage = alloc_page(GFP_KERNEL);
3909
3910 if (newpage == NULL)
3911 goto unwind;
3912 memcpy(page_address(newpage), buf, len);
3913 buf += len;
3914 buflen -= len;
3915 *pages++ = newpage;
3916 rc++;
3917 } while (buflen != 0);
3918
3919 return rc;
3920
3921unwind:
3922 for(; rc > 0; rc--)
3923 __free_page(spages[rc-1]);
3924 return -ENOMEM;
3925}
3926
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003927struct nfs4_cached_acl {
3928 int cached;
3929 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003930 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003931};
3932
3933static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003934{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003935 struct nfs_inode *nfsi = NFS_I(inode);
3936
3937 spin_lock(&inode->i_lock);
3938 kfree(nfsi->nfs4_acl);
3939 nfsi->nfs4_acl = acl;
3940 spin_unlock(&inode->i_lock);
3941}
3942
3943static void nfs4_zap_acl_attr(struct inode *inode)
3944{
3945 nfs4_set_cached_acl(inode, NULL);
3946}
3947
3948static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3949{
3950 struct nfs_inode *nfsi = NFS_I(inode);
3951 struct nfs4_cached_acl *acl;
3952 int ret = -ENOENT;
3953
3954 spin_lock(&inode->i_lock);
3955 acl = nfsi->nfs4_acl;
3956 if (acl == NULL)
3957 goto out;
3958 if (buf == NULL) /* user is just asking for length */
3959 goto out_len;
3960 if (acl->cached == 0)
3961 goto out;
3962 ret = -ERANGE; /* see getxattr(2) man page */
3963 if (acl->len > buflen)
3964 goto out;
3965 memcpy(buf, acl->data, acl->len);
3966out_len:
3967 ret = acl->len;
3968out:
3969 spin_unlock(&inode->i_lock);
3970 return ret;
3971}
3972
Sachin Prabhu5794d212012-04-17 14:36:40 +01003973static 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 +00003974{
3975 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003976 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003977
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003978 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003979 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003980 if (acl == NULL)
3981 goto out;
3982 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003983 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003984 } else {
3985 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3986 if (acl == NULL)
3987 goto out;
3988 acl->cached = 0;
3989 }
3990 acl->len = acl_len;
3991out:
3992 nfs4_set_cached_acl(inode, acl);
3993}
3994
Andy Adamsonbf118a32011-12-07 11:55:27 -05003995/*
3996 * The getxattr API returns the required buffer length when called with a
3997 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3998 * the required buf. On a NULL buf, we send a page of data to the server
3999 * guessing that the ACL request can be serviced by a page. If so, we cache
4000 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4001 * the cache. If not so, we throw away the page, and cache the required
4002 * length. The next getxattr call will then produce another round trip to
4003 * the server, this time with the input buf of the required size.
4004 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004005static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004006{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004007 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004008 struct nfs_getaclargs args = {
4009 .fh = NFS_FH(inode),
4010 .acl_pages = pages,
4011 .acl_len = buflen,
4012 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004013 struct nfs_getaclres res = {
4014 .acl_len = buflen,
4015 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004016 struct rpc_message msg = {
4017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4018 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004019 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004020 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004021 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4022 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004023
Andy Adamsonbf118a32011-12-07 11:55:27 -05004024 /* As long as we're doing a round trip to the server anyway,
4025 * let's be prepared for a page of acl data. */
4026 if (npages == 0)
4027 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004028 if (npages > ARRAY_SIZE(pages))
4029 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004030
Andy Adamsonbf118a32011-12-07 11:55:27 -05004031 for (i = 0; i < npages; i++) {
4032 pages[i] = alloc_page(GFP_KERNEL);
4033 if (!pages[i])
4034 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004035 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004036
4037 /* for decoding across pages */
4038 res.acl_scratch = alloc_page(GFP_KERNEL);
4039 if (!res.acl_scratch)
4040 goto out_free;
4041
Andy Adamsonbf118a32011-12-07 11:55:27 -05004042 args.acl_len = npages * PAGE_SIZE;
4043 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004044
Peng Taode040be2012-01-10 22:42:47 +08004045 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004046 __func__, buf, buflen, npages, args.acl_len);
4047 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4048 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004049 if (ret)
4050 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004051
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004052 /* Handle the case where the passed-in buffer is too short */
4053 if (res.acl_flags & NFS4_ACL_TRUNC) {
4054 /* Did the user only issue a request for the acl length? */
4055 if (buf == NULL)
4056 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004057 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004058 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004059 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004060 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4061 if (buf)
4062 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4063out_ok:
4064 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004065out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004066 for (i = 0; i < npages; i++)
4067 if (pages[i])
4068 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004069 if (res.acl_scratch)
4070 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004071 return ret;
4072}
4073
Trond Myklebust16b42892006-08-24 12:27:15 -04004074static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4075{
4076 struct nfs4_exception exception = { };
4077 ssize_t ret;
4078 do {
4079 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4080 if (ret >= 0)
4081 break;
4082 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4083 } while (exception.retry);
4084 return ret;
4085}
4086
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004087static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4088{
4089 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004090 int ret;
4091
4092 if (!nfs4_server_supports_acls(server))
4093 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004094 ret = nfs_revalidate_inode(server, inode);
4095 if (ret < 0)
4096 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004097 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4098 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004099 ret = nfs4_read_cached_acl(inode, buf, buflen);
4100 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004101 /* -ENOENT is returned if there is no ACL or if there is an ACL
4102 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004103 return ret;
4104 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004105}
4106
Trond Myklebust16b42892006-08-24 12:27:15 -04004107static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004108{
4109 struct nfs_server *server = NFS_SERVER(inode);
4110 struct page *pages[NFS4ACL_MAXPAGES];
4111 struct nfs_setaclargs arg = {
4112 .fh = NFS_FH(inode),
4113 .acl_pages = pages,
4114 .acl_len = buflen,
4115 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004116 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004117 struct rpc_message msg = {
4118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4119 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004120 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004121 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004122 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004123 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004124
4125 if (!nfs4_server_supports_acls(server))
4126 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004127 if (npages > ARRAY_SIZE(pages))
4128 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004129 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4130 if (i < 0)
4131 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004132 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004133 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004134
4135 /*
4136 * Free each page after tx, so the only ref left is
4137 * held by the network stack
4138 */
4139 for (; i > 0; i--)
4140 put_page(pages[i-1]);
4141
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004142 /*
4143 * Acl update can result in inode attribute update.
4144 * so mark the attribute cache invalid.
4145 */
4146 spin_lock(&inode->i_lock);
4147 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4148 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004149 nfs_access_zap_cache(inode);
4150 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004151 return ret;
4152}
4153
Trond Myklebust16b42892006-08-24 12:27:15 -04004154static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4155{
4156 struct nfs4_exception exception = { };
4157 int err;
4158 do {
4159 err = nfs4_handle_exception(NFS_SERVER(inode),
4160 __nfs4_proc_set_acl(inode, buf, buflen),
4161 &exception);
4162 } while (exception.retry);
4163 return err;
4164}
4165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004167nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004169 struct nfs_client *clp = server->nfs_client;
4170
4171 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 return 0;
4173 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004174 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004175 case -NFS4ERR_ADMIN_REVOKED:
4176 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004177 if (state == NULL)
4178 break;
4179 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004180 case -NFS4ERR_OPENMODE:
4181 if (state == NULL)
4182 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004183 nfs4_schedule_stateid_recovery(server, state);
4184 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004185 case -NFS4ERR_EXPIRED:
4186 if (state != NULL)
4187 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004189 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004190 nfs4_schedule_lease_recovery(clp);
4191 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004192#if defined(CONFIG_NFS_V4_1)
4193 case -NFS4ERR_BADSESSION:
4194 case -NFS4ERR_BADSLOT:
4195 case -NFS4ERR_BAD_HIGH_SLOT:
4196 case -NFS4ERR_DEADSESSION:
4197 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4198 case -NFS4ERR_SEQ_FALSE_RETRY:
4199 case -NFS4ERR_SEQ_MISORDERED:
4200 dprintk("%s ERROR %d, Reset session\n", __func__,
4201 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004202 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004203 task->tk_status = 0;
4204 return -EAGAIN;
4205#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004207 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004208 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004209 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4211 task->tk_status = 0;
4212 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004213 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 case -NFS4ERR_OLD_STATEID:
4215 task->tk_status = 0;
4216 return -EAGAIN;
4217 }
4218 task->tk_status = nfs4_map_errors(task->tk_status);
4219 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004220wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004221 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004222 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4223 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4224 task->tk_status = 0;
4225 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226}
4227
Chuck Leverf0920752012-05-21 22:45:41 -04004228static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4229 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004230{
4231 __be32 verf[2];
4232
Chuck Lever2c820d92012-05-21 22:45:33 -04004233 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4234 /* An impossible timestamp guarantees this value
4235 * will never match a generated boot time. */
4236 verf[0] = 0;
4237 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4238 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004239 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4240 verf[0] = (__be32)nn->boot_time.tv_sec;
4241 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004242 }
Chuck Levercd937102012-03-02 17:14:31 -05004243 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4244}
4245
Chuck Levere984a552012-09-14 17:24:21 -04004246static unsigned int
4247nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4248 char *buf, size_t len)
4249{
4250 unsigned int result;
4251
4252 rcu_read_lock();
4253 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4254 clp->cl_ipaddr,
4255 rpc_peeraddr2str(clp->cl_rpcclient,
4256 RPC_DISPLAY_ADDR),
4257 rpc_peeraddr2str(clp->cl_rpcclient,
4258 RPC_DISPLAY_PROTO));
4259 rcu_read_unlock();
4260 return result;
4261}
4262
4263static unsigned int
4264nfs4_init_uniform_client_string(const struct nfs_client *clp,
4265 char *buf, size_t len)
4266{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004267 char *nodename = clp->cl_rpcclient->cl_nodename;
4268
4269 if (nfs4_client_id_uniquifier[0] != '\0')
4270 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004271 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4272 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004273 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004274}
4275
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004276/**
4277 * nfs4_proc_setclientid - Negotiate client ID
4278 * @clp: state data structure
4279 * @program: RPC program for NFSv4 callback service
4280 * @port: IP port number for NFS4 callback service
4281 * @cred: RPC credential to use for this call
4282 * @res: where to place the result
4283 *
4284 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4285 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004286int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4287 unsigned short port, struct rpc_cred *cred,
4288 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
4290 nfs4_verifier sc_verifier;
4291 struct nfs4_setclientid setclientid = {
4292 .sc_verifier = &sc_verifier,
4293 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004294 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 };
4296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4298 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004299 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004300 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004302 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Chuck Leverde734832012-07-11 16:30:50 -04004304 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004305 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004306 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4307 setclientid.sc_name_len =
4308 nfs4_init_uniform_client_string(clp,
4309 setclientid.sc_name,
4310 sizeof(setclientid.sc_name));
4311 else
4312 setclientid.sc_name_len =
4313 nfs4_init_nonuniform_client_string(clp,
4314 setclientid.sc_name,
4315 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004316 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004317 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004318 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004319 sizeof(setclientid.sc_netid),
4320 rpc_peeraddr2str(clp->cl_rpcclient,
4321 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004322 rcu_read_unlock();
4323 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004324 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 clp->cl_ipaddr, port >> 8, port & 255);
4326
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004327 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4328 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4329 setclientid.sc_name_len, setclientid.sc_name);
4330 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4331 dprintk("NFS reply setclientid: %d\n", status);
4332 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333}
4334
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004335/**
4336 * nfs4_proc_setclientid_confirm - Confirm client ID
4337 * @clp: state data structure
4338 * @res: result of a previous SETCLIENTID
4339 * @cred: RPC credential to use for this call
4340 *
4341 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4342 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004343int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004344 struct nfs4_setclientid_res *arg,
4345 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
4347 struct nfs_fsinfo fsinfo;
4348 struct rpc_message msg = {
4349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004350 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004352 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 };
4354 unsigned long now;
4355 int status;
4356
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004357 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4358 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4359 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004361 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 if (status == 0) {
4363 spin_lock(&clp->cl_lock);
4364 clp->cl_lease_time = fsinfo.lease_time * HZ;
4365 clp->cl_last_renewal = now;
4366 spin_unlock(&clp->cl_lock);
4367 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004368 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return status;
4370}
4371
Trond Myklebustfe650402006-01-03 09:55:18 +01004372struct nfs4_delegreturndata {
4373 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004374 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004375 struct nfs_fh fh;
4376 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004377 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004378 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004379 int rpc_status;
4380};
4381
Trond Myklebustfe650402006-01-03 09:55:18 +01004382static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4383{
4384 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004385
Trond Myklebust14516c32010-07-31 14:29:06 -04004386 if (!nfs4_sequence_done(task, &data->res.seq_res))
4387 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004388
Ricardo Labiaga79708862009-12-07 09:23:21 -05004389 switch (task->tk_status) {
4390 case -NFS4ERR_STALE_STATEID:
4391 case -NFS4ERR_EXPIRED:
4392 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004393 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004394 break;
4395 default:
4396 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4397 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004398 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004399 return;
4400 }
4401 }
4402 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004403}
4404
4405static void nfs4_delegreturn_release(void *calldata)
4406{
Trond Myklebustfe650402006-01-03 09:55:18 +01004407 kfree(calldata);
4408}
4409
Andy Adamson938e1012009-04-01 09:22:28 -04004410#if defined(CONFIG_NFS_V4_1)
4411static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4412{
4413 struct nfs4_delegreturndata *d_data;
4414
4415 d_data = (struct nfs4_delegreturndata *)data;
4416
Trond Myklebust035168a2010-06-16 09:52:26 -04004417 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004418 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004419 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004420 return;
4421 rpc_call_start(task);
4422}
4423#endif /* CONFIG_NFS_V4_1 */
4424
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004425static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004426#if defined(CONFIG_NFS_V4_1)
4427 .rpc_call_prepare = nfs4_delegreturn_prepare,
4428#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004429 .rpc_call_done = nfs4_delegreturn_done,
4430 .rpc_release = nfs4_delegreturn_release,
4431};
4432
Trond Myklebuste6f81072008-01-24 18:14:34 -05004433static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004434{
4435 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004436 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004437 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4440 .rpc_cred = cred,
4441 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004442 struct rpc_task_setup task_setup_data = {
4443 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004444 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004445 .callback_ops = &nfs4_delegreturn_ops,
4446 .flags = RPC_TASK_ASYNC,
4447 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004448 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004449
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004450 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004451 if (data == NULL)
4452 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004453 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004454 data->args.fhandle = &data->fh;
4455 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004456 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004457 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004458 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004459 data->res.fattr = &data->fattr;
4460 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004461 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004462 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004463 data->rpc_status = 0;
4464
Trond Myklebustc970aa82007-07-14 15:39:59 -04004465 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004466 msg.rpc_argp = &data->args;
4467 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004468 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004469 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004470 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004471 if (!issync)
4472 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004473 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004474 if (status != 0)
4475 goto out;
4476 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004477 if (status == 0)
4478 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4479 else
4480 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004481out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004482 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004483 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
Trond Myklebuste6f81072008-01-24 18:14:34 -05004486int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487{
4488 struct nfs_server *server = NFS_SERVER(inode);
4489 struct nfs4_exception exception = { };
4490 int err;
4491 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004492 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 switch (err) {
4494 case -NFS4ERR_STALE_STATEID:
4495 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 case 0:
4497 return 0;
4498 }
4499 err = nfs4_handle_exception(server, err, &exception);
4500 } while (exception.retry);
4501 return err;
4502}
4503
4504#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4505#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4506
4507/*
4508 * sleep, with exponential backoff, and retry the LOCK operation.
4509 */
4510static unsigned long
4511nfs4_set_lock_task_retry(unsigned long timeout)
4512{
Jeff Laytond3103102011-12-01 22:44:39 +01004513 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 timeout <<= 1;
4515 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4516 return NFS4_LOCK_MAXTIMEOUT;
4517 return timeout;
4518}
4519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4521{
4522 struct inode *inode = state->inode;
4523 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004524 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004525 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004527 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004529 struct nfs_lockt_res res = {
4530 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 };
4532 struct rpc_message msg = {
4533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4534 .rpc_argp = &arg,
4535 .rpc_resp = &res,
4536 .rpc_cred = state->owner->so_cred,
4537 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 struct nfs4_lock_state *lsp;
4539 int status;
4540
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004541 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004542 status = nfs4_set_lock_state(state, request);
4543 if (status != 0)
4544 goto out;
4545 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004546 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004547 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004548 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004549 switch (status) {
4550 case 0:
4551 request->fl_type = F_UNLCK;
4552 break;
4553 case -NFS4ERR_DENIED:
4554 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004556 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004557out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 return status;
4559}
4560
4561static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4562{
4563 struct nfs4_exception exception = { };
4564 int err;
4565
4566 do {
4567 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4568 _nfs4_proc_getlk(state, cmd, request),
4569 &exception);
4570 } while (exception.retry);
4571 return err;
4572}
4573
4574static int do_vfs_lock(struct file *file, struct file_lock *fl)
4575{
4576 int res = 0;
4577 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4578 case FL_POSIX:
4579 res = posix_lock_file_wait(file, fl);
4580 break;
4581 case FL_FLOCK:
4582 res = flock_lock_file_wait(file, fl);
4583 break;
4584 default:
4585 BUG();
4586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return res;
4588}
4589
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004590struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004591 struct nfs_locku_args arg;
4592 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004593 struct nfs4_lock_state *lsp;
4594 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004595 struct file_lock fl;
4596 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004597 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004598};
4599
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004600static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4601 struct nfs_open_context *ctx,
4602 struct nfs4_lock_state *lsp,
4603 struct nfs_seqid *seqid)
4604{
4605 struct nfs4_unlockdata *p;
4606 struct inode *inode = lsp->ls_state->inode;
4607
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004608 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004609 if (p == NULL)
4610 return NULL;
4611 p->arg.fh = NFS_FH(inode);
4612 p->arg.fl = &p->fl;
4613 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004614 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004615 p->arg.stateid = &lsp->ls_stateid;
4616 p->lsp = lsp;
4617 atomic_inc(&lsp->ls_count);
4618 /* Ensure we don't close file until we're done freeing locks! */
4619 p->ctx = get_nfs_open_context(ctx);
4620 memcpy(&p->fl, fl, sizeof(p->fl));
4621 p->server = NFS_SERVER(inode);
4622 return p;
4623}
4624
Trond Myklebust06f814a2006-01-03 09:55:07 +01004625static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004626{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004627 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004628 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004629 nfs4_put_lock_state(calldata->lsp);
4630 put_nfs_open_context(calldata->ctx);
4631 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004632}
4633
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004634static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004635{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004636 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004637
Trond Myklebust14516c32010-07-31 14:29:06 -04004638 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4639 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004640 switch (task->tk_status) {
4641 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004642 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4643 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004644 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004645 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004646 case -NFS4ERR_BAD_STATEID:
4647 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004648 case -NFS4ERR_STALE_STATEID:
4649 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004650 break;
4651 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004652 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004653 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004654 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004655 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004656}
4657
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004658static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004659{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004660 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004662 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004663 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004664 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004665 /* Note: exit _without_ running nfs4_locku_done */
4666 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004667 return;
4668 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004669 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004670 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004671 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004672 &calldata->res.seq_res,
4673 task) != 0)
4674 nfs_release_seqid(calldata->arg.seqid);
4675 else
4676 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004679static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004680 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004681 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004682 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004683};
4684
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004685static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4686 struct nfs_open_context *ctx,
4687 struct nfs4_lock_state *lsp,
4688 struct nfs_seqid *seqid)
4689{
4690 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004691 struct rpc_message msg = {
4692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4693 .rpc_cred = ctx->cred,
4694 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004695 struct rpc_task_setup task_setup_data = {
4696 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004697 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004698 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004699 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004700 .flags = RPC_TASK_ASYNC,
4701 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004702
Frank Filz137d6ac2007-07-09 15:32:29 -07004703 /* Ensure this is an unlock - when canceling a lock, the
4704 * canceled lock is passed in, and it won't be an unlock.
4705 */
4706 fl->fl_type = F_UNLCK;
4707
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004708 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4709 if (data == NULL) {
4710 nfs_free_seqid(seqid);
4711 return ERR_PTR(-ENOMEM);
4712 }
4713
Trond Myklebust9d12b212012-01-17 22:04:25 -05004714 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004715 msg.rpc_argp = &data->arg;
4716 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004717 task_setup_data.callback_data = data;
4718 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004719}
4720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4722{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004723 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004724 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004725 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004726 struct rpc_task *task;
4727 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004728 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
Trond Myklebust9b073572006-06-29 16:38:34 -04004730 status = nfs4_set_lock_state(state, request);
4731 /* Unlock _before_ we do the RPC call */
4732 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004733 down_read(&nfsi->rwsem);
4734 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4735 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004736 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004737 }
4738 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004739 if (status != 0)
4740 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004741 /* Is this a delegated lock? */
4742 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004743 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004744 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004745 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004746 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004747 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004748 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004749 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750 status = PTR_ERR(task);
4751 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004752 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004754 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004755out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004756 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004757 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
4759
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004760struct nfs4_lockdata {
4761 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004762 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004763 struct nfs4_lock_state *lsp;
4764 struct nfs_open_context *ctx;
4765 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004766 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004767 int rpc_status;
4768 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004769 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004770};
4771
4772static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004773 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4774 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004775{
4776 struct nfs4_lockdata *p;
4777 struct inode *inode = lsp->ls_state->inode;
4778 struct nfs_server *server = NFS_SERVER(inode);
4779
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004780 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004781 if (p == NULL)
4782 return NULL;
4783
4784 p->arg.fh = NFS_FH(inode);
4785 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004786 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004787 if (p->arg.open_seqid == NULL)
4788 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004789 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004790 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004791 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004792 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004793 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004794 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004795 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004796 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004797 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004798 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004799 atomic_inc(&lsp->ls_count);
4800 p->ctx = get_nfs_open_context(ctx);
4801 memcpy(&p->fl, fl, sizeof(p->fl));
4802 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004803out_free_seqid:
4804 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004805out_free:
4806 kfree(p);
4807 return NULL;
4808}
4809
4810static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4811{
4812 struct nfs4_lockdata *data = calldata;
4813 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Harvey Harrison3110ff82008-05-02 13:42:44 -07004815 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004816 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4817 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818 /* Do we need to do an open_to_lock_owner? */
4819 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004820 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004821 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004822 data->arg.open_stateid = &state->stateid;
4823 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004824 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004825 } else
4826 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004827 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004828 if (nfs4_setup_sequence(data->server,
4829 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004830 &data->res.seq_res,
4831 task) == 0) {
4832 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004833 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004834 }
4835 nfs_release_seqid(data->arg.open_seqid);
4836out_release_lock_seqid:
4837 nfs_release_seqid(data->arg.lock_seqid);
4838 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004839}
4840
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004841static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4842{
4843 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4844 nfs4_lock_prepare(task, calldata);
4845}
4846
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4848{
4849 struct nfs4_lockdata *data = calldata;
4850
Harvey Harrison3110ff82008-05-02 13:42:44 -07004851 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852
Trond Myklebust14516c32010-07-31 14:29:06 -04004853 if (!nfs4_sequence_done(task, &data->res.seq_res))
4854 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004855
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004856 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004857 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004858 if (data->rpc_status == 0)
4859 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4860 else
4861 goto out;
4862 }
4863 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004864 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004865 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004866 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004867 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004868out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004869 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004870}
4871
4872static void nfs4_lock_release(void *calldata)
4873{
4874 struct nfs4_lockdata *data = calldata;
4875
Harvey Harrison3110ff82008-05-02 13:42:44 -07004876 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004877 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004878 if (data->cancelled != 0) {
4879 struct rpc_task *task;
4880 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4881 data->arg.lock_seqid);
4882 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004883 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004884 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004885 } else
4886 nfs_free_seqid(data->arg.lock_seqid);
4887 nfs4_put_lock_state(data->lsp);
4888 put_nfs_open_context(data->ctx);
4889 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004890 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004891}
4892
4893static const struct rpc_call_ops nfs4_lock_ops = {
4894 .rpc_call_prepare = nfs4_lock_prepare,
4895 .rpc_call_done = nfs4_lock_done,
4896 .rpc_release = nfs4_lock_release,
4897};
4898
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004899static const struct rpc_call_ops nfs4_recover_lock_ops = {
4900 .rpc_call_prepare = nfs4_recover_lock_prepare,
4901 .rpc_call_done = nfs4_lock_done,
4902 .rpc_release = nfs4_lock_release,
4903};
4904
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004905static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4906{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004907 switch (error) {
4908 case -NFS4ERR_ADMIN_REVOKED:
4909 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004910 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004911 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004912 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004913 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004914 break;
4915 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004916 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004917 case -NFS4ERR_EXPIRED:
4918 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004919 };
4920}
4921
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004922static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004923{
4924 struct nfs4_lockdata *data;
4925 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004926 struct rpc_message msg = {
4927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4928 .rpc_cred = state->owner->so_cred,
4929 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004930 struct rpc_task_setup task_setup_data = {
4931 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004932 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004933 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004934 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004935 .flags = RPC_TASK_ASYNC,
4936 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004937 int ret;
4938
Harvey Harrison3110ff82008-05-02 13:42:44 -07004939 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004940 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004941 fl->fl_u.nfs4_fl.owner,
4942 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004943 if (data == NULL)
4944 return -ENOMEM;
4945 if (IS_SETLKW(cmd))
4946 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004947 if (recovery_type > NFS_LOCK_NEW) {
4948 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004949 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004950 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4951 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004952 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004953 msg.rpc_argp = &data->arg;
4954 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004955 task_setup_data.callback_data = data;
4956 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004957 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004958 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004959 ret = nfs4_wait_for_completion_rpc_task(task);
4960 if (ret == 0) {
4961 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004962 if (ret)
4963 nfs4_handle_setlk_error(data->server, data->lsp,
4964 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004965 } else
4966 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004967 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004968 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004969 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970}
4971
4972static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4973{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004974 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004975 struct nfs4_exception exception = {
4976 .inode = state->inode,
4977 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004978 int err;
4979
4980 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004981 /* Cache the lock if possible... */
4982 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4983 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004984 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004985 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004986 break;
4987 nfs4_handle_exception(server, err, &exception);
4988 } while (exception.retry);
4989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990}
4991
4992static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4993{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004994 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004995 struct nfs4_exception exception = {
4996 .inode = state->inode,
4997 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004998 int err;
4999
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005000 err = nfs4_set_lock_state(state, request);
5001 if (err != 0)
5002 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005003 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005004 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5005 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005006 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005007 switch (err) {
5008 default:
5009 goto out;
5010 case -NFS4ERR_GRACE:
5011 case -NFS4ERR_DELAY:
5012 nfs4_handle_exception(server, err, &exception);
5013 err = 0;
5014 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005015 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005016out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005017 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018}
5019
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005020#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005021/**
5022 * nfs41_check_expired_locks - possibly free a lock stateid
5023 *
5024 * @state: NFSv4 state for an inode
5025 *
5026 * Returns NFS_OK if recovery for this stateid is now finished.
5027 * Otherwise a negative NFS4ERR value is returned.
5028 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005029static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005030{
Chuck Levereb64cf92012-07-11 16:30:05 -04005031 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005032 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005033 struct nfs_server *server = NFS_SERVER(state->inode);
5034
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005035 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005036 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005037 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5038 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005039 /* Free the stateid unless the server
5040 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005041 if (status != -NFS4ERR_BAD_STATEID)
5042 nfs41_free_stateid(server,
5043 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005044 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005045 ret = status;
5046 }
5047 }
5048 };
5049
5050 return ret;
5051}
5052
5053static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5054{
5055 int status = NFS_OK;
5056
5057 if (test_bit(LK_STATE_IN_USE, &state->flags))
5058 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005059 if (status != NFS_OK)
5060 status = nfs4_lock_expired(state, request);
5061 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005062}
5063#endif
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5066{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005067 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005068 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005069 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005071 if ((fl_flags & FL_POSIX) &&
5072 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5073 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005074 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005075 status = nfs4_set_lock_state(state, request);
5076 if (status != 0)
5077 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005078 request->fl_flags |= FL_ACCESS;
5079 status = do_vfs_lock(request->fl_file, request);
5080 if (status < 0)
5081 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005082 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005083 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005084 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005085 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005086 request->fl_flags = fl_flags & ~FL_SLEEP;
5087 status = do_vfs_lock(request->fl_file, request);
5088 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005089 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005090 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005091 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04005092 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005093 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005094 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005095 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005096 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5097 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005098out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005099 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005100out:
5101 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 return status;
5103}
5104
5105static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5106{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005107 struct nfs4_exception exception = {
5108 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005109 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 int err;
5112
5113 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005114 err = _nfs4_proc_setlk(state, cmd, request);
5115 if (err == -NFS4ERR_DENIED)
5116 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005118 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 } while (exception.retry);
5120 return err;
5121}
5122
5123static int
5124nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5125{
5126 struct nfs_open_context *ctx;
5127 struct nfs4_state *state;
5128 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5129 int status;
5130
5131 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005132 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 state = ctx->state;
5134
5135 if (request->fl_start < 0 || request->fl_end < 0)
5136 return -EINVAL;
5137
Trond Myklebustd9531262009-07-21 19:22:38 -04005138 if (IS_GETLK(cmd)) {
5139 if (state != NULL)
5140 return nfs4_proc_getlk(state, F_GETLK, request);
5141 return 0;
5142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
5144 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5145 return -EINVAL;
5146
Trond Myklebustd9531262009-07-21 19:22:38 -04005147 if (request->fl_type == F_UNLCK) {
5148 if (state != NULL)
5149 return nfs4_proc_unlck(state, cmd, request);
5150 return 0;
5151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
Trond Myklebustd9531262009-07-21 19:22:38 -04005153 if (state == NULL)
5154 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005155 /*
5156 * Don't rely on the VFS having checked the file open mode,
5157 * since it won't do this for flock() locks.
5158 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005159 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005160 case F_RDLCK:
5161 if (!(filp->f_mode & FMODE_READ))
5162 return -EBADF;
5163 break;
5164 case F_WRLCK:
5165 if (!(filp->f_mode & FMODE_WRITE))
5166 return -EBADF;
5167 }
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 do {
5170 status = nfs4_proc_setlk(state, cmd, request);
5171 if ((status != -EAGAIN) || IS_SETLK(cmd))
5172 break;
5173 timeout = nfs4_set_lock_task_retry(timeout);
5174 status = -ERESTARTSYS;
5175 if (signalled())
5176 break;
5177 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return status;
5179}
5180
Trond Myklebust888e6942005-11-04 15:38:11 -05005181int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5182{
5183 struct nfs_server *server = NFS_SERVER(state->inode);
5184 struct nfs4_exception exception = { };
5185 int err;
5186
5187 err = nfs4_set_lock_state(state, fl);
5188 if (err != 0)
5189 goto out;
5190 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005191 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005192 switch (err) {
5193 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005194 printk(KERN_ERR "NFS: %s: unhandled error "
5195 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005196 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005197 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005198 goto out;
5199 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005200 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005201 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005202 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005203 nfs4_schedule_lease_recovery(server->nfs_client);
5204 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005205 case -NFS4ERR_BADSESSION:
5206 case -NFS4ERR_BADSLOT:
5207 case -NFS4ERR_BAD_HIGH_SLOT:
5208 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5209 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005210 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005211 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005212 case -ERESTARTSYS:
5213 /*
5214 * The show must go on: exit, but mark the
5215 * stateid as needing recovery.
5216 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005217 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005218 case -NFS4ERR_ADMIN_REVOKED:
5219 case -NFS4ERR_BAD_STATEID:
5220 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005221 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005222 err = 0;
5223 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005224 case -EKEYEXPIRED:
5225 /*
5226 * User RPCSEC_GSS context has expired.
5227 * We cannot recover this stateid now, so
5228 * skip it and allow recovery thread to
5229 * proceed.
5230 */
5231 err = 0;
5232 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005233 case -ENOMEM:
5234 case -NFS4ERR_DENIED:
5235 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5236 err = 0;
5237 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005238 case -NFS4ERR_DELAY:
5239 break;
5240 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005241 err = nfs4_handle_exception(server, err, &exception);
5242 } while (exception.retry);
5243out:
5244 return err;
5245}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005246
Trond Myklebustcf470c32012-03-07 13:49:12 -05005247struct nfs_release_lockowner_data {
5248 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005249 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005250 struct nfs_release_lockowner_args args;
5251};
5252
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005253static void nfs4_release_lockowner_release(void *calldata)
5254{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005255 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005256 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005257 kfree(calldata);
5258}
5259
Trond Myklebust17280172012-03-11 13:11:00 -04005260static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005261 .rpc_release = nfs4_release_lockowner_release,
5262};
5263
Trond Myklebustcf470c32012-03-07 13:49:12 -05005264int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005265{
5266 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005267 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005268 struct rpc_message msg = {
5269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5270 };
5271
5272 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005273 return -EINVAL;
5274 data = kmalloc(sizeof(*data), GFP_NOFS);
5275 if (!data)
5276 return -ENOMEM;
5277 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005278 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005279 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5280 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5281 data->args.lock_owner.s_dev = server->s_dev;
5282 msg.rpc_argp = &data->args;
5283 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5284 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005285}
5286
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005287#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5288
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005289static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5290 const void *buf, size_t buflen,
5291 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005292{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005293 if (strcmp(key, "") != 0)
5294 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005295
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005296 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005297}
5298
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005299static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5300 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005301{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005302 if (strcmp(key, "") != 0)
5303 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005304
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005305 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005306}
5307
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005308static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5309 size_t list_len, const char *name,
5310 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005311{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005312 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005313
J. Bruce Fields096455a2006-03-20 23:23:42 -05005314 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5315 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005316
5317 if (list && len <= list_len)
5318 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005319 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005320}
5321
Andy Adamson533eb462011-06-13 18:25:56 -04005322/*
5323 * nfs_fhget will use either the mounted_on_fileid or the fileid
5324 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005325static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5326{
Andy Adamson533eb462011-06-13 18:25:56 -04005327 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5328 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5329 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005330 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005331 return;
5332
5333 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005334 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005335 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5336 fattr->nlink = 2;
5337}
5338
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005339static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5340 const struct qstr *name,
5341 struct nfs4_fs_locations *fs_locations,
5342 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005343{
5344 struct nfs_server *server = NFS_SERVER(dir);
5345 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005346 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005347 };
5348 struct nfs4_fs_locations_arg args = {
5349 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005350 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005351 .page = page,
5352 .bitmask = bitmask,
5353 };
Benny Halevy22958462009-04-01 09:22:02 -04005354 struct nfs4_fs_locations_res res = {
5355 .fs_locations = fs_locations,
5356 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005357 struct rpc_message msg = {
5358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5359 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005360 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005361 };
5362 int status;
5363
Harvey Harrison3110ff82008-05-02 13:42:44 -07005364 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005365
5366 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5367 * is not supported */
5368 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5369 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5370 else
5371 bitmask[0] |= FATTR4_WORD0_FILEID;
5372
Trond Myklebustc228fd32007-01-13 02:28:11 -05005373 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005374 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005375 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005376 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005377 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005378 return status;
5379}
5380
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005381int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5382 const struct qstr *name,
5383 struct nfs4_fs_locations *fs_locations,
5384 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005385{
5386 struct nfs4_exception exception = { };
5387 int err;
5388 do {
5389 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005390 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005391 &exception);
5392 } while (exception.retry);
5393 return err;
5394}
5395
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005396static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5397{
5398 int status;
5399 struct nfs4_secinfo_arg args = {
5400 .dir_fh = NFS_FH(dir),
5401 .name = name,
5402 };
5403 struct nfs4_secinfo_res res = {
5404 .flavors = flavors,
5405 };
5406 struct rpc_message msg = {
5407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5408 .rpc_argp = &args,
5409 .rpc_resp = &res,
5410 };
5411
5412 dprintk("NFS call secinfo %s\n", name->name);
5413 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5414 dprintk("NFS reply secinfo: %d\n", status);
5415 return status;
5416}
5417
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005418int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5419 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005420{
5421 struct nfs4_exception exception = { };
5422 int err;
5423 do {
5424 err = nfs4_handle_exception(NFS_SERVER(dir),
5425 _nfs4_proc_secinfo(dir, name, flavors),
5426 &exception);
5427 } while (exception.retry);
5428 return err;
5429}
5430
Andy Adamson557134a2009-04-01 09:21:53 -04005431#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005432/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005433 * Check the exchange flags returned by the server for invalid flags, having
5434 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5435 * DS flags set.
5436 */
5437static int nfs4_check_cl_exchange_flags(u32 flags)
5438{
5439 if (flags & ~EXCHGID4_FLAG_MASK_R)
5440 goto out_inval;
5441 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5442 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5443 goto out_inval;
5444 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5445 goto out_inval;
5446 return NFS_OK;
5447out_inval:
5448 return -NFS4ERR_INVAL;
5449}
5450
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005451static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005452nfs41_same_server_scope(struct nfs41_server_scope *a,
5453 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005454{
5455 if (a->server_scope_sz == b->server_scope_sz &&
5456 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5457 return true;
5458
5459 return false;
5460}
5461
Andy Adamson357f54d2010-12-14 10:11:57 -05005462/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005463 * nfs4_proc_bind_conn_to_session()
5464 *
5465 * The 4.1 client currently uses the same TCP connection for the
5466 * fore and backchannel.
5467 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005468int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005469{
5470 int status;
5471 struct nfs41_bind_conn_to_session_res res;
5472 struct rpc_message msg = {
5473 .rpc_proc =
5474 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5475 .rpc_argp = clp,
5476 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005477 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005478 };
5479
5480 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005481
5482 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5483 if (unlikely(res.session == NULL)) {
5484 status = -ENOMEM;
5485 goto out;
5486 }
5487
5488 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5489 if (status == 0) {
5490 if (memcmp(res.session->sess_id.data,
5491 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5492 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5493 status = -EIO;
5494 goto out_session;
5495 }
5496 if (res.dir != NFS4_CDFS4_BOTH) {
5497 dprintk("NFS: %s: Unexpected direction from server\n",
5498 __func__);
5499 status = -EIO;
5500 goto out_session;
5501 }
5502 if (res.use_conn_in_rdma_mode) {
5503 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5504 __func__);
5505 status = -EIO;
5506 goto out_session;
5507 }
5508 }
5509out_session:
5510 kfree(res.session);
5511out:
5512 dprintk("<-- %s status= %d\n", __func__, status);
5513 return status;
5514}
5515
5516/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005517 * nfs4_proc_exchange_id()
5518 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005519 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5520 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005521 * Since the clientid has expired, all compounds using sessions
5522 * associated with the stale clientid will be returning
5523 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5524 * be in some phase of session reset.
5525 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005526int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005527{
5528 nfs4_verifier verifier;
5529 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005530 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005531 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005532 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005533 };
5534 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005535 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005536 };
5537 int status;
5538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5540 .rpc_argp = &args,
5541 .rpc_resp = &res,
5542 .rpc_cred = cred,
5543 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005544
Chuck Leverf0920752012-05-21 22:45:41 -04005545 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005546 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5547 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005548 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5549 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5550 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005551
Chuck Leveracdeb692012-05-21 22:46:16 -04005552 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005553 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005554 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005555 status = -ENOMEM;
5556 goto out;
5557 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005558
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005559 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005560 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005561 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005562 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005563 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005564 }
5565
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005566 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005567 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005568 status = -ENOMEM;
5569 goto out_server_scope;
5570 }
5571
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005572 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005573 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005574 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005575
Chuck Lever177313f2012-05-21 22:44:58 -04005576 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005577 clp->cl_clientid = res.clientid;
5578 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5579 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5580 clp->cl_seqid = res.seqid;
5581
Chuck Leveracdeb692012-05-21 22:46:16 -04005582 kfree(clp->cl_serverowner);
5583 clp->cl_serverowner = res.server_owner;
5584 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005585
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005586 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005587 kfree(clp->cl_implid);
5588 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005589
Chuck Lever177313f2012-05-21 22:44:58 -04005590 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005591 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005592 res.server_scope)) {
5593 dprintk("%s: server_scope mismatch detected\n",
5594 __func__);
5595 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005596 kfree(clp->cl_serverscope);
5597 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005598 }
5599
Chuck Lever177313f2012-05-21 22:44:58 -04005600 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005601 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005602 goto out;
5603 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005604 } else
5605 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005606
Chuck Leveracdeb692012-05-21 22:46:16 -04005607out_server_owner:
5608 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005609out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005610 kfree(res.server_scope);
5611out:
Chuck Lever177313f2012-05-21 22:44:58 -04005612 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005613 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005614 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005615 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005616 clp->cl_implid->date.seconds,
5617 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005618 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005619 return status;
5620}
5621
Trond Myklebust66245532012-05-25 17:18:09 -04005622static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5623 struct rpc_cred *cred)
5624{
5625 struct rpc_message msg = {
5626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5627 .rpc_argp = clp,
5628 .rpc_cred = cred,
5629 };
5630 int status;
5631
5632 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5633 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005634 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005635 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5636 return status;
5637}
5638
5639static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5640 struct rpc_cred *cred)
5641{
5642 unsigned int loop;
5643 int ret;
5644
5645 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5646 ret = _nfs4_proc_destroy_clientid(clp, cred);
5647 switch (ret) {
5648 case -NFS4ERR_DELAY:
5649 case -NFS4ERR_CLIENTID_BUSY:
5650 ssleep(1);
5651 break;
5652 default:
5653 return ret;
5654 }
5655 }
5656 return 0;
5657}
5658
5659int nfs4_destroy_clientid(struct nfs_client *clp)
5660{
5661 struct rpc_cred *cred;
5662 int ret = 0;
5663
5664 if (clp->cl_mvops->minor_version < 1)
5665 goto out;
5666 if (clp->cl_exchange_flags == 0)
5667 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005668 if (clp->cl_preserve_clid)
5669 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005670 cred = nfs4_get_exchange_id_cred(clp);
5671 ret = nfs4_proc_destroy_clientid(clp, cred);
5672 if (cred)
5673 put_rpccred(cred);
5674 switch (ret) {
5675 case 0:
5676 case -NFS4ERR_STALE_CLIENTID:
5677 clp->cl_exchange_flags = 0;
5678 }
5679out:
5680 return ret;
5681}
5682
Andy Adamson2050f0c2009-04-01 09:22:30 -04005683struct nfs4_get_lease_time_data {
5684 struct nfs4_get_lease_time_args *args;
5685 struct nfs4_get_lease_time_res *res;
5686 struct nfs_client *clp;
5687};
5688
5689static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5690 void *calldata)
5691{
5692 int ret;
5693 struct nfs4_get_lease_time_data *data =
5694 (struct nfs4_get_lease_time_data *)calldata;
5695
5696 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005697 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005698 /* just setup sequence, do not trigger session recovery
5699 since we're invoked within one */
5700 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005701 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005702 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005703
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005704 if (ret != -EAGAIN)
5705 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005706 dprintk("<-- %s\n", __func__);
5707}
5708
5709/*
5710 * Called from nfs4_state_manager thread for session setup, so don't recover
5711 * from sequence operation or clientid errors.
5712 */
5713static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5714{
5715 struct nfs4_get_lease_time_data *data =
5716 (struct nfs4_get_lease_time_data *)calldata;
5717
5718 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005719 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5720 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005721 switch (task->tk_status) {
5722 case -NFS4ERR_DELAY:
5723 case -NFS4ERR_GRACE:
5724 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5725 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5726 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005727 /* fall through */
5728 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005729 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005730 return;
5731 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005732 dprintk("<-- %s\n", __func__);
5733}
5734
Trond Myklebust17280172012-03-11 13:11:00 -04005735static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005736 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5737 .rpc_call_done = nfs4_get_lease_time_done,
5738};
5739
5740int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5741{
5742 struct rpc_task *task;
5743 struct nfs4_get_lease_time_args args;
5744 struct nfs4_get_lease_time_res res = {
5745 .lr_fsinfo = fsinfo,
5746 };
5747 struct nfs4_get_lease_time_data data = {
5748 .args = &args,
5749 .res = &res,
5750 .clp = clp,
5751 };
5752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5754 .rpc_argp = &args,
5755 .rpc_resp = &res,
5756 };
5757 struct rpc_task_setup task_setup = {
5758 .rpc_client = clp->cl_rpcclient,
5759 .rpc_message = &msg,
5760 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005761 .callback_data = &data,
5762 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005763 };
5764 int status;
5765
Trond Myklebust9d12b212012-01-17 22:04:25 -05005766 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005767 dprintk("--> %s\n", __func__);
5768 task = rpc_run_task(&task_setup);
5769
5770 if (IS_ERR(task))
5771 status = PTR_ERR(task);
5772 else {
5773 status = task->tk_status;
5774 rpc_put_task(task);
5775 }
5776 dprintk("<-- %s return %d\n", __func__, status);
5777
5778 return status;
5779}
5780
Trond Myklebust87dda672012-11-20 19:49:20 -05005781static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
5782 u32 max_reqs, u32 ivalue)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005783{
Trond Myklebust87dda672012-11-20 19:49:20 -05005784 if (max_reqs <= tbl->max_slots)
5785 return 0;
Trond Myklebust69d206b2012-11-22 13:21:02 -05005786 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
Trond Myklebust87dda672012-11-20 19:49:20 -05005787 return 0;
5788 return -ENOMEM;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005789}
5790
Trond Myklebust87dda672012-11-20 19:49:20 -05005791static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
5792 u32 server_highest_slotid,
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005793 u32 ivalue)
5794{
Trond Myklebust87dda672012-11-20 19:49:20 -05005795 struct nfs4_slot **p;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005796
Trond Myklebust87dda672012-11-20 19:49:20 -05005797 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
5798 p = &tbl->slots;
5799 while (*p) {
5800 (*p)->seq_nr = ivalue;
5801 p = &(*p)->next;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005802 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005803 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust87dda672012-11-20 19:49:20 -05005804 tbl->target_highest_slotid = server_highest_slotid;
5805 tbl->server_highest_slotid = server_highest_slotid;
5806 tbl->max_slotid = server_highest_slotid;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005807}
5808
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005809/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005810 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005811 */
Trond Myklebust87dda672012-11-20 19:49:20 -05005812static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
5813 u32 max_reqs, u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005814{
Trond Myklebust87dda672012-11-20 19:49:20 -05005815 int ret;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005816
Andy Adamson104aeba2010-01-14 17:45:10 -05005817 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5818 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005819
Trond Myklebust87dda672012-11-20 19:49:20 -05005820 if (max_reqs > NFS4_MAX_SLOT_TABLE)
5821 max_reqs = NFS4_MAX_SLOT_TABLE;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005822
Trond Myklebust87dda672012-11-20 19:49:20 -05005823 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
5824 if (ret)
5825 goto out;
5826
5827 spin_lock(&tbl->slot_tbl_lock);
5828 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
5829 spin_unlock(&tbl->slot_tbl_lock);
5830
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005831 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5832 tbl, tbl->slots, tbl->max_slots);
5833out:
5834 dprintk("<-- %s: return %d\n", __func__, ret);
5835 return ret;
5836}
5837
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005838/* Destroy the slot table */
5839static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5840{
Trond Myklebust87dda672012-11-20 19:49:20 -05005841 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
5842 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005843}
5844
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005845/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005846 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005847 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005848static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005849{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005850 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005851 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005852
Andy Adamsonaacd5532011-11-09 13:58:21 -05005853 dprintk("--> %s\n", __func__);
5854 /* Fore channel */
5855 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005856 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005857 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5858 if (status) /* -ENOMEM */
5859 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005860 /* Back channel */
5861 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005862 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005863 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5864 if (status && tbl->slots == NULL)
5865 /* Fore and back channel share a connection so get
5866 * both slot tables or neither */
5867 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005868 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005869}
5870
5871struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5872{
5873 struct nfs4_session *session;
5874 struct nfs4_slot_table *tbl;
5875
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005876 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005877 if (!session)
5878 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005879
Andy Adamson557134a2009-04-01 09:21:53 -04005880 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005881 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005882 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005883 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005884 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005885
5886 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005887 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005888 spin_lock_init(&tbl->slot_tbl_lock);
5889 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005890 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005891
Trond Myklebust1055d762010-06-16 09:52:27 -04005892 session->session_state = 1<<NFS4_SESSION_INITING;
5893
Andy Adamson557134a2009-04-01 09:21:53 -04005894 session->clp = clp;
5895 return session;
5896}
5897
5898void nfs4_destroy_session(struct nfs4_session *session)
5899{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005900 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005901 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005902
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005903 cred = nfs4_get_exchange_id_cred(session->clp);
5904 nfs4_proc_destroy_session(session, cred);
5905 if (cred)
5906 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005907
5908 rcu_read_lock();
5909 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5910 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005911 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005912 __func__, xprt);
5913 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005914 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005915 kfree(session);
5916}
5917
Andy Adamsonfc931582009-04-01 09:22:31 -04005918/*
5919 * Initialize the values to be used by the client in CREATE_SESSION
5920 * If nfs4_init_session set the fore channel request and response sizes,
5921 * use them.
5922 *
5923 * Set the back channel max_resp_sz_cached to zero to force the client to
5924 * always set csa_cachethis to FALSE because the current implementation
5925 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5926 */
5927static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5928{
5929 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005930 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5931 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005932
5933 if (mxrqst_sz == 0)
5934 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5935 if (mxresp_sz == 0)
5936 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5937 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005938 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5939 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005940 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005941 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005942
5943 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005944 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005945 __func__,
5946 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005947 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005948
5949 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005950 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5951 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5952 args->bc_attrs.max_resp_sz_cached = 0;
5953 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5954 args->bc_attrs.max_reqs = 1;
5955
5956 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5957 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5958 __func__,
5959 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5960 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5961 args->bc_attrs.max_reqs);
5962}
5963
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005964static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005965{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005966 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5967 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5968
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005969 if (rcvd->max_resp_sz > sent->max_resp_sz)
5970 return -EINVAL;
5971 /*
5972 * Our requested max_ops is the minimum we need; we're not
5973 * prepared to break up compounds into smaller pieces than that.
5974 * So, no point even trying to continue if the server won't
5975 * cooperate:
5976 */
5977 if (rcvd->max_ops < sent->max_ops)
5978 return -EINVAL;
5979 if (rcvd->max_reqs == 0)
5980 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005981 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5982 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005983 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005984}
5985
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005986static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5987{
5988 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5989 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005990
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005991 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5992 return -EINVAL;
5993 if (rcvd->max_resp_sz < sent->max_resp_sz)
5994 return -EINVAL;
5995 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5996 return -EINVAL;
5997 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005998 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005999 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006000 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006001 return -EINVAL;
6002 return 0;
6003}
Andy Adamson8d353012009-04-01 09:22:32 -04006004
Andy Adamson8d353012009-04-01 09:22:32 -04006005static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6006 struct nfs4_session *session)
6007{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006008 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006009
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006010 ret = nfs4_verify_fore_channel_attrs(args, session);
6011 if (ret)
6012 return ret;
6013 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006014}
6015
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006016static int _nfs4_proc_create_session(struct nfs_client *clp,
6017 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006018{
6019 struct nfs4_session *session = clp->cl_session;
6020 struct nfs41_create_session_args args = {
6021 .client = clp,
6022 .cb_program = NFS4_CALLBACK,
6023 };
6024 struct nfs41_create_session_res res = {
6025 .client = clp,
6026 };
6027 struct rpc_message msg = {
6028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6029 .rpc_argp = &args,
6030 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006031 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006032 };
6033 int status;
6034
6035 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006036 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006037
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006038 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04006039
Trond Myklebust43095d32012-11-20 11:13:12 -05006040 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006041 /* Verify the session's negotiated channel_attrs values */
6042 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006043 /* Increment the clientid slot sequence id */
6044 clp->cl_seqid++;
6045 }
6046
6047 return status;
6048}
6049
6050/*
6051 * Issues a CREATE_SESSION operation to the server.
6052 * It is the responsibility of the caller to verify the session is
6053 * expired before calling this routine.
6054 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006055int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006056{
6057 int status;
6058 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006059 struct nfs4_session *session = clp->cl_session;
6060
6061 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6062
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006063 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006064 if (status)
6065 goto out;
6066
Andy Adamsonaacd5532011-11-09 13:58:21 -05006067 /* Init or reset the session slot tables */
6068 status = nfs4_setup_session_slot_tables(session);
6069 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006070 if (status)
6071 goto out;
6072
6073 ptr = (unsigned *)&session->sess_id.data[0];
6074 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6075 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006076out:
6077 dprintk("<-- %s\n", __func__);
6078 return status;
6079}
6080
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006081/*
6082 * Issue the over-the-wire RPC DESTROY_SESSION.
6083 * The caller must serialize access to this routine.
6084 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006085int nfs4_proc_destroy_session(struct nfs4_session *session,
6086 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006087{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006088 struct rpc_message msg = {
6089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6090 .rpc_argp = session,
6091 .rpc_cred = cred,
6092 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006093 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006094
6095 dprintk("--> nfs4_proc_destroy_session\n");
6096
6097 /* session is still being setup */
6098 if (session->clp->cl_cons_state != NFS_CS_READY)
6099 return status;
6100
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006101 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006102
6103 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006104 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006105 "Session has been destroyed regardless...\n", status);
6106
6107 dprintk("<-- nfs4_proc_destroy_session\n");
6108 return status;
6109}
6110
Trond Myklebust7b38c362012-05-23 13:23:31 -04006111/*
6112 * With sessions, the client is not marked ready until after a
6113 * successful EXCHANGE_ID and CREATE_SESSION.
6114 *
6115 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6116 * other versions of NFS can be tried.
6117 */
6118static int nfs41_check_session_ready(struct nfs_client *clp)
6119{
6120 int ret;
6121
6122 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6123 ret = nfs4_client_recover_expired_lease(clp);
6124 if (ret)
6125 return ret;
6126 }
6127 if (clp->cl_cons_state < NFS_CS_READY)
6128 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006129 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006130 return 0;
6131}
6132
Trond Myklebustfccba802009-07-21 16:48:07 -04006133int nfs4_init_session(struct nfs_server *server)
6134{
6135 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006136 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006137 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6138 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006139
6140 if (!nfs4_has_session(clp))
6141 return 0;
6142
Trond Myklebustae72ae672012-11-20 11:02:55 -05006143 if (server->rsize != 0)
6144 target_max_resp_sz = server->rsize;
6145 target_max_resp_sz += nfs41_maxread_overhead;
6146
6147 if (server->wsize != 0)
6148 target_max_rqst_sz = server->wsize;
6149 target_max_rqst_sz += nfs41_maxwrite_overhead;
6150
Trond Myklebust1055d762010-06-16 09:52:27 -04006151 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006152 spin_lock(&clp->cl_lock);
6153 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006154 /* Initialise targets and channel attributes */
6155 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6156 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6157 session->fc_target_max_resp_sz = target_max_resp_sz;
6158 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006159 } else {
6160 /* Just adjust the targets */
6161 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6162 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6163 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6164 }
6165 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6166 session->fc_target_max_resp_sz = target_max_resp_sz;
6167 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6168 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006169 }
6170 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006171
Trond Myklebust688a9022012-11-20 10:53:39 -05006172 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6173 nfs4_schedule_lease_recovery(clp);
6174
Trond Myklebust7b38c362012-05-23 13:23:31 -04006175 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006176}
6177
Trond Myklebust7b38c362012-05-23 13:23:31 -04006178int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006179{
6180 struct nfs4_session *session = clp->cl_session;
6181 int ret;
6182
Trond Myklebust7b38c362012-05-23 13:23:31 -04006183 spin_lock(&clp->cl_lock);
6184 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6185 /*
6186 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6187 * DS lease to be equal to the MDS lease.
6188 */
6189 clp->cl_lease_time = lease_time;
6190 clp->cl_last_renewal = jiffies;
6191 }
6192 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006193
Trond Myklebust7b38c362012-05-23 13:23:31 -04006194 ret = nfs41_check_session_ready(clp);
6195 if (ret)
6196 return ret;
6197 /* Test for the DS role */
6198 if (!is_ds_client(clp))
6199 return -ENODEV;
6200 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006201}
6202EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6203
6204
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006205/*
6206 * Renew the cl_session lease.
6207 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006208struct nfs4_sequence_data {
6209 struct nfs_client *clp;
6210 struct nfs4_sequence_args args;
6211 struct nfs4_sequence_res res;
6212};
6213
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006214static void nfs41_sequence_release(void *data)
6215{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006216 struct nfs4_sequence_data *calldata = data;
6217 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006218
Alexandros Batsakis71358402010-02-05 03:45:05 -08006219 if (atomic_read(&clp->cl_count) > 1)
6220 nfs4_schedule_state_renewal(clp);
6221 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006222 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006223}
6224
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006225static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6226{
6227 switch(task->tk_status) {
6228 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006229 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6230 return -EAGAIN;
6231 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006232 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006233 }
6234 return 0;
6235}
6236
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006237static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006238{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006239 struct nfs4_sequence_data *calldata = data;
6240 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006241
Trond Myklebust14516c32010-07-31 14:29:06 -04006242 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6243 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006244
6245 if (task->tk_status < 0) {
6246 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006247 if (atomic_read(&clp->cl_count) == 1)
6248 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006249
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006250 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6251 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006252 return;
6253 }
6254 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006255 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006256out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006257 dprintk("<-- %s\n", __func__);
6258}
6259
6260static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6261{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006262 struct nfs4_sequence_data *calldata = data;
6263 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006264 struct nfs4_sequence_args *args;
6265 struct nfs4_sequence_res *res;
6266
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006267 args = task->tk_msg.rpc_argp;
6268 res = task->tk_msg.rpc_resp;
6269
Trond Myklebust9d12b212012-01-17 22:04:25 -05006270 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006271 return;
6272 rpc_call_start(task);
6273}
6274
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006275static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6276{
6277 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6278 nfs41_sequence_prepare(task, data);
6279}
6280
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006281static const struct rpc_call_ops nfs41_sequence_ops = {
6282 .rpc_call_done = nfs41_sequence_call_done,
6283 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006284 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006285};
6286
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006287static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6288 .rpc_call_done = nfs41_sequence_call_done,
6289 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6290 .rpc_release = nfs41_sequence_release,
6291};
6292
6293static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6294 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006295{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006296 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006297 struct rpc_message msg = {
6298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6299 .rpc_cred = cred,
6300 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006301 struct rpc_task_setup task_setup_data = {
6302 .rpc_client = clp->cl_rpcclient,
6303 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006304 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006305 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6306 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006307
Alexandros Batsakis71358402010-02-05 03:45:05 -08006308 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006309 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006310 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006311 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006312 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006313 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006314 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006315 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006316 msg.rpc_argp = &calldata->args;
6317 msg.rpc_resp = &calldata->res;
6318 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006319 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006320
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006321 return rpc_run_task(&task_setup_data);
6322}
6323
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006324static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006325{
6326 struct rpc_task *task;
6327 int ret = 0;
6328
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006329 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6330 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006331 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006332 if (IS_ERR(task))
6333 ret = PTR_ERR(task);
6334 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006335 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006336 dprintk("<-- %s status=%d\n", __func__, ret);
6337 return ret;
6338}
6339
6340static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6341{
6342 struct rpc_task *task;
6343 int ret;
6344
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006345 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006346 if (IS_ERR(task)) {
6347 ret = PTR_ERR(task);
6348 goto out;
6349 }
6350 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006351 if (!ret) {
6352 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6353
6354 if (task->tk_status == 0)
6355 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006356 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006357 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006358 rpc_put_task(task);
6359out:
6360 dprintk("<-- %s status=%d\n", __func__, ret);
6361 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006362}
6363
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006364struct nfs4_reclaim_complete_data {
6365 struct nfs_client *clp;
6366 struct nfs41_reclaim_complete_args arg;
6367 struct nfs41_reclaim_complete_res res;
6368};
6369
6370static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6371{
6372 struct nfs4_reclaim_complete_data *calldata = data;
6373
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006374 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006375 if (nfs41_setup_sequence(calldata->clp->cl_session,
6376 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006377 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006378 return;
6379
6380 rpc_call_start(task);
6381}
6382
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006383static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6384{
6385 switch(task->tk_status) {
6386 case 0:
6387 case -NFS4ERR_COMPLETE_ALREADY:
6388 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6389 break;
6390 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006391 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006392 /* fall through */
6393 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006394 return -EAGAIN;
6395 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006396 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006397 }
6398 return 0;
6399}
6400
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006401static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6402{
6403 struct nfs4_reclaim_complete_data *calldata = data;
6404 struct nfs_client *clp = calldata->clp;
6405 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6406
6407 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006408 if (!nfs41_sequence_done(task, res))
6409 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006410
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006411 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6412 rpc_restart_call_prepare(task);
6413 return;
6414 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006415 dprintk("<-- %s\n", __func__);
6416}
6417
6418static void nfs4_free_reclaim_complete_data(void *data)
6419{
6420 struct nfs4_reclaim_complete_data *calldata = data;
6421
6422 kfree(calldata);
6423}
6424
6425static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6426 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6427 .rpc_call_done = nfs4_reclaim_complete_done,
6428 .rpc_release = nfs4_free_reclaim_complete_data,
6429};
6430
6431/*
6432 * Issue a global reclaim complete.
6433 */
6434static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6435{
6436 struct nfs4_reclaim_complete_data *calldata;
6437 struct rpc_task *task;
6438 struct rpc_message msg = {
6439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6440 };
6441 struct rpc_task_setup task_setup_data = {
6442 .rpc_client = clp->cl_rpcclient,
6443 .rpc_message = &msg,
6444 .callback_ops = &nfs4_reclaim_complete_call_ops,
6445 .flags = RPC_TASK_ASYNC,
6446 };
6447 int status = -ENOMEM;
6448
6449 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006450 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006451 if (calldata == NULL)
6452 goto out;
6453 calldata->clp = clp;
6454 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006455
Trond Myklebust9d12b212012-01-17 22:04:25 -05006456 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006457 msg.rpc_argp = &calldata->arg;
6458 msg.rpc_resp = &calldata->res;
6459 task_setup_data.callback_data = calldata;
6460 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006461 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006462 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006463 goto out;
6464 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006465 status = nfs4_wait_for_completion_rpc_task(task);
6466 if (status == 0)
6467 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006468 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006469 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006470out:
6471 dprintk("<-- %s status=%d\n", __func__, status);
6472 return status;
6473}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006474
6475static void
6476nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6477{
6478 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006479 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006480
6481 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006482 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6483 * right now covering the LAYOUTGET we are about to send.
6484 * However, that is not so catastrophic, and there seems
6485 * to be no way to prevent it completely.
6486 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006487 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006488 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006489 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006490 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6491 NFS_I(lgp->args.inode)->layout,
6492 lgp->args.ctx->state)) {
6493 rpc_exit(task, NFS4_OK);
6494 return;
6495 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006496 rpc_call_start(task);
6497}
6498
6499static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6500{
6501 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006502 struct inode *inode = lgp->args.inode;
6503 struct nfs_server *server = NFS_SERVER(inode);
6504 struct pnfs_layout_hdr *lo;
6505 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006506
6507 dprintk("--> %s\n", __func__);
6508
6509 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006510 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006511
6512 switch (task->tk_status) {
6513 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006514 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006515 case -NFS4ERR_LAYOUTTRYLATER:
6516 case -NFS4ERR_RECALLCONFLICT:
6517 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006518 break;
6519 case -NFS4ERR_EXPIRED:
6520 case -NFS4ERR_BAD_STATEID:
6521 spin_lock(&inode->i_lock);
6522 lo = NFS_I(inode)->layout;
6523 if (!lo || list_empty(&lo->plh_segs)) {
6524 spin_unlock(&inode->i_lock);
6525 /* If the open stateid was bad, then recover it. */
6526 state = lgp->args.ctx->state;
6527 } else {
6528 LIST_HEAD(head);
6529
6530 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6531 spin_unlock(&inode->i_lock);
6532 /* Mark the bad layout state as invalid, then
6533 * retry using the open stateid. */
6534 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006535 }
6536 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006537 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6538 rpc_restart_call_prepare(task);
6539out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006540 dprintk("<-- %s\n", __func__);
6541}
6542
Idan Kedar85541162012-08-02 11:47:10 +03006543static size_t max_response_pages(struct nfs_server *server)
6544{
6545 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6546 return nfs_page_array_len(0, max_resp_sz);
6547}
6548
6549static void nfs4_free_pages(struct page **pages, size_t size)
6550{
6551 int i;
6552
6553 if (!pages)
6554 return;
6555
6556 for (i = 0; i < size; i++) {
6557 if (!pages[i])
6558 break;
6559 __free_page(pages[i]);
6560 }
6561 kfree(pages);
6562}
6563
6564static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6565{
6566 struct page **pages;
6567 int i;
6568
6569 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6570 if (!pages) {
6571 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6572 return NULL;
6573 }
6574
6575 for (i = 0; i < size; i++) {
6576 pages[i] = alloc_page(gfp_flags);
6577 if (!pages[i]) {
6578 dprintk("%s: failed to allocate page\n", __func__);
6579 nfs4_free_pages(pages, size);
6580 return NULL;
6581 }
6582 }
6583
6584 return pages;
6585}
6586
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006587static void nfs4_layoutget_release(void *calldata)
6588{
6589 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006590 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6591 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006592
6593 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006594 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006595 put_nfs_open_context(lgp->args.ctx);
6596 kfree(calldata);
6597 dprintk("<-- %s\n", __func__);
6598}
6599
6600static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6601 .rpc_call_prepare = nfs4_layoutget_prepare,
6602 .rpc_call_done = nfs4_layoutget_done,
6603 .rpc_release = nfs4_layoutget_release,
6604};
6605
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006606struct pnfs_layout_segment *
6607nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006608{
6609 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006610 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006611 struct rpc_task *task;
6612 struct rpc_message msg = {
6613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6614 .rpc_argp = &lgp->args,
6615 .rpc_resp = &lgp->res,
6616 };
6617 struct rpc_task_setup task_setup_data = {
6618 .rpc_client = server->client,
6619 .rpc_message = &msg,
6620 .callback_ops = &nfs4_layoutget_call_ops,
6621 .callback_data = lgp,
6622 .flags = RPC_TASK_ASYNC,
6623 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006624 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006625 int status = 0;
6626
6627 dprintk("--> %s\n", __func__);
6628
Idan Kedar85541162012-08-02 11:47:10 +03006629 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6630 if (!lgp->args.layout.pages) {
6631 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006632 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006633 }
6634 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6635
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006636 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006637 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006638 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006639 task = rpc_run_task(&task_setup_data);
6640 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006641 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006642 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006643 if (status == 0)
6644 status = task->tk_status;
6645 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006646 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006647 rpc_put_task(task);
6648 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006649 if (status)
6650 return ERR_PTR(status);
6651 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006652}
6653
Benny Halevycbe82602011-05-22 19:52:37 +03006654static void
6655nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6656{
6657 struct nfs4_layoutreturn *lrp = calldata;
6658
6659 dprintk("--> %s\n", __func__);
6660 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006661 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006662 return;
6663 rpc_call_start(task);
6664}
6665
6666static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6667{
6668 struct nfs4_layoutreturn *lrp = calldata;
6669 struct nfs_server *server;
6670
6671 dprintk("--> %s\n", __func__);
6672
6673 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6674 return;
6675
6676 server = NFS_SERVER(lrp->args.inode);
6677 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006678 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006679 return;
6680 }
Benny Halevycbe82602011-05-22 19:52:37 +03006681 dprintk("<-- %s\n", __func__);
6682}
6683
6684static void nfs4_layoutreturn_release(void *calldata)
6685{
6686 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006687 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006688
6689 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006690 spin_lock(&lo->plh_inode->i_lock);
6691 if (lrp->res.lrs_present)
6692 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6693 lo->plh_block_lgets--;
6694 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006695 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006696 kfree(calldata);
6697 dprintk("<-- %s\n", __func__);
6698}
6699
6700static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6701 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6702 .rpc_call_done = nfs4_layoutreturn_done,
6703 .rpc_release = nfs4_layoutreturn_release,
6704};
6705
6706int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6707{
6708 struct rpc_task *task;
6709 struct rpc_message msg = {
6710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6711 .rpc_argp = &lrp->args,
6712 .rpc_resp = &lrp->res,
6713 };
6714 struct rpc_task_setup task_setup_data = {
6715 .rpc_client = lrp->clp->cl_rpcclient,
6716 .rpc_message = &msg,
6717 .callback_ops = &nfs4_layoutreturn_call_ops,
6718 .callback_data = lrp,
6719 };
6720 int status;
6721
6722 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006723 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006724 task = rpc_run_task(&task_setup_data);
6725 if (IS_ERR(task))
6726 return PTR_ERR(task);
6727 status = task->tk_status;
6728 dprintk("<-- %s status=%d\n", __func__, status);
6729 rpc_put_task(task);
6730 return status;
6731}
6732
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006733/*
6734 * Retrieve the list of Data Server devices from the MDS.
6735 */
6736static int _nfs4_getdevicelist(struct nfs_server *server,
6737 const struct nfs_fh *fh,
6738 struct pnfs_devicelist *devlist)
6739{
6740 struct nfs4_getdevicelist_args args = {
6741 .fh = fh,
6742 .layoutclass = server->pnfs_curr_ld->id,
6743 };
6744 struct nfs4_getdevicelist_res res = {
6745 .devlist = devlist,
6746 };
6747 struct rpc_message msg = {
6748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6749 .rpc_argp = &args,
6750 .rpc_resp = &res,
6751 };
6752 int status;
6753
6754 dprintk("--> %s\n", __func__);
6755 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6756 &res.seq_res, 0);
6757 dprintk("<-- %s status=%d\n", __func__, status);
6758 return status;
6759}
6760
6761int nfs4_proc_getdevicelist(struct nfs_server *server,
6762 const struct nfs_fh *fh,
6763 struct pnfs_devicelist *devlist)
6764{
6765 struct nfs4_exception exception = { };
6766 int err;
6767
6768 do {
6769 err = nfs4_handle_exception(server,
6770 _nfs4_getdevicelist(server, fh, devlist),
6771 &exception);
6772 } while (exception.retry);
6773
6774 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6775 err, devlist->num_devs);
6776
6777 return err;
6778}
6779EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6780
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006781static int
6782_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6783{
6784 struct nfs4_getdeviceinfo_args args = {
6785 .pdev = pdev,
6786 };
6787 struct nfs4_getdeviceinfo_res res = {
6788 .pdev = pdev,
6789 };
6790 struct rpc_message msg = {
6791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6792 .rpc_argp = &args,
6793 .rpc_resp = &res,
6794 };
6795 int status;
6796
6797 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006798 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006799 dprintk("<-- %s status=%d\n", __func__, status);
6800
6801 return status;
6802}
6803
6804int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6805{
6806 struct nfs4_exception exception = { };
6807 int err;
6808
6809 do {
6810 err = nfs4_handle_exception(server,
6811 _nfs4_proc_getdeviceinfo(server, pdev),
6812 &exception);
6813 } while (exception.retry);
6814 return err;
6815}
6816EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6817
Andy Adamson863a3c62011-03-23 13:27:54 +00006818static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6819{
6820 struct nfs4_layoutcommit_data *data = calldata;
6821 struct nfs_server *server = NFS_SERVER(data->args.inode);
6822
6823 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006824 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006825 return;
6826 rpc_call_start(task);
6827}
6828
6829static void
6830nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6831{
6832 struct nfs4_layoutcommit_data *data = calldata;
6833 struct nfs_server *server = NFS_SERVER(data->args.inode);
6834
6835 if (!nfs4_sequence_done(task, &data->res.seq_res))
6836 return;
6837
6838 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006839 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6840 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6841 case -NFS4ERR_BADLAYOUT: /* no layout */
6842 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006843 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006844 break;
6845 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006846 nfs_post_op_update_inode_force_wcc(data->args.inode,
6847 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006848 break;
6849 default:
6850 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6851 rpc_restart_call_prepare(task);
6852 return;
6853 }
6854 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006855}
6856
6857static void nfs4_layoutcommit_release(void *calldata)
6858{
6859 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006860 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006861 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006862
Andy Adamsondb29c082011-07-30 20:52:38 -04006863 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006864 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006865 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6866 list_del_init(&lseg->pls_lc_list);
6867 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6868 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006869 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006870 }
Peng Tao92407e72011-10-23 20:21:17 -07006871
6872 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6873 smp_mb__after_clear_bit();
6874 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6875
Andy Adamson863a3c62011-03-23 13:27:54 +00006876 put_rpccred(data->cred);
6877 kfree(data);
6878}
6879
6880static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6881 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6882 .rpc_call_done = nfs4_layoutcommit_done,
6883 .rpc_release = nfs4_layoutcommit_release,
6884};
6885
6886int
Andy Adamsonef311532011-03-12 02:58:10 -05006887nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006888{
6889 struct rpc_message msg = {
6890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6891 .rpc_argp = &data->args,
6892 .rpc_resp = &data->res,
6893 .rpc_cred = data->cred,
6894 };
6895 struct rpc_task_setup task_setup_data = {
6896 .task = &data->task,
6897 .rpc_client = NFS_CLIENT(data->args.inode),
6898 .rpc_message = &msg,
6899 .callback_ops = &nfs4_layoutcommit_ops,
6900 .callback_data = data,
6901 .flags = RPC_TASK_ASYNC,
6902 };
6903 struct rpc_task *task;
6904 int status = 0;
6905
6906 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6907 "lbw: %llu inode %lu\n",
6908 data->task.tk_pid, sync,
6909 data->args.lastbytewritten,
6910 data->args.inode->i_ino);
6911
Trond Myklebust9d12b212012-01-17 22:04:25 -05006912 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006913 task = rpc_run_task(&task_setup_data);
6914 if (IS_ERR(task))
6915 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006916 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006917 goto out;
6918 status = nfs4_wait_for_completion_rpc_task(task);
6919 if (status != 0)
6920 goto out;
6921 status = task->tk_status;
6922out:
6923 dprintk("%s: status %d\n", __func__, status);
6924 rpc_put_task(task);
6925 return status;
6926}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006927
6928static int
6929_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6930 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6931{
6932 struct nfs41_secinfo_no_name_args args = {
6933 .style = SECINFO_STYLE_CURRENT_FH,
6934 };
6935 struct nfs4_secinfo_res res = {
6936 .flavors = flavors,
6937 };
6938 struct rpc_message msg = {
6939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6940 .rpc_argp = &args,
6941 .rpc_resp = &res,
6942 };
6943 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6944}
6945
6946static int
6947nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6948 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6949{
6950 struct nfs4_exception exception = { };
6951 int err;
6952 do {
6953 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6954 switch (err) {
6955 case 0:
6956 case -NFS4ERR_WRONGSEC:
6957 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006958 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006959 default:
6960 err = nfs4_handle_exception(server, err, &exception);
6961 }
6962 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006963out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006964 return err;
6965}
6966
6967static int
6968nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6969 struct nfs_fsinfo *info)
6970{
6971 int err;
6972 struct page *page;
6973 rpc_authflavor_t flavor;
6974 struct nfs4_secinfo_flavors *flavors;
6975
6976 page = alloc_page(GFP_KERNEL);
6977 if (!page) {
6978 err = -ENOMEM;
6979 goto out;
6980 }
6981
6982 flavors = page_address(page);
6983 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6984
6985 /*
6986 * Fall back on "guess and check" method if
6987 * the server doesn't support SECINFO_NO_NAME
6988 */
6989 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6990 err = nfs4_find_root_sec(server, fhandle, info);
6991 goto out_freepage;
6992 }
6993 if (err)
6994 goto out_freepage;
6995
6996 flavor = nfs_find_best_sec(flavors);
6997 if (err == 0)
6998 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6999
7000out_freepage:
7001 put_page(page);
7002 if (err == -EACCES)
7003 return -EPERM;
7004out:
7005 return err;
7006}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007007
7008static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007009{
7010 int status;
7011 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007012 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007013 };
7014 struct nfs41_test_stateid_res res;
7015 struct rpc_message msg = {
7016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7017 .rpc_argp = &args,
7018 .rpc_resp = &res,
7019 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007020
Chuck Lever38527b12012-07-11 16:30:23 -04007021 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007022 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007023 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04007024 if (status != NFS_OK) {
7025 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007026 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007027 }
7028 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007029 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007030}
7031
Chuck Lever38527b12012-07-11 16:30:23 -04007032/**
7033 * nfs41_test_stateid - perform a TEST_STATEID operation
7034 *
7035 * @server: server / transport on which to perform the operation
7036 * @stateid: state ID to test
7037 *
7038 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7039 * Otherwise a negative NFS4ERR value is returned if the operation
7040 * failed or the state ID is not currently valid.
7041 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007042static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007043{
7044 struct nfs4_exception exception = { };
7045 int err;
7046 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007047 err = _nfs41_test_stateid(server, stateid);
7048 if (err != -NFS4ERR_DELAY)
7049 break;
7050 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007051 } while (exception.retry);
7052 return err;
7053}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007054
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007055static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007056{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007057 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007058 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007059 };
7060 struct nfs41_free_stateid_res res;
7061 struct rpc_message msg = {
7062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7063 .rpc_argp = &args,
7064 .rpc_resp = &res,
7065 };
Chuck Lever38527b12012-07-11 16:30:23 -04007066 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007067
Chuck Lever38527b12012-07-11 16:30:23 -04007068 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007069 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04007070 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04007071 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04007072 dprintk("NFS reply free_stateid: %d\n", status);
7073 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007074}
7075
Chuck Lever38527b12012-07-11 16:30:23 -04007076/**
7077 * nfs41_free_stateid - perform a FREE_STATEID operation
7078 *
7079 * @server: server / transport on which to perform the operation
7080 * @stateid: state ID to release
7081 *
7082 * Returns NFS_OK if the server freed "stateid". Otherwise a
7083 * negative NFS4ERR value is returned.
7084 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007085static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007086{
7087 struct nfs4_exception exception = { };
7088 int err;
7089 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007090 err = _nfs4_free_stateid(server, stateid);
7091 if (err != -NFS4ERR_DELAY)
7092 break;
7093 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007094 } while (exception.retry);
7095 return err;
7096}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007097
7098static bool nfs41_match_stateid(const nfs4_stateid *s1,
7099 const nfs4_stateid *s2)
7100{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007101 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007102 return false;
7103
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007104 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007105 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007106 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007107 return true;
7108
7109 return false;
7110}
7111
Andy Adamson557134a2009-04-01 09:21:53 -04007112#endif /* CONFIG_NFS_V4_1 */
7113
Trond Myklebust36281ca2012-03-04 18:13:56 -05007114static bool nfs4_match_stateid(const nfs4_stateid *s1,
7115 const nfs4_stateid *s2)
7116{
Trond Myklebustf597c532012-03-04 18:13:56 -05007117 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007118}
7119
7120
Trond Myklebust17280172012-03-11 13:11:00 -04007121static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007122 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007123 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 .recover_open = nfs4_open_reclaim,
7125 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007126 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007127 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007128 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129};
7130
Andy Adamson591d71c2009-04-01 09:22:47 -04007131#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007132static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007133 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7134 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7135 .recover_open = nfs4_open_reclaim,
7136 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007137 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007138 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007139 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007140 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007141};
7142#endif /* CONFIG_NFS_V4_1 */
7143
Trond Myklebust17280172012-03-11 13:11:00 -04007144static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007145 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007146 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 .recover_open = nfs4_open_expired,
7148 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007149 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007150 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151};
7152
Andy Adamson591d71c2009-04-01 09:22:47 -04007153#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007154static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007155 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7156 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007157 .recover_open = nfs41_open_expired,
7158 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007159 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007160 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007161};
7162#endif /* CONFIG_NFS_V4_1 */
7163
Trond Myklebust17280172012-03-11 13:11:00 -04007164static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007165 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007166 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007167 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007168};
7169
7170#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007171static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007172 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007173 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007174 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007175};
7176#endif
7177
Trond Myklebust97dc1352010-06-16 09:52:26 -04007178static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7179 .minor_version = 0,
7180 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007181 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007182 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007183 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7184 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7185 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007186};
7187
7188#if defined(CONFIG_NFS_V4_1)
7189static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7190 .minor_version = 1,
7191 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007192 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007193 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007194 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7195 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7196 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007197};
7198#endif
7199
Trond Myklebust97dc1352010-06-16 09:52:26 -04007200const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7201 [0] = &nfs_v4_0_minor_ops,
7202#if defined(CONFIG_NFS_V4_1)
7203 [1] = &nfs_v4_1_minor_ops,
7204#endif
7205};
7206
Bryan Schumaker73a79702012-07-16 16:39:12 -04007207const struct inode_operations nfs4_dir_inode_operations = {
7208 .create = nfs_create,
7209 .lookup = nfs_lookup,
7210 .atomic_open = nfs_atomic_open,
7211 .link = nfs_link,
7212 .unlink = nfs_unlink,
7213 .symlink = nfs_symlink,
7214 .mkdir = nfs_mkdir,
7215 .rmdir = nfs_rmdir,
7216 .mknod = nfs_mknod,
7217 .rename = nfs_rename,
7218 .permission = nfs_permission,
7219 .getattr = nfs_getattr,
7220 .setattr = nfs_setattr,
7221 .getxattr = generic_getxattr,
7222 .setxattr = generic_setxattr,
7223 .listxattr = generic_listxattr,
7224 .removexattr = generic_removexattr,
7225};
7226
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007227static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007228 .permission = nfs_permission,
7229 .getattr = nfs_getattr,
7230 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007231 .getxattr = generic_getxattr,
7232 .setxattr = generic_setxattr,
7233 .listxattr = generic_listxattr,
7234 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007235};
7236
David Howells509de812006-08-22 20:06:11 -04007237const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 .version = 4, /* protocol version */
7239 .dentry_ops = &nfs4_dentry_operations,
7240 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007241 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007242 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007244 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007245 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 .getattr = nfs4_proc_getattr,
7247 .setattr = nfs4_proc_setattr,
7248 .lookup = nfs4_proc_lookup,
7249 .access = nfs4_proc_access,
7250 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 .create = nfs4_proc_create,
7252 .remove = nfs4_proc_remove,
7253 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007254 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 .unlink_done = nfs4_proc_unlink_done,
7256 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007257 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007258 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007259 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 .link = nfs4_proc_link,
7261 .symlink = nfs4_proc_symlink,
7262 .mkdir = nfs4_proc_mkdir,
7263 .rmdir = nfs4_proc_remove,
7264 .readdir = nfs4_proc_readdir,
7265 .mknod = nfs4_proc_mknod,
7266 .statfs = nfs4_proc_statfs,
7267 .fsinfo = nfs4_proc_fsinfo,
7268 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007269 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 .decode_dirent = nfs4_decode_dirent,
7271 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007272 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007273 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007274 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007276 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007277 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007278 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007280 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007281 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007283 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007284 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007285 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007286 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007287 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007288 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007289 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007290 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007291 .create_server = nfs4_create_server,
7292 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293};
7294
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007295static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7296 .prefix = XATTR_NAME_NFSV4_ACL,
7297 .list = nfs4_xattr_list_nfs4_acl,
7298 .get = nfs4_xattr_get_nfs4_acl,
7299 .set = nfs4_xattr_set_nfs4_acl,
7300};
7301
7302const struct xattr_handler *nfs4_xattr_handlers[] = {
7303 &nfs4_xattr_nfs4_acl_handler,
7304 NULL
7305};
7306
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307/*
7308 * Local variables:
7309 * c-basic-offset: 8
7310 * End:
7311 */