blob: 0b0f11be40f94043e7ece278acaee7533cf7e928 [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;
Trond Myklebust5d633602012-11-23 13:09:38 -0500448 else {
Trond Myklebust45d43c22012-02-06 19:38:51 -0500449 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust5d633602012-11-23 13:09:38 -0500450 nfs4_session_drain_complete(tbl->session, tbl);
451 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400452 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500453 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
454 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400455}
456
Trond Myklebust961a8282012-01-17 22:57:37 -0500457bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
458{
459 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
460 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400461}
462
Trond Myklebustd185a332010-06-16 09:52:25 -0400463static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400464{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500465 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400466 struct nfs4_slot_table *tbl;
467
Benny Halevydfb4f3092010-09-24 09:17:01 -0400468 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400469 /* just wake up the next guy waiting since
470 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500471 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500472 return;
Andy Adamson13615872009-04-01 09:22:17 -0400473 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500474 tbl = res->sr_slot->table;
475 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500476
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500477 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500478 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebust5d633602012-11-23 13:09:38 -0500479 if (!nfs4_session_draining(session))
480 rpc_wake_up_first(&tbl->slot_tbl_waitq,
481 nfs4_set_task_privileged, NULL);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500482 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400483 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400484}
485
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500486/* Update the client's idea of target_highest_slotid */
487static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
488 u32 target_highest_slotid)
489{
Trond Myklebust97e548a2012-11-20 14:45:48 -0500490 unsigned int max_slotid, i;
491
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500492 if (tbl->target_highest_slotid == target_highest_slotid)
493 return;
494 tbl->target_highest_slotid = target_highest_slotid;
495 tbl->generation++;
Trond Myklebust97e548a2012-11-20 14:45:48 -0500496
Trond Myklebust87dda672012-11-20 19:49:20 -0500497 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
Trond Myklebust97e548a2012-11-20 14:45:48 -0500498 for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
499 rpc_wake_up_next(&tbl->slot_tbl_waitq);
500 tbl->max_slotid = max_slotid;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500501}
502
Trond Myklebustce008c42012-11-20 15:16:30 -0500503void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
504 u32 target_highest_slotid)
505{
506 spin_lock(&tbl->slot_tbl_lock);
507 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
508 spin_unlock(&tbl->slot_tbl_lock);
509}
510
Trond Myklebustda0507b2012-11-20 18:10:30 -0500511static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
512 u32 highest_slotid)
513{
Trond Myklebustda0507b2012-11-20 18:10:30 -0500514 if (tbl->server_highest_slotid == highest_slotid)
515 return;
516 if (tbl->highest_used_slotid > highest_slotid)
517 return;
Trond Myklebust87dda672012-11-20 19:49:20 -0500518 /* Deallocate slots */
519 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
Trond Myklebustda0507b2012-11-20 18:10:30 -0500520 tbl->server_highest_slotid = highest_slotid;
521}
522
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500523static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
524 struct nfs4_slot *slot,
525 struct nfs4_sequence_res *res)
526{
527 spin_lock(&tbl->slot_tbl_lock);
528 if (tbl->generation != slot->generation)
529 goto out;
Trond Myklebustda0507b2012-11-20 18:10:30 -0500530 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500531 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
532out:
533 spin_unlock(&tbl->slot_tbl_lock);
534}
535
Trond Myklebust14516c32010-07-31 14:29:06 -0400536static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400537{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500538 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500539 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400540 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400541 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400542
543 /*
544 * sr_status remains 1 if an RPC level error occurred. The server
545 * may or may not have processed the sequence operation..
546 * Proceed as if the server received and processed the sequence
547 * operation.
548 */
549 if (res->sr_status == 1)
550 res->sr_status = NFS_OK;
551
Bryan Schumaker468f8612011-04-18 15:57:32 -0400552 /* don't increment the sequence number if the task wasn't sent */
553 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400554 goto out;
555
Trond Myklebust933602e2012-11-16 12:12:38 -0500556 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500557 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500558
Andy Adamson691daf32009-12-04 15:55:39 -0500559 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400560 switch (res->sr_status) {
561 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400562 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500563 ++slot->seq_nr;
564 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500565 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400566 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500567 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500568 if (res->sr_status_flags != 0)
569 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500570 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400571 break;
572 case -NFS4ERR_DELAY:
573 /* The server detected a resend of the RPC call and
574 * returned NFS4ERR_DELAY as per Section 2.10.6.2
575 * of RFC5661.
576 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500577 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400578 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500579 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500580 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400581 goto out_retry;
582 default:
583 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500584 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400585 }
586out:
587 /* The session may be reset by one of the error handlers. */
588 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400589 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400590 return 1;
591out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400592 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400593 goto out;
594 rpc_delay(task, NFS4_POLL_RETRY_MAX);
595 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400596}
597
Trond Myklebust14516c32010-07-31 14:29:06 -0400598static int nfs4_sequence_done(struct rpc_task *task,
599 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400600{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500601 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400602 return 1;
603 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400604}
605
Trond Myklebust87dda672012-11-20 19:49:20 -0500606static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
607 u32 slotid, u32 seq_init, gfp_t gfp_mask)
608{
609 struct nfs4_slot *slot;
610
611 slot = kzalloc(sizeof(*slot), gfp_mask);
612 if (slot) {
613 slot->table = tbl;
614 slot->slot_nr = slotid;
615 slot->seq_nr = seq_init;
616 }
617 return slot;
618}
619
620static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
621 u32 slotid, u32 seq_init, gfp_t gfp_mask)
622{
623 struct nfs4_slot **p, *slot;
624
625 p = &tbl->slots;
626 for (;;) {
627 if (*p == NULL) {
628 *p = nfs4_new_slot(tbl, tbl->max_slots,
629 seq_init, gfp_mask);
630 if (*p == NULL)
631 break;
632 tbl->max_slots++;
633 }
634 slot = *p;
635 if (slot->slot_nr == slotid)
636 return slot;
637 p = &slot->next;
638 }
Trond Myklebust69d206b2012-11-22 13:21:02 -0500639 return ERR_PTR(-ENOMEM);
Trond Myklebust87dda672012-11-20 19:49:20 -0500640}
641
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400642/*
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500643 * nfs4_alloc_slot - efficiently look for a free slot
Benny Halevy510b8172009-04-01 09:22:14 -0400644 *
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500645 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
Benny Halevy510b8172009-04-01 09:22:14 -0400646 * If found, we mark the slot as used, update the highest_used_slotid,
647 * and respectively set up the sequence operation args.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400648 *
649 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400650 */
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500651static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400652{
Trond Myklebust69d206b2012-11-22 13:21:02 -0500653 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500654 u32 slotid;
Benny Halevy510b8172009-04-01 09:22:14 -0400655
Trond Myklebust45d43c22012-02-06 19:38:51 -0500656 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400657 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Trond Myklebust87dda672012-11-20 19:49:20 -0500658 tbl->max_slotid + 1);
Trond Myklebust97e548a2012-11-20 14:45:48 -0500659 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
660 if (slotid > tbl->max_slotid)
Benny Halevy510b8172009-04-01 09:22:14 -0400661 goto out;
Trond Myklebust87dda672012-11-20 19:49:20 -0500662 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500663 if (IS_ERR(ret))
Trond Myklebust87dda672012-11-20 19:49:20 -0500664 goto out;
Benny Halevy510b8172009-04-01 09:22:14 -0400665 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500666 if (slotid > tbl->highest_used_slotid ||
667 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400668 tbl->highest_used_slotid = slotid;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500669 ret->renewal_time = jiffies;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500670 ret->generation = tbl->generation;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500671
Benny Halevy510b8172009-04-01 09:22:14 -0400672out:
673 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500674 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Trond Myklebust69d206b2012-11-22 13:21:02 -0500675 !IS_ERR(ret) ? ret->slot_nr : -1);
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500676 return ret;
Benny Halevy510b8172009-04-01 09:22:14 -0400677}
678
Trond Myklebust9d12b212012-01-17 22:04:25 -0500679static void nfs41_init_sequence(struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res, int cache_reply)
681{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500682 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500683 args->sa_cache_this = 0;
684 if (cache_reply)
685 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500686 res->sr_slot = NULL;
687}
688
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000689int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 struct nfs4_sequence_args *args,
691 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 struct rpc_task *task)
693{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400694 struct nfs4_slot *slot;
695 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400696
697 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400699 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 return 0;
701
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400702 tbl = &session->fc_slot_table;
703
Trond Myklebust69d206b2012-11-22 13:21:02 -0500704 task->tk_timeout = 0;
705
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400706 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400707 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800708 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500709 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500710 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400711 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500712 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500713 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400714 }
715
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800716 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
717 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
718 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
719 spin_unlock(&tbl->slot_tbl_lock);
720 dprintk("%s enforce FIFO order\n", __func__);
721 return -EAGAIN;
722 }
723
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500724 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500725 if (IS_ERR(slot)) {
726 /* If out of memory, try again in 1/4 second */
727 if (slot == ERR_PTR(-ENOMEM))
728 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400729 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
730 spin_unlock(&tbl->slot_tbl_lock);
731 dprintk("<-- %s: no free slots\n", __func__);
732 return -EAGAIN;
733 }
734 spin_unlock(&tbl->slot_tbl_lock);
735
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800736 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500737
738 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400739
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500740 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
741 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742
Benny Halevydfb4f3092010-09-24 09:17:01 -0400743 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400744 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400745 /*
746 * sr_status is only set in decode_sequence, and so will remain
747 * set to 1 if an rpc level failure occurs.
748 */
749 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400750 return 0;
751}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000752EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400753
Trond Myklebust035168a2010-06-16 09:52:26 -0400754int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400755 struct nfs4_sequence_args *args,
756 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400757 struct rpc_task *task)
758{
Trond Myklebust035168a2010-06-16 09:52:26 -0400759 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760 int ret = 0;
761
Trond Myklebust9d12b212012-01-17 22:04:25 -0500762 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400763 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400764
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500765 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400766 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500767 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400768
Trond Myklebust9d12b212012-01-17 22:04:25 -0500769 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400770out:
771 dprintk("<-- %s status=%d\n", __func__, ret);
772 return ret;
773}
774
775struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400776 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400777 struct nfs4_sequence_args *seq_args;
778 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400779};
780
781static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
782{
783 struct nfs41_call_sync_data *data = calldata;
784
Trond Myklebust035168a2010-06-16 09:52:26 -0400785 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
786
787 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500788 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400789 return;
790 rpc_call_start(task);
791}
792
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800793static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
794{
795 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
796 nfs41_call_sync_prepare(task, calldata);
797}
798
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
800{
801 struct nfs41_call_sync_data *data = calldata;
802
Trond Myklebust14516c32010-07-31 14:29:06 -0400803 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804}
805
Trond Myklebust17280172012-03-11 13:11:00 -0400806static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400807 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400808 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809};
810
Trond Myklebust17280172012-03-11 13:11:00 -0400811static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800812 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
813 .rpc_call_done = nfs41_call_sync_done,
814};
815
Bryan Schumaker7c513052011-03-24 17:12:24 +0000816static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
817 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400818 struct rpc_message *msg,
819 struct nfs4_sequence_args *args,
820 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800821 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400822{
823 int ret;
824 struct rpc_task *task;
825 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400826 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400827 .seq_args = args,
828 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400829 };
830 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000831 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400832 .rpc_message = msg,
833 .callback_ops = &nfs41_call_sync_ops,
834 .callback_data = &data
835 };
836
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800837 if (privileged)
838 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400839 task = rpc_run_task(&task_setup);
840 if (IS_ERR(task))
841 ret = PTR_ERR(task);
842 else {
843 ret = task->tk_status;
844 rpc_put_task(task);
845 }
846 return ret;
847}
848
Bryan Schumaker7c513052011-03-24 17:12:24 +0000849int _nfs4_call_sync_session(struct rpc_clnt *clnt,
850 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400851 struct rpc_message *msg,
852 struct nfs4_sequence_args *args,
853 struct nfs4_sequence_res *res,
854 int cache_reply)
855{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500856 nfs41_init_sequence(args, res, cache_reply);
857 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400858}
859
Trond Myklebustdf896452010-06-16 09:52:26 -0400860#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500861static inline
862void nfs41_init_sequence(struct nfs4_sequence_args *args,
863 struct nfs4_sequence_res *res, int cache_reply)
864{
865}
866
Trond Myklebust14516c32010-07-31 14:29:06 -0400867static int nfs4_sequence_done(struct rpc_task *task,
868 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400869{
Trond Myklebust14516c32010-07-31 14:29:06 -0400870 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400871}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400872#endif /* CONFIG_NFS_V4_1 */
873
Bryan Schumaker7c513052011-03-24 17:12:24 +0000874int _nfs4_call_sync(struct rpc_clnt *clnt,
875 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400876 struct rpc_message *msg,
877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
879 int cache_reply)
880{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500881 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000882 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400883}
884
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000885static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000886int nfs4_call_sync(struct rpc_clnt *clnt,
887 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000888 struct rpc_message *msg,
889 struct nfs4_sequence_args *args,
890 struct nfs4_sequence_res *res,
891 int cache_reply)
892{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000893 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
894 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000895}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400896
Trond Myklebust38478b22006-05-25 01:40:57 -0400897static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Trond Myklebust38478b22006-05-25 01:40:57 -0400899 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Trond Myklebust38478b22006-05-25 01:40:57 -0400901 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400902 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700903 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400904 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700905 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400906 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100909struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400910 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100911 struct nfs_openargs o_arg;
912 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100913 struct nfs_open_confirmargs c_arg;
914 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500915 struct nfs4_string owner_name;
916 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100917 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100918 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400919 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100920 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400921 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100922 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100923 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400924 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100925 int rpc_status;
926 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100927};
928
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400929
930static void nfs4_init_opendata_res(struct nfs4_opendata *p)
931{
932 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400933 p->o_res.seqid = p->o_arg.seqid;
934 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400935 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400936 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400937 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500938 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400939}
940
Al Viro82a2c1b2011-06-22 18:30:55 -0400941static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400943 const struct iattr *attrs,
944 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100945{
Al Viro82a2c1b2011-06-22 18:30:55 -0400946 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100947 struct inode *dir = parent->d_inode;
948 struct nfs_server *server = NFS_SERVER(dir);
949 struct nfs4_opendata *p;
950
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400951 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100952 if (p == NULL)
953 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400954 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 if (p->o_arg.seqid == NULL)
956 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400957 nfs_sb_active(dentry->d_sb);
958 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959 p->dir = parent;
960 p->owner = sp;
961 atomic_inc(&sp->so_count);
962 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 p->o_arg.open_flags = flags;
964 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700965 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
966 * will return permission denied for all bits until close */
967 if (!(flags & O_EXCL)) {
968 /* ask server to check for all possible rights as results
969 * are cached */
970 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
971 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700972 }
David Howells7539bba2006-08-22 20:06:09 -0400973 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400974 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
975 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400976 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100977 p->o_arg.server = server;
978 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400979 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100980 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500981 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500982 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400983
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984 p->o_arg.u.attrs = &p->attrs;
985 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500986
987 verf[0] = jiffies;
988 verf[1] = current->pid;
989 memcpy(p->o_arg.u.verifier.data, verf,
990 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100991 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100992 p->c_arg.fh = &p->o_res.fh;
993 p->c_arg.stateid = &p->o_res.stateid;
994 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400995 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400996 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100997 return p;
998err_free:
999 kfree(p);
1000err:
1001 dput(parent);
1002 return NULL;
1003}
1004
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001005static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001007 struct nfs4_opendata *p = container_of(kref,
1008 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001009 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001010
1011 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001012 if (p->state != NULL)
1013 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001014 nfs4_put_state_owner(p->owner);
1015 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001016 dput(p->dentry);
1017 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001018 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001019 kfree(p);
1020}
1021
1022static void nfs4_opendata_put(struct nfs4_opendata *p)
1023{
1024 if (p != NULL)
1025 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001026}
1027
Trond Myklebust06f814a2006-01-03 09:55:07 +01001028static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1029{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001030 int ret;
1031
Trond Myklebust06f814a2006-01-03 09:55:07 +01001032 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001033 return ret;
1034}
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001037{
1038 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039
Trond Myklebust536e43d2012-01-17 22:04:26 -05001040 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041 goto out;
1042 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001043 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001044 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1045 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001046 break;
1047 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001048 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1049 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050 break;
1051 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001052 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1053 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001054 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001056 return ret;
1057}
1058
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001061 if (delegation == NULL)
1062 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001065 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001067 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 return 1;
1069}
1070
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001072{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001074 case FMODE_WRITE:
1075 state->n_wronly++;
1076 break;
1077 case FMODE_READ:
1078 state->n_rdonly++;
1079 break;
1080 case FMODE_READ|FMODE_WRITE:
1081 state->n_rdwr++;
1082 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001084}
1085
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001087{
1088 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001089 nfs4_stateid_copy(&state->stateid, stateid);
1090 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001092 case FMODE_READ:
1093 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1094 break;
1095 case FMODE_WRITE:
1096 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1097 break;
1098 case FMODE_READ|FMODE_WRITE:
1099 set_bit(NFS_O_RDWR_STATE, &state->flags);
1100 }
1101}
1102
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001104{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001105 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001107 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001108}
1109
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110static 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 -07001111{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001112 /*
1113 * Protect the call to nfs4_state_set_mode_locked and
1114 * serialise the stateid update
1115 */
1116 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001117 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001118 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001119 set_bit(NFS_DELEGATED_STATE, &state->flags);
1120 }
1121 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001123 write_sequnlock(&state->seqlock);
1124 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001126 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001129static 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 -05001130{
1131 struct nfs_inode *nfsi = NFS_I(state->inode);
1132 struct nfs_delegation *deleg_cur;
1133 int ret = 0;
1134
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001136
1137 rcu_read_lock();
1138 deleg_cur = rcu_dereference(nfsi->delegation);
1139 if (deleg_cur == NULL)
1140 goto no_delegation;
1141
1142 spin_lock(&deleg_cur->lock);
1143 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001144 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001145 goto no_delegation_unlock;
1146
1147 if (delegation == NULL)
1148 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001149 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001150 goto no_delegation_unlock;
1151
Trond Myklebustb7391f42008-12-23 15:21:52 -05001152 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001154 ret = 1;
1155no_delegation_unlock:
1156 spin_unlock(&deleg_cur->lock);
1157no_delegation:
1158 rcu_read_unlock();
1159
1160 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001161 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001162 ret = 1;
1163 }
1164
1165 return ret;
1166}
1167
1168
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001169static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001170{
1171 struct nfs_delegation *delegation;
1172
1173 rcu_read_lock();
1174 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001175 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001176 rcu_read_unlock();
1177 return;
1178 }
1179 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001180 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001181}
1182
Trond Myklebust6ee41262007-07-08 14:11:36 -04001183static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001184{
1185 struct nfs4_state *state = opendata->state;
1186 struct nfs_inode *nfsi = NFS_I(state->inode);
1187 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001188 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001189 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001190 nfs4_stateid stateid;
1191 int ret = -EAGAIN;
1192
Trond Myklebustaac00a82007-07-05 19:02:21 -04001193 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001194 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001195 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001196 if (can_open_cached(state, fmode, open_mode)) {
1197 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001198 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001199 goto out_return_state;
1200 }
1201 spin_unlock(&state->owner->so_lock);
1202 }
Trond Myklebust34310432008-12-23 15:21:38 -05001203 rcu_read_lock();
1204 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001205 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001206 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001207 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001208 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001209 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001210 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001211 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001212 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001213 if (ret != 0)
1214 goto out;
1215 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001216
1217 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001218 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001219 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001220 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001221out:
1222 return ERR_PTR(ret);
1223out_return_state:
1224 atomic_inc(&state->count);
1225 return state;
1226}
1227
Andy Adamsone23008e2012-10-02 21:07:32 -04001228static void
1229nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1230{
1231 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1232 struct nfs_delegation *delegation;
1233 int delegation_flags = 0;
1234
1235 rcu_read_lock();
1236 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1237 if (delegation)
1238 delegation_flags = delegation->flags;
1239 rcu_read_unlock();
1240 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1241 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1242 "returning a delegation for "
1243 "OPEN(CLAIM_DELEGATE_CUR)\n",
1244 clp->cl_hostname);
1245 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1246 nfs_inode_set_delegation(state->inode,
1247 data->owner->so_cred,
1248 &data->o_res);
1249 else
1250 nfs_inode_reclaim_delegation(state->inode,
1251 data->owner->so_cred,
1252 &data->o_res);
1253}
1254
1255/*
1256 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1257 * and update the nfs4_state.
1258 */
1259static struct nfs4_state *
1260_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1261{
1262 struct inode *inode = data->state->inode;
1263 struct nfs4_state *state = data->state;
1264 int ret;
1265
1266 if (!data->rpc_done) {
1267 ret = data->rpc_status;
1268 goto err;
1269 }
1270
1271 ret = -ESTALE;
1272 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1273 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1274 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1275 goto err;
1276
1277 ret = -ENOMEM;
1278 state = nfs4_get_open_state(inode, data->owner);
1279 if (state == NULL)
1280 goto err;
1281
1282 ret = nfs_refresh_inode(inode, &data->f_attr);
1283 if (ret)
1284 goto err;
1285
1286 if (data->o_res.delegation_type != 0)
1287 nfs4_opendata_check_deleg(data, state);
1288 update_open_stateid(state, &data->o_res.stateid, NULL,
1289 data->o_arg.fmode);
1290
1291 return state;
1292err:
1293 return ERR_PTR(ret);
1294
1295}
1296
1297static struct nfs4_state *
1298_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001299{
1300 struct inode *inode;
1301 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001302 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001303
Trond Myklebustaac00a82007-07-05 19:02:21 -04001304 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001305 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001306 goto out;
1307 }
1308
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001309 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001310 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001311 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001312 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001313 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001314 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001315 goto err;
1316 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001317 state = nfs4_get_open_state(inode, data->owner);
1318 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001319 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001320 if (data->o_res.delegation_type != 0)
1321 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001322 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001323 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001324 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001325out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001326 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001327err_put_inode:
1328 iput(inode);
1329err:
1330 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001331}
1332
Andy Adamsone23008e2012-10-02 21:07:32 -04001333static struct nfs4_state *
1334nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1335{
1336 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1337 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1338 return _nfs4_opendata_to_nfs4_state(data);
1339}
1340
Trond Myklebust864472e2006-01-03 09:55:15 +01001341static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1342{
1343 struct nfs_inode *nfsi = NFS_I(state->inode);
1344 struct nfs_open_context *ctx;
1345
1346 spin_lock(&state->inode->i_lock);
1347 list_for_each_entry(ctx, &nfsi->open_files, list) {
1348 if (ctx->state != state)
1349 continue;
1350 get_nfs_open_context(ctx);
1351 spin_unlock(&state->inode->i_lock);
1352 return ctx;
1353 }
1354 spin_unlock(&state->inode->i_lock);
1355 return ERR_PTR(-ENOENT);
1356}
1357
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001358static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1359{
1360 struct nfs4_opendata *opendata;
1361
Al Viro3d4ff432011-06-22 18:40:12 -04001362 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001363 if (opendata == NULL)
1364 return ERR_PTR(-ENOMEM);
1365 opendata->state = state;
1366 atomic_inc(&state->count);
1367 return opendata;
1368}
1369
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001370static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001371{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001372 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001373 int ret;
1374
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375 opendata->o_arg.open_flags = 0;
1376 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001377 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1378 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1379 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001380 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001381 if (ret != 0)
1382 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001383 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001384 if (IS_ERR(newstate))
1385 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001386 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001387 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001388 return 0;
1389}
1390
1391static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1392{
Trond Myklebust864472e2006-01-03 09:55:15 +01001393 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001394 int ret;
1395
1396 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001397 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001398 smp_rmb();
1399 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001400 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001401 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001402 if (ret != 0)
1403 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001404 if (newstate != state)
1405 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001406 }
1407 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001408 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001409 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001410 if (ret != 0)
1411 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001412 if (newstate != state)
1413 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001414 }
1415 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001416 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001417 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001418 if (ret != 0)
1419 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001420 if (newstate != state)
1421 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001422 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001423 /*
1424 * We may have performed cached opens for all three recoveries.
1425 * Check if we need to update the current stateid.
1426 */
1427 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001428 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001429 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001430 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001431 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001432 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001433 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001434 return 0;
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
1438 * OPEN_RECLAIM:
1439 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001441static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001443 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001444 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int status;
1447
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001448 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1449 if (IS_ERR(opendata))
1450 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1452 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001453 rcu_read_lock();
1454 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001455 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001456 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001457 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001458 opendata->o_arg.u.delegation_type = delegation_type;
1459 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001460 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return status;
1462}
1463
Trond Myklebust539cd032007-06-05 11:46:42 -04001464static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct nfs_server *server = NFS_SERVER(state->inode);
1467 struct nfs4_exception exception = { };
1468 int err;
1469 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001470 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001471 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001472 break;
1473 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 } while (exception.retry);
1475 return err;
1476}
1477
Trond Myklebust864472e2006-01-03 09:55:15 +01001478static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1479{
1480 struct nfs_open_context *ctx;
1481 int ret;
1482
1483 ctx = nfs4_state_find_open_context(state);
1484 if (IS_ERR(ctx))
1485 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001486 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001487 put_nfs_open_context(ctx);
1488 return ret;
1489}
1490
Trond Myklebust13437e12007-07-06 15:10:43 -04001491static 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 -07001492{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001493 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001494 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001496 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1497 if (IS_ERR(opendata))
1498 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001499 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001500 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001501 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001502 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001503 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Trond Myklebust13437e12007-07-06 15:10:43 -04001506int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001509 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int err;
1511 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001512 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 switch (err) {
1514 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001515 case -ENOENT:
1516 case -ESTALE:
1517 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001518 case -NFS4ERR_BADSESSION:
1519 case -NFS4ERR_BADSLOT:
1520 case -NFS4ERR_BAD_HIGH_SLOT:
1521 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1522 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001523 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001524 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 case -NFS4ERR_STALE_CLIENTID:
1526 case -NFS4ERR_STALE_STATEID:
1527 case -NFS4ERR_EXPIRED:
1528 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001529 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001530 goto out;
1531 case -ERESTARTSYS:
1532 /*
1533 * The show must go on: exit, but mark the
1534 * stateid as needing recovery.
1535 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001536 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001537 case -NFS4ERR_ADMIN_REVOKED:
1538 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001539 nfs_inode_find_state_and_recover(state->inode,
1540 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001541 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001542 case -EKEYEXPIRED:
1543 /*
1544 * User RPCSEC_GSS context has expired.
1545 * We cannot recover this stateid now, so
1546 * skip it and allow recovery thread to
1547 * proceed.
1548 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001549 case -ENOMEM:
1550 err = 0;
1551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 err = nfs4_handle_exception(server, err, &exception);
1554 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001555out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return err;
1557}
1558
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001559static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1560{
1561 struct nfs4_opendata *data = calldata;
1562
1563 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001564 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001565 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001566 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001567 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001568 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001569 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001570}
1571
1572static void nfs4_open_confirm_release(void *calldata)
1573{
1574 struct nfs4_opendata *data = calldata;
1575 struct nfs4_state *state = NULL;
1576
1577 /* If this request hasn't been cancelled, do nothing */
1578 if (data->cancelled == 0)
1579 goto out_free;
1580 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001581 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001582 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001583 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001584 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001585 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001586out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001587 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001588}
1589
1590static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001591 .rpc_call_done = nfs4_open_confirm_done,
1592 .rpc_release = nfs4_open_confirm_release,
1593};
1594
1595/*
1596 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1597 */
1598static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1599{
1600 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1601 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001602 struct rpc_message msg = {
1603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1604 .rpc_argp = &data->c_arg,
1605 .rpc_resp = &data->c_res,
1606 .rpc_cred = data->owner->so_cred,
1607 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001608 struct rpc_task_setup task_setup_data = {
1609 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001610 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001611 .callback_ops = &nfs4_open_confirm_ops,
1612 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001613 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001614 .flags = RPC_TASK_ASYNC,
1615 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 int status;
1617
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001618 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001619 data->rpc_done = 0;
1620 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001621 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001622 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001623 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001624 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001625 status = nfs4_wait_for_completion_rpc_task(task);
1626 if (status != 0) {
1627 data->cancelled = 1;
1628 smp_wmb();
1629 } else
1630 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001631 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return status;
1633}
1634
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001635static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001637 struct nfs4_opendata *data = calldata;
1638 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001639
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001640 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1641 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642 /*
1643 * Check if we still need to send an OPEN call, or if we can use
1644 * a delegation instead.
1645 */
1646 if (data->state != NULL) {
1647 struct nfs_delegation *delegation;
1648
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001649 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001650 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 rcu_read_lock();
1652 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001653 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1654 can_open_delegated(delegation, data->o_arg.fmode))
1655 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001656 rcu_read_unlock();
1657 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001658 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001659 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001660 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001661 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001662 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001663 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1664 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001665 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001666 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001667 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001668 &data->o_res.seq_res,
1669 task) != 0)
1670 nfs_release_seqid(data->o_arg.seqid);
1671 else
1672 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001673 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001674unlock_no_action:
1675 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001676out_no_action:
1677 task->tk_action = NULL;
1678
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001681static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1682{
1683 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1684 nfs4_open_prepare(task, calldata);
1685}
1686
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687static void nfs4_open_done(struct rpc_task *task, void *calldata)
1688{
1689 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001691 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001692
Trond Myklebust14516c32010-07-31 14:29:06 -04001693 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1694 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001695
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001697 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1698 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001699 case S_IFREG:
1700 break;
1701 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001702 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001703 break;
1704 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001706 break;
1707 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001709 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001710 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001711 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001712 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1713 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001714 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001715 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001716}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001717
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001718static void nfs4_open_release(void *calldata)
1719{
1720 struct nfs4_opendata *data = calldata;
1721 struct nfs4_state *state = NULL;
1722
1723 /* If this request hasn't been cancelled, do nothing */
1724 if (data->cancelled == 0)
1725 goto out_free;
1726 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001727 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001728 goto out_free;
1729 /* In case we need an open_confirm, no cleanup! */
1730 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1731 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001732 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001733 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001734 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001735out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001736 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001737}
1738
1739static const struct rpc_call_ops nfs4_open_ops = {
1740 .rpc_call_prepare = nfs4_open_prepare,
1741 .rpc_call_done = nfs4_open_done,
1742 .rpc_release = nfs4_open_release,
1743};
1744
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001745static const struct rpc_call_ops nfs4_recover_open_ops = {
1746 .rpc_call_prepare = nfs4_recover_open_prepare,
1747 .rpc_call_done = nfs4_open_done,
1748 .rpc_release = nfs4_open_release,
1749};
1750
1751static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001752{
1753 struct inode *dir = data->dir->d_inode;
1754 struct nfs_server *server = NFS_SERVER(dir);
1755 struct nfs_openargs *o_arg = &data->o_arg;
1756 struct nfs_openres *o_res = &data->o_res;
1757 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001758 struct rpc_message msg = {
1759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1760 .rpc_argp = o_arg,
1761 .rpc_resp = o_res,
1762 .rpc_cred = data->owner->so_cred,
1763 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001764 struct rpc_task_setup task_setup_data = {
1765 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001766 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001767 .callback_ops = &nfs4_open_ops,
1768 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001769 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001770 .flags = RPC_TASK_ASYNC,
1771 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 int status;
1773
Trond Myklebust9d12b212012-01-17 22:04:25 -05001774 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001775 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001776 data->rpc_done = 0;
1777 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001778 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001779 if (isrecover)
1780 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001781 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001782 if (IS_ERR(task))
1783 return PTR_ERR(task);
1784 status = nfs4_wait_for_completion_rpc_task(task);
1785 if (status != 0) {
1786 data->cancelled = 1;
1787 smp_wmb();
1788 } else
1789 status = data->rpc_status;
1790 rpc_put_task(task);
1791
1792 return status;
1793}
1794
1795static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1796{
1797 struct inode *dir = data->dir->d_inode;
1798 struct nfs_openres *o_res = &data->o_res;
1799 int status;
1800
1801 status = nfs4_run_open_task(data, 1);
1802 if (status != 0 || !data->rpc_done)
1803 return status;
1804
Trond Myklebust6926afd2012-01-07 13:22:46 -05001805 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1806
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001807 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1808 status = _nfs4_proc_open_confirm(data);
1809 if (status != 0)
1810 return status;
1811 }
1812
1813 return status;
1814}
1815
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001816static int nfs4_opendata_access(struct rpc_cred *cred,
1817 struct nfs4_opendata *opendata,
1818 struct nfs4_state *state, fmode_t fmode)
1819{
1820 struct nfs_access_entry cache;
1821 u32 mask;
1822
1823 /* access call failed or for some reason the server doesn't
1824 * support any access modes -- defer access call until later */
1825 if (opendata->o_res.access_supported == 0)
1826 return 0;
1827
1828 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001829 /* don't check MAY_WRITE - a newly created file may not have
1830 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001831 if (fmode & FMODE_READ)
1832 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001833 if (fmode & FMODE_EXEC)
1834 mask |= MAY_EXEC;
1835
1836 cache.cred = cred;
1837 cache.jiffies = jiffies;
1838 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1839 nfs_access_add_cache(state->inode, &cache);
1840
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001841 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001842 return 0;
1843
1844 /* even though OPEN succeeded, access is denied. Close the file */
1845 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001846 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001847}
1848
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001849/*
1850 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1851 */
1852static int _nfs4_proc_open(struct nfs4_opendata *data)
1853{
1854 struct inode *dir = data->dir->d_inode;
1855 struct nfs_server *server = NFS_SERVER(dir);
1856 struct nfs_openargs *o_arg = &data->o_arg;
1857 struct nfs_openres *o_res = &data->o_res;
1858 int status;
1859
1860 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001861 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001862 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001863 if (status != 0) {
1864 if (status == -NFS4ERR_BADNAME &&
1865 !(o_arg->open_flags & O_CREAT))
1866 return -ENOENT;
1867 return status;
1868 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001869
Trond Myklebust6926afd2012-01-07 13:22:46 -05001870 nfs_fattr_map_and_free_names(server, &data->f_attr);
1871
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001872 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001873 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001874 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1875 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001877 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001879 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001882 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Andy Adamsond83217c2011-03-01 01:34:17 +00001886static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001887{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001888 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001889 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001890
Trond Myklebusta78cb572009-08-09 15:06:19 -04001891 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001892 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001893 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001894 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001895 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1896 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001897 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001898 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001899 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001900 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001901 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001902}
1903
Andy Adamsond83217c2011-03-01 01:34:17 +00001904static int nfs4_recover_expired_lease(struct nfs_server *server)
1905{
1906 return nfs4_client_recover_expired_lease(server->nfs_client);
1907}
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909/*
1910 * OPEN_EXPIRED:
1911 * reclaim state on the server after a network partition.
1912 * Assumes caller holds the appropriate lock
1913 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001914static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001916 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001917 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001919 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1920 if (IS_ERR(opendata))
1921 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001923 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001924 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001925 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001926 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001929static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001930{
Trond Myklebust539cd032007-06-05 11:46:42 -04001931 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001932 struct nfs4_exception exception = { };
1933 int err;
1934
1935 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001936 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001937 switch (err) {
1938 default:
1939 goto out;
1940 case -NFS4ERR_GRACE:
1941 case -NFS4ERR_DELAY:
1942 nfs4_handle_exception(server, err, &exception);
1943 err = 0;
1944 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001945 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001946out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001947 return err;
1948}
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1951{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001953 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Trond Myklebust864472e2006-01-03 09:55:15 +01001955 ctx = nfs4_state_find_open_context(state);
1956 if (IS_ERR(ctx))
1957 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001958 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001959 put_nfs_open_context(ctx);
1960 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001963#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001964static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001965{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001966 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001967 nfs4_stateid *stateid = &state->stateid;
1968 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001969
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001970 /* If a state reset has been done, test_stateid is unneeded */
1971 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1972 return;
1973
1974 status = nfs41_test_stateid(server, stateid);
1975 if (status != NFS_OK) {
1976 /* Free the stateid unless the server explicitly
1977 * informs us the stateid is unrecognized. */
1978 if (status != -NFS4ERR_BAD_STATEID)
1979 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001980 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001981
Bryan Schumaker69388672012-09-26 15:25:52 -04001982 write_seqlock(&state->seqlock);
1983 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1984 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001985 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1986 }
1987}
1988
1989/**
1990 * nfs41_check_open_stateid - possibly free an open stateid
1991 *
1992 * @state: NFSv4 state for an inode
1993 *
1994 * Returns NFS_OK if recovery for this stateid is now finished.
1995 * Otherwise a negative NFS4ERR value is returned.
1996 */
1997static int nfs41_check_open_stateid(struct nfs4_state *state)
1998{
1999 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002000 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002001 int status;
2002
2003 /* If a state reset has been done, test_stateid is unneeded */
2004 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2005 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2006 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2007 return -NFS4ERR_BAD_STATEID;
2008
2009 status = nfs41_test_stateid(server, stateid);
2010 if (status != NFS_OK) {
2011 /* Free the stateid unless the server explicitly
2012 * informs us the stateid is unrecognized. */
2013 if (status != -NFS4ERR_BAD_STATEID)
2014 nfs41_free_stateid(server, stateid);
2015
2016 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2017 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2018 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002019 }
2020 return status;
2021}
2022
2023static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2024{
Chuck Levereb64cf92012-07-11 16:30:05 -04002025 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002026
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002027 nfs41_clear_delegation_stateid(state);
2028 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002029 if (status != NFS_OK)
2030 status = nfs4_open_expired(sp, state);
2031 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002032}
2033#endif
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002036 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2037 * fields corresponding to attributes that were used to store the verifier.
2038 * Make sure we clobber those fields in the later setattr call
2039 */
2040static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2041{
2042 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2043 !(sattr->ia_valid & ATTR_ATIME_SET))
2044 sattr->ia_valid |= ATTR_ATIME;
2045
2046 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2047 !(sattr->ia_valid & ATTR_MTIME_SET))
2048 sattr->ia_valid |= ATTR_MTIME;
2049}
2050
2051/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002052 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 */
Andy Adamson82be4172012-05-23 05:02:35 -04002054static int _nfs4_do_open(struct inode *dir,
2055 struct dentry *dentry,
2056 fmode_t fmode,
2057 int flags,
2058 struct iattr *sattr,
2059 struct rpc_cred *cred,
2060 struct nfs4_state **res,
2061 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 struct nfs4_state_owner *sp;
2064 struct nfs4_state *state = NULL;
2065 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002066 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002067 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002071 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2072 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2074 goto out_err;
2075 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002076 status = nfs4_recover_expired_lease(server);
2077 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002078 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002079 if (dentry->d_inode != NULL)
2080 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002081 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04002082 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002083 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002084 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Andy Adamson82be4172012-05-23 05:02:35 -04002086 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2087 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2088 if (!opendata->f_attr.mdsthreshold)
2089 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002090 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002091 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002092 if (dentry->d_inode != NULL)
2093 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002094
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002095 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002097 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002099 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002100 status = PTR_ERR(state);
2101 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002102 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002103 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002104 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002105
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002106 status = nfs4_opendata_access(cred, opendata, state, fmode);
2107 if (status != 0)
2108 goto err_opendata_put;
2109
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002110 if (opendata->o_arg.open_flags & O_EXCL) {
2111 nfs4_exclusive_attrset(opendata, sattr);
2112
2113 nfs_fattr_init(opendata->o_res.f_attr);
2114 status = nfs4_do_setattr(state->inode, cred,
2115 opendata->o_res.f_attr, sattr,
2116 state);
2117 if (status == 0)
2118 nfs_setattr_update_inode(state->inode, sattr);
2119 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2120 }
Andy Adamson82be4172012-05-23 05:02:35 -04002121
2122 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2123 *ctx_th = opendata->f_attr.mdsthreshold;
2124 else
2125 kfree(opendata->f_attr.mdsthreshold);
2126 opendata->f_attr.mdsthreshold = NULL;
2127
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002128 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 *res = state;
2131 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002132err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002133 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002134 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002135err_put_state_owner:
2136 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 *res = NULL;
2139 return status;
2140}
2141
2142
Andy Adamson82be4172012-05-23 05:02:35 -04002143static struct nfs4_state *nfs4_do_open(struct inode *dir,
2144 struct dentry *dentry,
2145 fmode_t fmode,
2146 int flags,
2147 struct iattr *sattr,
2148 struct rpc_cred *cred,
2149 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 struct nfs4_exception exception = { };
2152 struct nfs4_state *res;
2153 int status;
2154
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002155 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002157 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2158 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (status == 0)
2160 break;
2161 /* NOTE: BAD_SEQID means the server and client disagree about the
2162 * book-keeping w.r.t. state-changing operations
2163 * (OPEN/CLOSE/LOCK/LOCKU...)
2164 * It is actually a sign of a bug on the client or on the server.
2165 *
2166 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002167 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 * have unhashed the old state_owner for us, and that we can
2169 * therefore safely retry using a new one. We should still warn
2170 * the user though...
2171 */
2172 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002173 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002174 " returned a bad sequence-id error!\n",
2175 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 exception.retry = 1;
2177 continue;
2178 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002179 /*
2180 * BAD_STATEID on OPEN means that the server cancelled our
2181 * state before it received the OPEN_CONFIRM.
2182 * Recover by retrying the request as per the discussion
2183 * on Page 181 of RFC3530.
2184 */
2185 if (status == -NFS4ERR_BAD_STATEID) {
2186 exception.retry = 1;
2187 continue;
2188 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002189 if (status == -EAGAIN) {
2190 /* We must have found a delegation */
2191 exception.retry = 1;
2192 continue;
2193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2195 status, &exception));
2196 } while (exception.retry);
2197 return res;
2198}
2199
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002200static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2201 struct nfs_fattr *fattr, struct iattr *sattr,
2202 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002204 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002206 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 .iap = sattr,
2208 .server = server,
2209 .bitmask = server->attr_bitmask,
2210 };
2211 struct nfs_setattrres res = {
2212 .fattr = fattr,
2213 .server = server,
2214 };
2215 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2217 .rpc_argp = &arg,
2218 .rpc_resp = &res,
2219 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002221 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002222 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Trond Myklebust0e574af2005-10-27 22:12:38 -04002224 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Trond Myklebust4fc87962012-03-08 17:42:01 -05002226 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002227 struct nfs_lockowner lockowner = {
2228 .l_owner = current->files,
2229 .l_pid = current->tgid,
2230 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002231 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002232 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002233 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2234 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002235 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002236 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002237 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Bryan Schumaker7c513052011-03-24 17:12:24 +00002239 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002240 if (status == 0 && state != NULL)
2241 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002242 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002245static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2246 struct nfs_fattr *fattr, struct iattr *sattr,
2247 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002249 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002250 struct nfs4_exception exception = {
2251 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002252 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002253 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 int err;
2255 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002256 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2257 switch (err) {
2258 case -NFS4ERR_OPENMODE:
2259 if (state && !(state->state & FMODE_WRITE)) {
2260 err = -EBADF;
2261 if (sattr->ia_valid & ATTR_OPEN)
2262 err = -EACCES;
2263 goto out;
2264 }
2265 }
2266 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002268out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return err;
2270}
2271
2272struct nfs4_closedata {
2273 struct inode *inode;
2274 struct nfs4_state *state;
2275 struct nfs_closeargs arg;
2276 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002277 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002278 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002279 bool roc;
2280 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281};
2282
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002283static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002284{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002285 struct nfs4_closedata *calldata = data;
2286 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002287 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002288
Fred Isamanf7e89172011-01-06 11:36:32 +00002289 if (calldata->roc)
2290 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002291 nfs4_put_open_state(calldata->state);
2292 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002293 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002294 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002295 kfree(calldata);
2296}
2297
Trond Myklebust88069f72009-12-08 08:33:16 -05002298static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2299 fmode_t fmode)
2300{
2301 spin_lock(&state->owner->so_lock);
2302 if (!(fmode & FMODE_READ))
2303 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2304 if (!(fmode & FMODE_WRITE))
2305 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2306 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2307 spin_unlock(&state->owner->so_lock);
2308}
2309
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002310static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002312 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 struct nfs_server *server = NFS_SERVER(calldata->inode);
2315
Chuck Levera3ca5652012-03-01 17:00:40 -05002316 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002317 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2318 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 /* hmm. we are done with the inode, and in the process of freeing
2320 * the state_owner. we keep this around to process errors
2321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 switch (task->tk_status) {
2323 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002324 if (calldata->roc)
2325 pnfs_roc_set_barrier(state->inode,
2326 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002327 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002328 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002329 nfs4_close_clear_stateid_flags(state,
2330 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 break;
2332 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002333 case -NFS4ERR_OLD_STATEID:
2334 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002336 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002337 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002339 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2340 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002342 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002343 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002344 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002347static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002349 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002350 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002351 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002352 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002353
Chuck Levera3ca5652012-03-01 17:00:40 -05002354 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002355 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002356 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002357
Trond Myklebust88069f72009-12-08 08:33:16 -05002358 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2359 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002360 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002361 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002362 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002363 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002364 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2365 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2366 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002367 }
2368 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002369 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2370 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2371 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002372 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002373 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002374 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002375
2376 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002377 /* Note: exit _without_ calling nfs4_close_done */
2378 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002379 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002380 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002381
Fred Isamanf7e89172011-01-06 11:36:32 +00002382 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002383 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002384 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002385 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002386 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002387 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002388
Trond Myklebust516a6af2005-10-27 22:12:41 -04002389 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002390 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002391 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002392 &calldata->arg.seq_args,
2393 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002394 task) != 0)
2395 nfs_release_seqid(calldata->arg.seqid);
2396 else
2397 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002398out:
2399 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002402static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002403 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002404 .rpc_call_done = nfs4_close_done,
2405 .rpc_release = nfs4_free_closedata,
2406};
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408/*
2409 * It is possible for data to be read/written from a mem-mapped file
2410 * after the sys_close call (which hits the vfs layer as a flush).
2411 * This means that we can't safely call nfsv4 close on a file until
2412 * the inode is cleared. This in turn means that we are not good
2413 * NFSv4 citizens - we do not indicate to the server to update the file's
2414 * share state even when we are done with one of the three share
2415 * stateid's in the inode.
2416 *
2417 * NOTE: Caller must be holding the sp->so_owner semaphore!
2418 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002419int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002421 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002423 struct nfs4_state_owner *sp = state->owner;
2424 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002425 struct rpc_message msg = {
2426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2427 .rpc_cred = state->owner->so_cred,
2428 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002429 struct rpc_task_setup task_setup_data = {
2430 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002431 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002432 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002433 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002434 .flags = RPC_TASK_ASYNC,
2435 };
Trond Myklebust95121352005-10-18 14:20:12 -07002436 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002438 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002440 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002441 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002442 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002444 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002445 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002447 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002448 if (calldata->arg.seqid == NULL)
2449 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002450 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002451 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002452 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002453 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002454 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002455 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002456 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002457
Trond Myklebust1174dd12010-12-21 10:52:24 -05002458 msg.rpc_argp = &calldata->arg;
2459 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002460 task_setup_data.callback_data = calldata;
2461 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002462 if (IS_ERR(task))
2463 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002464 status = 0;
2465 if (wait)
2466 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002467 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002468 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002469out_free_calldata:
2470 kfree(calldata);
2471out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002472 nfs4_put_open_state(state);
2473 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002474 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Trond Myklebust2b484292010-09-17 10:56:51 -04002477static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002478nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 struct nfs4_state *state;
2481
Trond Myklebust565277f2007-10-15 18:17:53 -04002482 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002483 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2484 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002485 if (IS_ERR(state))
2486 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002487 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002488 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
Trond Myklebust1185a552009-12-03 15:54:02 -05002491static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002492{
2493 if (ctx->state == NULL)
2494 return;
2495 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002496 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002497 else
Al Viro643168c2011-06-22 18:20:23 -04002498 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2502{
Benny Halevy43652ad2009-04-01 09:21:54 -04002503 struct nfs4_server_caps_arg args = {
2504 .fhandle = fhandle,
2505 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 struct nfs4_server_caps_res res = {};
2507 struct rpc_message msg = {
2508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002509 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 .rpc_resp = &res,
2511 };
2512 int status;
2513
Bryan Schumaker7c513052011-03-24 17:12:24 +00002514 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (status == 0) {
2516 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002517 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2518 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2519 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2520 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2521 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2523 server->caps |= NFS_CAP_ACLS;
2524 if (res.has_links != 0)
2525 server->caps |= NFS_CAP_HARDLINKS;
2526 if (res.has_symlinks != 0)
2527 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002528 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2529 server->caps |= NFS_CAP_FILEID;
2530 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2531 server->caps |= NFS_CAP_MODE;
2532 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2533 server->caps |= NFS_CAP_NLINK;
2534 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2535 server->caps |= NFS_CAP_OWNER;
2536 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2537 server->caps |= NFS_CAP_OWNER_GROUP;
2538 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2539 server->caps |= NFS_CAP_ATIME;
2540 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2541 server->caps |= NFS_CAP_CTIME;
2542 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2543 server->caps |= NFS_CAP_MTIME;
2544
Trond Myklebusta65318b2009-03-11 14:10:28 -04002545 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2546 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2547 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002549 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return status;
2553}
2554
Trond Myklebust55a97592006-06-09 09:34:19 -04002555int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
2557 struct nfs4_exception exception = { };
2558 int err;
2559 do {
2560 err = nfs4_handle_exception(server,
2561 _nfs4_server_capabilities(server, fhandle),
2562 &exception);
2563 } while (exception.retry);
2564 return err;
2565}
2566
2567static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2568 struct nfs_fsinfo *info)
2569{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct nfs4_lookup_root_arg args = {
2571 .bitmask = nfs4_fattr_bitmap,
2572 };
2573 struct nfs4_lookup_res res = {
2574 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002575 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 .fh = fhandle,
2577 };
2578 struct rpc_message msg = {
2579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2580 .rpc_argp = &args,
2581 .rpc_resp = &res,
2582 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002583
Trond Myklebust0e574af2005-10-27 22:12:38 -04002584 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002585 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
2588static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2589 struct nfs_fsinfo *info)
2590{
2591 struct nfs4_exception exception = { };
2592 int err;
2593 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002594 err = _nfs4_lookup_root(server, fhandle, info);
2595 switch (err) {
2596 case 0:
2597 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002598 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002599 default:
2600 err = nfs4_handle_exception(server, err, &exception);
2601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002603out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return err;
2605}
2606
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002607static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2608 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2609{
2610 struct rpc_auth *auth;
2611 int ret;
2612
2613 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002614 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002615 ret = -EIO;
2616 goto out;
2617 }
2618 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002619out:
2620 return ret;
2621}
2622
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002623static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002624 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002626 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002627 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002629 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002630 if (len < 0)
2631 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002632
2633 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002634 /* AUTH_UNIX is the default flavor if none was specified,
2635 * thus has already been tried. */
2636 if (flav_array[i] == RPC_AUTH_UNIX)
2637 continue;
2638
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002639 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002640 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002641 continue;
2642 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002643 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002644 /*
2645 * -EACCESS could mean that the user doesn't have correct permissions
2646 * to access the mount. It could also mean that we tried to mount
2647 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2648 * existing mount programs don't handle -EACCES very well so it should
2649 * be mapped to -EPERM instead.
2650 */
2651 if (status == -EACCES)
2652 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002653 return status;
2654}
2655
2656/*
2657 * get the file handle for the "/" directory on the server
2658 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002659int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2660 struct nfs_fsinfo *info)
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002661{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002662 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002663 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002664 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2665 /*
2666 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2667 * by nfs4_map_errors() as this function exits.
2668 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002669 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (status == 0)
2671 status = nfs4_server_capabilities(server, fhandle);
2672 if (status == 0)
2673 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002674 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Bryan Schumakerbae36242012-05-10 15:07:31 -04002677static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2678 struct nfs_fsinfo *info)
2679{
2680 int error;
2681 struct nfs_fattr *fattr = info->fattr;
2682
2683 error = nfs4_server_capabilities(server, mntfh);
2684 if (error < 0) {
2685 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2686 return error;
2687 }
2688
2689 error = nfs4_proc_getattr(server, mntfh, fattr);
2690 if (error < 0) {
2691 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2692 return error;
2693 }
2694
2695 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2696 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2697 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2698
2699 return error;
2700}
2701
Manoj Naik6b97fd32006-06-09 09:34:29 -04002702/*
2703 * Get locations and (maybe) other attributes of a referral.
2704 * Note that we'll actually follow the referral later when
2705 * we detect fsid mismatch in inode revalidation
2706 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002707static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2708 const struct qstr *name, struct nfs_fattr *fattr,
2709 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002710{
2711 int status = -ENOMEM;
2712 struct page *page = NULL;
2713 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002714
2715 page = alloc_page(GFP_KERNEL);
2716 if (page == NULL)
2717 goto out;
2718 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2719 if (locations == NULL)
2720 goto out;
2721
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002722 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002723 if (status != 0)
2724 goto out;
2725 /* Make sure server returned a different fsid for the referral */
2726 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002727 dprintk("%s: server did not return a different fsid for"
2728 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002729 status = -EIO;
2730 goto out;
2731 }
Andy Adamson533eb462011-06-13 18:25:56 -04002732 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2733 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002734
Andy Adamson533eb462011-06-13 18:25:56 -04002735 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002736 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002737 memset(fhandle, 0, sizeof(struct nfs_fh));
2738out:
2739 if (page)
2740 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002741 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002742 return status;
2743}
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2746{
2747 struct nfs4_getattr_arg args = {
2748 .fh = fhandle,
2749 .bitmask = server->attr_bitmask,
2750 };
2751 struct nfs4_getattr_res res = {
2752 .fattr = fattr,
2753 .server = server,
2754 };
2755 struct rpc_message msg = {
2756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2757 .rpc_argp = &args,
2758 .rpc_resp = &res,
2759 };
2760
Trond Myklebust0e574af2005-10-27 22:12:38 -04002761 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002762 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
2765static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2766{
2767 struct nfs4_exception exception = { };
2768 int err;
2769 do {
2770 err = nfs4_handle_exception(server,
2771 _nfs4_proc_getattr(server, fhandle, fattr),
2772 &exception);
2773 } while (exception.retry);
2774 return err;
2775}
2776
2777/*
2778 * The file is not closed if it is opened due to the a request to change
2779 * the size of the file. The open call will not be needed once the
2780 * VFS layer lookup-intents are implemented.
2781 *
2782 * Close is called when the inode is destroyed.
2783 * If we haven't opened the file for O_WRONLY, we
2784 * need to in the size_change case to obtain a stateid.
2785 *
2786 * Got race?
2787 * Because OPEN is always done by name in nfsv4, it is
2788 * possible that we opened a different file by the same
2789 * name. We can recognize this race condition, but we
2790 * can't do anything about it besides returning an error.
2791 *
2792 * This will be fixed with VFS changes (lookup-intent).
2793 */
2794static int
2795nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2796 struct iattr *sattr)
2797{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002798 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002799 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002800 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 int status;
2802
Benny Halevy8a1636c2010-07-14 15:43:57 -04002803 if (pnfs_ld_layoutret_on_setattr(inode))
2804 pnfs_return_layout(inode);
2805
Trond Myklebust0e574af2005-10-27 22:12:38 -04002806 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Andy Adamson26699402012-05-30 16:12:24 -04002808 /* Deal with open(O_TRUNC) */
2809 if (sattr->ia_valid & ATTR_OPEN)
2810 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2811
2812 /* Optimization: if the end result is no change, don't RPC */
2813 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2814 return 0;
2815
Trond Myklebustd5308382005-11-04 15:33:38 -05002816 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002817 if (sattr->ia_valid & ATTR_FILE) {
2818 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002819
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002820 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002821 if (ctx) {
2822 cred = ctx->cred;
2823 state = ctx->state;
2824 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002825 }
2826
2827 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002828 if (status == 0)
2829 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return status;
2831}
2832
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002833static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2834 const struct qstr *name, struct nfs_fh *fhandle,
2835 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002836{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002837 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002838 int status;
2839 struct nfs4_lookup_arg args = {
2840 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002841 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002842 .name = name,
2843 };
2844 struct nfs4_lookup_res res = {
2845 .server = server,
2846 .fattr = fattr,
2847 .fh = fhandle,
2848 };
2849 struct rpc_message msg = {
2850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2851 .rpc_argp = &args,
2852 .rpc_resp = &res,
2853 };
2854
2855 nfs_fattr_init(fattr);
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002858 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 dprintk("NFS reply lookup: %d\n", status);
2860 return status;
2861}
2862
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002863static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002864{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002865 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002866 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002867 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2868 fattr->nlink = 2;
2869}
2870
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002871static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2872 struct qstr *name, struct nfs_fh *fhandle,
2873 struct nfs_fattr *fattr)
2874{
2875 struct nfs4_exception exception = { };
2876 struct rpc_clnt *client = *clnt;
2877 int err;
2878 do {
2879 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2880 switch (err) {
2881 case -NFS4ERR_BADNAME:
2882 err = -ENOENT;
2883 goto out;
2884 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002885 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002886 goto out;
2887 case -NFS4ERR_WRONGSEC:
2888 err = -EPERM;
2889 if (client != *clnt)
2890 goto out;
2891
2892 client = nfs4_create_sec_client(client, dir, name);
2893 if (IS_ERR(client))
2894 return PTR_ERR(client);
2895
2896 exception.retry = 1;
2897 break;
2898 default:
2899 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2900 }
2901 } while (exception.retry);
2902
2903out:
2904 if (err == 0)
2905 *clnt = client;
2906 else if (client != *clnt)
2907 rpc_shutdown_client(client);
2908
2909 return err;
2910}
2911
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002912static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002913 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002915 int status;
2916 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002917
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002918 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2919 if (client != NFS_CLIENT(dir)) {
2920 rpc_shutdown_client(client);
2921 nfs_fixup_secinfo_attributes(fattr);
2922 }
2923 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002926struct rpc_clnt *
2927nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2928 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2929{
2930 int status;
2931 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2932
2933 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2934 if (status < 0) {
2935 rpc_shutdown_client(client);
2936 return ERR_PTR(status);
2937 }
2938 return client;
2939}
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2942{
Trond Myklebust76b32992007-08-10 17:45:11 -04002943 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 struct nfs4_accessargs args = {
2945 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002946 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002948 struct nfs4_accessres res = {
2949 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002950 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 struct rpc_message msg = {
2952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2953 .rpc_argp = &args,
2954 .rpc_resp = &res,
2955 .rpc_cred = entry->cred,
2956 };
2957 int mode = entry->mask;
2958 int status;
2959
2960 /*
2961 * Determine which access bits we want to ask for...
2962 */
2963 if (mode & MAY_READ)
2964 args.access |= NFS4_ACCESS_READ;
2965 if (S_ISDIR(inode->i_mode)) {
2966 if (mode & MAY_WRITE)
2967 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2968 if (mode & MAY_EXEC)
2969 args.access |= NFS4_ACCESS_LOOKUP;
2970 } else {
2971 if (mode & MAY_WRITE)
2972 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2973 if (mode & MAY_EXEC)
2974 args.access |= NFS4_ACCESS_EXECUTE;
2975 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002976
2977 res.fattr = nfs_alloc_fattr();
2978 if (res.fattr == NULL)
2979 return -ENOMEM;
2980
Bryan Schumaker7c513052011-03-24 17:12:24 +00002981 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002983 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002984 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002986 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return status;
2988}
2989
2990static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2991{
2992 struct nfs4_exception exception = { };
2993 int err;
2994 do {
2995 err = nfs4_handle_exception(NFS_SERVER(inode),
2996 _nfs4_proc_access(inode, entry),
2997 &exception);
2998 } while (exception.retry);
2999 return err;
3000}
3001
3002/*
3003 * TODO: For the time being, we don't try to get any attributes
3004 * along with any of the zero-copy operations READ, READDIR,
3005 * READLINK, WRITE.
3006 *
3007 * In the case of the first three, we want to put the GETATTR
3008 * after the read-type operation -- this is because it is hard
3009 * to predict the length of a GETATTR response in v4, and thus
3010 * align the READ data correctly. This means that the GETATTR
3011 * may end up partially falling into the page cache, and we should
3012 * shift it into the 'tail' of the xdr_buf before processing.
3013 * To do this efficiently, we need to know the total length
3014 * of data received, which doesn't seem to be available outside
3015 * of the RPC layer.
3016 *
3017 * In the case of WRITE, we also want to put the GETATTR after
3018 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003019 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 *
3021 * Both of these changes to the XDR layer would in fact be quite
3022 * minor, but I decided to leave them for a subsequent patch.
3023 */
3024static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3025 unsigned int pgbase, unsigned int pglen)
3026{
3027 struct nfs4_readlink args = {
3028 .fh = NFS_FH(inode),
3029 .pgbase = pgbase,
3030 .pglen = pglen,
3031 .pages = &page,
3032 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003033 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 struct rpc_message msg = {
3035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3036 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003037 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 };
3039
Bryan Schumaker7c513052011-03-24 17:12:24 +00003040 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 -07003041}
3042
3043static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3044 unsigned int pgbase, unsigned int pglen)
3045{
3046 struct nfs4_exception exception = { };
3047 int err;
3048 do {
3049 err = nfs4_handle_exception(NFS_SERVER(inode),
3050 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3051 &exception);
3052 } while (exception.retry);
3053 return err;
3054}
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003057 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059static int
3060nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003061 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003063 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 int status = 0;
3066
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003067 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3068 if (IS_ERR(ctx))
3069 return PTR_ERR(ctx);
3070
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003071 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003072 state = nfs4_do_open(dir, dentry, ctx->mode,
3073 flags, sattr, ctx->cred,
3074 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003075 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (IS_ERR(state)) {
3077 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003080 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003081 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003082 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003084 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return status;
3086}
3087
3088static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3089{
Trond Myklebust16e42952005-10-27 22:12:44 -04003090 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003091 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003093 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003095 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003096 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3100 .rpc_argp = &args,
3101 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003103 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003104
Bryan Schumaker7c513052011-03-24 17:12:24 +00003105 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003106 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003107 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return status;
3109}
3110
3111static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3112{
3113 struct nfs4_exception exception = { };
3114 int err;
3115 do {
3116 err = nfs4_handle_exception(NFS_SERVER(dir),
3117 _nfs4_proc_remove(dir, name),
3118 &exception);
3119 } while (exception.retry);
3120 return err;
3121}
3122
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003123static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003125 struct nfs_server *server = NFS_SERVER(dir);
3126 struct nfs_removeargs *args = msg->rpc_argp;
3127 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003129 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003131 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132}
3133
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003134static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3135{
3136 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3137 &data->args.seq_args,
3138 &data->res.seq_res,
3139 task))
3140 return;
3141 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003144static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003146 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3147
Trond Myklebust14516c32010-07-31 14:29:06 -04003148 if (!nfs4_sequence_done(task, &res->seq_res))
3149 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003150 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003151 return 0;
3152 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003153 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
Jeff Laytond3d41522010-09-17 17:31:57 -04003156static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3157{
3158 struct nfs_server *server = NFS_SERVER(dir);
3159 struct nfs_renameargs *arg = msg->rpc_argp;
3160 struct nfs_renameres *res = msg->rpc_resp;
3161
3162 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003163 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003164 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003165}
3166
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003167static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3168{
3169 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3170 &data->args.seq_args,
3171 &data->res.seq_res,
3172 task))
3173 return;
3174 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003175}
3176
3177static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3178 struct inode *new_dir)
3179{
3180 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3181
3182 if (!nfs4_sequence_done(task, &res->seq_res))
3183 return 0;
3184 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3185 return 0;
3186
3187 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003188 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003189 return 1;
3190}
3191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3193 struct inode *new_dir, struct qstr *new_name)
3194{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003195 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003196 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 .old_dir = NFS_FH(old_dir),
3198 .new_dir = NFS_FH(new_dir),
3199 .old_name = old_name,
3200 .new_name = new_name,
3201 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003202 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003203 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003204 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3207 .rpc_argp = &arg,
3208 .rpc_resp = &res,
3209 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003210 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Bryan Schumaker7c513052011-03-24 17:12:24 +00003212 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (!status) {
3214 update_changeattr(old_dir, &res.old_cinfo);
3215 update_changeattr(new_dir, &res.new_cinfo);
3216 }
3217 return status;
3218}
3219
3220static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3221 struct inode *new_dir, struct qstr *new_name)
3222{
3223 struct nfs4_exception exception = { };
3224 int err;
3225 do {
3226 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3227 _nfs4_proc_rename(old_dir, old_name,
3228 new_dir, new_name),
3229 &exception);
3230 } while (exception.retry);
3231 return err;
3232}
3233
3234static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3235{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003236 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 struct nfs4_link_arg arg = {
3238 .fh = NFS_FH(inode),
3239 .dir_fh = NFS_FH(dir),
3240 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003241 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003243 struct nfs4_link_res res = {
3244 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003245 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct rpc_message msg = {
3247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3248 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003249 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003251 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Trond Myklebust136f2622010-04-16 16:22:49 -04003253 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003254 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003255 goto out;
3256
Bryan Schumaker7c513052011-03-24 17:12:24 +00003257 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003258 if (!status) {
3259 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003260 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003261 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003262out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003263 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return status;
3265}
3266
3267static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3268{
3269 struct nfs4_exception exception = { };
3270 int err;
3271 do {
3272 err = nfs4_handle_exception(NFS_SERVER(inode),
3273 _nfs4_proc_link(inode, dir, name),
3274 &exception);
3275 } while (exception.retry);
3276 return err;
3277}
3278
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003279struct nfs4_createdata {
3280 struct rpc_message msg;
3281 struct nfs4_create_arg arg;
3282 struct nfs4_create_res res;
3283 struct nfs_fh fh;
3284 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003285};
3286
3287static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3288 struct qstr *name, struct iattr *sattr, u32 ftype)
3289{
3290 struct nfs4_createdata *data;
3291
3292 data = kzalloc(sizeof(*data), GFP_KERNEL);
3293 if (data != NULL) {
3294 struct nfs_server *server = NFS_SERVER(dir);
3295
3296 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3297 data->msg.rpc_argp = &data->arg;
3298 data->msg.rpc_resp = &data->res;
3299 data->arg.dir_fh = NFS_FH(dir);
3300 data->arg.server = server;
3301 data->arg.name = name;
3302 data->arg.attrs = sattr;
3303 data->arg.ftype = ftype;
3304 data->arg.bitmask = server->attr_bitmask;
3305 data->res.server = server;
3306 data->res.fh = &data->fh;
3307 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003309 }
3310 return data;
3311}
3312
3313static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3314{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003315 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003316 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003317 if (status == 0) {
3318 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003319 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3320 }
3321 return status;
3322}
3323
3324static void nfs4_free_createdata(struct nfs4_createdata *data)
3325{
3326 kfree(data);
3327}
3328
Chuck Lever4f390c12006-08-22 20:06:22 -04003329static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003330 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003332 struct nfs4_createdata *data;
3333 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Chuck Lever94a6d752006-08-22 20:06:23 -04003335 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003336 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003337
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003338 status = -ENOMEM;
3339 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3340 if (data == NULL)
3341 goto out;
3342
3343 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3344 data->arg.u.symlink.pages = &page;
3345 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003347 status = nfs4_do_create(dir, dentry, data);
3348
3349 nfs4_free_createdata(data);
3350out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return status;
3352}
3353
Chuck Lever4f390c12006-08-22 20:06:22 -04003354static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003355 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
3357 struct nfs4_exception exception = { };
3358 int err;
3359 do {
3360 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003361 _nfs4_proc_symlink(dir, dentry, page,
3362 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 &exception);
3364 } while (exception.retry);
3365 return err;
3366}
3367
3368static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3369 struct iattr *sattr)
3370{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003371 struct nfs4_createdata *data;
3372 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003374 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3375 if (data == NULL)
3376 goto out;
3377
3378 status = nfs4_do_create(dir, dentry, data);
3379
3380 nfs4_free_createdata(data);
3381out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return status;
3383}
3384
3385static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3386 struct iattr *sattr)
3387{
3388 struct nfs4_exception exception = { };
3389 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003390
3391 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 do {
3393 err = nfs4_handle_exception(NFS_SERVER(dir),
3394 _nfs4_proc_mkdir(dir, dentry, sattr),
3395 &exception);
3396 } while (exception.retry);
3397 return err;
3398}
3399
3400static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003401 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
3403 struct inode *dir = dentry->d_inode;
3404 struct nfs4_readdir_arg args = {
3405 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003406 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 .pgbase = 0,
3408 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003409 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003410 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 };
3412 struct nfs4_readdir_res res;
3413 struct rpc_message msg = {
3414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3415 .rpc_argp = &args,
3416 .rpc_resp = &res,
3417 .rpc_cred = cred,
3418 };
3419 int status;
3420
Harvey Harrison3110ff82008-05-02 13:42:44 -07003421 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003422 dentry->d_parent->d_name.name,
3423 dentry->d_name.name,
3424 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003425 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003427 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 -05003428 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003429 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003430 status += args.pgbase;
3431 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003432
3433 nfs_invalidate_atime(dir);
3434
Harvey Harrison3110ff82008-05-02 13:42:44 -07003435 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return status;
3437}
3438
3439static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003440 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
3442 struct nfs4_exception exception = { };
3443 int err;
3444 do {
3445 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3446 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003447 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 &exception);
3449 } while (exception.retry);
3450 return err;
3451}
3452
3453static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3454 struct iattr *sattr, dev_t rdev)
3455{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003456 struct nfs4_createdata *data;
3457 int mode = sattr->ia_mode;
3458 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003460 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3461 if (data == NULL)
3462 goto out;
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003465 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003467 data->arg.ftype = NF4BLK;
3468 data->arg.u.device.specdata1 = MAJOR(rdev);
3469 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
3471 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003472 data->arg.ftype = NF4CHR;
3473 data->arg.u.device.specdata1 = MAJOR(rdev);
3474 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003475 } else if (!S_ISSOCK(mode)) {
3476 status = -EINVAL;
3477 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003480 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003481out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003482 nfs4_free_createdata(data);
3483out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return status;
3485}
3486
3487static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3488 struct iattr *sattr, dev_t rdev)
3489{
3490 struct nfs4_exception exception = { };
3491 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003492
3493 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 do {
3495 err = nfs4_handle_exception(NFS_SERVER(dir),
3496 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3497 &exception);
3498 } while (exception.retry);
3499 return err;
3500}
3501
3502static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3503 struct nfs_fsstat *fsstat)
3504{
3505 struct nfs4_statfs_arg args = {
3506 .fh = fhandle,
3507 .bitmask = server->attr_bitmask,
3508 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003509 struct nfs4_statfs_res res = {
3510 .fsstat = fsstat,
3511 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 struct rpc_message msg = {
3513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3514 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003515 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 };
3517
Trond Myklebust0e574af2005-10-27 22:12:38 -04003518 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003519 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520}
3521
3522static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3523{
3524 struct nfs4_exception exception = { };
3525 int err;
3526 do {
3527 err = nfs4_handle_exception(server,
3528 _nfs4_proc_statfs(server, fhandle, fsstat),
3529 &exception);
3530 } while (exception.retry);
3531 return err;
3532}
3533
3534static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3535 struct nfs_fsinfo *fsinfo)
3536{
3537 struct nfs4_fsinfo_arg args = {
3538 .fh = fhandle,
3539 .bitmask = server->attr_bitmask,
3540 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003541 struct nfs4_fsinfo_res res = {
3542 .fsinfo = fsinfo,
3543 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 struct rpc_message msg = {
3545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3546 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003547 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 };
3549
Bryan Schumaker7c513052011-03-24 17:12:24 +00003550 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551}
3552
3553static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3554{
3555 struct nfs4_exception exception = { };
3556 int err;
3557
3558 do {
3559 err = nfs4_handle_exception(server,
3560 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3561 &exception);
3562 } while (exception.retry);
3563 return err;
3564}
3565
3566static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3567{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003568 int error;
3569
Trond Myklebust0e574af2005-10-27 22:12:38 -04003570 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003571 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003572 if (error == 0) {
3573 /* block layout checks this! */
3574 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003575 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003576 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003577
3578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
3581static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3582 struct nfs_pathconf *pathconf)
3583{
3584 struct nfs4_pathconf_arg args = {
3585 .fh = fhandle,
3586 .bitmask = server->attr_bitmask,
3587 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003588 struct nfs4_pathconf_res res = {
3589 .pathconf = pathconf,
3590 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 struct rpc_message msg = {
3592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3593 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003594 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 };
3596
3597 /* None of the pathconf attributes are mandatory to implement */
3598 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3599 memset(pathconf, 0, sizeof(*pathconf));
3600 return 0;
3601 }
3602
Trond Myklebust0e574af2005-10-27 22:12:38 -04003603 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003604 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
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_exception exception = { };
3611 int err;
3612
3613 do {
3614 err = nfs4_handle_exception(server,
3615 _nfs4_proc_pathconf(server, fhandle, pathconf),
3616 &exception);
3617 } while (exception.retry);
3618 return err;
3619}
3620
Benny Halevyd20581a2011-05-22 19:52:03 +03003621void __nfs4_read_done_cb(struct nfs_read_data *data)
3622{
Fred Isamancd841602012-04-20 14:47:44 -04003623 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003624}
3625
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003626static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
Fred Isamancd841602012-04-20 14:47:44 -04003628 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003630 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003631 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003632 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003634
Benny Halevyd20581a2011-05-22 19:52:03 +03003635 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003637 renew_lease(server, data->timestamp);
3638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003641static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3642{
3643
3644 dprintk("--> %s\n", __func__);
3645
3646 if (!nfs4_sequence_done(task, &data->res.seq_res))
3647 return -EAGAIN;
3648
Benny Halevyd20581a2011-05-22 19:52:03 +03003649 return data->read_done_cb ? data->read_done_cb(task, data) :
3650 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003651}
3652
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003653static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003656 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003657 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003658 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003661static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3662{
Fred Isamancd841602012-04-20 14:47:44 -04003663 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003664 &data->args.seq_args,
3665 &data->res.seq_res,
3666 task))
3667 return;
3668 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
3670
Fred Isamanb029bc92011-03-03 15:13:42 +00003671static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Fred Isamancd841602012-04-20 14:47:44 -04003673 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003675 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003676 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003677 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003679 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003681 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003682 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
3685
Fred Isamanb029bc92011-03-03 15:13:42 +00003686static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3687{
3688 if (!nfs4_sequence_done(task, &data->res.seq_res))
3689 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003690 return data->write_done_cb ? data->write_done_cb(task, data) :
3691 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003692}
3693
Trond Myklebust5a37f852012-04-28 14:55:16 -04003694static
3695bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003696{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003697 const struct nfs_pgio_header *hdr = data->header;
3698
3699 /* Don't request attributes for pNFS or O_DIRECT writes */
3700 if (data->ds_clp != NULL || hdr->dreq != NULL)
3701 return false;
3702 /* Otherwise, request attributes if and only if we don't hold
3703 * a delegation
3704 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003705 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003706}
Fred Isamana69aef12011-03-03 15:13:47 +00003707
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003708static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
Fred Isamancd841602012-04-20 14:47:44 -04003710 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003711
Trond Myklebust5a37f852012-04-28 14:55:16 -04003712 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003713 data->args.bitmask = NULL;
3714 data->res.fattr = NULL;
3715 } else
3716 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003717
Fred Isamanb029bc92011-03-03 15:13:42 +00003718 if (!data->write_done_cb)
3719 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003720 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 data->timestamp = jiffies;
3722
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003723 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003724 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003727static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3728{
Fred Isamancd841602012-04-20 14:47:44 -04003729 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003730 &data->args.seq_args,
3731 &data->res.seq_res,
3732 task))
3733 return;
3734 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
Fred Isaman0b7c0152012-04-20 14:47:39 -04003737static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3738{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3740 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003741 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return;
3744 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003745}
3746
Fred Isaman0b7c0152012-04-20 14:47:39 -04003747static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
3749 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003750
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003751 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003752 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003753 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757
Fred Isaman0b7c0152012-04-20 14:47:39 -04003758static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003759{
3760 if (!nfs4_sequence_done(task, &data->res.seq_res))
3761 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003762 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003763}
3764
Fred Isaman0b7c0152012-04-20 14:47:39 -04003765static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003767 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003768
Fred Isaman0b7c0152012-04-20 14:47:39 -04003769 if (data->commit_done_cb == NULL)
3770 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003771 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003772 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003773 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774}
3775
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003776struct nfs4_renewdata {
3777 struct nfs_client *client;
3778 unsigned long timestamp;
3779};
3780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781/*
3782 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3783 * standalone procedure for queueing an asynchronous RENEW.
3784 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003785static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003786{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003787 struct nfs4_renewdata *data = calldata;
3788 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003789
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003790 if (atomic_read(&clp->cl_count) > 1)
3791 nfs4_schedule_state_renewal(clp);
3792 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003793 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003794}
3795
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003796static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003798 struct nfs4_renewdata *data = calldata;
3799 struct nfs_client *clp = data->client;
3800 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003803 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003804 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3805 return;
3806 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003807 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003808 return;
3809 }
3810 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003812 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813}
3814
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003815static const struct rpc_call_ops nfs4_renew_ops = {
3816 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003817 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003818};
3819
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003820static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
3822 struct rpc_message msg = {
3823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3824 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003825 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003827 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003829 if (renew_flags == 0)
3830 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003831 if (!atomic_inc_not_zero(&clp->cl_count))
3832 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003833 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003834 if (data == NULL)
3835 return -ENOMEM;
3836 data->client = clp;
3837 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003839 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840}
3841
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003842static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843{
3844 struct rpc_message msg = {
3845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3846 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003847 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 };
3849 unsigned long now = jiffies;
3850 int status;
3851
3852 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3853 if (status < 0)
3854 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003855 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return 0;
3857}
3858
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003859static inline int nfs4_server_supports_acls(struct nfs_server *server)
3860{
3861 return (server->caps & NFS_CAP_ACLS)
3862 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3863 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3864}
3865
Trond Myklebust21f498c2012-08-24 10:59:25 -04003866/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3867 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003868 * the stack.
3869 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003870#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003871
Neil Hormane9e3d722011-03-04 19:26:03 -05003872static int buf_to_pages_noslab(const void *buf, size_t buflen,
3873 struct page **pages, unsigned int *pgbase)
3874{
3875 struct page *newpage, **spages;
3876 int rc = 0;
3877 size_t len;
3878 spages = pages;
3879
3880 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003881 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003882 newpage = alloc_page(GFP_KERNEL);
3883
3884 if (newpage == NULL)
3885 goto unwind;
3886 memcpy(page_address(newpage), buf, len);
3887 buf += len;
3888 buflen -= len;
3889 *pages++ = newpage;
3890 rc++;
3891 } while (buflen != 0);
3892
3893 return rc;
3894
3895unwind:
3896 for(; rc > 0; rc--)
3897 __free_page(spages[rc-1]);
3898 return -ENOMEM;
3899}
3900
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003901struct nfs4_cached_acl {
3902 int cached;
3903 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003904 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003905};
3906
3907static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003908{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003909 struct nfs_inode *nfsi = NFS_I(inode);
3910
3911 spin_lock(&inode->i_lock);
3912 kfree(nfsi->nfs4_acl);
3913 nfsi->nfs4_acl = acl;
3914 spin_unlock(&inode->i_lock);
3915}
3916
3917static void nfs4_zap_acl_attr(struct inode *inode)
3918{
3919 nfs4_set_cached_acl(inode, NULL);
3920}
3921
3922static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3923{
3924 struct nfs_inode *nfsi = NFS_I(inode);
3925 struct nfs4_cached_acl *acl;
3926 int ret = -ENOENT;
3927
3928 spin_lock(&inode->i_lock);
3929 acl = nfsi->nfs4_acl;
3930 if (acl == NULL)
3931 goto out;
3932 if (buf == NULL) /* user is just asking for length */
3933 goto out_len;
3934 if (acl->cached == 0)
3935 goto out;
3936 ret = -ERANGE; /* see getxattr(2) man page */
3937 if (acl->len > buflen)
3938 goto out;
3939 memcpy(buf, acl->data, acl->len);
3940out_len:
3941 ret = acl->len;
3942out:
3943 spin_unlock(&inode->i_lock);
3944 return ret;
3945}
3946
Sachin Prabhu5794d212012-04-17 14:36:40 +01003947static 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 +00003948{
3949 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003950 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003951
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003952 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003953 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003954 if (acl == NULL)
3955 goto out;
3956 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003957 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003958 } else {
3959 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3960 if (acl == NULL)
3961 goto out;
3962 acl->cached = 0;
3963 }
3964 acl->len = acl_len;
3965out:
3966 nfs4_set_cached_acl(inode, acl);
3967}
3968
Andy Adamsonbf118a32011-12-07 11:55:27 -05003969/*
3970 * The getxattr API returns the required buffer length when called with a
3971 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3972 * the required buf. On a NULL buf, we send a page of data to the server
3973 * guessing that the ACL request can be serviced by a page. If so, we cache
3974 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3975 * the cache. If not so, we throw away the page, and cache the required
3976 * length. The next getxattr call will then produce another round trip to
3977 * the server, this time with the input buf of the required size.
3978 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003979static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003980{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003981 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003982 struct nfs_getaclargs args = {
3983 .fh = NFS_FH(inode),
3984 .acl_pages = pages,
3985 .acl_len = buflen,
3986 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003987 struct nfs_getaclres res = {
3988 .acl_len = buflen,
3989 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003990 struct rpc_message msg = {
3991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3992 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003993 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003994 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003995 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3996 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003997
Andy Adamsonbf118a32011-12-07 11:55:27 -05003998 /* As long as we're doing a round trip to the server anyway,
3999 * let's be prepared for a page of acl data. */
4000 if (npages == 0)
4001 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004002 if (npages > ARRAY_SIZE(pages))
4003 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004004
Andy Adamsonbf118a32011-12-07 11:55:27 -05004005 for (i = 0; i < npages; i++) {
4006 pages[i] = alloc_page(GFP_KERNEL);
4007 if (!pages[i])
4008 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004009 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004010
4011 /* for decoding across pages */
4012 res.acl_scratch = alloc_page(GFP_KERNEL);
4013 if (!res.acl_scratch)
4014 goto out_free;
4015
Andy Adamsonbf118a32011-12-07 11:55:27 -05004016 args.acl_len = npages * PAGE_SIZE;
4017 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004018
Peng Taode040be2012-01-10 22:42:47 +08004019 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004020 __func__, buf, buflen, npages, args.acl_len);
4021 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4022 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004023 if (ret)
4024 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004025
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004026 /* Handle the case where the passed-in buffer is too short */
4027 if (res.acl_flags & NFS4_ACL_TRUNC) {
4028 /* Did the user only issue a request for the acl length? */
4029 if (buf == NULL)
4030 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004031 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004032 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004033 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004034 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4035 if (buf)
4036 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4037out_ok:
4038 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004039out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004040 for (i = 0; i < npages; i++)
4041 if (pages[i])
4042 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004043 if (res.acl_scratch)
4044 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004045 return ret;
4046}
4047
Trond Myklebust16b42892006-08-24 12:27:15 -04004048static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4049{
4050 struct nfs4_exception exception = { };
4051 ssize_t ret;
4052 do {
4053 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4054 if (ret >= 0)
4055 break;
4056 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4057 } while (exception.retry);
4058 return ret;
4059}
4060
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004061static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4062{
4063 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004064 int ret;
4065
4066 if (!nfs4_server_supports_acls(server))
4067 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004068 ret = nfs_revalidate_inode(server, inode);
4069 if (ret < 0)
4070 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004071 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4072 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004073 ret = nfs4_read_cached_acl(inode, buf, buflen);
4074 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004075 /* -ENOENT is returned if there is no ACL or if there is an ACL
4076 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004077 return ret;
4078 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004079}
4080
Trond Myklebust16b42892006-08-24 12:27:15 -04004081static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004082{
4083 struct nfs_server *server = NFS_SERVER(inode);
4084 struct page *pages[NFS4ACL_MAXPAGES];
4085 struct nfs_setaclargs arg = {
4086 .fh = NFS_FH(inode),
4087 .acl_pages = pages,
4088 .acl_len = buflen,
4089 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004090 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004091 struct rpc_message msg = {
4092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4093 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004094 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004095 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004096 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004097 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004098
4099 if (!nfs4_server_supports_acls(server))
4100 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004101 if (npages > ARRAY_SIZE(pages))
4102 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004103 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4104 if (i < 0)
4105 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004106 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004107 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004108
4109 /*
4110 * Free each page after tx, so the only ref left is
4111 * held by the network stack
4112 */
4113 for (; i > 0; i--)
4114 put_page(pages[i-1]);
4115
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004116 /*
4117 * Acl update can result in inode attribute update.
4118 * so mark the attribute cache invalid.
4119 */
4120 spin_lock(&inode->i_lock);
4121 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4122 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004123 nfs_access_zap_cache(inode);
4124 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004125 return ret;
4126}
4127
Trond Myklebust16b42892006-08-24 12:27:15 -04004128static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4129{
4130 struct nfs4_exception exception = { };
4131 int err;
4132 do {
4133 err = nfs4_handle_exception(NFS_SERVER(inode),
4134 __nfs4_proc_set_acl(inode, buf, buflen),
4135 &exception);
4136 } while (exception.retry);
4137 return err;
4138}
4139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004141nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004143 struct nfs_client *clp = server->nfs_client;
4144
4145 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return 0;
4147 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004148 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004149 case -NFS4ERR_ADMIN_REVOKED:
4150 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004151 if (state == NULL)
4152 break;
4153 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004154 case -NFS4ERR_OPENMODE:
4155 if (state == NULL)
4156 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004157 nfs4_schedule_stateid_recovery(server, state);
4158 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004159 case -NFS4ERR_EXPIRED:
4160 if (state != NULL)
4161 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004163 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004164 nfs4_schedule_lease_recovery(clp);
4165 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004166#if defined(CONFIG_NFS_V4_1)
4167 case -NFS4ERR_BADSESSION:
4168 case -NFS4ERR_BADSLOT:
4169 case -NFS4ERR_BAD_HIGH_SLOT:
4170 case -NFS4ERR_DEADSESSION:
4171 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4172 case -NFS4ERR_SEQ_FALSE_RETRY:
4173 case -NFS4ERR_SEQ_MISORDERED:
4174 dprintk("%s ERROR %d, Reset session\n", __func__,
4175 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004176 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004177 task->tk_status = 0;
4178 return -EAGAIN;
4179#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004181 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004182 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004183 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4185 task->tk_status = 0;
4186 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004187 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 case -NFS4ERR_OLD_STATEID:
4189 task->tk_status = 0;
4190 return -EAGAIN;
4191 }
4192 task->tk_status = nfs4_map_errors(task->tk_status);
4193 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004194wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004195 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004196 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4197 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4198 task->tk_status = 0;
4199 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200}
4201
Chuck Leverf0920752012-05-21 22:45:41 -04004202static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4203 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004204{
4205 __be32 verf[2];
4206
Chuck Lever2c820d92012-05-21 22:45:33 -04004207 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4208 /* An impossible timestamp guarantees this value
4209 * will never match a generated boot time. */
4210 verf[0] = 0;
4211 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4212 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004213 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4214 verf[0] = (__be32)nn->boot_time.tv_sec;
4215 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004216 }
Chuck Levercd937102012-03-02 17:14:31 -05004217 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4218}
4219
Chuck Levere984a552012-09-14 17:24:21 -04004220static unsigned int
4221nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4222 char *buf, size_t len)
4223{
4224 unsigned int result;
4225
4226 rcu_read_lock();
4227 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4228 clp->cl_ipaddr,
4229 rpc_peeraddr2str(clp->cl_rpcclient,
4230 RPC_DISPLAY_ADDR),
4231 rpc_peeraddr2str(clp->cl_rpcclient,
4232 RPC_DISPLAY_PROTO));
4233 rcu_read_unlock();
4234 return result;
4235}
4236
4237static unsigned int
4238nfs4_init_uniform_client_string(const struct nfs_client *clp,
4239 char *buf, size_t len)
4240{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004241 char *nodename = clp->cl_rpcclient->cl_nodename;
4242
4243 if (nfs4_client_id_uniquifier[0] != '\0')
4244 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004245 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4246 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004247 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004248}
4249
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004250/**
4251 * nfs4_proc_setclientid - Negotiate client ID
4252 * @clp: state data structure
4253 * @program: RPC program for NFSv4 callback service
4254 * @port: IP port number for NFS4 callback service
4255 * @cred: RPC credential to use for this call
4256 * @res: where to place the result
4257 *
4258 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4259 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004260int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4261 unsigned short port, struct rpc_cred *cred,
4262 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 nfs4_verifier sc_verifier;
4265 struct nfs4_setclientid setclientid = {
4266 .sc_verifier = &sc_verifier,
4267 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004268 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 };
4270 struct rpc_message msg = {
4271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4272 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004273 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004274 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004276 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Chuck Leverde734832012-07-11 16:30:50 -04004278 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004279 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004280 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4281 setclientid.sc_name_len =
4282 nfs4_init_uniform_client_string(clp,
4283 setclientid.sc_name,
4284 sizeof(setclientid.sc_name));
4285 else
4286 setclientid.sc_name_len =
4287 nfs4_init_nonuniform_client_string(clp,
4288 setclientid.sc_name,
4289 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004290 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004291 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004292 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004293 sizeof(setclientid.sc_netid),
4294 rpc_peeraddr2str(clp->cl_rpcclient,
4295 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004296 rcu_read_unlock();
4297 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004298 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 clp->cl_ipaddr, port >> 8, port & 255);
4300
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004301 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4302 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4303 setclientid.sc_name_len, setclientid.sc_name);
4304 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4305 dprintk("NFS reply setclientid: %d\n", status);
4306 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004309/**
4310 * nfs4_proc_setclientid_confirm - Confirm client ID
4311 * @clp: state data structure
4312 * @res: result of a previous SETCLIENTID
4313 * @cred: RPC credential to use for this call
4314 *
4315 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4316 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004317int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004318 struct nfs4_setclientid_res *arg,
4319 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
4321 struct nfs_fsinfo fsinfo;
4322 struct rpc_message msg = {
4323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004324 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004326 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 };
4328 unsigned long now;
4329 int status;
4330
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004331 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4332 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4333 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004335 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 if (status == 0) {
4337 spin_lock(&clp->cl_lock);
4338 clp->cl_lease_time = fsinfo.lease_time * HZ;
4339 clp->cl_last_renewal = now;
4340 spin_unlock(&clp->cl_lock);
4341 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004342 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return status;
4344}
4345
Trond Myklebustfe650402006-01-03 09:55:18 +01004346struct nfs4_delegreturndata {
4347 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004348 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004349 struct nfs_fh fh;
4350 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004351 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004352 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004353 int rpc_status;
4354};
4355
Trond Myklebustfe650402006-01-03 09:55:18 +01004356static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4357{
4358 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004359
Trond Myklebust14516c32010-07-31 14:29:06 -04004360 if (!nfs4_sequence_done(task, &data->res.seq_res))
4361 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004362
Ricardo Labiaga79708862009-12-07 09:23:21 -05004363 switch (task->tk_status) {
4364 case -NFS4ERR_STALE_STATEID:
4365 case -NFS4ERR_EXPIRED:
4366 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004367 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004368 break;
4369 default:
4370 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4371 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004372 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004373 return;
4374 }
4375 }
4376 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004377}
4378
4379static void nfs4_delegreturn_release(void *calldata)
4380{
Trond Myklebustfe650402006-01-03 09:55:18 +01004381 kfree(calldata);
4382}
4383
Andy Adamson938e1012009-04-01 09:22:28 -04004384#if defined(CONFIG_NFS_V4_1)
4385static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4386{
4387 struct nfs4_delegreturndata *d_data;
4388
4389 d_data = (struct nfs4_delegreturndata *)data;
4390
Trond Myklebust035168a2010-06-16 09:52:26 -04004391 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004392 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004393 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004394 return;
4395 rpc_call_start(task);
4396}
4397#endif /* CONFIG_NFS_V4_1 */
4398
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004399static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004400#if defined(CONFIG_NFS_V4_1)
4401 .rpc_call_prepare = nfs4_delegreturn_prepare,
4402#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004403 .rpc_call_done = nfs4_delegreturn_done,
4404 .rpc_release = nfs4_delegreturn_release,
4405};
4406
Trond Myklebuste6f81072008-01-24 18:14:34 -05004407static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004408{
4409 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004410 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004411 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004412 struct rpc_message msg = {
4413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4414 .rpc_cred = cred,
4415 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004416 struct rpc_task_setup task_setup_data = {
4417 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004418 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004419 .callback_ops = &nfs4_delegreturn_ops,
4420 .flags = RPC_TASK_ASYNC,
4421 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004422 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004423
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004424 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004425 if (data == NULL)
4426 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004427 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004428 data->args.fhandle = &data->fh;
4429 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004430 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004431 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004432 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004433 data->res.fattr = &data->fattr;
4434 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004435 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004436 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004437 data->rpc_status = 0;
4438
Trond Myklebustc970aa82007-07-14 15:39:59 -04004439 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004440 msg.rpc_argp = &data->args;
4441 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004442 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004443 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004444 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004445 if (!issync)
4446 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004447 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004448 if (status != 0)
4449 goto out;
4450 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004451 if (status == 0)
4452 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4453 else
4454 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004455out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004456 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004457 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458}
4459
Trond Myklebuste6f81072008-01-24 18:14:34 -05004460int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
4462 struct nfs_server *server = NFS_SERVER(inode);
4463 struct nfs4_exception exception = { };
4464 int err;
4465 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004466 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 switch (err) {
4468 case -NFS4ERR_STALE_STATEID:
4469 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 case 0:
4471 return 0;
4472 }
4473 err = nfs4_handle_exception(server, err, &exception);
4474 } while (exception.retry);
4475 return err;
4476}
4477
4478#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4479#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4480
4481/*
4482 * sleep, with exponential backoff, and retry the LOCK operation.
4483 */
4484static unsigned long
4485nfs4_set_lock_task_retry(unsigned long timeout)
4486{
Jeff Laytond3103102011-12-01 22:44:39 +01004487 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 timeout <<= 1;
4489 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4490 return NFS4_LOCK_MAXTIMEOUT;
4491 return timeout;
4492}
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4495{
4496 struct inode *inode = state->inode;
4497 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004498 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004499 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004501 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004503 struct nfs_lockt_res res = {
4504 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 };
4506 struct rpc_message msg = {
4507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4508 .rpc_argp = &arg,
4509 .rpc_resp = &res,
4510 .rpc_cred = state->owner->so_cred,
4511 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 struct nfs4_lock_state *lsp;
4513 int status;
4514
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004515 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004516 status = nfs4_set_lock_state(state, request);
4517 if (status != 0)
4518 goto out;
4519 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004520 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004521 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004522 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004523 switch (status) {
4524 case 0:
4525 request->fl_type = F_UNLCK;
4526 break;
4527 case -NFS4ERR_DENIED:
4528 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004530 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004531out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return status;
4533}
4534
4535static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4536{
4537 struct nfs4_exception exception = { };
4538 int err;
4539
4540 do {
4541 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4542 _nfs4_proc_getlk(state, cmd, request),
4543 &exception);
4544 } while (exception.retry);
4545 return err;
4546}
4547
4548static int do_vfs_lock(struct file *file, struct file_lock *fl)
4549{
4550 int res = 0;
4551 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4552 case FL_POSIX:
4553 res = posix_lock_file_wait(file, fl);
4554 break;
4555 case FL_FLOCK:
4556 res = flock_lock_file_wait(file, fl);
4557 break;
4558 default:
4559 BUG();
4560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 return res;
4562}
4563
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004564struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004565 struct nfs_locku_args arg;
4566 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004567 struct nfs4_lock_state *lsp;
4568 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004569 struct file_lock fl;
4570 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004571 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004572};
4573
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004574static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4575 struct nfs_open_context *ctx,
4576 struct nfs4_lock_state *lsp,
4577 struct nfs_seqid *seqid)
4578{
4579 struct nfs4_unlockdata *p;
4580 struct inode *inode = lsp->ls_state->inode;
4581
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004582 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004583 if (p == NULL)
4584 return NULL;
4585 p->arg.fh = NFS_FH(inode);
4586 p->arg.fl = &p->fl;
4587 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004588 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004589 p->arg.stateid = &lsp->ls_stateid;
4590 p->lsp = lsp;
4591 atomic_inc(&lsp->ls_count);
4592 /* Ensure we don't close file until we're done freeing locks! */
4593 p->ctx = get_nfs_open_context(ctx);
4594 memcpy(&p->fl, fl, sizeof(p->fl));
4595 p->server = NFS_SERVER(inode);
4596 return p;
4597}
4598
Trond Myklebust06f814a2006-01-03 09:55:07 +01004599static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004600{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004601 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004602 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004603 nfs4_put_lock_state(calldata->lsp);
4604 put_nfs_open_context(calldata->ctx);
4605 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004606}
4607
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004608static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004609{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004610 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004611
Trond Myklebust14516c32010-07-31 14:29:06 -04004612 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4613 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004614 switch (task->tk_status) {
4615 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004616 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4617 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004618 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004619 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004620 case -NFS4ERR_BAD_STATEID:
4621 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004622 case -NFS4ERR_STALE_STATEID:
4623 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004624 break;
4625 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004626 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004627 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004628 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004629 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004630}
4631
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004632static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004633{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004634 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004636 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004637 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004638 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004639 /* Note: exit _without_ running nfs4_locku_done */
4640 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004641 return;
4642 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004643 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004644 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004645 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004646 &calldata->res.seq_res,
4647 task) != 0)
4648 nfs_release_seqid(calldata->arg.seqid);
4649 else
4650 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651}
4652
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004653static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004654 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004655 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004656 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004657};
4658
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4660 struct nfs_open_context *ctx,
4661 struct nfs4_lock_state *lsp,
4662 struct nfs_seqid *seqid)
4663{
4664 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004665 struct rpc_message msg = {
4666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4667 .rpc_cred = ctx->cred,
4668 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004669 struct rpc_task_setup task_setup_data = {
4670 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004671 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004672 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004673 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004674 .flags = RPC_TASK_ASYNC,
4675 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004676
Frank Filz137d6ac2007-07-09 15:32:29 -07004677 /* Ensure this is an unlock - when canceling a lock, the
4678 * canceled lock is passed in, and it won't be an unlock.
4679 */
4680 fl->fl_type = F_UNLCK;
4681
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004682 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4683 if (data == NULL) {
4684 nfs_free_seqid(seqid);
4685 return ERR_PTR(-ENOMEM);
4686 }
4687
Trond Myklebust9d12b212012-01-17 22:04:25 -05004688 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004689 msg.rpc_argp = &data->arg;
4690 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004691 task_setup_data.callback_data = data;
4692 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004693}
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4696{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004697 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004698 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004699 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004700 struct rpc_task *task;
4701 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004702 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Trond Myklebust9b073572006-06-29 16:38:34 -04004704 status = nfs4_set_lock_state(state, request);
4705 /* Unlock _before_ we do the RPC call */
4706 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004707 down_read(&nfsi->rwsem);
4708 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4709 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004710 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004711 }
4712 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004713 if (status != 0)
4714 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004715 /* Is this a delegated lock? */
4716 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004717 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004718 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004719 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004720 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004721 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004722 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004723 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004724 status = PTR_ERR(task);
4725 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004726 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004727 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004728 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004729out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004730 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004731 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732}
4733
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004734struct nfs4_lockdata {
4735 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004736 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737 struct nfs4_lock_state *lsp;
4738 struct nfs_open_context *ctx;
4739 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004740 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004741 int rpc_status;
4742 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004743 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004744};
4745
4746static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004747 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4748 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004749{
4750 struct nfs4_lockdata *p;
4751 struct inode *inode = lsp->ls_state->inode;
4752 struct nfs_server *server = NFS_SERVER(inode);
4753
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004754 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004755 if (p == NULL)
4756 return NULL;
4757
4758 p->arg.fh = NFS_FH(inode);
4759 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004760 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004761 if (p->arg.open_seqid == NULL)
4762 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004763 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004765 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004766 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004767 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004768 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004769 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004770 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004771 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004772 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004773 atomic_inc(&lsp->ls_count);
4774 p->ctx = get_nfs_open_context(ctx);
4775 memcpy(&p->fl, fl, sizeof(p->fl));
4776 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004777out_free_seqid:
4778 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004779out_free:
4780 kfree(p);
4781 return NULL;
4782}
4783
4784static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4785{
4786 struct nfs4_lockdata *data = calldata;
4787 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
Harvey Harrison3110ff82008-05-02 13:42:44 -07004789 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004790 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4791 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004792 /* Do we need to do an open_to_lock_owner? */
4793 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004794 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004795 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004796 data->arg.open_stateid = &state->stateid;
4797 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004798 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004799 } else
4800 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004801 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004802 if (nfs4_setup_sequence(data->server,
4803 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004804 &data->res.seq_res,
4805 task) == 0) {
4806 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004807 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004808 }
4809 nfs_release_seqid(data->arg.open_seqid);
4810out_release_lock_seqid:
4811 nfs_release_seqid(data->arg.lock_seqid);
4812 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004813}
4814
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004815static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4816{
4817 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4818 nfs4_lock_prepare(task, calldata);
4819}
4820
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004821static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4822{
4823 struct nfs4_lockdata *data = calldata;
4824
Harvey Harrison3110ff82008-05-02 13:42:44 -07004825 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004826
Trond Myklebust14516c32010-07-31 14:29:06 -04004827 if (!nfs4_sequence_done(task, &data->res.seq_res))
4828 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004829
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004830 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004831 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004832 if (data->rpc_status == 0)
4833 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4834 else
4835 goto out;
4836 }
4837 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004838 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004839 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004840 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004841 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004842out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004843 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004844}
4845
4846static void nfs4_lock_release(void *calldata)
4847{
4848 struct nfs4_lockdata *data = calldata;
4849
Harvey Harrison3110ff82008-05-02 13:42:44 -07004850 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004851 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852 if (data->cancelled != 0) {
4853 struct rpc_task *task;
4854 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4855 data->arg.lock_seqid);
4856 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004857 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004858 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004859 } else
4860 nfs_free_seqid(data->arg.lock_seqid);
4861 nfs4_put_lock_state(data->lsp);
4862 put_nfs_open_context(data->ctx);
4863 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004864 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004865}
4866
4867static const struct rpc_call_ops nfs4_lock_ops = {
4868 .rpc_call_prepare = nfs4_lock_prepare,
4869 .rpc_call_done = nfs4_lock_done,
4870 .rpc_release = nfs4_lock_release,
4871};
4872
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004873static const struct rpc_call_ops nfs4_recover_lock_ops = {
4874 .rpc_call_prepare = nfs4_recover_lock_prepare,
4875 .rpc_call_done = nfs4_lock_done,
4876 .rpc_release = nfs4_lock_release,
4877};
4878
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004879static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4880{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004881 switch (error) {
4882 case -NFS4ERR_ADMIN_REVOKED:
4883 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004884 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004885 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004886 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004887 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004888 break;
4889 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004890 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004891 case -NFS4ERR_EXPIRED:
4892 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004893 };
4894}
4895
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004896static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004897{
4898 struct nfs4_lockdata *data;
4899 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004900 struct rpc_message msg = {
4901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4902 .rpc_cred = state->owner->so_cred,
4903 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004904 struct rpc_task_setup task_setup_data = {
4905 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004906 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004907 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004908 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004909 .flags = RPC_TASK_ASYNC,
4910 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004911 int ret;
4912
Harvey Harrison3110ff82008-05-02 13:42:44 -07004913 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004914 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004915 fl->fl_u.nfs4_fl.owner,
4916 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004917 if (data == NULL)
4918 return -ENOMEM;
4919 if (IS_SETLKW(cmd))
4920 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004921 if (recovery_type > NFS_LOCK_NEW) {
4922 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004923 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004924 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4925 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004926 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004927 msg.rpc_argp = &data->arg;
4928 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004929 task_setup_data.callback_data = data;
4930 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004931 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004932 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004933 ret = nfs4_wait_for_completion_rpc_task(task);
4934 if (ret == 0) {
4935 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004936 if (ret)
4937 nfs4_handle_setlk_error(data->server, data->lsp,
4938 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004939 } else
4940 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004941 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004942 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004943 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
4946static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4947{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004948 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004949 struct nfs4_exception exception = {
4950 .inode = state->inode,
4951 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004952 int err;
4953
4954 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004955 /* Cache the lock if possible... */
4956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4957 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004958 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004959 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004960 break;
4961 nfs4_handle_exception(server, err, &exception);
4962 } while (exception.retry);
4963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964}
4965
4966static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4967{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004968 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004969 struct nfs4_exception exception = {
4970 .inode = state->inode,
4971 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004972 int err;
4973
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004974 err = nfs4_set_lock_state(state, request);
4975 if (err != 0)
4976 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004977 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004978 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4979 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004980 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004981 switch (err) {
4982 default:
4983 goto out;
4984 case -NFS4ERR_GRACE:
4985 case -NFS4ERR_DELAY:
4986 nfs4_handle_exception(server, err, &exception);
4987 err = 0;
4988 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004989 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004990out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004991 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992}
4993
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004994#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004995/**
4996 * nfs41_check_expired_locks - possibly free a lock stateid
4997 *
4998 * @state: NFSv4 state for an inode
4999 *
5000 * Returns NFS_OK if recovery for this stateid is now finished.
5001 * Otherwise a negative NFS4ERR value is returned.
5002 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005003static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005004{
Chuck Levereb64cf92012-07-11 16:30:05 -04005005 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005006 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005007 struct nfs_server *server = NFS_SERVER(state->inode);
5008
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005009 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005010 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005011 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5012 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005013 /* Free the stateid unless the server
5014 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005015 if (status != -NFS4ERR_BAD_STATEID)
5016 nfs41_free_stateid(server,
5017 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005018 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005019 ret = status;
5020 }
5021 }
5022 };
5023
5024 return ret;
5025}
5026
5027static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5028{
5029 int status = NFS_OK;
5030
5031 if (test_bit(LK_STATE_IN_USE, &state->flags))
5032 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005033 if (status != NFS_OK)
5034 status = nfs4_lock_expired(state, request);
5035 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005036}
5037#endif
5038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5040{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005041 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005042 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005043 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005045 if ((fl_flags & FL_POSIX) &&
5046 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5047 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005048 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005049 status = nfs4_set_lock_state(state, request);
5050 if (status != 0)
5051 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005052 request->fl_flags |= FL_ACCESS;
5053 status = do_vfs_lock(request->fl_file, request);
5054 if (status < 0)
5055 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005056 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005057 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005058 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005059 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005060 request->fl_flags = fl_flags & ~FL_SLEEP;
5061 status = do_vfs_lock(request->fl_file, request);
5062 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005063 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005064 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005065 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04005066 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005067 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005068 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005069 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005070 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5071 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005072out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005073 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005074out:
5075 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 return status;
5077}
5078
5079static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5080{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005081 struct nfs4_exception exception = {
5082 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005083 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 int err;
5086
5087 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005088 err = _nfs4_proc_setlk(state, cmd, request);
5089 if (err == -NFS4ERR_DENIED)
5090 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005092 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 } while (exception.retry);
5094 return err;
5095}
5096
5097static int
5098nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5099{
5100 struct nfs_open_context *ctx;
5101 struct nfs4_state *state;
5102 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5103 int status;
5104
5105 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005106 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 state = ctx->state;
5108
5109 if (request->fl_start < 0 || request->fl_end < 0)
5110 return -EINVAL;
5111
Trond Myklebustd9531262009-07-21 19:22:38 -04005112 if (IS_GETLK(cmd)) {
5113 if (state != NULL)
5114 return nfs4_proc_getlk(state, F_GETLK, request);
5115 return 0;
5116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
5118 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5119 return -EINVAL;
5120
Trond Myklebustd9531262009-07-21 19:22:38 -04005121 if (request->fl_type == F_UNLCK) {
5122 if (state != NULL)
5123 return nfs4_proc_unlck(state, cmd, request);
5124 return 0;
5125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
Trond Myklebustd9531262009-07-21 19:22:38 -04005127 if (state == NULL)
5128 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005129 /*
5130 * Don't rely on the VFS having checked the file open mode,
5131 * since it won't do this for flock() locks.
5132 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005133 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005134 case F_RDLCK:
5135 if (!(filp->f_mode & FMODE_READ))
5136 return -EBADF;
5137 break;
5138 case F_WRLCK:
5139 if (!(filp->f_mode & FMODE_WRITE))
5140 return -EBADF;
5141 }
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 do {
5144 status = nfs4_proc_setlk(state, cmd, request);
5145 if ((status != -EAGAIN) || IS_SETLK(cmd))
5146 break;
5147 timeout = nfs4_set_lock_task_retry(timeout);
5148 status = -ERESTARTSYS;
5149 if (signalled())
5150 break;
5151 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 return status;
5153}
5154
Trond Myklebust888e6942005-11-04 15:38:11 -05005155int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5156{
5157 struct nfs_server *server = NFS_SERVER(state->inode);
5158 struct nfs4_exception exception = { };
5159 int err;
5160
5161 err = nfs4_set_lock_state(state, fl);
5162 if (err != 0)
5163 goto out;
5164 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005165 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005166 switch (err) {
5167 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005168 printk(KERN_ERR "NFS: %s: unhandled error "
5169 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005170 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005171 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005172 goto out;
5173 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005174 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005175 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005176 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005177 nfs4_schedule_lease_recovery(server->nfs_client);
5178 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005179 case -NFS4ERR_BADSESSION:
5180 case -NFS4ERR_BADSLOT:
5181 case -NFS4ERR_BAD_HIGH_SLOT:
5182 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5183 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005184 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005185 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005186 case -ERESTARTSYS:
5187 /*
5188 * The show must go on: exit, but mark the
5189 * stateid as needing recovery.
5190 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005191 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005192 case -NFS4ERR_ADMIN_REVOKED:
5193 case -NFS4ERR_BAD_STATEID:
5194 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005195 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005196 err = 0;
5197 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005198 case -EKEYEXPIRED:
5199 /*
5200 * User RPCSEC_GSS context has expired.
5201 * We cannot recover this stateid now, so
5202 * skip it and allow recovery thread to
5203 * proceed.
5204 */
5205 err = 0;
5206 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005207 case -ENOMEM:
5208 case -NFS4ERR_DENIED:
5209 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5210 err = 0;
5211 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005212 case -NFS4ERR_DELAY:
5213 break;
5214 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005215 err = nfs4_handle_exception(server, err, &exception);
5216 } while (exception.retry);
5217out:
5218 return err;
5219}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005220
Trond Myklebustcf470c32012-03-07 13:49:12 -05005221struct nfs_release_lockowner_data {
5222 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005223 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005224 struct nfs_release_lockowner_args args;
5225};
5226
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005227static void nfs4_release_lockowner_release(void *calldata)
5228{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005229 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005230 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005231 kfree(calldata);
5232}
5233
Trond Myklebust17280172012-03-11 13:11:00 -04005234static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005235 .rpc_release = nfs4_release_lockowner_release,
5236};
5237
Trond Myklebustcf470c32012-03-07 13:49:12 -05005238int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005239{
5240 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005241 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005242 struct rpc_message msg = {
5243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5244 };
5245
5246 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005247 return -EINVAL;
5248 data = kmalloc(sizeof(*data), GFP_NOFS);
5249 if (!data)
5250 return -ENOMEM;
5251 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005252 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005253 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5254 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5255 data->args.lock_owner.s_dev = server->s_dev;
5256 msg.rpc_argp = &data->args;
5257 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5258 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005259}
5260
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005261#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5262
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005263static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5264 const void *buf, size_t buflen,
5265 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005266{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005267 if (strcmp(key, "") != 0)
5268 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005269
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005270 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005271}
5272
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005273static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5274 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005275{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005276 if (strcmp(key, "") != 0)
5277 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005278
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005279 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005280}
5281
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005282static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5283 size_t list_len, const char *name,
5284 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005285{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005286 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005287
J. Bruce Fields096455a2006-03-20 23:23:42 -05005288 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5289 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005290
5291 if (list && len <= list_len)
5292 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005293 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005294}
5295
Andy Adamson533eb462011-06-13 18:25:56 -04005296/*
5297 * nfs_fhget will use either the mounted_on_fileid or the fileid
5298 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005299static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5300{
Andy Adamson533eb462011-06-13 18:25:56 -04005301 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5302 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5303 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005304 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005305 return;
5306
5307 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005308 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005309 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5310 fattr->nlink = 2;
5311}
5312
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005313static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5314 const struct qstr *name,
5315 struct nfs4_fs_locations *fs_locations,
5316 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005317{
5318 struct nfs_server *server = NFS_SERVER(dir);
5319 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005320 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005321 };
5322 struct nfs4_fs_locations_arg args = {
5323 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005324 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005325 .page = page,
5326 .bitmask = bitmask,
5327 };
Benny Halevy22958462009-04-01 09:22:02 -04005328 struct nfs4_fs_locations_res res = {
5329 .fs_locations = fs_locations,
5330 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005331 struct rpc_message msg = {
5332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5333 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005334 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005335 };
5336 int status;
5337
Harvey Harrison3110ff82008-05-02 13:42:44 -07005338 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005339
5340 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5341 * is not supported */
5342 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5343 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5344 else
5345 bitmask[0] |= FATTR4_WORD0_FILEID;
5346
Trond Myklebustc228fd32007-01-13 02:28:11 -05005347 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005348 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005349 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005350 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005351 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005352 return status;
5353}
5354
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005355int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5356 const struct qstr *name,
5357 struct nfs4_fs_locations *fs_locations,
5358 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005359{
5360 struct nfs4_exception exception = { };
5361 int err;
5362 do {
5363 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005364 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005365 &exception);
5366 } while (exception.retry);
5367 return err;
5368}
5369
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005370static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5371{
5372 int status;
5373 struct nfs4_secinfo_arg args = {
5374 .dir_fh = NFS_FH(dir),
5375 .name = name,
5376 };
5377 struct nfs4_secinfo_res res = {
5378 .flavors = flavors,
5379 };
5380 struct rpc_message msg = {
5381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5382 .rpc_argp = &args,
5383 .rpc_resp = &res,
5384 };
5385
5386 dprintk("NFS call secinfo %s\n", name->name);
5387 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5388 dprintk("NFS reply secinfo: %d\n", status);
5389 return status;
5390}
5391
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005392int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5393 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005394{
5395 struct nfs4_exception exception = { };
5396 int err;
5397 do {
5398 err = nfs4_handle_exception(NFS_SERVER(dir),
5399 _nfs4_proc_secinfo(dir, name, flavors),
5400 &exception);
5401 } while (exception.retry);
5402 return err;
5403}
5404
Andy Adamson557134a2009-04-01 09:21:53 -04005405#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005406/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005407 * Check the exchange flags returned by the server for invalid flags, having
5408 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5409 * DS flags set.
5410 */
5411static int nfs4_check_cl_exchange_flags(u32 flags)
5412{
5413 if (flags & ~EXCHGID4_FLAG_MASK_R)
5414 goto out_inval;
5415 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5416 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5417 goto out_inval;
5418 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5419 goto out_inval;
5420 return NFS_OK;
5421out_inval:
5422 return -NFS4ERR_INVAL;
5423}
5424
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005425static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005426nfs41_same_server_scope(struct nfs41_server_scope *a,
5427 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005428{
5429 if (a->server_scope_sz == b->server_scope_sz &&
5430 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5431 return true;
5432
5433 return false;
5434}
5435
Andy Adamson357f54d2010-12-14 10:11:57 -05005436/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005437 * nfs4_proc_bind_conn_to_session()
5438 *
5439 * The 4.1 client currently uses the same TCP connection for the
5440 * fore and backchannel.
5441 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005442int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005443{
5444 int status;
5445 struct nfs41_bind_conn_to_session_res res;
5446 struct rpc_message msg = {
5447 .rpc_proc =
5448 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5449 .rpc_argp = clp,
5450 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005451 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005452 };
5453
5454 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005455
5456 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5457 if (unlikely(res.session == NULL)) {
5458 status = -ENOMEM;
5459 goto out;
5460 }
5461
5462 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5463 if (status == 0) {
5464 if (memcmp(res.session->sess_id.data,
5465 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5466 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5467 status = -EIO;
5468 goto out_session;
5469 }
5470 if (res.dir != NFS4_CDFS4_BOTH) {
5471 dprintk("NFS: %s: Unexpected direction from server\n",
5472 __func__);
5473 status = -EIO;
5474 goto out_session;
5475 }
5476 if (res.use_conn_in_rdma_mode) {
5477 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5478 __func__);
5479 status = -EIO;
5480 goto out_session;
5481 }
5482 }
5483out_session:
5484 kfree(res.session);
5485out:
5486 dprintk("<-- %s status= %d\n", __func__, status);
5487 return status;
5488}
5489
5490/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005491 * nfs4_proc_exchange_id()
5492 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005493 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5494 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005495 * Since the clientid has expired, all compounds using sessions
5496 * associated with the stale clientid will be returning
5497 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5498 * be in some phase of session reset.
5499 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005500int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005501{
5502 nfs4_verifier verifier;
5503 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005504 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005505 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005506 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005507 };
5508 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005509 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005510 };
5511 int status;
5512 struct rpc_message msg = {
5513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5514 .rpc_argp = &args,
5515 .rpc_resp = &res,
5516 .rpc_cred = cred,
5517 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005518
Chuck Leverf0920752012-05-21 22:45:41 -04005519 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005520 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5521 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005522 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5523 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5524 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005525
Chuck Leveracdeb692012-05-21 22:46:16 -04005526 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005527 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005528 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005529 status = -ENOMEM;
5530 goto out;
5531 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005532
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005533 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005534 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005535 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005536 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005537 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005538 }
5539
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005540 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005541 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005542 status = -ENOMEM;
5543 goto out_server_scope;
5544 }
5545
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005546 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005547 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005548 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005549
Chuck Lever177313f2012-05-21 22:44:58 -04005550 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005551 clp->cl_clientid = res.clientid;
5552 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5553 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5554 clp->cl_seqid = res.seqid;
5555
Chuck Leveracdeb692012-05-21 22:46:16 -04005556 kfree(clp->cl_serverowner);
5557 clp->cl_serverowner = res.server_owner;
5558 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005559
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005560 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005561 kfree(clp->cl_implid);
5562 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005563
Chuck Lever177313f2012-05-21 22:44:58 -04005564 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005565 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005566 res.server_scope)) {
5567 dprintk("%s: server_scope mismatch detected\n",
5568 __func__);
5569 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005570 kfree(clp->cl_serverscope);
5571 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005572 }
5573
Chuck Lever177313f2012-05-21 22:44:58 -04005574 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005575 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005576 goto out;
5577 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005578 } else
5579 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005580
Chuck Leveracdeb692012-05-21 22:46:16 -04005581out_server_owner:
5582 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005583out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005584 kfree(res.server_scope);
5585out:
Chuck Lever177313f2012-05-21 22:44:58 -04005586 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005587 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005588 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005589 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005590 clp->cl_implid->date.seconds,
5591 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005592 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005593 return status;
5594}
5595
Trond Myklebust66245532012-05-25 17:18:09 -04005596static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5597 struct rpc_cred *cred)
5598{
5599 struct rpc_message msg = {
5600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5601 .rpc_argp = clp,
5602 .rpc_cred = cred,
5603 };
5604 int status;
5605
5606 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5607 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005608 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005609 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5610 return status;
5611}
5612
5613static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5614 struct rpc_cred *cred)
5615{
5616 unsigned int loop;
5617 int ret;
5618
5619 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5620 ret = _nfs4_proc_destroy_clientid(clp, cred);
5621 switch (ret) {
5622 case -NFS4ERR_DELAY:
5623 case -NFS4ERR_CLIENTID_BUSY:
5624 ssleep(1);
5625 break;
5626 default:
5627 return ret;
5628 }
5629 }
5630 return 0;
5631}
5632
5633int nfs4_destroy_clientid(struct nfs_client *clp)
5634{
5635 struct rpc_cred *cred;
5636 int ret = 0;
5637
5638 if (clp->cl_mvops->minor_version < 1)
5639 goto out;
5640 if (clp->cl_exchange_flags == 0)
5641 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005642 if (clp->cl_preserve_clid)
5643 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005644 cred = nfs4_get_exchange_id_cred(clp);
5645 ret = nfs4_proc_destroy_clientid(clp, cred);
5646 if (cred)
5647 put_rpccred(cred);
5648 switch (ret) {
5649 case 0:
5650 case -NFS4ERR_STALE_CLIENTID:
5651 clp->cl_exchange_flags = 0;
5652 }
5653out:
5654 return ret;
5655}
5656
Andy Adamson2050f0c2009-04-01 09:22:30 -04005657struct nfs4_get_lease_time_data {
5658 struct nfs4_get_lease_time_args *args;
5659 struct nfs4_get_lease_time_res *res;
5660 struct nfs_client *clp;
5661};
5662
5663static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5664 void *calldata)
5665{
5666 int ret;
5667 struct nfs4_get_lease_time_data *data =
5668 (struct nfs4_get_lease_time_data *)calldata;
5669
5670 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005671 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005672 /* just setup sequence, do not trigger session recovery
5673 since we're invoked within one */
5674 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005675 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005676 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005677
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005678 if (ret != -EAGAIN)
5679 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005680 dprintk("<-- %s\n", __func__);
5681}
5682
5683/*
5684 * Called from nfs4_state_manager thread for session setup, so don't recover
5685 * from sequence operation or clientid errors.
5686 */
5687static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5688{
5689 struct nfs4_get_lease_time_data *data =
5690 (struct nfs4_get_lease_time_data *)calldata;
5691
5692 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005693 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5694 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005695 switch (task->tk_status) {
5696 case -NFS4ERR_DELAY:
5697 case -NFS4ERR_GRACE:
5698 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5699 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5700 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005701 /* fall through */
5702 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005703 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005704 return;
5705 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005706 dprintk("<-- %s\n", __func__);
5707}
5708
Trond Myklebust17280172012-03-11 13:11:00 -04005709static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005710 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5711 .rpc_call_done = nfs4_get_lease_time_done,
5712};
5713
5714int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5715{
5716 struct rpc_task *task;
5717 struct nfs4_get_lease_time_args args;
5718 struct nfs4_get_lease_time_res res = {
5719 .lr_fsinfo = fsinfo,
5720 };
5721 struct nfs4_get_lease_time_data data = {
5722 .args = &args,
5723 .res = &res,
5724 .clp = clp,
5725 };
5726 struct rpc_message msg = {
5727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5728 .rpc_argp = &args,
5729 .rpc_resp = &res,
5730 };
5731 struct rpc_task_setup task_setup = {
5732 .rpc_client = clp->cl_rpcclient,
5733 .rpc_message = &msg,
5734 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005735 .callback_data = &data,
5736 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005737 };
5738 int status;
5739
Trond Myklebust9d12b212012-01-17 22:04:25 -05005740 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005741 dprintk("--> %s\n", __func__);
5742 task = rpc_run_task(&task_setup);
5743
5744 if (IS_ERR(task))
5745 status = PTR_ERR(task);
5746 else {
5747 status = task->tk_status;
5748 rpc_put_task(task);
5749 }
5750 dprintk("<-- %s return %d\n", __func__, status);
5751
5752 return status;
5753}
5754
Trond Myklebust87dda672012-11-20 19:49:20 -05005755static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
5756 u32 max_reqs, u32 ivalue)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005757{
Trond Myklebust87dda672012-11-20 19:49:20 -05005758 if (max_reqs <= tbl->max_slots)
5759 return 0;
Trond Myklebust69d206b2012-11-22 13:21:02 -05005760 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
Trond Myklebust87dda672012-11-20 19:49:20 -05005761 return 0;
5762 return -ENOMEM;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005763}
5764
Trond Myklebust87dda672012-11-20 19:49:20 -05005765static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
5766 u32 server_highest_slotid,
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005767 u32 ivalue)
5768{
Trond Myklebust87dda672012-11-20 19:49:20 -05005769 struct nfs4_slot **p;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005770
Trond Myklebust87dda672012-11-20 19:49:20 -05005771 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
5772 p = &tbl->slots;
5773 while (*p) {
5774 (*p)->seq_nr = ivalue;
5775 p = &(*p)->next;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005776 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005777 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust87dda672012-11-20 19:49:20 -05005778 tbl->target_highest_slotid = server_highest_slotid;
5779 tbl->server_highest_slotid = server_highest_slotid;
5780 tbl->max_slotid = server_highest_slotid;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005781}
5782
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005783/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005784 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005785 */
Trond Myklebust87dda672012-11-20 19:49:20 -05005786static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
5787 u32 max_reqs, u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005788{
Trond Myklebust87dda672012-11-20 19:49:20 -05005789 int ret;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005790
Andy Adamson104aeba2010-01-14 17:45:10 -05005791 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5792 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005793
Trond Myklebust87dda672012-11-20 19:49:20 -05005794 if (max_reqs > NFS4_MAX_SLOT_TABLE)
5795 max_reqs = NFS4_MAX_SLOT_TABLE;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005796
Trond Myklebust87dda672012-11-20 19:49:20 -05005797 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
5798 if (ret)
5799 goto out;
5800
5801 spin_lock(&tbl->slot_tbl_lock);
5802 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
5803 spin_unlock(&tbl->slot_tbl_lock);
5804
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005805 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5806 tbl, tbl->slots, tbl->max_slots);
5807out:
5808 dprintk("<-- %s: return %d\n", __func__, ret);
5809 return ret;
5810}
5811
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005812/* Destroy the slot table */
5813static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5814{
Trond Myklebust87dda672012-11-20 19:49:20 -05005815 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
5816 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005817}
5818
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005819/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005820 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005821 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005822static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005823{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005824 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005825 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005826
Andy Adamsonaacd5532011-11-09 13:58:21 -05005827 dprintk("--> %s\n", __func__);
5828 /* Fore channel */
5829 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005830 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005831 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5832 if (status) /* -ENOMEM */
5833 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005834 /* Back channel */
5835 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005836 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005837 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5838 if (status && tbl->slots == NULL)
5839 /* Fore and back channel share a connection so get
5840 * both slot tables or neither */
5841 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005842 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005843}
5844
5845struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5846{
5847 struct nfs4_session *session;
5848 struct nfs4_slot_table *tbl;
5849
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005850 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005851 if (!session)
5852 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005853
Andy Adamson557134a2009-04-01 09:21:53 -04005854 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005855 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005856 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005857 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005858 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005859
5860 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005861 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005862 spin_lock_init(&tbl->slot_tbl_lock);
5863 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005864 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005865
Trond Myklebust1055d762010-06-16 09:52:27 -04005866 session->session_state = 1<<NFS4_SESSION_INITING;
5867
Andy Adamson557134a2009-04-01 09:21:53 -04005868 session->clp = clp;
5869 return session;
5870}
5871
5872void nfs4_destroy_session(struct nfs4_session *session)
5873{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005874 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005875 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005876
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005877 cred = nfs4_get_exchange_id_cred(session->clp);
5878 nfs4_proc_destroy_session(session, cred);
5879 if (cred)
5880 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005881
5882 rcu_read_lock();
5883 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5884 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005885 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005886 __func__, xprt);
5887 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005888 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005889 kfree(session);
5890}
5891
Andy Adamsonfc931582009-04-01 09:22:31 -04005892/*
5893 * Initialize the values to be used by the client in CREATE_SESSION
5894 * If nfs4_init_session set the fore channel request and response sizes,
5895 * use them.
5896 *
5897 * Set the back channel max_resp_sz_cached to zero to force the client to
5898 * always set csa_cachethis to FALSE because the current implementation
5899 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5900 */
5901static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5902{
5903 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005904 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5905 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005906
5907 if (mxrqst_sz == 0)
5908 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5909 if (mxresp_sz == 0)
5910 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5911 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005912 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5913 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005914 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005915 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005916
5917 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005918 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005919 __func__,
5920 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005921 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005922
5923 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005924 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5925 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5926 args->bc_attrs.max_resp_sz_cached = 0;
5927 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5928 args->bc_attrs.max_reqs = 1;
5929
5930 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5931 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5932 __func__,
5933 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5934 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5935 args->bc_attrs.max_reqs);
5936}
5937
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005938static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005939{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005940 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5941 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5942
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005943 if (rcvd->max_resp_sz > sent->max_resp_sz)
5944 return -EINVAL;
5945 /*
5946 * Our requested max_ops is the minimum we need; we're not
5947 * prepared to break up compounds into smaller pieces than that.
5948 * So, no point even trying to continue if the server won't
5949 * cooperate:
5950 */
5951 if (rcvd->max_ops < sent->max_ops)
5952 return -EINVAL;
5953 if (rcvd->max_reqs == 0)
5954 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005955 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5956 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005957 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005958}
5959
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005960static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5961{
5962 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5963 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005964
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005965 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5966 return -EINVAL;
5967 if (rcvd->max_resp_sz < sent->max_resp_sz)
5968 return -EINVAL;
5969 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5970 return -EINVAL;
5971 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005972 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005973 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005974 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005975 return -EINVAL;
5976 return 0;
5977}
Andy Adamson8d353012009-04-01 09:22:32 -04005978
Andy Adamson8d353012009-04-01 09:22:32 -04005979static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5980 struct nfs4_session *session)
5981{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005982 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005983
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005984 ret = nfs4_verify_fore_channel_attrs(args, session);
5985 if (ret)
5986 return ret;
5987 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005988}
5989
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005990static int _nfs4_proc_create_session(struct nfs_client *clp,
5991 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005992{
5993 struct nfs4_session *session = clp->cl_session;
5994 struct nfs41_create_session_args args = {
5995 .client = clp,
5996 .cb_program = NFS4_CALLBACK,
5997 };
5998 struct nfs41_create_session_res res = {
5999 .client = clp,
6000 };
6001 struct rpc_message msg = {
6002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6003 .rpc_argp = &args,
6004 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006005 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006006 };
6007 int status;
6008
6009 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006010 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006011
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006012 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04006013
Trond Myklebust43095d32012-11-20 11:13:12 -05006014 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006015 /* Verify the session's negotiated channel_attrs values */
6016 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006017 /* Increment the clientid slot sequence id */
6018 clp->cl_seqid++;
6019 }
6020
6021 return status;
6022}
6023
6024/*
6025 * Issues a CREATE_SESSION operation to the server.
6026 * It is the responsibility of the caller to verify the session is
6027 * expired before calling this routine.
6028 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006029int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006030{
6031 int status;
6032 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006033 struct nfs4_session *session = clp->cl_session;
6034
6035 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6036
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006037 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006038 if (status)
6039 goto out;
6040
Andy Adamsonaacd5532011-11-09 13:58:21 -05006041 /* Init or reset the session slot tables */
6042 status = nfs4_setup_session_slot_tables(session);
6043 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006044 if (status)
6045 goto out;
6046
6047 ptr = (unsigned *)&session->sess_id.data[0];
6048 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6049 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006050out:
6051 dprintk("<-- %s\n", __func__);
6052 return status;
6053}
6054
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006055/*
6056 * Issue the over-the-wire RPC DESTROY_SESSION.
6057 * The caller must serialize access to this routine.
6058 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006059int nfs4_proc_destroy_session(struct nfs4_session *session,
6060 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006061{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006062 struct rpc_message msg = {
6063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6064 .rpc_argp = session,
6065 .rpc_cred = cred,
6066 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006067 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006068
6069 dprintk("--> nfs4_proc_destroy_session\n");
6070
6071 /* session is still being setup */
6072 if (session->clp->cl_cons_state != NFS_CS_READY)
6073 return status;
6074
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006075 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006076
6077 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006078 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006079 "Session has been destroyed regardless...\n", status);
6080
6081 dprintk("<-- nfs4_proc_destroy_session\n");
6082 return status;
6083}
6084
Trond Myklebust7b38c362012-05-23 13:23:31 -04006085/*
6086 * With sessions, the client is not marked ready until after a
6087 * successful EXCHANGE_ID and CREATE_SESSION.
6088 *
6089 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6090 * other versions of NFS can be tried.
6091 */
6092static int nfs41_check_session_ready(struct nfs_client *clp)
6093{
6094 int ret;
6095
6096 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6097 ret = nfs4_client_recover_expired_lease(clp);
6098 if (ret)
6099 return ret;
6100 }
6101 if (clp->cl_cons_state < NFS_CS_READY)
6102 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006103 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006104 return 0;
6105}
6106
Trond Myklebustfccba802009-07-21 16:48:07 -04006107int nfs4_init_session(struct nfs_server *server)
6108{
6109 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006110 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006111 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6112 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006113
6114 if (!nfs4_has_session(clp))
6115 return 0;
6116
Trond Myklebustae72ae672012-11-20 11:02:55 -05006117 if (server->rsize != 0)
6118 target_max_resp_sz = server->rsize;
6119 target_max_resp_sz += nfs41_maxread_overhead;
6120
6121 if (server->wsize != 0)
6122 target_max_rqst_sz = server->wsize;
6123 target_max_rqst_sz += nfs41_maxwrite_overhead;
6124
Trond Myklebust1055d762010-06-16 09:52:27 -04006125 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006126 spin_lock(&clp->cl_lock);
6127 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006128 /* Initialise targets and channel attributes */
6129 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6130 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6131 session->fc_target_max_resp_sz = target_max_resp_sz;
6132 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006133 } else {
6134 /* Just adjust the targets */
6135 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6136 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6137 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6138 }
6139 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6140 session->fc_target_max_resp_sz = target_max_resp_sz;
6141 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6142 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006143 }
6144 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006145
Trond Myklebust688a9022012-11-20 10:53:39 -05006146 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6147 nfs4_schedule_lease_recovery(clp);
6148
Trond Myklebust7b38c362012-05-23 13:23:31 -04006149 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006150}
6151
Trond Myklebust7b38c362012-05-23 13:23:31 -04006152int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006153{
6154 struct nfs4_session *session = clp->cl_session;
6155 int ret;
6156
Trond Myklebust7b38c362012-05-23 13:23:31 -04006157 spin_lock(&clp->cl_lock);
6158 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6159 /*
6160 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6161 * DS lease to be equal to the MDS lease.
6162 */
6163 clp->cl_lease_time = lease_time;
6164 clp->cl_last_renewal = jiffies;
6165 }
6166 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006167
Trond Myklebust7b38c362012-05-23 13:23:31 -04006168 ret = nfs41_check_session_ready(clp);
6169 if (ret)
6170 return ret;
6171 /* Test for the DS role */
6172 if (!is_ds_client(clp))
6173 return -ENODEV;
6174 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006175}
6176EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6177
6178
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006179/*
6180 * Renew the cl_session lease.
6181 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006182struct nfs4_sequence_data {
6183 struct nfs_client *clp;
6184 struct nfs4_sequence_args args;
6185 struct nfs4_sequence_res res;
6186};
6187
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006188static void nfs41_sequence_release(void *data)
6189{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006190 struct nfs4_sequence_data *calldata = data;
6191 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006192
Alexandros Batsakis71358402010-02-05 03:45:05 -08006193 if (atomic_read(&clp->cl_count) > 1)
6194 nfs4_schedule_state_renewal(clp);
6195 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006196 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006197}
6198
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006199static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6200{
6201 switch(task->tk_status) {
6202 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006203 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6204 return -EAGAIN;
6205 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006206 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006207 }
6208 return 0;
6209}
6210
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006211static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006212{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006213 struct nfs4_sequence_data *calldata = data;
6214 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006215
Trond Myklebust14516c32010-07-31 14:29:06 -04006216 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6217 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006218
6219 if (task->tk_status < 0) {
6220 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006221 if (atomic_read(&clp->cl_count) == 1)
6222 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006223
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006224 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6225 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006226 return;
6227 }
6228 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006229 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006230out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006231 dprintk("<-- %s\n", __func__);
6232}
6233
6234static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6235{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006236 struct nfs4_sequence_data *calldata = data;
6237 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006238 struct nfs4_sequence_args *args;
6239 struct nfs4_sequence_res *res;
6240
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006241 args = task->tk_msg.rpc_argp;
6242 res = task->tk_msg.rpc_resp;
6243
Trond Myklebust9d12b212012-01-17 22:04:25 -05006244 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006245 return;
6246 rpc_call_start(task);
6247}
6248
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006249static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6250{
6251 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6252 nfs41_sequence_prepare(task, data);
6253}
6254
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006255static const struct rpc_call_ops nfs41_sequence_ops = {
6256 .rpc_call_done = nfs41_sequence_call_done,
6257 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006258 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006259};
6260
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006261static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6262 .rpc_call_done = nfs41_sequence_call_done,
6263 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6264 .rpc_release = nfs41_sequence_release,
6265};
6266
6267static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6268 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006269{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006270 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006271 struct rpc_message msg = {
6272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6273 .rpc_cred = cred,
6274 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006275 struct rpc_task_setup task_setup_data = {
6276 .rpc_client = clp->cl_rpcclient,
6277 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006278 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006279 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6280 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006281
Alexandros Batsakis71358402010-02-05 03:45:05 -08006282 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006283 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006284 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006285 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006286 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006287 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006288 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006289 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006290 msg.rpc_argp = &calldata->args;
6291 msg.rpc_resp = &calldata->res;
6292 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006293 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006294
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006295 return rpc_run_task(&task_setup_data);
6296}
6297
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006298static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006299{
6300 struct rpc_task *task;
6301 int ret = 0;
6302
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006303 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6304 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006305 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006306 if (IS_ERR(task))
6307 ret = PTR_ERR(task);
6308 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006309 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006310 dprintk("<-- %s status=%d\n", __func__, ret);
6311 return ret;
6312}
6313
6314static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6315{
6316 struct rpc_task *task;
6317 int ret;
6318
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006319 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006320 if (IS_ERR(task)) {
6321 ret = PTR_ERR(task);
6322 goto out;
6323 }
6324 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006325 if (!ret) {
6326 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6327
6328 if (task->tk_status == 0)
6329 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006330 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006331 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006332 rpc_put_task(task);
6333out:
6334 dprintk("<-- %s status=%d\n", __func__, ret);
6335 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006336}
6337
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006338struct nfs4_reclaim_complete_data {
6339 struct nfs_client *clp;
6340 struct nfs41_reclaim_complete_args arg;
6341 struct nfs41_reclaim_complete_res res;
6342};
6343
6344static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6345{
6346 struct nfs4_reclaim_complete_data *calldata = data;
6347
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006348 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006349 if (nfs41_setup_sequence(calldata->clp->cl_session,
6350 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006351 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006352 return;
6353
6354 rpc_call_start(task);
6355}
6356
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006357static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6358{
6359 switch(task->tk_status) {
6360 case 0:
6361 case -NFS4ERR_COMPLETE_ALREADY:
6362 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6363 break;
6364 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006365 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006366 /* fall through */
6367 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006368 return -EAGAIN;
6369 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006370 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006371 }
6372 return 0;
6373}
6374
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006375static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6376{
6377 struct nfs4_reclaim_complete_data *calldata = data;
6378 struct nfs_client *clp = calldata->clp;
6379 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6380
6381 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006382 if (!nfs41_sequence_done(task, res))
6383 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006384
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006385 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6386 rpc_restart_call_prepare(task);
6387 return;
6388 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006389 dprintk("<-- %s\n", __func__);
6390}
6391
6392static void nfs4_free_reclaim_complete_data(void *data)
6393{
6394 struct nfs4_reclaim_complete_data *calldata = data;
6395
6396 kfree(calldata);
6397}
6398
6399static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6400 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6401 .rpc_call_done = nfs4_reclaim_complete_done,
6402 .rpc_release = nfs4_free_reclaim_complete_data,
6403};
6404
6405/*
6406 * Issue a global reclaim complete.
6407 */
6408static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6409{
6410 struct nfs4_reclaim_complete_data *calldata;
6411 struct rpc_task *task;
6412 struct rpc_message msg = {
6413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6414 };
6415 struct rpc_task_setup task_setup_data = {
6416 .rpc_client = clp->cl_rpcclient,
6417 .rpc_message = &msg,
6418 .callback_ops = &nfs4_reclaim_complete_call_ops,
6419 .flags = RPC_TASK_ASYNC,
6420 };
6421 int status = -ENOMEM;
6422
6423 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006424 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006425 if (calldata == NULL)
6426 goto out;
6427 calldata->clp = clp;
6428 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006429
Trond Myklebust9d12b212012-01-17 22:04:25 -05006430 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006431 msg.rpc_argp = &calldata->arg;
6432 msg.rpc_resp = &calldata->res;
6433 task_setup_data.callback_data = calldata;
6434 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006435 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006436 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006437 goto out;
6438 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006439 status = nfs4_wait_for_completion_rpc_task(task);
6440 if (status == 0)
6441 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006442 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006443 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006444out:
6445 dprintk("<-- %s status=%d\n", __func__, status);
6446 return status;
6447}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006448
6449static void
6450nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6451{
6452 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006453 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006454
6455 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006456 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6457 * right now covering the LAYOUTGET we are about to send.
6458 * However, that is not so catastrophic, and there seems
6459 * to be no way to prevent it completely.
6460 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006461 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006462 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006463 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006464 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6465 NFS_I(lgp->args.inode)->layout,
6466 lgp->args.ctx->state)) {
6467 rpc_exit(task, NFS4_OK);
6468 return;
6469 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006470 rpc_call_start(task);
6471}
6472
6473static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6474{
6475 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006476 struct inode *inode = lgp->args.inode;
6477 struct nfs_server *server = NFS_SERVER(inode);
6478 struct pnfs_layout_hdr *lo;
6479 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006480
6481 dprintk("--> %s\n", __func__);
6482
6483 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006484 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006485
6486 switch (task->tk_status) {
6487 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006488 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006489 case -NFS4ERR_LAYOUTTRYLATER:
6490 case -NFS4ERR_RECALLCONFLICT:
6491 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006492 break;
6493 case -NFS4ERR_EXPIRED:
6494 case -NFS4ERR_BAD_STATEID:
6495 spin_lock(&inode->i_lock);
6496 lo = NFS_I(inode)->layout;
6497 if (!lo || list_empty(&lo->plh_segs)) {
6498 spin_unlock(&inode->i_lock);
6499 /* If the open stateid was bad, then recover it. */
6500 state = lgp->args.ctx->state;
6501 } else {
6502 LIST_HEAD(head);
6503
6504 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6505 spin_unlock(&inode->i_lock);
6506 /* Mark the bad layout state as invalid, then
6507 * retry using the open stateid. */
6508 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006509 }
6510 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006511 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6512 rpc_restart_call_prepare(task);
6513out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006514 dprintk("<-- %s\n", __func__);
6515}
6516
Idan Kedar85541162012-08-02 11:47:10 +03006517static size_t max_response_pages(struct nfs_server *server)
6518{
6519 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6520 return nfs_page_array_len(0, max_resp_sz);
6521}
6522
6523static void nfs4_free_pages(struct page **pages, size_t size)
6524{
6525 int i;
6526
6527 if (!pages)
6528 return;
6529
6530 for (i = 0; i < size; i++) {
6531 if (!pages[i])
6532 break;
6533 __free_page(pages[i]);
6534 }
6535 kfree(pages);
6536}
6537
6538static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6539{
6540 struct page **pages;
6541 int i;
6542
6543 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6544 if (!pages) {
6545 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6546 return NULL;
6547 }
6548
6549 for (i = 0; i < size; i++) {
6550 pages[i] = alloc_page(gfp_flags);
6551 if (!pages[i]) {
6552 dprintk("%s: failed to allocate page\n", __func__);
6553 nfs4_free_pages(pages, size);
6554 return NULL;
6555 }
6556 }
6557
6558 return pages;
6559}
6560
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006561static void nfs4_layoutget_release(void *calldata)
6562{
6563 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006564 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6565 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006566
6567 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006568 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006569 put_nfs_open_context(lgp->args.ctx);
6570 kfree(calldata);
6571 dprintk("<-- %s\n", __func__);
6572}
6573
6574static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6575 .rpc_call_prepare = nfs4_layoutget_prepare,
6576 .rpc_call_done = nfs4_layoutget_done,
6577 .rpc_release = nfs4_layoutget_release,
6578};
6579
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006580struct pnfs_layout_segment *
6581nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006582{
6583 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006584 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006585 struct rpc_task *task;
6586 struct rpc_message msg = {
6587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6588 .rpc_argp = &lgp->args,
6589 .rpc_resp = &lgp->res,
6590 };
6591 struct rpc_task_setup task_setup_data = {
6592 .rpc_client = server->client,
6593 .rpc_message = &msg,
6594 .callback_ops = &nfs4_layoutget_call_ops,
6595 .callback_data = lgp,
6596 .flags = RPC_TASK_ASYNC,
6597 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006598 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006599 int status = 0;
6600
6601 dprintk("--> %s\n", __func__);
6602
Idan Kedar85541162012-08-02 11:47:10 +03006603 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6604 if (!lgp->args.layout.pages) {
6605 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006606 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006607 }
6608 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6609
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006610 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006611 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006612 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006613 task = rpc_run_task(&task_setup_data);
6614 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006615 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006616 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006617 if (status == 0)
6618 status = task->tk_status;
6619 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006620 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006621 rpc_put_task(task);
6622 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006623 if (status)
6624 return ERR_PTR(status);
6625 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006626}
6627
Benny Halevycbe82602011-05-22 19:52:37 +03006628static void
6629nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6630{
6631 struct nfs4_layoutreturn *lrp = calldata;
6632
6633 dprintk("--> %s\n", __func__);
6634 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006635 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006636 return;
6637 rpc_call_start(task);
6638}
6639
6640static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6641{
6642 struct nfs4_layoutreturn *lrp = calldata;
6643 struct nfs_server *server;
6644
6645 dprintk("--> %s\n", __func__);
6646
6647 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6648 return;
6649
6650 server = NFS_SERVER(lrp->args.inode);
6651 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006652 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006653 return;
6654 }
Benny Halevycbe82602011-05-22 19:52:37 +03006655 dprintk("<-- %s\n", __func__);
6656}
6657
6658static void nfs4_layoutreturn_release(void *calldata)
6659{
6660 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006661 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006662
6663 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006664 spin_lock(&lo->plh_inode->i_lock);
6665 if (lrp->res.lrs_present)
6666 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6667 lo->plh_block_lgets--;
6668 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006669 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006670 kfree(calldata);
6671 dprintk("<-- %s\n", __func__);
6672}
6673
6674static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6675 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6676 .rpc_call_done = nfs4_layoutreturn_done,
6677 .rpc_release = nfs4_layoutreturn_release,
6678};
6679
6680int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6681{
6682 struct rpc_task *task;
6683 struct rpc_message msg = {
6684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6685 .rpc_argp = &lrp->args,
6686 .rpc_resp = &lrp->res,
6687 };
6688 struct rpc_task_setup task_setup_data = {
6689 .rpc_client = lrp->clp->cl_rpcclient,
6690 .rpc_message = &msg,
6691 .callback_ops = &nfs4_layoutreturn_call_ops,
6692 .callback_data = lrp,
6693 };
6694 int status;
6695
6696 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006697 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006698 task = rpc_run_task(&task_setup_data);
6699 if (IS_ERR(task))
6700 return PTR_ERR(task);
6701 status = task->tk_status;
6702 dprintk("<-- %s status=%d\n", __func__, status);
6703 rpc_put_task(task);
6704 return status;
6705}
6706
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006707/*
6708 * Retrieve the list of Data Server devices from the MDS.
6709 */
6710static int _nfs4_getdevicelist(struct nfs_server *server,
6711 const struct nfs_fh *fh,
6712 struct pnfs_devicelist *devlist)
6713{
6714 struct nfs4_getdevicelist_args args = {
6715 .fh = fh,
6716 .layoutclass = server->pnfs_curr_ld->id,
6717 };
6718 struct nfs4_getdevicelist_res res = {
6719 .devlist = devlist,
6720 };
6721 struct rpc_message msg = {
6722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6723 .rpc_argp = &args,
6724 .rpc_resp = &res,
6725 };
6726 int status;
6727
6728 dprintk("--> %s\n", __func__);
6729 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6730 &res.seq_res, 0);
6731 dprintk("<-- %s status=%d\n", __func__, status);
6732 return status;
6733}
6734
6735int nfs4_proc_getdevicelist(struct nfs_server *server,
6736 const struct nfs_fh *fh,
6737 struct pnfs_devicelist *devlist)
6738{
6739 struct nfs4_exception exception = { };
6740 int err;
6741
6742 do {
6743 err = nfs4_handle_exception(server,
6744 _nfs4_getdevicelist(server, fh, devlist),
6745 &exception);
6746 } while (exception.retry);
6747
6748 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6749 err, devlist->num_devs);
6750
6751 return err;
6752}
6753EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6754
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006755static int
6756_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6757{
6758 struct nfs4_getdeviceinfo_args args = {
6759 .pdev = pdev,
6760 };
6761 struct nfs4_getdeviceinfo_res res = {
6762 .pdev = pdev,
6763 };
6764 struct rpc_message msg = {
6765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6766 .rpc_argp = &args,
6767 .rpc_resp = &res,
6768 };
6769 int status;
6770
6771 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006772 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006773 dprintk("<-- %s status=%d\n", __func__, status);
6774
6775 return status;
6776}
6777
6778int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6779{
6780 struct nfs4_exception exception = { };
6781 int err;
6782
6783 do {
6784 err = nfs4_handle_exception(server,
6785 _nfs4_proc_getdeviceinfo(server, pdev),
6786 &exception);
6787 } while (exception.retry);
6788 return err;
6789}
6790EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6791
Andy Adamson863a3c62011-03-23 13:27:54 +00006792static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6793{
6794 struct nfs4_layoutcommit_data *data = calldata;
6795 struct nfs_server *server = NFS_SERVER(data->args.inode);
6796
6797 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006798 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006799 return;
6800 rpc_call_start(task);
6801}
6802
6803static void
6804nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6805{
6806 struct nfs4_layoutcommit_data *data = calldata;
6807 struct nfs_server *server = NFS_SERVER(data->args.inode);
6808
6809 if (!nfs4_sequence_done(task, &data->res.seq_res))
6810 return;
6811
6812 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006813 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6814 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6815 case -NFS4ERR_BADLAYOUT: /* no layout */
6816 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006817 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006818 break;
6819 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006820 nfs_post_op_update_inode_force_wcc(data->args.inode,
6821 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006822 break;
6823 default:
6824 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6825 rpc_restart_call_prepare(task);
6826 return;
6827 }
6828 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006829}
6830
6831static void nfs4_layoutcommit_release(void *calldata)
6832{
6833 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006834 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006835 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006836
Andy Adamsondb29c082011-07-30 20:52:38 -04006837 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006838 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006839 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6840 list_del_init(&lseg->pls_lc_list);
6841 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6842 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006843 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006844 }
Peng Tao92407e72011-10-23 20:21:17 -07006845
6846 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6847 smp_mb__after_clear_bit();
6848 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6849
Andy Adamson863a3c62011-03-23 13:27:54 +00006850 put_rpccred(data->cred);
6851 kfree(data);
6852}
6853
6854static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6855 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6856 .rpc_call_done = nfs4_layoutcommit_done,
6857 .rpc_release = nfs4_layoutcommit_release,
6858};
6859
6860int
Andy Adamsonef311532011-03-12 02:58:10 -05006861nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006862{
6863 struct rpc_message msg = {
6864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6865 .rpc_argp = &data->args,
6866 .rpc_resp = &data->res,
6867 .rpc_cred = data->cred,
6868 };
6869 struct rpc_task_setup task_setup_data = {
6870 .task = &data->task,
6871 .rpc_client = NFS_CLIENT(data->args.inode),
6872 .rpc_message = &msg,
6873 .callback_ops = &nfs4_layoutcommit_ops,
6874 .callback_data = data,
6875 .flags = RPC_TASK_ASYNC,
6876 };
6877 struct rpc_task *task;
6878 int status = 0;
6879
6880 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6881 "lbw: %llu inode %lu\n",
6882 data->task.tk_pid, sync,
6883 data->args.lastbytewritten,
6884 data->args.inode->i_ino);
6885
Trond Myklebust9d12b212012-01-17 22:04:25 -05006886 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006887 task = rpc_run_task(&task_setup_data);
6888 if (IS_ERR(task))
6889 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006890 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006891 goto out;
6892 status = nfs4_wait_for_completion_rpc_task(task);
6893 if (status != 0)
6894 goto out;
6895 status = task->tk_status;
6896out:
6897 dprintk("%s: status %d\n", __func__, status);
6898 rpc_put_task(task);
6899 return status;
6900}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006901
6902static int
6903_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6904 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6905{
6906 struct nfs41_secinfo_no_name_args args = {
6907 .style = SECINFO_STYLE_CURRENT_FH,
6908 };
6909 struct nfs4_secinfo_res res = {
6910 .flavors = flavors,
6911 };
6912 struct rpc_message msg = {
6913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6914 .rpc_argp = &args,
6915 .rpc_resp = &res,
6916 };
6917 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6918}
6919
6920static int
6921nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6922 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6923{
6924 struct nfs4_exception exception = { };
6925 int err;
6926 do {
6927 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6928 switch (err) {
6929 case 0:
6930 case -NFS4ERR_WRONGSEC:
6931 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006932 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006933 default:
6934 err = nfs4_handle_exception(server, err, &exception);
6935 }
6936 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006937out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006938 return err;
6939}
6940
6941static int
6942nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6943 struct nfs_fsinfo *info)
6944{
6945 int err;
6946 struct page *page;
6947 rpc_authflavor_t flavor;
6948 struct nfs4_secinfo_flavors *flavors;
6949
6950 page = alloc_page(GFP_KERNEL);
6951 if (!page) {
6952 err = -ENOMEM;
6953 goto out;
6954 }
6955
6956 flavors = page_address(page);
6957 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6958
6959 /*
6960 * Fall back on "guess and check" method if
6961 * the server doesn't support SECINFO_NO_NAME
6962 */
6963 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6964 err = nfs4_find_root_sec(server, fhandle, info);
6965 goto out_freepage;
6966 }
6967 if (err)
6968 goto out_freepage;
6969
6970 flavor = nfs_find_best_sec(flavors);
6971 if (err == 0)
6972 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6973
6974out_freepage:
6975 put_page(page);
6976 if (err == -EACCES)
6977 return -EPERM;
6978out:
6979 return err;
6980}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006981
6982static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006983{
6984 int status;
6985 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006986 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006987 };
6988 struct nfs41_test_stateid_res res;
6989 struct rpc_message msg = {
6990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6991 .rpc_argp = &args,
6992 .rpc_resp = &res,
6993 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006994
Chuck Lever38527b12012-07-11 16:30:23 -04006995 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006996 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006997 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006998 if (status != NFS_OK) {
6999 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007000 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007001 }
7002 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007003 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007004}
7005
Chuck Lever38527b12012-07-11 16:30:23 -04007006/**
7007 * nfs41_test_stateid - perform a TEST_STATEID operation
7008 *
7009 * @server: server / transport on which to perform the operation
7010 * @stateid: state ID to test
7011 *
7012 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7013 * Otherwise a negative NFS4ERR value is returned if the operation
7014 * failed or the state ID is not currently valid.
7015 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007016static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007017{
7018 struct nfs4_exception exception = { };
7019 int err;
7020 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007021 err = _nfs41_test_stateid(server, stateid);
7022 if (err != -NFS4ERR_DELAY)
7023 break;
7024 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007025 } while (exception.retry);
7026 return err;
7027}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007028
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007029static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007030{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007031 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007032 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007033 };
7034 struct nfs41_free_stateid_res res;
7035 struct rpc_message msg = {
7036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7037 .rpc_argp = &args,
7038 .rpc_resp = &res,
7039 };
Chuck Lever38527b12012-07-11 16:30:23 -04007040 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007041
Chuck Lever38527b12012-07-11 16:30:23 -04007042 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007043 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04007044 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04007045 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04007046 dprintk("NFS reply free_stateid: %d\n", status);
7047 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007048}
7049
Chuck Lever38527b12012-07-11 16:30:23 -04007050/**
7051 * nfs41_free_stateid - perform a FREE_STATEID operation
7052 *
7053 * @server: server / transport on which to perform the operation
7054 * @stateid: state ID to release
7055 *
7056 * Returns NFS_OK if the server freed "stateid". Otherwise a
7057 * negative NFS4ERR value is returned.
7058 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007059static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007060{
7061 struct nfs4_exception exception = { };
7062 int err;
7063 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007064 err = _nfs4_free_stateid(server, stateid);
7065 if (err != -NFS4ERR_DELAY)
7066 break;
7067 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007068 } while (exception.retry);
7069 return err;
7070}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007071
7072static bool nfs41_match_stateid(const nfs4_stateid *s1,
7073 const nfs4_stateid *s2)
7074{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007075 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007076 return false;
7077
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007078 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007079 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007080 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007081 return true;
7082
7083 return false;
7084}
7085
Andy Adamson557134a2009-04-01 09:21:53 -04007086#endif /* CONFIG_NFS_V4_1 */
7087
Trond Myklebust36281ca2012-03-04 18:13:56 -05007088static bool nfs4_match_stateid(const nfs4_stateid *s1,
7089 const nfs4_stateid *s2)
7090{
Trond Myklebustf597c532012-03-04 18:13:56 -05007091 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007092}
7093
7094
Trond Myklebust17280172012-03-11 13:11:00 -04007095static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007096 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007097 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 .recover_open = nfs4_open_reclaim,
7099 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007100 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007101 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007102 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103};
7104
Andy Adamson591d71c2009-04-01 09:22:47 -04007105#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007106static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007107 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7108 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7109 .recover_open = nfs4_open_reclaim,
7110 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007111 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007112 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007113 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007114 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007115};
7116#endif /* CONFIG_NFS_V4_1 */
7117
Trond Myklebust17280172012-03-11 13:11:00 -04007118static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007119 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007120 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 .recover_open = nfs4_open_expired,
7122 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007123 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007124 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125};
7126
Andy Adamson591d71c2009-04-01 09:22:47 -04007127#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007128static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007129 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7130 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007131 .recover_open = nfs41_open_expired,
7132 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007133 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007134 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007135};
7136#endif /* CONFIG_NFS_V4_1 */
7137
Trond Myklebust17280172012-03-11 13:11:00 -04007138static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007139 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007140 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007141 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007142};
7143
7144#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007145static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007146 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007147 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007148 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007149};
7150#endif
7151
Trond Myklebust97dc1352010-06-16 09:52:26 -04007152static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7153 .minor_version = 0,
7154 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007155 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007156 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007157 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7158 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7159 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007160};
7161
7162#if defined(CONFIG_NFS_V4_1)
7163static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7164 .minor_version = 1,
7165 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007166 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007167 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007168 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7169 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7170 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007171};
7172#endif
7173
Trond Myklebust97dc1352010-06-16 09:52:26 -04007174const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7175 [0] = &nfs_v4_0_minor_ops,
7176#if defined(CONFIG_NFS_V4_1)
7177 [1] = &nfs_v4_1_minor_ops,
7178#endif
7179};
7180
Bryan Schumaker73a79702012-07-16 16:39:12 -04007181const struct inode_operations nfs4_dir_inode_operations = {
7182 .create = nfs_create,
7183 .lookup = nfs_lookup,
7184 .atomic_open = nfs_atomic_open,
7185 .link = nfs_link,
7186 .unlink = nfs_unlink,
7187 .symlink = nfs_symlink,
7188 .mkdir = nfs_mkdir,
7189 .rmdir = nfs_rmdir,
7190 .mknod = nfs_mknod,
7191 .rename = nfs_rename,
7192 .permission = nfs_permission,
7193 .getattr = nfs_getattr,
7194 .setattr = nfs_setattr,
7195 .getxattr = generic_getxattr,
7196 .setxattr = generic_setxattr,
7197 .listxattr = generic_listxattr,
7198 .removexattr = generic_removexattr,
7199};
7200
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007201static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007202 .permission = nfs_permission,
7203 .getattr = nfs_getattr,
7204 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007205 .getxattr = generic_getxattr,
7206 .setxattr = generic_setxattr,
7207 .listxattr = generic_listxattr,
7208 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007209};
7210
David Howells509de812006-08-22 20:06:11 -04007211const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 .version = 4, /* protocol version */
7213 .dentry_ops = &nfs4_dentry_operations,
7214 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007215 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007216 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007218 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007219 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 .getattr = nfs4_proc_getattr,
7221 .setattr = nfs4_proc_setattr,
7222 .lookup = nfs4_proc_lookup,
7223 .access = nfs4_proc_access,
7224 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 .create = nfs4_proc_create,
7226 .remove = nfs4_proc_remove,
7227 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007228 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 .unlink_done = nfs4_proc_unlink_done,
7230 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007231 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007232 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007233 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 .link = nfs4_proc_link,
7235 .symlink = nfs4_proc_symlink,
7236 .mkdir = nfs4_proc_mkdir,
7237 .rmdir = nfs4_proc_remove,
7238 .readdir = nfs4_proc_readdir,
7239 .mknod = nfs4_proc_mknod,
7240 .statfs = nfs4_proc_statfs,
7241 .fsinfo = nfs4_proc_fsinfo,
7242 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007243 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 .decode_dirent = nfs4_decode_dirent,
7245 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007246 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007247 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007248 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007250 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007251 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007252 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007254 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007255 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007257 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007258 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007259 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007260 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007261 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007262 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007263 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007264 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007265 .create_server = nfs4_create_server,
7266 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267};
7268
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007269static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7270 .prefix = XATTR_NAME_NFSV4_ACL,
7271 .list = nfs4_xattr_list_nfs4_acl,
7272 .get = nfs4_xattr_get_nfs4_acl,
7273 .set = nfs4_xattr_set_nfs4_acl,
7274};
7275
7276const struct xattr_handler *nfs4_xattr_handlers[] = {
7277 &nfs4_xattr_nfs4_acl_handler,
7278 NULL
7279};
7280
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281/*
7282 * Local variables:
7283 * c-basic-offset: 8
7284 * End:
7285 */