blob: 37eb38566fbcb823168758c8cb250b0db2320e26 [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>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
336 ret = nfs4_delay(server->client, &exception->timeout);
337 if (ret != 0)
338 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400339 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500340 case -NFS4ERR_OLD_STATEID:
341 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800342 break;
343 case -NFS4ERR_BADOWNER:
344 /* The following works around a Linux server bug! */
345 case -NFS4ERR_BADNAME:
346 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348 exception->retry = 1;
349 printk(KERN_WARNING "NFS: v4 server %s "
350 "does not accept raw "
351 "uid/gids. "
352 "Reenabling the idmapper.\n",
353 server->nfs_client->cl_hostname);
354 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 }
356 /* We failed to handle the error */
357 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500358wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500359 ret = nfs4_wait_clnt_recover(clp);
360 if (ret == 0)
361 exception->retry = 1;
362 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500363}
364
365
Trond Myklebust452e9352010-07-31 14:29:06 -0400366static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_lock(&clp->cl_lock);
369 if (time_before(clp->cl_last_renewal,timestamp))
370 clp->cl_last_renewal = timestamp;
371 spin_unlock(&clp->cl_lock);
372}
373
Trond Myklebust452e9352010-07-31 14:29:06 -0400374static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
375{
376 do_renew_lease(server->nfs_client, timestamp);
377}
378
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400379#if defined(CONFIG_NFS_V4_1)
380
Trond Myklebustd185a332010-06-16 09:52:25 -0400381static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400382{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500383 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400384 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500385 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400386
Benny Halevydfb4f3092010-09-24 09:17:01 -0400387 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400388 /* just wake up the next guy waiting since
389 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500390 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500391 return;
Andy Adamson13615872009-04-01 09:22:17 -0400392 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500393 tbl = res->sr_slot->table;
394 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500395
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500396 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500397 /* Be nice to the server: try to ensure that the last transmitted
398 * value for highest_user_slotid <= target_highest_slotid
399 */
400 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
401 send_new_highest_used_slotid = true;
402
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500403 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
404 send_new_highest_used_slotid = false;
405 goto out_unlock;
406 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500407 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500408
409 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
410 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500411out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500412 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400413 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500414 if (send_new_highest_used_slotid)
415 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400416}
417
Trond Myklebust14516c32010-07-31 14:29:06 -0400418static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400419{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500420 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500421 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400422 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500423 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500424 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400425
Bryan Schumaker468f8612011-04-18 15:57:32 -0400426 /* don't increment the sequence number if the task wasn't sent */
427 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400428 goto out;
429
Trond Myklebust933602e2012-11-16 12:12:38 -0500430 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500431 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500432
Trond Myklebustac20d162012-12-15 15:36:07 -0500433 if (slot->interrupted) {
434 slot->interrupted = 0;
435 interrupted = true;
436 }
437
Andy Adamson691daf32009-12-04 15:55:39 -0500438 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400439 switch (res->sr_status) {
440 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500442 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500443 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500444 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500445 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500446 if (res->sr_status_flags != 0)
447 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500448 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500450 case 1:
451 /*
452 * sr_status remains 1 if an RPC level error occurred.
453 * The server may or may not have processed the sequence
454 * operation..
455 * Mark the slot as having hosted an interrupted RPC call.
456 */
457 slot->interrupted = 1;
458 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 case -NFS4ERR_DELAY:
460 /* The server detected a resend of the RPC call and
461 * returned NFS4ERR_DELAY as per Section 2.10.6.2
462 * of RFC5661.
463 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500464 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400465 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500466 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500467 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500469 case -NFS4ERR_BADSLOT:
470 /*
471 * The slot id we used was probably retired. Try again
472 * using a different slot id.
473 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500474 goto retry_nowait;
475 case -NFS4ERR_SEQ_MISORDERED:
476 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500477 * Was the last operation on this sequence interrupted?
478 * If so, retry after bumping the sequence number.
479 */
480 if (interrupted) {
481 ++slot->seq_nr;
482 goto retry_nowait;
483 }
484 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500485 * Could this slot have been previously retired?
486 * If so, then the server may be expecting seq_nr = 1!
487 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500488 if (slot->seq_nr != 1) {
489 slot->seq_nr = 1;
490 goto retry_nowait;
491 }
492 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500493 case -NFS4ERR_SEQ_FALSE_RETRY:
494 ++slot->seq_nr;
495 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400496 default:
497 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500498 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400499 }
500out:
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400503 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500504 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500505retry_nowait:
506 if (rpc_restart_call_prepare(task)) {
507 task->tk_status = 0;
508 ret = 0;
509 }
510 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400511out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400512 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 goto out;
514 rpc_delay(task, NFS4_POLL_RETRY_MAX);
515 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400516}
517
Trond Myklebust14516c32010-07-31 14:29:06 -0400518static int nfs4_sequence_done(struct rpc_task *task,
519 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400520{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500521 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400522 return 1;
523 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400524}
525
Trond Myklebust9d12b212012-01-17 22:04:25 -0500526static void nfs41_init_sequence(struct nfs4_sequence_args *args,
527 struct nfs4_sequence_res *res, int cache_reply)
528{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500529 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500530 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400531 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500532 if (cache_reply)
533 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500534 res->sr_slot = NULL;
535}
536
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400537static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
538{
539 args->sa_privileged = 1;
540}
541
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000542int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400543 struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400545 struct rpc_task *task)
546{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 struct nfs4_slot *slot;
548 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549
550 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400551 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400552 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400553 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 tbl = &session->fc_slot_table;
556
Trond Myklebust69d206b2012-11-22 13:21:02 -0500557 task->tk_timeout = 0;
558
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400559 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400560 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400561 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500562 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500563 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400564 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400565 }
566
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500567 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500568 if (IS_ERR(slot)) {
569 /* If out of memory, try again in 1/4 second */
570 if (slot == ERR_PTR(-ENOMEM))
571 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400572 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400573 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 }
575 spin_unlock(&tbl->slot_tbl_lock);
576
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500577 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400578
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
580 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400581
Benny Halevydfb4f3092010-09-24 09:17:01 -0400582 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500583 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400584 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 /*
586 * sr_status is only set in decode_sequence, and so will remain
587 * set to 1 if an rpc level failure occurs.
588 */
589 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400590out_success:
591 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400593out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400594 /* Privileged tasks are queued with top priority */
595 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400596 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
597 NULL, RPC_PRIORITY_PRIVILEGED);
598 else
599 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400600 spin_unlock(&tbl->slot_tbl_lock);
601 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000603EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604
Trond Myklebust035168ab2010-06-16 09:52:26 -0400605int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 struct rpc_task *task)
609{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400610 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611 int ret = 0;
612
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400613 if (session == NULL) {
614 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400616 }
Trond Myklebust035168ab2010-06-16 09:52:26 -0400617
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500618 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400619 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500620 res->sr_slot->slot_nr : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400621
Trond Myklebust9d12b212012-01-17 22:04:25 -0500622 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623out:
624 dprintk("<-- %s status=%d\n", __func__, ret);
625 return ret;
626}
627
628struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400629 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct nfs4_sequence_args *seq_args;
631 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632};
633
634static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
635{
636 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400637 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638
Trond Myklebust035168ab2010-06-16 09:52:26 -0400639 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
640
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400641 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642}
643
Andy Adamson69ab40c2009-04-01 09:22:19 -0400644static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
645{
646 struct nfs41_call_sync_data *data = calldata;
647
Trond Myklebust14516c32010-07-31 14:29:06 -0400648 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400649}
650
Trond Myklebust17280172012-03-11 13:11:00 -0400651static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400653 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654};
655
Bryan Schumaker7c513052011-03-24 17:12:24 +0000656static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
657 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 struct rpc_message *msg,
659 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400660 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661{
662 int ret;
663 struct rpc_task *task;
664 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400665 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666 .seq_args = args,
667 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 };
669 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000670 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671 .rpc_message = msg,
672 .callback_ops = &nfs41_call_sync_ops,
673 .callback_data = &data
674 };
675
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676 task = rpc_run_task(&task_setup);
677 if (IS_ERR(task))
678 ret = PTR_ERR(task);
679 else {
680 ret = task->tk_status;
681 rpc_put_task(task);
682 }
683 return ret;
684}
685
Trond Myklebustdf896452010-06-16 09:52:26 -0400686#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400687static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500688void nfs41_init_sequence(struct nfs4_sequence_args *args,
689 struct nfs4_sequence_res *res, int cache_reply)
690{
691}
692
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400693static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
694{
695}
696
697
Trond Myklebust14516c32010-07-31 14:29:06 -0400698static int nfs4_sequence_done(struct rpc_task *task,
699 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400700{
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400702}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400703#endif /* CONFIG_NFS_V4_1 */
704
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400705static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000706int _nfs4_call_sync(struct rpc_clnt *clnt,
707 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400710 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000712 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713}
714
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400715static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000716int nfs4_call_sync(struct rpc_clnt *clnt,
717 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000718 struct rpc_message *msg,
719 struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res,
721 int cache_reply)
722{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400723 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400725 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000726}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400727
Trond Myklebust38478b22006-05-25 01:40:57 -0400728static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Trond Myklebust38478b22006-05-25 01:40:57 -0400730 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Trond Myklebust38478b22006-05-25 01:40:57 -0400732 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400733 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700734 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400735 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700736 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000737 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400738 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400742 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743 struct nfs_openargs o_arg;
744 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100745 struct nfs_open_confirmargs c_arg;
746 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500747 struct nfs4_string owner_name;
748 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400751 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400753 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100755 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400756 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100757 int rpc_status;
758 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759};
760
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400761
762static void nfs4_init_opendata_res(struct nfs4_opendata *p)
763{
764 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400765 p->o_res.seqid = p->o_arg.seqid;
766 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400767 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400768 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400769 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500770 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400771}
772
Al Viro82a2c1b2011-06-22 18:30:55 -0400773static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500774 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400775 const struct iattr *attrs,
776 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777{
Al Viro82a2c1b2011-06-22 18:30:55 -0400778 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 struct inode *dir = parent->d_inode;
780 struct nfs_server *server = NFS_SERVER(dir);
781 struct nfs4_opendata *p;
782
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400783 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 if (p == NULL)
785 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400786 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 if (p->o_arg.seqid == NULL)
788 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400789 nfs_sb_active(dentry->d_sb);
790 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 p->dir = parent;
792 p->owner = sp;
793 atomic_inc(&sp->so_count);
794 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500795 p->o_arg.open_flags = flags;
796 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700797 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
798 * will return permission denied for all bits until close */
799 if (!(flags & O_EXCL)) {
800 /* ask server to check for all possible rights as results
801 * are cached */
802 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
803 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700804 }
David Howells7539bba2006-08-22 20:06:09 -0400805 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400806 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
807 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400808 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 p->o_arg.server = server;
810 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400811 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500813 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500814 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400815
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 p->o_arg.u.attrs = &p->attrs;
817 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500818
819 verf[0] = jiffies;
820 verf[1] = current->pid;
821 memcpy(p->o_arg.u.verifier.data, verf,
822 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100824 p->c_arg.fh = &p->o_res.fh;
825 p->c_arg.stateid = &p->o_res.stateid;
826 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400827 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400828 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 return p;
830err_free:
831 kfree(p);
832err:
833 dput(parent);
834 return NULL;
835}
836
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400837static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400839 struct nfs4_opendata *p = container_of(kref,
840 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400841 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400842
843 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400844 if (p->state != NULL)
845 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846 nfs4_put_state_owner(p->owner);
847 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 dput(p->dentry);
849 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500850 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400851 kfree(p);
852}
853
854static void nfs4_opendata_put(struct nfs4_opendata *p)
855{
856 if (p != NULL)
857 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858}
859
Trond Myklebust06f814a2006-01-03 09:55:07 +0100860static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
861{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100862 int ret;
863
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100865 return ret;
866}
867
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400869{
870 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871
Trond Myklebust536e43d2012-01-17 22:04:26 -0500872 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873 goto out;
874 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400875 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500876 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
877 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400878 break;
879 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500880 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
881 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400882 break;
883 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500884 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
885 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400886 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888 return ret;
889}
890
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400892{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500893 if (delegation == NULL)
894 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500897 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500899 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
900 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500901 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 return 1;
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100906{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100908 case FMODE_WRITE:
909 state->n_wronly++;
910 break;
911 case FMODE_READ:
912 state->n_rdonly++;
913 break;
914 case FMODE_READ|FMODE_WRITE:
915 state->n_rdwr++;
916 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500923 nfs4_stateid_copy(&state->stateid, stateid);
924 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400926 case FMODE_READ:
927 set_bit(NFS_O_RDONLY_STATE, &state->flags);
928 break;
929 case FMODE_WRITE:
930 set_bit(NFS_O_WRONLY_STATE, &state->flags);
931 break;
932 case FMODE_READ|FMODE_WRITE:
933 set_bit(NFS_O_RDWR_STATE, &state->flags);
934 }
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400938{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static 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 -0700945{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400946 /*
947 * Protect the call to nfs4_state_set_mode_locked and
948 * serialise the stateid update
949 */
950 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500952 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400953 set_bit(NFS_DELEGATED_STATE, &state->flags);
954 }
955 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400957 write_sequnlock(&state->seqlock);
958 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700960 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static 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 -0500964{
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *deleg_cur;
967 int ret = 0;
968
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500970
971 rcu_read_lock();
972 deleg_cur = rcu_dereference(nfsi->delegation);
973 if (deleg_cur == NULL)
974 goto no_delegation;
975
976 spin_lock(&deleg_cur->lock);
977 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -0500978 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500980 goto no_delegation_unlock;
981
982 if (delegation == NULL)
983 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500984 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500985 goto no_delegation_unlock;
986
Trond Myklebustb7391f42008-12-23 15:21:52 -0500987 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500989 ret = 1;
990no_delegation_unlock:
991 spin_unlock(&deleg_cur->lock);
992no_delegation:
993 rcu_read_unlock();
994
995 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500997 ret = 1;
998 }
999
1000 return ret;
1001}
1002
1003
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005{
1006 struct nfs_delegation *delegation;
1007
1008 rcu_read_lock();
1009 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 rcu_read_unlock();
1012 return;
1013 }
1014 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001015 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016}
1017
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019{
1020 struct nfs4_state *state = opendata->state;
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001023 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 nfs4_stateid stateid;
1026 int ret = -EAGAIN;
1027
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 if (can_open_cached(state, fmode, open_mode)) {
1032 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034 goto out_return_state;
1035 }
1036 spin_unlock(&state->owner->so_lock);
1037 }
Trond Myklebust34310432008-12-23 15:21:38 -05001038 rcu_read_lock();
1039 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001040 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001041 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001042 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001043 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001045 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001047 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 if (ret != 0)
1049 goto out;
1050 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001051
1052 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001054 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056out:
1057 return ERR_PTR(ret);
1058out_return_state:
1059 atomic_inc(&state->count);
1060 return state;
1061}
1062
Andy Adamsone23008e2012-10-02 21:07:32 -04001063static void
1064nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1065{
1066 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1067 struct nfs_delegation *delegation;
1068 int delegation_flags = 0;
1069
1070 rcu_read_lock();
1071 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1072 if (delegation)
1073 delegation_flags = delegation->flags;
1074 rcu_read_unlock();
1075 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1076 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1077 "returning a delegation for "
1078 "OPEN(CLAIM_DELEGATE_CUR)\n",
1079 clp->cl_hostname);
1080 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1081 nfs_inode_set_delegation(state->inode,
1082 data->owner->so_cred,
1083 &data->o_res);
1084 else
1085 nfs_inode_reclaim_delegation(state->inode,
1086 data->owner->so_cred,
1087 &data->o_res);
1088}
1089
1090/*
1091 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1092 * and update the nfs4_state.
1093 */
1094static struct nfs4_state *
1095_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1096{
1097 struct inode *inode = data->state->inode;
1098 struct nfs4_state *state = data->state;
1099 int ret;
1100
1101 if (!data->rpc_done) {
1102 ret = data->rpc_status;
1103 goto err;
1104 }
1105
1106 ret = -ESTALE;
1107 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1108 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1109 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1110 goto err;
1111
1112 ret = -ENOMEM;
1113 state = nfs4_get_open_state(inode, data->owner);
1114 if (state == NULL)
1115 goto err;
1116
1117 ret = nfs_refresh_inode(inode, &data->f_attr);
1118 if (ret)
1119 goto err;
1120
1121 if (data->o_res.delegation_type != 0)
1122 nfs4_opendata_check_deleg(data, state);
1123 update_open_stateid(state, &data->o_res.stateid, NULL,
1124 data->o_arg.fmode);
1125
1126 return state;
1127err:
1128 return ERR_PTR(ret);
1129
1130}
1131
1132static struct nfs4_state *
1133_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134{
1135 struct inode *inode;
1136 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001137 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138
Trond Myklebustaac00a82007-07-05 19:02:21 -04001139 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001140 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001141 goto out;
1142 }
1143
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001144 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001145 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001146 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001148 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001149 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001150 goto err;
1151 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001152 state = nfs4_get_open_state(inode, data->owner);
1153 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001154 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001155 if (data->o_res.delegation_type != 0)
1156 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001157 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001159 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001160out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001161 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001162 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001163err_put_inode:
1164 iput(inode);
1165err:
1166 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001167}
1168
Andy Adamsone23008e2012-10-02 21:07:32 -04001169static struct nfs4_state *
1170nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1171{
1172 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1173 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1174 return _nfs4_opendata_to_nfs4_state(data);
1175}
1176
Trond Myklebust864472e2006-01-03 09:55:15 +01001177static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1178{
1179 struct nfs_inode *nfsi = NFS_I(state->inode);
1180 struct nfs_open_context *ctx;
1181
1182 spin_lock(&state->inode->i_lock);
1183 list_for_each_entry(ctx, &nfsi->open_files, list) {
1184 if (ctx->state != state)
1185 continue;
1186 get_nfs_open_context(ctx);
1187 spin_unlock(&state->inode->i_lock);
1188 return ctx;
1189 }
1190 spin_unlock(&state->inode->i_lock);
1191 return ERR_PTR(-ENOENT);
1192}
1193
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001194static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1195{
1196 struct nfs4_opendata *opendata;
1197
Al Viro3d4ff432011-06-22 18:40:12 -04001198 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001199 if (opendata == NULL)
1200 return ERR_PTR(-ENOMEM);
1201 opendata->state = state;
1202 atomic_inc(&state->count);
1203 return opendata;
1204}
1205
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001206static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001207{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001208 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 int ret;
1210
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001211 opendata->o_arg.open_flags = 0;
1212 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1214 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1215 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001216 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001217 if (ret != 0)
1218 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001220 if (IS_ERR(newstate))
1221 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001222 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001223 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 return 0;
1225}
1226
1227static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1228{
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 int ret;
1231
1232 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 smp_rmb();
1235 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001236 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001237 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 if (ret != 0)
1239 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001240 if (newstate != state)
1241 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 }
1243 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001244 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001245 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 if (ret != 0)
1247 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001248 if (newstate != state)
1249 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 }
1251 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001252 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001253 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 if (ret != 0)
1255 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001256 if (newstate != state)
1257 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001258 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001259 /*
1260 * We may have performed cached opens for all three recoveries.
1261 * Check if we need to update the current stateid.
1262 */
1263 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001264 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001265 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001266 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001267 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001268 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 return 0;
1271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273/*
1274 * OPEN_RECLAIM:
1275 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001277static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001279 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int status;
1283
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001284 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1285 if (IS_ERR(opendata))
1286 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1288 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001289 rcu_read_lock();
1290 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001291 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001292 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001293 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 opendata->o_arg.u.delegation_type = delegation_type;
1295 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001296 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return status;
1298}
1299
Trond Myklebust539cd032007-06-05 11:46:42 -04001300static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct nfs_server *server = NFS_SERVER(state->inode);
1303 struct nfs4_exception exception = { };
1304 int err;
1305 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001306 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001307 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001308 break;
1309 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 } while (exception.retry);
1311 return err;
1312}
1313
Trond Myklebust864472e2006-01-03 09:55:15 +01001314static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1315{
1316 struct nfs_open_context *ctx;
1317 int ret;
1318
1319 ctx = nfs4_state_find_open_context(state);
1320 if (IS_ERR(ctx))
1321 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001322 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001323 put_nfs_open_context(ctx);
1324 return ret;
1325}
1326
Trond Myklebust13437e12007-07-06 15:10:43 -04001327static 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 -07001328{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001329 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001330 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001332 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1333 if (IS_ERR(opendata))
1334 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001335 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001336 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001337 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001338 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Trond Myklebust13437e12007-07-06 15:10:43 -04001342int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001345 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int err;
1347 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001348 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 switch (err) {
1350 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001351 case -ENOENT:
1352 case -ESTALE:
1353 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001354 case -NFS4ERR_BADSESSION:
1355 case -NFS4ERR_BADSLOT:
1356 case -NFS4ERR_BAD_HIGH_SLOT:
1357 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1358 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05001359 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04001360 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05001361 err = -EAGAIN;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 case -NFS4ERR_STALE_CLIENTID:
1364 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05001365 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 case -NFS4ERR_EXPIRED:
1367 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001368 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05001369 err = -EAGAIN;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001370 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001371 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001372 case -NFS4ERR_ADMIN_REVOKED:
1373 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001374 nfs_inode_find_state_and_recover(state->inode,
1375 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001376 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001377 case -ENOMEM:
1378 err = 0;
1379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001381 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 err = nfs4_handle_exception(server, err, &exception);
1383 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001384out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return err;
1386}
1387
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001388static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1389{
1390 struct nfs4_opendata *data = calldata;
1391
1392 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001393 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001394 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001395 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001396 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001397 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001398 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399}
1400
1401static void nfs4_open_confirm_release(void *calldata)
1402{
1403 struct nfs4_opendata *data = calldata;
1404 struct nfs4_state *state = NULL;
1405
1406 /* If this request hasn't been cancelled, do nothing */
1407 if (data->cancelled == 0)
1408 goto out_free;
1409 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001410 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001411 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001412 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001413 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001414 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001415out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001416 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417}
1418
1419static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001420 .rpc_call_done = nfs4_open_confirm_done,
1421 .rpc_release = nfs4_open_confirm_release,
1422};
1423
1424/*
1425 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1426 */
1427static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1428{
1429 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1430 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001431 struct rpc_message msg = {
1432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1433 .rpc_argp = &data->c_arg,
1434 .rpc_resp = &data->c_res,
1435 .rpc_cred = data->owner->so_cred,
1436 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001437 struct rpc_task_setup task_setup_data = {
1438 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001439 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001440 .callback_ops = &nfs4_open_confirm_ops,
1441 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001442 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001443 .flags = RPC_TASK_ASYNC,
1444 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int status;
1446
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001447 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001448 data->rpc_done = 0;
1449 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001450 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001451 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001452 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001453 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001454 status = nfs4_wait_for_completion_rpc_task(task);
1455 if (status != 0) {
1456 data->cancelled = 1;
1457 smp_wmb();
1458 } else
1459 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001460 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return status;
1462}
1463
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466 struct nfs4_opendata *data = calldata;
1467 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001468
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001470 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001471 /*
1472 * Check if we still need to send an OPEN call, or if we can use
1473 * a delegation instead.
1474 */
1475 if (data->state != NULL) {
1476 struct nfs_delegation *delegation;
1477
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001478 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001479 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001480 rcu_read_lock();
1481 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001482 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1483 can_open_delegated(delegation, data->o_arg.fmode))
1484 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001485 rcu_read_unlock();
1486 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001487 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001488 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001489 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001490 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001491 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001492 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1493 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001494 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001495 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001496 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001497 &data->o_res.seq_res,
1498 task) != 0)
1499 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001500 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001501unlock_no_action:
1502 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001503out_no_action:
1504 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001505out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001506 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001507}
1508
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509static void nfs4_open_done(struct rpc_task *task, void *calldata)
1510{
1511 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001514
Trond Myklebust14516c32010-07-31 14:29:06 -04001515 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1516 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001517
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001519 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1520 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001521 case S_IFREG:
1522 break;
1523 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001525 break;
1526 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001528 break;
1529 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001531 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001532 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001533 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001534 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1535 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001536 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001537 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001539
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540static void nfs4_open_release(void *calldata)
1541{
1542 struct nfs4_opendata *data = calldata;
1543 struct nfs4_state *state = NULL;
1544
1545 /* If this request hasn't been cancelled, do nothing */
1546 if (data->cancelled == 0)
1547 goto out_free;
1548 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001549 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550 goto out_free;
1551 /* In case we need an open_confirm, no cleanup! */
1552 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1553 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001555 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001556 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001558 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559}
1560
1561static const struct rpc_call_ops nfs4_open_ops = {
1562 .rpc_call_prepare = nfs4_open_prepare,
1563 .rpc_call_done = nfs4_open_done,
1564 .rpc_release = nfs4_open_release,
1565};
1566
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001567static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001568{
1569 struct inode *dir = data->dir->d_inode;
1570 struct nfs_server *server = NFS_SERVER(dir);
1571 struct nfs_openargs *o_arg = &data->o_arg;
1572 struct nfs_openres *o_res = &data->o_res;
1573 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001574 struct rpc_message msg = {
1575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1576 .rpc_argp = o_arg,
1577 .rpc_resp = o_res,
1578 .rpc_cred = data->owner->so_cred,
1579 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001580 struct rpc_task_setup task_setup_data = {
1581 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001582 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001583 .callback_ops = &nfs4_open_ops,
1584 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001585 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001586 .flags = RPC_TASK_ASYNC,
1587 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001588 int status;
1589
Trond Myklebust9d12b212012-01-17 22:04:25 -05001590 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001591 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001592 data->rpc_done = 0;
1593 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001594 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001595 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001596 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001597 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001598 if (IS_ERR(task))
1599 return PTR_ERR(task);
1600 status = nfs4_wait_for_completion_rpc_task(task);
1601 if (status != 0) {
1602 data->cancelled = 1;
1603 smp_wmb();
1604 } else
1605 status = data->rpc_status;
1606 rpc_put_task(task);
1607
1608 return status;
1609}
1610
1611static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1612{
1613 struct inode *dir = data->dir->d_inode;
1614 struct nfs_openres *o_res = &data->o_res;
1615 int status;
1616
1617 status = nfs4_run_open_task(data, 1);
1618 if (status != 0 || !data->rpc_done)
1619 return status;
1620
Trond Myklebust6926afd2012-01-07 13:22:46 -05001621 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1622
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001623 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1624 status = _nfs4_proc_open_confirm(data);
1625 if (status != 0)
1626 return status;
1627 }
1628
1629 return status;
1630}
1631
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001632static int nfs4_opendata_access(struct rpc_cred *cred,
1633 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001634 struct nfs4_state *state, fmode_t fmode,
1635 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001636{
1637 struct nfs_access_entry cache;
1638 u32 mask;
1639
1640 /* access call failed or for some reason the server doesn't
1641 * support any access modes -- defer access call until later */
1642 if (opendata->o_res.access_supported == 0)
1643 return 0;
1644
1645 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001646 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001647 * write mode bits, but POSIX allows the creating process to write.
1648 * use openflags to check for exec, because fmode won't
1649 * always have FMODE_EXEC set when file open for exec. */
1650 if (openflags & __FMODE_EXEC) {
1651 /* ONLY check for exec rights */
1652 mask = MAY_EXEC;
1653 } else if (fmode & FMODE_READ)
1654 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001655
1656 cache.cred = cred;
1657 cache.jiffies = jiffies;
1658 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1659 nfs_access_add_cache(state->inode, &cache);
1660
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001661 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001662 return 0;
1663
1664 /* even though OPEN succeeded, access is denied. Close the file */
1665 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001666 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001667}
1668
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001669/*
1670 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1671 */
1672static int _nfs4_proc_open(struct nfs4_opendata *data)
1673{
1674 struct inode *dir = data->dir->d_inode;
1675 struct nfs_server *server = NFS_SERVER(dir);
1676 struct nfs_openargs *o_arg = &data->o_arg;
1677 struct nfs_openres *o_res = &data->o_res;
1678 int status;
1679
1680 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001681 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001683 if (status != 0) {
1684 if (status == -NFS4ERR_BADNAME &&
1685 !(o_arg->open_flags & O_CREAT))
1686 return -ENOENT;
1687 return status;
1688 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001689
Trond Myklebust6926afd2012-01-07 13:22:46 -05001690 nfs_fattr_map_and_free_names(server, &data->f_attr);
1691
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001692 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001693 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001694 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1695 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001697 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001699 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001702 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Andy Adamsond83217c2011-03-01 01:34:17 +00001706static int nfs4_recover_expired_lease(struct nfs_server *server)
1707{
1708 return nfs4_client_recover_expired_lease(server->nfs_client);
1709}
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711/*
1712 * OPEN_EXPIRED:
1713 * reclaim state on the server after a network partition.
1714 * Assumes caller holds the appropriate lock
1715 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001716static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001718 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001721 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1722 if (IS_ERR(opendata))
1723 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001724 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001725 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001726 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001727 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001728 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001731static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001732{
Trond Myklebust539cd032007-06-05 11:46:42 -04001733 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001734 struct nfs4_exception exception = { };
1735 int err;
1736
1737 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001738 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001739 switch (err) {
1740 default:
1741 goto out;
1742 case -NFS4ERR_GRACE:
1743 case -NFS4ERR_DELAY:
1744 nfs4_handle_exception(server, err, &exception);
1745 err = 0;
1746 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001747 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001748out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001749 return err;
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1753{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001755 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Trond Myklebust864472e2006-01-03 09:55:15 +01001757 ctx = nfs4_state_find_open_context(state);
1758 if (IS_ERR(ctx))
1759 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001760 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001761 put_nfs_open_context(ctx);
1762 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001765#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001766static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001767{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001768 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001769 nfs4_stateid *stateid = &state->stateid;
1770 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001771
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001772 /* If a state reset has been done, test_stateid is unneeded */
1773 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1774 return;
1775
1776 status = nfs41_test_stateid(server, stateid);
1777 if (status != NFS_OK) {
1778 /* Free the stateid unless the server explicitly
1779 * informs us the stateid is unrecognized. */
1780 if (status != -NFS4ERR_BAD_STATEID)
1781 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001782 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001783
Bryan Schumaker69388672012-09-26 15:25:52 -04001784 write_seqlock(&state->seqlock);
1785 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1786 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001787 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1788 }
1789}
1790
1791/**
1792 * nfs41_check_open_stateid - possibly free an open stateid
1793 *
1794 * @state: NFSv4 state for an inode
1795 *
1796 * Returns NFS_OK if recovery for this stateid is now finished.
1797 * Otherwise a negative NFS4ERR value is returned.
1798 */
1799static int nfs41_check_open_stateid(struct nfs4_state *state)
1800{
1801 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001802 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001803 int status;
1804
1805 /* If a state reset has been done, test_stateid is unneeded */
1806 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1807 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1808 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1809 return -NFS4ERR_BAD_STATEID;
1810
1811 status = nfs41_test_stateid(server, stateid);
1812 if (status != NFS_OK) {
1813 /* Free the stateid unless the server explicitly
1814 * informs us the stateid is unrecognized. */
1815 if (status != -NFS4ERR_BAD_STATEID)
1816 nfs41_free_stateid(server, stateid);
1817
1818 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1819 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1820 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001821 }
1822 return status;
1823}
1824
1825static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1826{
Chuck Levereb64cf92012-07-11 16:30:05 -04001827 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001828
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001829 nfs41_clear_delegation_stateid(state);
1830 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001831 if (status != NFS_OK)
1832 status = nfs4_open_expired(sp, state);
1833 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001834}
1835#endif
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001838 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1839 * fields corresponding to attributes that were used to store the verifier.
1840 * Make sure we clobber those fields in the later setattr call
1841 */
1842static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1843{
1844 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1845 !(sattr->ia_valid & ATTR_ATIME_SET))
1846 sattr->ia_valid |= ATTR_ATIME;
1847
1848 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1849 !(sattr->ia_valid & ATTR_MTIME_SET))
1850 sattr->ia_valid |= ATTR_MTIME;
1851}
1852
Trond Myklebustc21443c2013-02-07 14:26:21 -05001853static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1854 fmode_t fmode,
1855 int flags,
1856 struct nfs4_state **res)
1857{
1858 struct nfs4_state_owner *sp = opendata->owner;
1859 struct nfs_server *server = sp->so_server;
1860 struct nfs4_state *state;
1861 unsigned int seq;
1862 int ret;
1863
1864 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1865
1866 ret = _nfs4_proc_open(opendata);
1867 if (ret != 0)
1868 goto out;
1869
1870 state = nfs4_opendata_to_nfs4_state(opendata);
1871 ret = PTR_ERR(state);
1872 if (IS_ERR(state))
1873 goto out;
1874 if (server->caps & NFS_CAP_POSIX_LOCK)
1875 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1876
1877 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1878 if (ret != 0)
1879 goto out;
1880
1881 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1882 nfs4_schedule_stateid_recovery(server, state);
1883 nfs4_wait_clnt_recover(server->nfs_client);
1884 }
1885 *res = state;
1886out:
1887 return ret;
1888}
1889
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001890/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001891 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 */
Andy Adamson82be4172012-05-23 05:02:35 -04001893static int _nfs4_do_open(struct inode *dir,
1894 struct dentry *dentry,
1895 fmode_t fmode,
1896 int flags,
1897 struct iattr *sattr,
1898 struct rpc_cred *cred,
1899 struct nfs4_state **res,
1900 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 struct nfs4_state_owner *sp;
1903 struct nfs4_state *state = NULL;
1904 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001905 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001906 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001910 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1911 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1913 goto out_err;
1914 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001915 status = nfs4_recover_expired_lease(server);
1916 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001917 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001918 if (dentry->d_inode != NULL)
1919 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001920 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001921 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001922 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001923 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Andy Adamson82be4172012-05-23 05:02:35 -04001925 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1926 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1927 if (!opendata->f_attr.mdsthreshold)
1928 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001929 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001930 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001931 if (dentry->d_inode != NULL)
1932 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001933
Trond Myklebustc21443c2013-02-07 14:26:21 -05001934 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001935 if (status != 0)
1936 goto err_opendata_put;
1937
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001938 if (opendata->o_arg.open_flags & O_EXCL) {
1939 nfs4_exclusive_attrset(opendata, sattr);
1940
1941 nfs_fattr_init(opendata->o_res.f_attr);
1942 status = nfs4_do_setattr(state->inode, cred,
1943 opendata->o_res.f_attr, sattr,
1944 state);
1945 if (status == 0)
1946 nfs_setattr_update_inode(state->inode, sattr);
1947 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1948 }
Andy Adamson82be4172012-05-23 05:02:35 -04001949
1950 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1951 *ctx_th = opendata->f_attr.mdsthreshold;
1952 else
1953 kfree(opendata->f_attr.mdsthreshold);
1954 opendata->f_attr.mdsthreshold = NULL;
1955
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001956 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 *res = state;
1959 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001960err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001961 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001962 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001963err_put_state_owner:
1964 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 *res = NULL;
1967 return status;
1968}
1969
1970
Andy Adamson82be4172012-05-23 05:02:35 -04001971static struct nfs4_state *nfs4_do_open(struct inode *dir,
1972 struct dentry *dentry,
1973 fmode_t fmode,
1974 int flags,
1975 struct iattr *sattr,
1976 struct rpc_cred *cred,
1977 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct nfs4_exception exception = { };
1980 struct nfs4_state *res;
1981 int status;
1982
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001983 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001985 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1986 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (status == 0)
1988 break;
1989 /* NOTE: BAD_SEQID means the server and client disagree about the
1990 * book-keeping w.r.t. state-changing operations
1991 * (OPEN/CLOSE/LOCK/LOCKU...)
1992 * It is actually a sign of a bug on the client or on the server.
1993 *
1994 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001995 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 * have unhashed the old state_owner for us, and that we can
1997 * therefore safely retry using a new one. We should still warn
1998 * the user though...
1999 */
2000 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002001 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002002 " returned a bad sequence-id error!\n",
2003 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 exception.retry = 1;
2005 continue;
2006 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002007 /*
2008 * BAD_STATEID on OPEN means that the server cancelled our
2009 * state before it received the OPEN_CONFIRM.
2010 * Recover by retrying the request as per the discussion
2011 * on Page 181 of RFC3530.
2012 */
2013 if (status == -NFS4ERR_BAD_STATEID) {
2014 exception.retry = 1;
2015 continue;
2016 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002017 if (status == -EAGAIN) {
2018 /* We must have found a delegation */
2019 exception.retry = 1;
2020 continue;
2021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2023 status, &exception));
2024 } while (exception.retry);
2025 return res;
2026}
2027
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002028static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2029 struct nfs_fattr *fattr, struct iattr *sattr,
2030 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002032 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002034 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 .iap = sattr,
2036 .server = server,
2037 .bitmask = server->attr_bitmask,
2038 };
2039 struct nfs_setattrres res = {
2040 .fattr = fattr,
2041 .server = server,
2042 };
2043 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2045 .rpc_argp = &arg,
2046 .rpc_resp = &res,
2047 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002049 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002050 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Trond Myklebust0e574af2005-10-27 22:12:38 -04002052 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Trond Myklebust4fc87962012-03-08 17:42:01 -05002054 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002055 struct nfs_lockowner lockowner = {
2056 .l_owner = current->files,
2057 .l_pid = current->tgid,
2058 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002059 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002060 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002061 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2062 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002063 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002064 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002065 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Bryan Schumaker7c513052011-03-24 17:12:24 +00002067 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002068 if (status == 0 && state != NULL)
2069 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002070 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2074 struct nfs_fattr *fattr, struct iattr *sattr,
2075 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002077 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002078 struct nfs4_exception exception = {
2079 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002080 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002081 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int err;
2083 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002084 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2085 switch (err) {
2086 case -NFS4ERR_OPENMODE:
2087 if (state && !(state->state & FMODE_WRITE)) {
2088 err = -EBADF;
2089 if (sattr->ia_valid & ATTR_OPEN)
2090 err = -EACCES;
2091 goto out;
2092 }
2093 }
2094 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002096out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return err;
2098}
2099
2100struct nfs4_closedata {
2101 struct inode *inode;
2102 struct nfs4_state *state;
2103 struct nfs_closeargs arg;
2104 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002105 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002106 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002107 bool roc;
2108 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109};
2110
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002111static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002112{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002113 struct nfs4_closedata *calldata = data;
2114 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002115 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002116
Fred Isamanf7e89172011-01-06 11:36:32 +00002117 if (calldata->roc)
2118 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002119 nfs4_put_open_state(calldata->state);
2120 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002121 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002122 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002123 kfree(calldata);
2124}
2125
Trond Myklebust88069f72009-12-08 08:33:16 -05002126static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2127 fmode_t fmode)
2128{
2129 spin_lock(&state->owner->so_lock);
2130 if (!(fmode & FMODE_READ))
2131 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2132 if (!(fmode & FMODE_WRITE))
2133 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2134 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2135 spin_unlock(&state->owner->so_lock);
2136}
2137
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002138static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002140 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 struct nfs_server *server = NFS_SERVER(calldata->inode);
2143
Chuck Levera3ca5652012-03-01 17:00:40 -05002144 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002145 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2146 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /* hmm. we are done with the inode, and in the process of freeing
2148 * the state_owner. we keep this around to process errors
2149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 switch (task->tk_status) {
2151 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002152 if (calldata->roc)
2153 pnfs_roc_set_barrier(state->inode,
2154 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002155 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002156 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002157 nfs4_close_clear_stateid_flags(state,
2158 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 break;
2160 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002161 case -NFS4ERR_OLD_STATEID:
2162 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002164 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002167 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2168 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002170 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002171 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002172 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002175static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002177 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002178 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002179 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002180 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002181
Chuck Levera3ca5652012-03-01 17:00:40 -05002182 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002183 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002184 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002185
Trond Myklebust88069f72009-12-08 08:33:16 -05002186 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2187 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002188 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002189 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002190 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002191 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002192 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2193 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2194 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002195 }
2196 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002197 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2198 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2199 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002200 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002201 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002202 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002203
2204 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002205 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002206 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002207 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002208
Fred Isamanf7e89172011-01-06 11:36:32 +00002209 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002210 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002211 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002212 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002213 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002214 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002215
Trond Myklebust516a6af2005-10-27 22:12:41 -04002216 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002217 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002218 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002219 &calldata->arg.seq_args,
2220 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002221 task) != 0)
2222 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002223 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002224 return;
2225out_no_action:
2226 task->tk_action = NULL;
2227out_wait:
2228 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002231static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002232 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002233 .rpc_call_done = nfs4_close_done,
2234 .rpc_release = nfs4_free_closedata,
2235};
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237/*
2238 * It is possible for data to be read/written from a mem-mapped file
2239 * after the sys_close call (which hits the vfs layer as a flush).
2240 * This means that we can't safely call nfsv4 close on a file until
2241 * the inode is cleared. This in turn means that we are not good
2242 * NFSv4 citizens - we do not indicate to the server to update the file's
2243 * share state even when we are done with one of the three share
2244 * stateid's in the inode.
2245 *
2246 * NOTE: Caller must be holding the sp->so_owner semaphore!
2247 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002248int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002250 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002252 struct nfs4_state_owner *sp = state->owner;
2253 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002254 struct rpc_message msg = {
2255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2256 .rpc_cred = state->owner->so_cred,
2257 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002258 struct rpc_task_setup task_setup_data = {
2259 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002260 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002261 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002262 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 .flags = RPC_TASK_ASYNC,
2264 };
Trond Myklebust95121352005-10-18 14:20:12 -07002265 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002267 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002269 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002270 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002271 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002273 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002274 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002276 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002277 if (calldata->arg.seqid == NULL)
2278 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002279 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002280 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002281 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002282 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002283 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002284 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002285 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002286
Trond Myklebust1174dd12010-12-21 10:52:24 -05002287 msg.rpc_argp = &calldata->arg;
2288 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002289 task_setup_data.callback_data = calldata;
2290 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002291 if (IS_ERR(task))
2292 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002293 status = 0;
2294 if (wait)
2295 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002296 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002297 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002298out_free_calldata:
2299 kfree(calldata);
2300out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002301 nfs4_put_open_state(state);
2302 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002303 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Trond Myklebust2b484292010-09-17 10:56:51 -04002306static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002307nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 struct nfs4_state *state;
2310
Trond Myklebust565277f2007-10-15 18:17:53 -04002311 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002312 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2313 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002314 if (IS_ERR(state))
2315 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002316 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002317 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
Trond Myklebust1185a552009-12-03 15:54:02 -05002320static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002321{
2322 if (ctx->state == NULL)
2323 return;
2324 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002325 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002326 else
Al Viro643168c2011-06-22 18:20:23 -04002327 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2331{
Benny Halevy43652ad2009-04-01 09:21:54 -04002332 struct nfs4_server_caps_arg args = {
2333 .fhandle = fhandle,
2334 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 struct nfs4_server_caps_res res = {};
2336 struct rpc_message msg = {
2337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002338 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 .rpc_resp = &res,
2340 };
2341 int status;
2342
Bryan Schumaker7c513052011-03-24 17:12:24 +00002343 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (status == 0) {
2345 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002346 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2347 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2348 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2349 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2350 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2352 server->caps |= NFS_CAP_ACLS;
2353 if (res.has_links != 0)
2354 server->caps |= NFS_CAP_HARDLINKS;
2355 if (res.has_symlinks != 0)
2356 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002357 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2358 server->caps |= NFS_CAP_FILEID;
2359 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2360 server->caps |= NFS_CAP_MODE;
2361 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2362 server->caps |= NFS_CAP_NLINK;
2363 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2364 server->caps |= NFS_CAP_OWNER;
2365 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2366 server->caps |= NFS_CAP_OWNER_GROUP;
2367 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2368 server->caps |= NFS_CAP_ATIME;
2369 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2370 server->caps |= NFS_CAP_CTIME;
2371 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2372 server->caps |= NFS_CAP_MTIME;
2373
Trond Myklebusta65318b2009-03-11 14:10:28 -04002374 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2375 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2376 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002378 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return status;
2382}
2383
Trond Myklebust55a97592006-06-09 09:34:19 -04002384int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 struct nfs4_exception exception = { };
2387 int err;
2388 do {
2389 err = nfs4_handle_exception(server,
2390 _nfs4_server_capabilities(server, fhandle),
2391 &exception);
2392 } while (exception.retry);
2393 return err;
2394}
2395
2396static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2397 struct nfs_fsinfo *info)
2398{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 struct nfs4_lookup_root_arg args = {
2400 .bitmask = nfs4_fattr_bitmap,
2401 };
2402 struct nfs4_lookup_res res = {
2403 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002404 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 .fh = fhandle,
2406 };
2407 struct rpc_message msg = {
2408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2409 .rpc_argp = &args,
2410 .rpc_resp = &res,
2411 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002412
Trond Myklebust0e574af2005-10-27 22:12:38 -04002413 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002414 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
2417static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2418 struct nfs_fsinfo *info)
2419{
2420 struct nfs4_exception exception = { };
2421 int err;
2422 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002423 err = _nfs4_lookup_root(server, fhandle, info);
2424 switch (err) {
2425 case 0:
2426 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002427 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002428 default:
2429 err = nfs4_handle_exception(server, err, &exception);
2430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002432out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return err;
2434}
2435
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002436static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2437 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2438{
2439 struct rpc_auth *auth;
2440 int ret;
2441
2442 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002443 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002444 ret = -EIO;
2445 goto out;
2446 }
2447 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002448out:
2449 return ret;
2450}
2451
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002452static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002453 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002455 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002456 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002458 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002459 if (len < 0)
2460 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002461
2462 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002463 /* AUTH_UNIX is the default flavor if none was specified,
2464 * thus has already been tried. */
2465 if (flav_array[i] == RPC_AUTH_UNIX)
2466 continue;
2467
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002468 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002469 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002470 continue;
2471 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002472 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002473 /*
2474 * -EACCESS could mean that the user doesn't have correct permissions
2475 * to access the mount. It could also mean that we tried to mount
2476 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2477 * existing mount programs don't handle -EACCES very well so it should
2478 * be mapped to -EPERM instead.
2479 */
2480 if (status == -EACCES)
2481 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002482 return status;
2483}
2484
2485/*
2486 * get the file handle for the "/" directory on the server
2487 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002488int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2489 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002490{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002491 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002492 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002493 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2494 /*
2495 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2496 * by nfs4_map_errors() as this function exits.
2497 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002498 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (status == 0)
2500 status = nfs4_server_capabilities(server, fhandle);
2501 if (status == 0)
2502 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002503 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Bryan Schumakerbae36242012-05-10 15:07:31 -04002506static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2507 struct nfs_fsinfo *info)
2508{
2509 int error;
2510 struct nfs_fattr *fattr = info->fattr;
2511
2512 error = nfs4_server_capabilities(server, mntfh);
2513 if (error < 0) {
2514 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2515 return error;
2516 }
2517
2518 error = nfs4_proc_getattr(server, mntfh, fattr);
2519 if (error < 0) {
2520 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2521 return error;
2522 }
2523
2524 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2525 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2526 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2527
2528 return error;
2529}
2530
Manoj Naik6b97fd32006-06-09 09:34:29 -04002531/*
2532 * Get locations and (maybe) other attributes of a referral.
2533 * Note that we'll actually follow the referral later when
2534 * we detect fsid mismatch in inode revalidation
2535 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002536static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2537 const struct qstr *name, struct nfs_fattr *fattr,
2538 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002539{
2540 int status = -ENOMEM;
2541 struct page *page = NULL;
2542 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002543
2544 page = alloc_page(GFP_KERNEL);
2545 if (page == NULL)
2546 goto out;
2547 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2548 if (locations == NULL)
2549 goto out;
2550
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002551 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002552 if (status != 0)
2553 goto out;
2554 /* Make sure server returned a different fsid for the referral */
2555 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002556 dprintk("%s: server did not return a different fsid for"
2557 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002558 status = -EIO;
2559 goto out;
2560 }
Andy Adamson533eb462011-06-13 18:25:56 -04002561 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2562 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002563
Andy Adamson533eb462011-06-13 18:25:56 -04002564 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002565 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002566 memset(fhandle, 0, sizeof(struct nfs_fh));
2567out:
2568 if (page)
2569 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002570 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002571 return status;
2572}
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2575{
2576 struct nfs4_getattr_arg args = {
2577 .fh = fhandle,
2578 .bitmask = server->attr_bitmask,
2579 };
2580 struct nfs4_getattr_res res = {
2581 .fattr = fattr,
2582 .server = server,
2583 };
2584 struct rpc_message msg = {
2585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2586 .rpc_argp = &args,
2587 .rpc_resp = &res,
2588 };
2589
Trond Myklebust0e574af2005-10-27 22:12:38 -04002590 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002591 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
2594static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2595{
2596 struct nfs4_exception exception = { };
2597 int err;
2598 do {
2599 err = nfs4_handle_exception(server,
2600 _nfs4_proc_getattr(server, fhandle, fattr),
2601 &exception);
2602 } while (exception.retry);
2603 return err;
2604}
2605
2606/*
2607 * The file is not closed if it is opened due to the a request to change
2608 * the size of the file. The open call will not be needed once the
2609 * VFS layer lookup-intents are implemented.
2610 *
2611 * Close is called when the inode is destroyed.
2612 * If we haven't opened the file for O_WRONLY, we
2613 * need to in the size_change case to obtain a stateid.
2614 *
2615 * Got race?
2616 * Because OPEN is always done by name in nfsv4, it is
2617 * possible that we opened a different file by the same
2618 * name. We can recognize this race condition, but we
2619 * can't do anything about it besides returning an error.
2620 *
2621 * This will be fixed with VFS changes (lookup-intent).
2622 */
2623static int
2624nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2625 struct iattr *sattr)
2626{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002627 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002628 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002629 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 int status;
2631
Benny Halevy8a1636c2010-07-14 15:43:57 -04002632 if (pnfs_ld_layoutret_on_setattr(inode))
2633 pnfs_return_layout(inode);
2634
Trond Myklebust0e574af2005-10-27 22:12:38 -04002635 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Andy Adamson26699402012-05-30 16:12:24 -04002637 /* Deal with open(O_TRUNC) */
2638 if (sattr->ia_valid & ATTR_OPEN)
2639 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2640
2641 /* Optimization: if the end result is no change, don't RPC */
2642 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2643 return 0;
2644
Trond Myklebustd5308382005-11-04 15:33:38 -05002645 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002646 if (sattr->ia_valid & ATTR_FILE) {
2647 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002648
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002649 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002650 if (ctx) {
2651 cred = ctx->cred;
2652 state = ctx->state;
2653 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002654 }
2655
2656 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002657 if (status == 0)
2658 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return status;
2660}
2661
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002662static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2663 const struct qstr *name, struct nfs_fh *fhandle,
2664 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002665{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002666 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002667 int status;
2668 struct nfs4_lookup_arg args = {
2669 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002670 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002671 .name = name,
2672 };
2673 struct nfs4_lookup_res res = {
2674 .server = server,
2675 .fattr = fattr,
2676 .fh = fhandle,
2677 };
2678 struct rpc_message msg = {
2679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2680 .rpc_argp = &args,
2681 .rpc_resp = &res,
2682 };
2683
2684 nfs_fattr_init(fattr);
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002687 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 dprintk("NFS reply lookup: %d\n", status);
2689 return status;
2690}
2691
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002692static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002693{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002694 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002695 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002696 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2697 fattr->nlink = 2;
2698}
2699
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002700static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2701 struct qstr *name, struct nfs_fh *fhandle,
2702 struct nfs_fattr *fattr)
2703{
2704 struct nfs4_exception exception = { };
2705 struct rpc_clnt *client = *clnt;
2706 int err;
2707 do {
2708 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2709 switch (err) {
2710 case -NFS4ERR_BADNAME:
2711 err = -ENOENT;
2712 goto out;
2713 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002714 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002715 goto out;
2716 case -NFS4ERR_WRONGSEC:
2717 err = -EPERM;
2718 if (client != *clnt)
2719 goto out;
2720
2721 client = nfs4_create_sec_client(client, dir, name);
2722 if (IS_ERR(client))
2723 return PTR_ERR(client);
2724
2725 exception.retry = 1;
2726 break;
2727 default:
2728 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2729 }
2730 } while (exception.retry);
2731
2732out:
2733 if (err == 0)
2734 *clnt = client;
2735 else if (client != *clnt)
2736 rpc_shutdown_client(client);
2737
2738 return err;
2739}
2740
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002741static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002742 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002744 int status;
2745 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002746
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002747 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2748 if (client != NFS_CLIENT(dir)) {
2749 rpc_shutdown_client(client);
2750 nfs_fixup_secinfo_attributes(fattr);
2751 }
2752 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002755struct rpc_clnt *
2756nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2757 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2758{
2759 int status;
2760 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2761
2762 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2763 if (status < 0) {
2764 rpc_shutdown_client(client);
2765 return ERR_PTR(status);
2766 }
2767 return client;
2768}
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2771{
Trond Myklebust76b32992007-08-10 17:45:11 -04002772 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct nfs4_accessargs args = {
2774 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002775 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002777 struct nfs4_accessres res = {
2778 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002779 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct rpc_message msg = {
2781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2782 .rpc_argp = &args,
2783 .rpc_resp = &res,
2784 .rpc_cred = entry->cred,
2785 };
2786 int mode = entry->mask;
2787 int status;
2788
2789 /*
2790 * Determine which access bits we want to ask for...
2791 */
2792 if (mode & MAY_READ)
2793 args.access |= NFS4_ACCESS_READ;
2794 if (S_ISDIR(inode->i_mode)) {
2795 if (mode & MAY_WRITE)
2796 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2797 if (mode & MAY_EXEC)
2798 args.access |= NFS4_ACCESS_LOOKUP;
2799 } else {
2800 if (mode & MAY_WRITE)
2801 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2802 if (mode & MAY_EXEC)
2803 args.access |= NFS4_ACCESS_EXECUTE;
2804 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002805
2806 res.fattr = nfs_alloc_fattr();
2807 if (res.fattr == NULL)
2808 return -ENOMEM;
2809
Bryan Schumaker7c513052011-03-24 17:12:24 +00002810 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002812 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002813 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002815 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return status;
2817}
2818
2819static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2820{
2821 struct nfs4_exception exception = { };
2822 int err;
2823 do {
2824 err = nfs4_handle_exception(NFS_SERVER(inode),
2825 _nfs4_proc_access(inode, entry),
2826 &exception);
2827 } while (exception.retry);
2828 return err;
2829}
2830
2831/*
2832 * TODO: For the time being, we don't try to get any attributes
2833 * along with any of the zero-copy operations READ, READDIR,
2834 * READLINK, WRITE.
2835 *
2836 * In the case of the first three, we want to put the GETATTR
2837 * after the read-type operation -- this is because it is hard
2838 * to predict the length of a GETATTR response in v4, and thus
2839 * align the READ data correctly. This means that the GETATTR
2840 * may end up partially falling into the page cache, and we should
2841 * shift it into the 'tail' of the xdr_buf before processing.
2842 * To do this efficiently, we need to know the total length
2843 * of data received, which doesn't seem to be available outside
2844 * of the RPC layer.
2845 *
2846 * In the case of WRITE, we also want to put the GETATTR after
2847 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002848 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 *
2850 * Both of these changes to the XDR layer would in fact be quite
2851 * minor, but I decided to leave them for a subsequent patch.
2852 */
2853static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2854 unsigned int pgbase, unsigned int pglen)
2855{
2856 struct nfs4_readlink args = {
2857 .fh = NFS_FH(inode),
2858 .pgbase = pgbase,
2859 .pglen = pglen,
2860 .pages = &page,
2861 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002862 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 struct rpc_message msg = {
2864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2865 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002866 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 };
2868
Bryan Schumaker7c513052011-03-24 17:12:24 +00002869 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 -07002870}
2871
2872static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2873 unsigned int pgbase, unsigned int pglen)
2874{
2875 struct nfs4_exception exception = { };
2876 int err;
2877 do {
2878 err = nfs4_handle_exception(NFS_SERVER(inode),
2879 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2880 &exception);
2881 } while (exception.retry);
2882 return err;
2883}
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002886 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888static int
2889nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002890 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002892 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 int status = 0;
2895
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002896 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2897 if (IS_ERR(ctx))
2898 return PTR_ERR(ctx);
2899
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002900 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002901 state = nfs4_do_open(dir, dentry, ctx->mode,
2902 flags, sattr, ctx->cred,
2903 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002904 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (IS_ERR(state)) {
2906 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002907 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002909 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002910 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002911 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002913 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return status;
2915}
2916
2917static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2918{
Trond Myklebust16e42952005-10-27 22:12:44 -04002919 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002920 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002922 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002924 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002925 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002926 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2929 .rpc_argp = &args,
2930 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002932 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002933
Bryan Schumaker7c513052011-03-24 17:12:24 +00002934 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002935 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002936 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return status;
2938}
2939
2940static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2941{
2942 struct nfs4_exception exception = { };
2943 int err;
2944 do {
2945 err = nfs4_handle_exception(NFS_SERVER(dir),
2946 _nfs4_proc_remove(dir, name),
2947 &exception);
2948 } while (exception.retry);
2949 return err;
2950}
2951
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002952static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002954 struct nfs_server *server = NFS_SERVER(dir);
2955 struct nfs_removeargs *args = msg->rpc_argp;
2956 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002958 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002960 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002963static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2964{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002965 nfs4_setup_sequence(NFS_SERVER(data->dir),
2966 &data->args.seq_args,
2967 &data->res.seq_res,
2968 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002971static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002973 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2974
Trond Myklebust14516c32010-07-31 14:29:06 -04002975 if (!nfs4_sequence_done(task, &res->seq_res))
2976 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002977 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002978 return 0;
2979 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002980 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Jeff Laytond3d41522010-09-17 17:31:57 -04002983static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2984{
2985 struct nfs_server *server = NFS_SERVER(dir);
2986 struct nfs_renameargs *arg = msg->rpc_argp;
2987 struct nfs_renameres *res = msg->rpc_resp;
2988
2989 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002990 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002991 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002992}
2993
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002994static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2995{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002996 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2997 &data->args.seq_args,
2998 &data->res.seq_res,
2999 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003000}
3001
3002static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3003 struct inode *new_dir)
3004{
3005 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3006
3007 if (!nfs4_sequence_done(task, &res->seq_res))
3008 return 0;
3009 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3010 return 0;
3011
3012 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003013 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003014 return 1;
3015}
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3018 struct inode *new_dir, struct qstr *new_name)
3019{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003020 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003021 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 .old_dir = NFS_FH(old_dir),
3023 .new_dir = NFS_FH(new_dir),
3024 .old_name = old_name,
3025 .new_name = new_name,
3026 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003027 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003028 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct rpc_message msg = {
3031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3032 .rpc_argp = &arg,
3033 .rpc_resp = &res,
3034 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003035 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Bryan Schumaker7c513052011-03-24 17:12:24 +00003037 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (!status) {
3039 update_changeattr(old_dir, &res.old_cinfo);
3040 update_changeattr(new_dir, &res.new_cinfo);
3041 }
3042 return status;
3043}
3044
3045static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3046 struct inode *new_dir, struct qstr *new_name)
3047{
3048 struct nfs4_exception exception = { };
3049 int err;
3050 do {
3051 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3052 _nfs4_proc_rename(old_dir, old_name,
3053 new_dir, new_name),
3054 &exception);
3055 } while (exception.retry);
3056 return err;
3057}
3058
3059static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3060{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003061 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct nfs4_link_arg arg = {
3063 .fh = NFS_FH(inode),
3064 .dir_fh = NFS_FH(dir),
3065 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003066 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003068 struct nfs4_link_res res = {
3069 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003070 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 struct rpc_message msg = {
3072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3073 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003074 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003076 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Trond Myklebust136f2622010-04-16 16:22:49 -04003078 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003079 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003080 goto out;
3081
Bryan Schumaker7c513052011-03-24 17:12:24 +00003082 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003083 if (!status) {
3084 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003085 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003086 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003087out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003088 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return status;
3090}
3091
3092static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3093{
3094 struct nfs4_exception exception = { };
3095 int err;
3096 do {
3097 err = nfs4_handle_exception(NFS_SERVER(inode),
3098 _nfs4_proc_link(inode, dir, name),
3099 &exception);
3100 } while (exception.retry);
3101 return err;
3102}
3103
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003104struct nfs4_createdata {
3105 struct rpc_message msg;
3106 struct nfs4_create_arg arg;
3107 struct nfs4_create_res res;
3108 struct nfs_fh fh;
3109 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110};
3111
3112static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3113 struct qstr *name, struct iattr *sattr, u32 ftype)
3114{
3115 struct nfs4_createdata *data;
3116
3117 data = kzalloc(sizeof(*data), GFP_KERNEL);
3118 if (data != NULL) {
3119 struct nfs_server *server = NFS_SERVER(dir);
3120
3121 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3122 data->msg.rpc_argp = &data->arg;
3123 data->msg.rpc_resp = &data->res;
3124 data->arg.dir_fh = NFS_FH(dir);
3125 data->arg.server = server;
3126 data->arg.name = name;
3127 data->arg.attrs = sattr;
3128 data->arg.ftype = ftype;
3129 data->arg.bitmask = server->attr_bitmask;
3130 data->res.server = server;
3131 data->res.fh = &data->fh;
3132 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003133 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003134 }
3135 return data;
3136}
3137
3138static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3139{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003140 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003141 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003142 if (status == 0) {
3143 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003144 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3145 }
3146 return status;
3147}
3148
3149static void nfs4_free_createdata(struct nfs4_createdata *data)
3150{
3151 kfree(data);
3152}
3153
Chuck Lever4f390c12006-08-22 20:06:22 -04003154static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003155 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003157 struct nfs4_createdata *data;
3158 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Chuck Lever94a6d752006-08-22 20:06:23 -04003160 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003161 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003162
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003163 status = -ENOMEM;
3164 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3165 if (data == NULL)
3166 goto out;
3167
3168 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3169 data->arg.u.symlink.pages = &page;
3170 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003172 status = nfs4_do_create(dir, dentry, data);
3173
3174 nfs4_free_createdata(data);
3175out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 return status;
3177}
3178
Chuck Lever4f390c12006-08-22 20:06:22 -04003179static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003180 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
3182 struct nfs4_exception exception = { };
3183 int err;
3184 do {
3185 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003186 _nfs4_proc_symlink(dir, dentry, page,
3187 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 &exception);
3189 } while (exception.retry);
3190 return err;
3191}
3192
3193static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3194 struct iattr *sattr)
3195{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003196 struct nfs4_createdata *data;
3197 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003199 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3200 if (data == NULL)
3201 goto out;
3202
3203 status = nfs4_do_create(dir, dentry, data);
3204
3205 nfs4_free_createdata(data);
3206out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return status;
3208}
3209
3210static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3211 struct iattr *sattr)
3212{
3213 struct nfs4_exception exception = { };
3214 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003215
3216 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 do {
3218 err = nfs4_handle_exception(NFS_SERVER(dir),
3219 _nfs4_proc_mkdir(dir, dentry, sattr),
3220 &exception);
3221 } while (exception.retry);
3222 return err;
3223}
3224
3225static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003226 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
3228 struct inode *dir = dentry->d_inode;
3229 struct nfs4_readdir_arg args = {
3230 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003231 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 .pgbase = 0,
3233 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003234 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003235 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 };
3237 struct nfs4_readdir_res res;
3238 struct rpc_message msg = {
3239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3240 .rpc_argp = &args,
3241 .rpc_resp = &res,
3242 .rpc_cred = cred,
3243 };
3244 int status;
3245
Harvey Harrison3110ff82008-05-02 13:42:44 -07003246 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003247 dentry->d_parent->d_name.name,
3248 dentry->d_name.name,
3249 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003250 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003252 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 -05003253 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003254 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003255 status += args.pgbase;
3256 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003257
3258 nfs_invalidate_atime(dir);
3259
Harvey Harrison3110ff82008-05-02 13:42:44 -07003260 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return status;
3262}
3263
3264static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003265 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
3267 struct nfs4_exception exception = { };
3268 int err;
3269 do {
3270 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3271 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003272 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 &exception);
3274 } while (exception.retry);
3275 return err;
3276}
3277
3278static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3279 struct iattr *sattr, dev_t rdev)
3280{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003281 struct nfs4_createdata *data;
3282 int mode = sattr->ia_mode;
3283 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003285 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3286 if (data == NULL)
3287 goto out;
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003290 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003292 data->arg.ftype = NF4BLK;
3293 data->arg.u.device.specdata1 = MAJOR(rdev);
3294 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
3296 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003297 data->arg.ftype = NF4CHR;
3298 data->arg.u.device.specdata1 = MAJOR(rdev);
3299 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003300 } else if (!S_ISSOCK(mode)) {
3301 status = -EINVAL;
3302 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003305 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003306out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003307 nfs4_free_createdata(data);
3308out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return status;
3310}
3311
3312static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3313 struct iattr *sattr, dev_t rdev)
3314{
3315 struct nfs4_exception exception = { };
3316 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003317
3318 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 do {
3320 err = nfs4_handle_exception(NFS_SERVER(dir),
3321 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3322 &exception);
3323 } while (exception.retry);
3324 return err;
3325}
3326
3327static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3328 struct nfs_fsstat *fsstat)
3329{
3330 struct nfs4_statfs_arg args = {
3331 .fh = fhandle,
3332 .bitmask = server->attr_bitmask,
3333 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003334 struct nfs4_statfs_res res = {
3335 .fsstat = fsstat,
3336 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct rpc_message msg = {
3338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3339 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003340 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 };
3342
Trond Myklebust0e574af2005-10-27 22:12:38 -04003343 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003344 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
3347static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3348{
3349 struct nfs4_exception exception = { };
3350 int err;
3351 do {
3352 err = nfs4_handle_exception(server,
3353 _nfs4_proc_statfs(server, fhandle, fsstat),
3354 &exception);
3355 } while (exception.retry);
3356 return err;
3357}
3358
3359static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3360 struct nfs_fsinfo *fsinfo)
3361{
3362 struct nfs4_fsinfo_arg args = {
3363 .fh = fhandle,
3364 .bitmask = server->attr_bitmask,
3365 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003366 struct nfs4_fsinfo_res res = {
3367 .fsinfo = fsinfo,
3368 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 struct rpc_message msg = {
3370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3371 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003372 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 };
3374
Bryan Schumaker7c513052011-03-24 17:12:24 +00003375 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
3378static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3379{
3380 struct nfs4_exception exception = { };
3381 int err;
3382
3383 do {
3384 err = nfs4_handle_exception(server,
3385 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3386 &exception);
3387 } while (exception.retry);
3388 return err;
3389}
3390
3391static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3392{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003393 int error;
3394
Trond Myklebust0e574af2005-10-27 22:12:38 -04003395 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003396 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003397 if (error == 0) {
3398 /* block layout checks this! */
3399 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003400 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003401 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003402
3403 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
3406static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3407 struct nfs_pathconf *pathconf)
3408{
3409 struct nfs4_pathconf_arg args = {
3410 .fh = fhandle,
3411 .bitmask = server->attr_bitmask,
3412 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003413 struct nfs4_pathconf_res res = {
3414 .pathconf = pathconf,
3415 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 struct rpc_message msg = {
3417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3418 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003419 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 };
3421
3422 /* None of the pathconf attributes are mandatory to implement */
3423 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3424 memset(pathconf, 0, sizeof(*pathconf));
3425 return 0;
3426 }
3427
Trond Myklebust0e574af2005-10-27 22:12:38 -04003428 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003429 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
3432static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3433 struct nfs_pathconf *pathconf)
3434{
3435 struct nfs4_exception exception = { };
3436 int err;
3437
3438 do {
3439 err = nfs4_handle_exception(server,
3440 _nfs4_proc_pathconf(server, fhandle, pathconf),
3441 &exception);
3442 } while (exception.retry);
3443 return err;
3444}
3445
Benny Halevyd20581a2011-05-22 19:52:03 +03003446void __nfs4_read_done_cb(struct nfs_read_data *data)
3447{
Fred Isamancd841602012-04-20 14:47:44 -04003448 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003449}
3450
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003451static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Fred Isamancd841602012-04-20 14:47:44 -04003453 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003455 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003456 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003457 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003459
Benny Halevyd20581a2011-05-22 19:52:03 +03003460 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003462 renew_lease(server, data->timestamp);
3463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
3465
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003466static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3467{
3468
3469 dprintk("--> %s\n", __func__);
3470
3471 if (!nfs4_sequence_done(task, &data->res.seq_res))
3472 return -EAGAIN;
3473
Benny Halevyd20581a2011-05-22 19:52:03 +03003474 return data->read_done_cb ? data->read_done_cb(task, data) :
3475 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003476}
3477
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003478static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003481 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003482 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003483 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003486static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3487{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003488 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3489 &data->args.seq_args,
3490 &data->res.seq_res,
3491 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Fred Isamanb029bc92011-03-03 15:13:42 +00003494static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Fred Isamancd841602012-04-20 14:47:44 -04003496 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003498 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003499 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003500 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003502 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003504 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003505 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
Fred Isamanb029bc92011-03-03 15:13:42 +00003509static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3510{
3511 if (!nfs4_sequence_done(task, &data->res.seq_res))
3512 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003513 return data->write_done_cb ? data->write_done_cb(task, data) :
3514 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003515}
3516
Trond Myklebust5a37f852012-04-28 14:55:16 -04003517static
3518bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003519{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003520 const struct nfs_pgio_header *hdr = data->header;
3521
3522 /* Don't request attributes for pNFS or O_DIRECT writes */
3523 if (data->ds_clp != NULL || hdr->dreq != NULL)
3524 return false;
3525 /* Otherwise, request attributes if and only if we don't hold
3526 * a delegation
3527 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003528 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003529}
Fred Isamana69aef12011-03-03 15:13:47 +00003530
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003531static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Fred Isamancd841602012-04-20 14:47:44 -04003533 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003534
Trond Myklebust5a37f852012-04-28 14:55:16 -04003535 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003536 data->args.bitmask = NULL;
3537 data->res.fattr = NULL;
3538 } else
3539 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003540
Fred Isamanb029bc92011-03-03 15:13:42 +00003541 if (!data->write_done_cb)
3542 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003543 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 data->timestamp = jiffies;
3545
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003546 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003547 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003550static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3551{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003552 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3553 &data->args.seq_args,
3554 &data->res.seq_res,
3555 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
3557
Fred Isaman0b7c0152012-04-20 14:47:39 -04003558static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3559{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003560 nfs4_setup_sequence(NFS_SERVER(data->inode),
3561 &data->args.seq_args,
3562 &data->res.seq_res,
3563 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Fred Isaman0b7c0152012-04-20 14:47:39 -04003566static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003569
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003570 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003571 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003572 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
Fred Isaman0b7c0152012-04-20 14:47:39 -04003577static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003578{
3579 if (!nfs4_sequence_done(task, &data->res.seq_res))
3580 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003581 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003582}
3583
Fred Isaman0b7c0152012-04-20 14:47:39 -04003584static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003586 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003587
Fred Isaman0b7c0152012-04-20 14:47:39 -04003588 if (data->commit_done_cb == NULL)
3589 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003590 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003591 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003592 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003595struct nfs4_renewdata {
3596 struct nfs_client *client;
3597 unsigned long timestamp;
3598};
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600/*
3601 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3602 * standalone procedure for queueing an asynchronous RENEW.
3603 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003604static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003605{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003606 struct nfs4_renewdata *data = calldata;
3607 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003608
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003609 if (atomic_read(&clp->cl_count) > 1)
3610 nfs4_schedule_state_renewal(clp);
3611 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003612 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003613}
3614
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003615static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003617 struct nfs4_renewdata *data = calldata;
3618 struct nfs_client *clp = data->client;
3619 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
3621 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003622 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003623 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3624 return;
3625 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003626 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003627 return;
3628 }
3629 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003631 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003634static const struct rpc_call_ops nfs4_renew_ops = {
3635 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003636 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003637};
3638
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003639static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
3641 struct rpc_message msg = {
3642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3643 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003644 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003646 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003648 if (renew_flags == 0)
3649 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003650 if (!atomic_inc_not_zero(&clp->cl_count))
3651 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003652 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003653 if (data == NULL)
3654 return -ENOMEM;
3655 data->client = clp;
3656 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003658 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003661static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
3663 struct rpc_message msg = {
3664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3665 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003666 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 };
3668 unsigned long now = jiffies;
3669 int status;
3670
3671 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3672 if (status < 0)
3673 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003674 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return 0;
3676}
3677
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003678static inline int nfs4_server_supports_acls(struct nfs_server *server)
3679{
3680 return (server->caps & NFS_CAP_ACLS)
3681 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3682 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3683}
3684
Trond Myklebust21f498c2012-08-24 10:59:25 -04003685/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3686 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003687 * the stack.
3688 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003689#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003690
Neil Hormane9e3d722011-03-04 19:26:03 -05003691static int buf_to_pages_noslab(const void *buf, size_t buflen,
3692 struct page **pages, unsigned int *pgbase)
3693{
3694 struct page *newpage, **spages;
3695 int rc = 0;
3696 size_t len;
3697 spages = pages;
3698
3699 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003700 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003701 newpage = alloc_page(GFP_KERNEL);
3702
3703 if (newpage == NULL)
3704 goto unwind;
3705 memcpy(page_address(newpage), buf, len);
3706 buf += len;
3707 buflen -= len;
3708 *pages++ = newpage;
3709 rc++;
3710 } while (buflen != 0);
3711
3712 return rc;
3713
3714unwind:
3715 for(; rc > 0; rc--)
3716 __free_page(spages[rc-1]);
3717 return -ENOMEM;
3718}
3719
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003720struct nfs4_cached_acl {
3721 int cached;
3722 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003723 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003724};
3725
3726static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003727{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003728 struct nfs_inode *nfsi = NFS_I(inode);
3729
3730 spin_lock(&inode->i_lock);
3731 kfree(nfsi->nfs4_acl);
3732 nfsi->nfs4_acl = acl;
3733 spin_unlock(&inode->i_lock);
3734}
3735
3736static void nfs4_zap_acl_attr(struct inode *inode)
3737{
3738 nfs4_set_cached_acl(inode, NULL);
3739}
3740
3741static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3742{
3743 struct nfs_inode *nfsi = NFS_I(inode);
3744 struct nfs4_cached_acl *acl;
3745 int ret = -ENOENT;
3746
3747 spin_lock(&inode->i_lock);
3748 acl = nfsi->nfs4_acl;
3749 if (acl == NULL)
3750 goto out;
3751 if (buf == NULL) /* user is just asking for length */
3752 goto out_len;
3753 if (acl->cached == 0)
3754 goto out;
3755 ret = -ERANGE; /* see getxattr(2) man page */
3756 if (acl->len > buflen)
3757 goto out;
3758 memcpy(buf, acl->data, acl->len);
3759out_len:
3760 ret = acl->len;
3761out:
3762 spin_unlock(&inode->i_lock);
3763 return ret;
3764}
3765
Sachin Prabhu5794d212012-04-17 14:36:40 +01003766static 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 +00003767{
3768 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003769 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003771 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003772 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 if (acl == NULL)
3774 goto out;
3775 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003776 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003777 } else {
3778 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3779 if (acl == NULL)
3780 goto out;
3781 acl->cached = 0;
3782 }
3783 acl->len = acl_len;
3784out:
3785 nfs4_set_cached_acl(inode, acl);
3786}
3787
Andy Adamsonbf118a32011-12-07 11:55:27 -05003788/*
3789 * The getxattr API returns the required buffer length when called with a
3790 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3791 * the required buf. On a NULL buf, we send a page of data to the server
3792 * guessing that the ACL request can be serviced by a page. If so, we cache
3793 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3794 * the cache. If not so, we throw away the page, and cache the required
3795 * length. The next getxattr call will then produce another round trip to
3796 * the server, this time with the input buf of the required size.
3797 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003798static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003799{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003800 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003801 struct nfs_getaclargs args = {
3802 .fh = NFS_FH(inode),
3803 .acl_pages = pages,
3804 .acl_len = buflen,
3805 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003806 struct nfs_getaclres res = {
3807 .acl_len = buflen,
3808 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003809 struct rpc_message msg = {
3810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3811 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003812 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003813 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003814 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3815 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816
Andy Adamsonbf118a32011-12-07 11:55:27 -05003817 /* As long as we're doing a round trip to the server anyway,
3818 * let's be prepared for a page of acl data. */
3819 if (npages == 0)
3820 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003821 if (npages > ARRAY_SIZE(pages))
3822 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003823
Andy Adamsonbf118a32011-12-07 11:55:27 -05003824 for (i = 0; i < npages; i++) {
3825 pages[i] = alloc_page(GFP_KERNEL);
3826 if (!pages[i])
3827 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003829
3830 /* for decoding across pages */
3831 res.acl_scratch = alloc_page(GFP_KERNEL);
3832 if (!res.acl_scratch)
3833 goto out_free;
3834
Andy Adamsonbf118a32011-12-07 11:55:27 -05003835 args.acl_len = npages * PAGE_SIZE;
3836 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003837
Peng Taode040be2012-01-10 22:42:47 +08003838 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003839 __func__, buf, buflen, npages, args.acl_len);
3840 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3841 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003842 if (ret)
3843 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003844
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003845 /* Handle the case where the passed-in buffer is too short */
3846 if (res.acl_flags & NFS4_ACL_TRUNC) {
3847 /* Did the user only issue a request for the acl length? */
3848 if (buf == NULL)
3849 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003850 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003851 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003852 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003853 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003854 if (buf) {
3855 if (res.acl_len > buflen) {
3856 ret = -ERANGE;
3857 goto out_free;
3858 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003859 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003860 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003861out_ok:
3862 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003863out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003864 for (i = 0; i < npages; i++)
3865 if (pages[i])
3866 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003867 if (res.acl_scratch)
3868 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003869 return ret;
3870}
3871
Trond Myklebust16b42892006-08-24 12:27:15 -04003872static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3873{
3874 struct nfs4_exception exception = { };
3875 ssize_t ret;
3876 do {
3877 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3878 if (ret >= 0)
3879 break;
3880 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3881 } while (exception.retry);
3882 return ret;
3883}
3884
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003885static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3886{
3887 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003888 int ret;
3889
3890 if (!nfs4_server_supports_acls(server))
3891 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003892 ret = nfs_revalidate_inode(server, inode);
3893 if (ret < 0)
3894 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003895 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3896 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003897 ret = nfs4_read_cached_acl(inode, buf, buflen);
3898 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003899 /* -ENOENT is returned if there is no ACL or if there is an ACL
3900 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003901 return ret;
3902 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003903}
3904
Trond Myklebust16b42892006-08-24 12:27:15 -04003905static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003906{
3907 struct nfs_server *server = NFS_SERVER(inode);
3908 struct page *pages[NFS4ACL_MAXPAGES];
3909 struct nfs_setaclargs arg = {
3910 .fh = NFS_FH(inode),
3911 .acl_pages = pages,
3912 .acl_len = buflen,
3913 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003914 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003915 struct rpc_message msg = {
3916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3917 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003918 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003919 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003920 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003921 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003922
3923 if (!nfs4_server_supports_acls(server))
3924 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003925 if (npages > ARRAY_SIZE(pages))
3926 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003927 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3928 if (i < 0)
3929 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003930 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003931 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003932
3933 /*
3934 * Free each page after tx, so the only ref left is
3935 * held by the network stack
3936 */
3937 for (; i > 0; i--)
3938 put_page(pages[i-1]);
3939
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003940 /*
3941 * Acl update can result in inode attribute update.
3942 * so mark the attribute cache invalid.
3943 */
3944 spin_lock(&inode->i_lock);
3945 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3946 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003947 nfs_access_zap_cache(inode);
3948 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003949 return ret;
3950}
3951
Trond Myklebust16b42892006-08-24 12:27:15 -04003952static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3953{
3954 struct nfs4_exception exception = { };
3955 int err;
3956 do {
3957 err = nfs4_handle_exception(NFS_SERVER(inode),
3958 __nfs4_proc_set_acl(inode, buf, buflen),
3959 &exception);
3960 } while (exception.retry);
3961 return err;
3962}
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003965nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003967 struct nfs_client *clp = server->nfs_client;
3968
3969 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 return 0;
3971 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003972 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003973 case -NFS4ERR_ADMIN_REVOKED:
3974 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003975 if (state == NULL)
3976 break;
3977 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003978 case -NFS4ERR_OPENMODE:
3979 if (state == NULL)
3980 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003981 nfs4_schedule_stateid_recovery(server, state);
3982 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003983 case -NFS4ERR_EXPIRED:
3984 if (state != NULL)
3985 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003987 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003988 nfs4_schedule_lease_recovery(clp);
3989 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003990#if defined(CONFIG_NFS_V4_1)
3991 case -NFS4ERR_BADSESSION:
3992 case -NFS4ERR_BADSLOT:
3993 case -NFS4ERR_BAD_HIGH_SLOT:
3994 case -NFS4ERR_DEADSESSION:
3995 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3996 case -NFS4ERR_SEQ_FALSE_RETRY:
3997 case -NFS4ERR_SEQ_MISORDERED:
3998 dprintk("%s ERROR %d, Reset session\n", __func__,
3999 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004000 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004001 task->tk_status = 0;
4002 return -EAGAIN;
4003#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004005 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004006 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4008 task->tk_status = 0;
4009 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004010 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 case -NFS4ERR_OLD_STATEID:
4012 task->tk_status = 0;
4013 return -EAGAIN;
4014 }
4015 task->tk_status = nfs4_map_errors(task->tk_status);
4016 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004017wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004018 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004019 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4020 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4021 task->tk_status = 0;
4022 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023}
4024
Chuck Leverf0920752012-05-21 22:45:41 -04004025static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4026 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004027{
4028 __be32 verf[2];
4029
Chuck Lever2c820d92012-05-21 22:45:33 -04004030 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4031 /* An impossible timestamp guarantees this value
4032 * will never match a generated boot time. */
4033 verf[0] = 0;
4034 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4035 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004036 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4037 verf[0] = (__be32)nn->boot_time.tv_sec;
4038 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004039 }
Chuck Levercd937102012-03-02 17:14:31 -05004040 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4041}
4042
Chuck Levere984a552012-09-14 17:24:21 -04004043static unsigned int
4044nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4045 char *buf, size_t len)
4046{
4047 unsigned int result;
4048
4049 rcu_read_lock();
4050 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4051 clp->cl_ipaddr,
4052 rpc_peeraddr2str(clp->cl_rpcclient,
4053 RPC_DISPLAY_ADDR),
4054 rpc_peeraddr2str(clp->cl_rpcclient,
4055 RPC_DISPLAY_PROTO));
4056 rcu_read_unlock();
4057 return result;
4058}
4059
4060static unsigned int
4061nfs4_init_uniform_client_string(const struct nfs_client *clp,
4062 char *buf, size_t len)
4063{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004064 char *nodename = clp->cl_rpcclient->cl_nodename;
4065
4066 if (nfs4_client_id_uniquifier[0] != '\0')
4067 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004068 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4069 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004070 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004071}
4072
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004073/**
4074 * nfs4_proc_setclientid - Negotiate client ID
4075 * @clp: state data structure
4076 * @program: RPC program for NFSv4 callback service
4077 * @port: IP port number for NFS4 callback service
4078 * @cred: RPC credential to use for this call
4079 * @res: where to place the result
4080 *
4081 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4082 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004083int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4084 unsigned short port, struct rpc_cred *cred,
4085 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086{
4087 nfs4_verifier sc_verifier;
4088 struct nfs4_setclientid setclientid = {
4089 .sc_verifier = &sc_verifier,
4090 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004091 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 };
4093 struct rpc_message msg = {
4094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4095 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004096 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004097 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004099 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Chuck Leverde734832012-07-11 16:30:50 -04004101 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004102 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004103 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4104 setclientid.sc_name_len =
4105 nfs4_init_uniform_client_string(clp,
4106 setclientid.sc_name,
4107 sizeof(setclientid.sc_name));
4108 else
4109 setclientid.sc_name_len =
4110 nfs4_init_nonuniform_client_string(clp,
4111 setclientid.sc_name,
4112 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004113 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004114 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004115 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004116 sizeof(setclientid.sc_netid),
4117 rpc_peeraddr2str(clp->cl_rpcclient,
4118 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004119 rcu_read_unlock();
4120 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004121 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 clp->cl_ipaddr, port >> 8, port & 255);
4123
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004124 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4125 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4126 setclientid.sc_name_len, setclientid.sc_name);
4127 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4128 dprintk("NFS reply setclientid: %d\n", status);
4129 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004132/**
4133 * nfs4_proc_setclientid_confirm - Confirm client ID
4134 * @clp: state data structure
4135 * @res: result of a previous SETCLIENTID
4136 * @cred: RPC credential to use for this call
4137 *
4138 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4139 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004140int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004141 struct nfs4_setclientid_res *arg,
4142 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
4144 struct nfs_fsinfo fsinfo;
4145 struct rpc_message msg = {
4146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004147 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004149 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 };
4151 unsigned long now;
4152 int status;
4153
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004154 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4155 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4156 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004158 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 if (status == 0) {
4160 spin_lock(&clp->cl_lock);
4161 clp->cl_lease_time = fsinfo.lease_time * HZ;
4162 clp->cl_last_renewal = now;
4163 spin_unlock(&clp->cl_lock);
4164 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004165 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 return status;
4167}
4168
Trond Myklebustfe650402006-01-03 09:55:18 +01004169struct nfs4_delegreturndata {
4170 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004171 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004172 struct nfs_fh fh;
4173 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004174 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004175 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004176 int rpc_status;
4177};
4178
Trond Myklebustfe650402006-01-03 09:55:18 +01004179static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4180{
4181 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004182
Trond Myklebust14516c32010-07-31 14:29:06 -04004183 if (!nfs4_sequence_done(task, &data->res.seq_res))
4184 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004185
Ricardo Labiaga79708862009-12-07 09:23:21 -05004186 switch (task->tk_status) {
4187 case -NFS4ERR_STALE_STATEID:
4188 case -NFS4ERR_EXPIRED:
4189 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004190 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004191 break;
4192 default:
4193 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4194 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004195 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004196 return;
4197 }
4198 }
4199 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004200}
4201
4202static void nfs4_delegreturn_release(void *calldata)
4203{
Trond Myklebustfe650402006-01-03 09:55:18 +01004204 kfree(calldata);
4205}
4206
Andy Adamson938e1012009-04-01 09:22:28 -04004207#if defined(CONFIG_NFS_V4_1)
4208static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4209{
4210 struct nfs4_delegreturndata *d_data;
4211
4212 d_data = (struct nfs4_delegreturndata *)data;
4213
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004214 nfs4_setup_sequence(d_data->res.server,
4215 &d_data->args.seq_args,
4216 &d_data->res.seq_res,
4217 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004218}
4219#endif /* CONFIG_NFS_V4_1 */
4220
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004221static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004222#if defined(CONFIG_NFS_V4_1)
4223 .rpc_call_prepare = nfs4_delegreturn_prepare,
4224#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004225 .rpc_call_done = nfs4_delegreturn_done,
4226 .rpc_release = nfs4_delegreturn_release,
4227};
4228
Trond Myklebuste6f81072008-01-24 18:14:34 -05004229static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004230{
4231 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004232 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004233 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004234 struct rpc_message msg = {
4235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4236 .rpc_cred = cred,
4237 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004238 struct rpc_task_setup task_setup_data = {
4239 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004240 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004241 .callback_ops = &nfs4_delegreturn_ops,
4242 .flags = RPC_TASK_ASYNC,
4243 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004244 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004245
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004246 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004247 if (data == NULL)
4248 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004249 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004250 data->args.fhandle = &data->fh;
4251 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004252 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004253 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004254 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004255 data->res.fattr = &data->fattr;
4256 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004257 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004258 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004259 data->rpc_status = 0;
4260
Trond Myklebustc970aa82007-07-14 15:39:59 -04004261 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004262 msg.rpc_argp = &data->args;
4263 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004264 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004265 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004266 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004267 if (!issync)
4268 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004269 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004270 if (status != 0)
4271 goto out;
4272 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004273 if (status == 0)
4274 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4275 else
4276 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004277out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004278 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004279 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
Trond Myklebuste6f81072008-01-24 18:14:34 -05004282int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
4284 struct nfs_server *server = NFS_SERVER(inode);
4285 struct nfs4_exception exception = { };
4286 int err;
4287 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004288 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 switch (err) {
4290 case -NFS4ERR_STALE_STATEID:
4291 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 case 0:
4293 return 0;
4294 }
4295 err = nfs4_handle_exception(server, err, &exception);
4296 } while (exception.retry);
4297 return err;
4298}
4299
4300#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4301#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4302
4303/*
4304 * sleep, with exponential backoff, and retry the LOCK operation.
4305 */
4306static unsigned long
4307nfs4_set_lock_task_retry(unsigned long timeout)
4308{
Jeff Laytond3103102011-12-01 22:44:39 +01004309 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 timeout <<= 1;
4311 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4312 return NFS4_LOCK_MAXTIMEOUT;
4313 return timeout;
4314}
4315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4317{
4318 struct inode *inode = state->inode;
4319 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004320 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004321 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004323 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004325 struct nfs_lockt_res res = {
4326 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 };
4328 struct rpc_message msg = {
4329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4330 .rpc_argp = &arg,
4331 .rpc_resp = &res,
4332 .rpc_cred = state->owner->so_cred,
4333 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 struct nfs4_lock_state *lsp;
4335 int status;
4336
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004337 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004338 status = nfs4_set_lock_state(state, request);
4339 if (status != 0)
4340 goto out;
4341 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004342 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004343 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004344 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004345 switch (status) {
4346 case 0:
4347 request->fl_type = F_UNLCK;
4348 break;
4349 case -NFS4ERR_DENIED:
4350 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004352 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004353out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 return status;
4355}
4356
4357static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4358{
4359 struct nfs4_exception exception = { };
4360 int err;
4361
4362 do {
4363 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4364 _nfs4_proc_getlk(state, cmd, request),
4365 &exception);
4366 } while (exception.retry);
4367 return err;
4368}
4369
4370static int do_vfs_lock(struct file *file, struct file_lock *fl)
4371{
4372 int res = 0;
4373 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4374 case FL_POSIX:
4375 res = posix_lock_file_wait(file, fl);
4376 break;
4377 case FL_FLOCK:
4378 res = flock_lock_file_wait(file, fl);
4379 break;
4380 default:
4381 BUG();
4382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return res;
4384}
4385
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004386struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004387 struct nfs_locku_args arg;
4388 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004389 struct nfs4_lock_state *lsp;
4390 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004391 struct file_lock fl;
4392 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004393 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004394};
4395
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004396static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4397 struct nfs_open_context *ctx,
4398 struct nfs4_lock_state *lsp,
4399 struct nfs_seqid *seqid)
4400{
4401 struct nfs4_unlockdata *p;
4402 struct inode *inode = lsp->ls_state->inode;
4403
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004404 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004405 if (p == NULL)
4406 return NULL;
4407 p->arg.fh = NFS_FH(inode);
4408 p->arg.fl = &p->fl;
4409 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004410 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004411 p->arg.stateid = &lsp->ls_stateid;
4412 p->lsp = lsp;
4413 atomic_inc(&lsp->ls_count);
4414 /* Ensure we don't close file until we're done freeing locks! */
4415 p->ctx = get_nfs_open_context(ctx);
4416 memcpy(&p->fl, fl, sizeof(p->fl));
4417 p->server = NFS_SERVER(inode);
4418 return p;
4419}
4420
Trond Myklebust06f814a2006-01-03 09:55:07 +01004421static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004422{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004423 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004424 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004425 nfs4_put_lock_state(calldata->lsp);
4426 put_nfs_open_context(calldata->ctx);
4427 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004428}
4429
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004430static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004431{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004432 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004433
Trond Myklebust14516c32010-07-31 14:29:06 -04004434 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4435 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004436 switch (task->tk_status) {
4437 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004438 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4439 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004440 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004441 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004442 case -NFS4ERR_BAD_STATEID:
4443 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004444 case -NFS4ERR_STALE_STATEID:
4445 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004446 break;
4447 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004448 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004449 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004450 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004451 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004452}
4453
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004454static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004455{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004456 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004458 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004459 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004460 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004461 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004462 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004463 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004464 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004465 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004466 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004467 &calldata->res.seq_res,
4468 task) != 0)
4469 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004470 return;
4471out_no_action:
4472 task->tk_action = NULL;
4473out_wait:
4474 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475}
4476
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004477static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004478 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004479 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004480 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004481};
4482
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004483static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4484 struct nfs_open_context *ctx,
4485 struct nfs4_lock_state *lsp,
4486 struct nfs_seqid *seqid)
4487{
4488 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004489 struct rpc_message msg = {
4490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4491 .rpc_cred = ctx->cred,
4492 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004493 struct rpc_task_setup task_setup_data = {
4494 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004495 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004496 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004497 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004498 .flags = RPC_TASK_ASYNC,
4499 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004500
Frank Filz137d6ac2007-07-09 15:32:29 -07004501 /* Ensure this is an unlock - when canceling a lock, the
4502 * canceled lock is passed in, and it won't be an unlock.
4503 */
4504 fl->fl_type = F_UNLCK;
4505
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004506 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4507 if (data == NULL) {
4508 nfs_free_seqid(seqid);
4509 return ERR_PTR(-ENOMEM);
4510 }
4511
Trond Myklebust9d12b212012-01-17 22:04:25 -05004512 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004513 msg.rpc_argp = &data->arg;
4514 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004515 task_setup_data.callback_data = data;
4516 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517}
4518
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4520{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004521 struct inode *inode = state->inode;
4522 struct nfs4_state_owner *sp = state->owner;
4523 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004524 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004525 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004526 struct rpc_task *task;
4527 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004528 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Trond Myklebust9b073572006-06-29 16:38:34 -04004530 status = nfs4_set_lock_state(state, request);
4531 /* Unlock _before_ we do the RPC call */
4532 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004533 /* Exclude nfs_delegation_claim_locks() */
4534 mutex_lock(&sp->so_delegreturn_mutex);
4535 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004536 down_read(&nfsi->rwsem);
4537 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4538 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004539 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004540 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004541 }
4542 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004543 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004544 if (status != 0)
4545 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004546 /* Is this a delegated lock? */
4547 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004548 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004549 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004550 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004551 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004552 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004553 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004554 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004555 status = PTR_ERR(task);
4556 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004557 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004558 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004559 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004560out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004561 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004562 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563}
4564
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004565struct nfs4_lockdata {
4566 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004567 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568 struct nfs4_lock_state *lsp;
4569 struct nfs_open_context *ctx;
4570 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004571 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004572 int rpc_status;
4573 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004574 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575};
4576
4577static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004578 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4579 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580{
4581 struct nfs4_lockdata *p;
4582 struct inode *inode = lsp->ls_state->inode;
4583 struct nfs_server *server = NFS_SERVER(inode);
4584
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004585 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004586 if (p == NULL)
4587 return NULL;
4588
4589 p->arg.fh = NFS_FH(inode);
4590 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004591 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004592 if (p->arg.open_seqid == NULL)
4593 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004594 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004596 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004598 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004599 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004600 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004601 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004603 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004604 atomic_inc(&lsp->ls_count);
4605 p->ctx = get_nfs_open_context(ctx);
4606 memcpy(&p->fl, fl, sizeof(p->fl));
4607 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004608out_free_seqid:
4609 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610out_free:
4611 kfree(p);
4612 return NULL;
4613}
4614
4615static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4616{
4617 struct nfs4_lockdata *data = calldata;
4618 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
Harvey Harrison3110ff82008-05-02 13:42:44 -07004620 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004621 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004622 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623 /* Do we need to do an open_to_lock_owner? */
4624 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004625 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004626 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004627 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004628 data->arg.open_stateid = &state->stateid;
4629 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004630 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004631 } else
4632 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004633 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004634 if (nfs4_setup_sequence(data->server,
4635 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004636 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004637 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004638 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004639 nfs_release_seqid(data->arg.open_seqid);
4640out_release_lock_seqid:
4641 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004642out_wait:
4643 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004644 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004645}
4646
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004647static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4648{
4649 struct nfs4_lockdata *data = calldata;
4650
Harvey Harrison3110ff82008-05-02 13:42:44 -07004651 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652
Trond Myklebust14516c32010-07-31 14:29:06 -04004653 if (!nfs4_sequence_done(task, &data->res.seq_res))
4654 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004655
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004656 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004658 if (data->rpc_status == 0)
4659 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4660 else
4661 goto out;
4662 }
4663 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004664 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004665 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004666 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004667 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004669 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004670}
4671
4672static void nfs4_lock_release(void *calldata)
4673{
4674 struct nfs4_lockdata *data = calldata;
4675
Harvey Harrison3110ff82008-05-02 13:42:44 -07004676 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004677 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678 if (data->cancelled != 0) {
4679 struct rpc_task *task;
4680 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4681 data->arg.lock_seqid);
4682 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004683 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004684 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004685 } else
4686 nfs_free_seqid(data->arg.lock_seqid);
4687 nfs4_put_lock_state(data->lsp);
4688 put_nfs_open_context(data->ctx);
4689 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004690 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004691}
4692
4693static const struct rpc_call_ops nfs4_lock_ops = {
4694 .rpc_call_prepare = nfs4_lock_prepare,
4695 .rpc_call_done = nfs4_lock_done,
4696 .rpc_release = nfs4_lock_release,
4697};
4698
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004699static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4700{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004701 switch (error) {
4702 case -NFS4ERR_ADMIN_REVOKED:
4703 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004704 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004705 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004706 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004707 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004708 break;
4709 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004710 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004711 case -NFS4ERR_EXPIRED:
4712 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004713 };
4714}
4715
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004716static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717{
4718 struct nfs4_lockdata *data;
4719 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004720 struct rpc_message msg = {
4721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4722 .rpc_cred = state->owner->so_cred,
4723 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004724 struct rpc_task_setup task_setup_data = {
4725 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004726 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004727 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004728 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004729 .flags = RPC_TASK_ASYNC,
4730 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004731 int ret;
4732
Harvey Harrison3110ff82008-05-02 13:42:44 -07004733 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004734 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004735 fl->fl_u.nfs4_fl.owner,
4736 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737 if (data == NULL)
4738 return -ENOMEM;
4739 if (IS_SETLKW(cmd))
4740 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004741 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004742 msg.rpc_argp = &data->arg;
4743 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004744 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004745 if (recovery_type > NFS_LOCK_NEW) {
4746 if (recovery_type == NFS_LOCK_RECLAIM)
4747 data->arg.reclaim = NFS_LOCK_RECLAIM;
4748 nfs4_set_sequence_privileged(&data->arg.seq_args);
4749 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004750 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004751 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004752 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753 ret = nfs4_wait_for_completion_rpc_task(task);
4754 if (ret == 0) {
4755 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004756 if (ret)
4757 nfs4_handle_setlk_error(data->server, data->lsp,
4758 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004759 } else
4760 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004761 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004762 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004763 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
4766static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4767{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004768 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004769 struct nfs4_exception exception = {
4770 .inode = state->inode,
4771 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004772 int err;
4773
4774 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004775 /* Cache the lock if possible... */
4776 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4777 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004778 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004779 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004780 break;
4781 nfs4_handle_exception(server, err, &exception);
4782 } while (exception.retry);
4783 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784}
4785
4786static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4787{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004788 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004789 struct nfs4_exception exception = {
4790 .inode = state->inode,
4791 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004792 int err;
4793
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004794 err = nfs4_set_lock_state(state, request);
4795 if (err != 0)
4796 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004797 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004798 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4799 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004800 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004801 switch (err) {
4802 default:
4803 goto out;
4804 case -NFS4ERR_GRACE:
4805 case -NFS4ERR_DELAY:
4806 nfs4_handle_exception(server, err, &exception);
4807 err = 0;
4808 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004809 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004810out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004811 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812}
4813
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004814#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004815/**
4816 * nfs41_check_expired_locks - possibly free a lock stateid
4817 *
4818 * @state: NFSv4 state for an inode
4819 *
4820 * Returns NFS_OK if recovery for this stateid is now finished.
4821 * Otherwise a negative NFS4ERR value is returned.
4822 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004823static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004824{
Chuck Levereb64cf92012-07-11 16:30:05 -04004825 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004826 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004827 struct nfs_server *server = NFS_SERVER(state->inode);
4828
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004829 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004830 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004831 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4832 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004833 /* Free the stateid unless the server
4834 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004835 if (status != -NFS4ERR_BAD_STATEID)
4836 nfs41_free_stateid(server,
4837 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004838 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004839 ret = status;
4840 }
4841 }
4842 };
4843
4844 return ret;
4845}
4846
4847static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4848{
4849 int status = NFS_OK;
4850
4851 if (test_bit(LK_STATE_IN_USE, &state->flags))
4852 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004853 if (status != NFS_OK)
4854 status = nfs4_lock_expired(state, request);
4855 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004856}
4857#endif
4858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4860{
Trond Myklebust9a99af42013-02-04 20:17:49 -05004861 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004862 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004863 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05004864 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004865 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004867 if ((fl_flags & FL_POSIX) &&
4868 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4869 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004870 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004871 status = nfs4_set_lock_state(state, request);
4872 if (status != 0)
4873 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004874 request->fl_flags |= FL_ACCESS;
4875 status = do_vfs_lock(request->fl_file, request);
4876 if (status < 0)
4877 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004878 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004879 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004880 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004881 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004882 request->fl_flags = fl_flags & ~FL_SLEEP;
4883 status = do_vfs_lock(request->fl_file, request);
4884 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004885 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05004886 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4887 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004888 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004889 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05004890 goto out;
4891 down_read(&nfsi->rwsem);
4892 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4893 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004894 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05004895 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004896 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004897 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004898 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004899 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4900 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004901out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004902 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004903out:
4904 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 return status;
4906}
4907
4908static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4909{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004910 struct nfs4_exception exception = {
4911 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004912 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004913 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 int err;
4915
4916 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004917 err = _nfs4_proc_setlk(state, cmd, request);
4918 if (err == -NFS4ERR_DENIED)
4919 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004921 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 } while (exception.retry);
4923 return err;
4924}
4925
4926static int
4927nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4928{
4929 struct nfs_open_context *ctx;
4930 struct nfs4_state *state;
4931 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4932 int status;
4933
4934 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004935 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 state = ctx->state;
4937
4938 if (request->fl_start < 0 || request->fl_end < 0)
4939 return -EINVAL;
4940
Trond Myklebustd9531262009-07-21 19:22:38 -04004941 if (IS_GETLK(cmd)) {
4942 if (state != NULL)
4943 return nfs4_proc_getlk(state, F_GETLK, request);
4944 return 0;
4945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
4947 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4948 return -EINVAL;
4949
Trond Myklebustd9531262009-07-21 19:22:38 -04004950 if (request->fl_type == F_UNLCK) {
4951 if (state != NULL)
4952 return nfs4_proc_unlck(state, cmd, request);
4953 return 0;
4954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Trond Myklebustd9531262009-07-21 19:22:38 -04004956 if (state == NULL)
4957 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004958 /*
4959 * Don't rely on the VFS having checked the file open mode,
4960 * since it won't do this for flock() locks.
4961 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004962 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004963 case F_RDLCK:
4964 if (!(filp->f_mode & FMODE_READ))
4965 return -EBADF;
4966 break;
4967 case F_WRLCK:
4968 if (!(filp->f_mode & FMODE_WRITE))
4969 return -EBADF;
4970 }
4971
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 do {
4973 status = nfs4_proc_setlk(state, cmd, request);
4974 if ((status != -EAGAIN) || IS_SETLK(cmd))
4975 break;
4976 timeout = nfs4_set_lock_task_retry(timeout);
4977 status = -ERESTARTSYS;
4978 if (signalled())
4979 break;
4980 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 return status;
4982}
4983
Trond Myklebust888e6942005-11-04 15:38:11 -05004984int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4985{
4986 struct nfs_server *server = NFS_SERVER(state->inode);
4987 struct nfs4_exception exception = { };
4988 int err;
4989
4990 err = nfs4_set_lock_state(state, fl);
4991 if (err != 0)
4992 goto out;
4993 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004994 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004995 switch (err) {
4996 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004997 printk(KERN_ERR "NFS: %s: unhandled error "
4998 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004999 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005000 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005001 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005002 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005003 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05005004 set_bit(NFS_DELEGATED_STATE, &state->flags);
5005 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005006 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05005007 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005008 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005009 case -NFS4ERR_BADSESSION:
5010 case -NFS4ERR_BADSLOT:
5011 case -NFS4ERR_BAD_HIGH_SLOT:
5012 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5013 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005014 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005015 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005016 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005017 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005018 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005019 case -NFS4ERR_ADMIN_REVOKED:
5020 case -NFS4ERR_BAD_STATEID:
5021 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005022 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005023 err = 0;
5024 goto out;
5025 case -ENOMEM:
5026 case -NFS4ERR_DENIED:
5027 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5028 err = 0;
5029 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005030 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005031 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005032 err = nfs4_handle_exception(server, err, &exception);
5033 } while (exception.retry);
5034out:
5035 return err;
5036}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005037
Trond Myklebustcf470c32012-03-07 13:49:12 -05005038struct nfs_release_lockowner_data {
5039 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005040 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005041 struct nfs_release_lockowner_args args;
5042};
5043
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005044static void nfs4_release_lockowner_release(void *calldata)
5045{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005046 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005047 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005048 kfree(calldata);
5049}
5050
Trond Myklebust17280172012-03-11 13:11:00 -04005051static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005052 .rpc_release = nfs4_release_lockowner_release,
5053};
5054
Trond Myklebustcf470c32012-03-07 13:49:12 -05005055int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005056{
5057 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005058 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005059 struct rpc_message msg = {
5060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5061 };
5062
5063 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005064 return -EINVAL;
5065 data = kmalloc(sizeof(*data), GFP_NOFS);
5066 if (!data)
5067 return -ENOMEM;
5068 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005069 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005070 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5071 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5072 data->args.lock_owner.s_dev = server->s_dev;
5073 msg.rpc_argp = &data->args;
5074 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5075 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005076}
5077
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005078#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5079
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005080static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5081 const void *buf, size_t buflen,
5082 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005083{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005084 if (strcmp(key, "") != 0)
5085 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005086
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005087 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005088}
5089
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005090static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5091 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005092{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005093 if (strcmp(key, "") != 0)
5094 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005095
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005096 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005097}
5098
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005099static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5100 size_t list_len, const char *name,
5101 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005102{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005103 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005104
J. Bruce Fields096455a2006-03-20 23:23:42 -05005105 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5106 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005107
5108 if (list && len <= list_len)
5109 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005110 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005111}
5112
Andy Adamson533eb462011-06-13 18:25:56 -04005113/*
5114 * nfs_fhget will use either the mounted_on_fileid or the fileid
5115 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005116static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5117{
Andy Adamson533eb462011-06-13 18:25:56 -04005118 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5119 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5120 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005121 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005122 return;
5123
5124 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005125 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005126 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5127 fattr->nlink = 2;
5128}
5129
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005130static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5131 const struct qstr *name,
5132 struct nfs4_fs_locations *fs_locations,
5133 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005134{
5135 struct nfs_server *server = NFS_SERVER(dir);
5136 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005137 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005138 };
5139 struct nfs4_fs_locations_arg args = {
5140 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005141 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005142 .page = page,
5143 .bitmask = bitmask,
5144 };
Benny Halevy22958462009-04-01 09:22:02 -04005145 struct nfs4_fs_locations_res res = {
5146 .fs_locations = fs_locations,
5147 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005148 struct rpc_message msg = {
5149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5150 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005151 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005152 };
5153 int status;
5154
Harvey Harrison3110ff82008-05-02 13:42:44 -07005155 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005156
5157 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5158 * is not supported */
5159 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5160 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5161 else
5162 bitmask[0] |= FATTR4_WORD0_FILEID;
5163
Trond Myklebustc228fd32007-01-13 02:28:11 -05005164 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005165 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005166 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005167 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005168 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005169 return status;
5170}
5171
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005172int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5173 const struct qstr *name,
5174 struct nfs4_fs_locations *fs_locations,
5175 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005176{
5177 struct nfs4_exception exception = { };
5178 int err;
5179 do {
5180 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005181 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005182 &exception);
5183 } while (exception.retry);
5184 return err;
5185}
5186
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005187static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5188{
5189 int status;
5190 struct nfs4_secinfo_arg args = {
5191 .dir_fh = NFS_FH(dir),
5192 .name = name,
5193 };
5194 struct nfs4_secinfo_res res = {
5195 .flavors = flavors,
5196 };
5197 struct rpc_message msg = {
5198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5199 .rpc_argp = &args,
5200 .rpc_resp = &res,
5201 };
5202
5203 dprintk("NFS call secinfo %s\n", name->name);
5204 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5205 dprintk("NFS reply secinfo: %d\n", status);
5206 return status;
5207}
5208
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005209int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5210 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005211{
5212 struct nfs4_exception exception = { };
5213 int err;
5214 do {
5215 err = nfs4_handle_exception(NFS_SERVER(dir),
5216 _nfs4_proc_secinfo(dir, name, flavors),
5217 &exception);
5218 } while (exception.retry);
5219 return err;
5220}
5221
Andy Adamson557134a2009-04-01 09:21:53 -04005222#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005223/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005224 * Check the exchange flags returned by the server for invalid flags, having
5225 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5226 * DS flags set.
5227 */
5228static int nfs4_check_cl_exchange_flags(u32 flags)
5229{
5230 if (flags & ~EXCHGID4_FLAG_MASK_R)
5231 goto out_inval;
5232 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5233 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5234 goto out_inval;
5235 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5236 goto out_inval;
5237 return NFS_OK;
5238out_inval:
5239 return -NFS4ERR_INVAL;
5240}
5241
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005242static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005243nfs41_same_server_scope(struct nfs41_server_scope *a,
5244 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005245{
5246 if (a->server_scope_sz == b->server_scope_sz &&
5247 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5248 return true;
5249
5250 return false;
5251}
5252
Andy Adamson357f54d2010-12-14 10:11:57 -05005253/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005254 * nfs4_proc_bind_conn_to_session()
5255 *
5256 * The 4.1 client currently uses the same TCP connection for the
5257 * fore and backchannel.
5258 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005259int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005260{
5261 int status;
5262 struct nfs41_bind_conn_to_session_res res;
5263 struct rpc_message msg = {
5264 .rpc_proc =
5265 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5266 .rpc_argp = clp,
5267 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005268 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005269 };
5270
5271 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005272
5273 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5274 if (unlikely(res.session == NULL)) {
5275 status = -ENOMEM;
5276 goto out;
5277 }
5278
5279 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5280 if (status == 0) {
5281 if (memcmp(res.session->sess_id.data,
5282 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5283 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5284 status = -EIO;
5285 goto out_session;
5286 }
5287 if (res.dir != NFS4_CDFS4_BOTH) {
5288 dprintk("NFS: %s: Unexpected direction from server\n",
5289 __func__);
5290 status = -EIO;
5291 goto out_session;
5292 }
5293 if (res.use_conn_in_rdma_mode) {
5294 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5295 __func__);
5296 status = -EIO;
5297 goto out_session;
5298 }
5299 }
5300out_session:
5301 kfree(res.session);
5302out:
5303 dprintk("<-- %s status= %d\n", __func__, status);
5304 return status;
5305}
5306
5307/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005308 * nfs4_proc_exchange_id()
5309 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005310 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5311 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005312 * Since the clientid has expired, all compounds using sessions
5313 * associated with the stale clientid will be returning
5314 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5315 * be in some phase of session reset.
5316 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005317int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005318{
5319 nfs4_verifier verifier;
5320 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005321 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005322 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005323 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005324 };
5325 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005326 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005327 };
5328 int status;
5329 struct rpc_message msg = {
5330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5331 .rpc_argp = &args,
5332 .rpc_resp = &res,
5333 .rpc_cred = cred,
5334 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005335
Chuck Leverf0920752012-05-21 22:45:41 -04005336 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005337 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5338 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005339 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5340 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5341 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005342
Chuck Leveracdeb692012-05-21 22:46:16 -04005343 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005344 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005345 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005346 status = -ENOMEM;
5347 goto out;
5348 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005349
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005350 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005351 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005352 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005353 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005354 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005355 }
5356
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005357 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005358 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005359 status = -ENOMEM;
5360 goto out_server_scope;
5361 }
5362
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005363 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005364 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005365 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005366
Chuck Lever177313f2012-05-21 22:44:58 -04005367 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005368 clp->cl_clientid = res.clientid;
5369 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5370 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5371 clp->cl_seqid = res.seqid;
5372
Chuck Leveracdeb692012-05-21 22:46:16 -04005373 kfree(clp->cl_serverowner);
5374 clp->cl_serverowner = res.server_owner;
5375 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005376
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005377 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005378 kfree(clp->cl_implid);
5379 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005380
Chuck Lever177313f2012-05-21 22:44:58 -04005381 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005382 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005383 res.server_scope)) {
5384 dprintk("%s: server_scope mismatch detected\n",
5385 __func__);
5386 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005387 kfree(clp->cl_serverscope);
5388 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005389 }
5390
Chuck Lever177313f2012-05-21 22:44:58 -04005391 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005392 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005393 goto out;
5394 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005395 } else
5396 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005397
Chuck Leveracdeb692012-05-21 22:46:16 -04005398out_server_owner:
5399 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005400out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005401 kfree(res.server_scope);
5402out:
Chuck Lever177313f2012-05-21 22:44:58 -04005403 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005404 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005405 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005406 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005407 clp->cl_implid->date.seconds,
5408 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005409 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005410 return status;
5411}
5412
Trond Myklebust66245532012-05-25 17:18:09 -04005413static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5414 struct rpc_cred *cred)
5415{
5416 struct rpc_message msg = {
5417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5418 .rpc_argp = clp,
5419 .rpc_cred = cred,
5420 };
5421 int status;
5422
5423 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5424 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005425 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005426 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5427 return status;
5428}
5429
5430static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5431 struct rpc_cred *cred)
5432{
5433 unsigned int loop;
5434 int ret;
5435
5436 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5437 ret = _nfs4_proc_destroy_clientid(clp, cred);
5438 switch (ret) {
5439 case -NFS4ERR_DELAY:
5440 case -NFS4ERR_CLIENTID_BUSY:
5441 ssleep(1);
5442 break;
5443 default:
5444 return ret;
5445 }
5446 }
5447 return 0;
5448}
5449
5450int nfs4_destroy_clientid(struct nfs_client *clp)
5451{
5452 struct rpc_cred *cred;
5453 int ret = 0;
5454
5455 if (clp->cl_mvops->minor_version < 1)
5456 goto out;
5457 if (clp->cl_exchange_flags == 0)
5458 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005459 if (clp->cl_preserve_clid)
5460 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005461 cred = nfs4_get_exchange_id_cred(clp);
5462 ret = nfs4_proc_destroy_clientid(clp, cred);
5463 if (cred)
5464 put_rpccred(cred);
5465 switch (ret) {
5466 case 0:
5467 case -NFS4ERR_STALE_CLIENTID:
5468 clp->cl_exchange_flags = 0;
5469 }
5470out:
5471 return ret;
5472}
5473
Andy Adamson2050f0c2009-04-01 09:22:30 -04005474struct nfs4_get_lease_time_data {
5475 struct nfs4_get_lease_time_args *args;
5476 struct nfs4_get_lease_time_res *res;
5477 struct nfs_client *clp;
5478};
5479
5480static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5481 void *calldata)
5482{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005483 struct nfs4_get_lease_time_data *data =
5484 (struct nfs4_get_lease_time_data *)calldata;
5485
5486 dprintk("--> %s\n", __func__);
5487 /* just setup sequence, do not trigger session recovery
5488 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005489 nfs41_setup_sequence(data->clp->cl_session,
5490 &data->args->la_seq_args,
5491 &data->res->lr_seq_res,
5492 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005493 dprintk("<-- %s\n", __func__);
5494}
5495
5496/*
5497 * Called from nfs4_state_manager thread for session setup, so don't recover
5498 * from sequence operation or clientid errors.
5499 */
5500static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5501{
5502 struct nfs4_get_lease_time_data *data =
5503 (struct nfs4_get_lease_time_data *)calldata;
5504
5505 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005506 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5507 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005508 switch (task->tk_status) {
5509 case -NFS4ERR_DELAY:
5510 case -NFS4ERR_GRACE:
5511 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5512 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5513 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005514 /* fall through */
5515 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005516 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005517 return;
5518 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005519 dprintk("<-- %s\n", __func__);
5520}
5521
Trond Myklebust17280172012-03-11 13:11:00 -04005522static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005523 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5524 .rpc_call_done = nfs4_get_lease_time_done,
5525};
5526
5527int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5528{
5529 struct rpc_task *task;
5530 struct nfs4_get_lease_time_args args;
5531 struct nfs4_get_lease_time_res res = {
5532 .lr_fsinfo = fsinfo,
5533 };
5534 struct nfs4_get_lease_time_data data = {
5535 .args = &args,
5536 .res = &res,
5537 .clp = clp,
5538 };
5539 struct rpc_message msg = {
5540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5541 .rpc_argp = &args,
5542 .rpc_resp = &res,
5543 };
5544 struct rpc_task_setup task_setup = {
5545 .rpc_client = clp->cl_rpcclient,
5546 .rpc_message = &msg,
5547 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005548 .callback_data = &data,
5549 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005550 };
5551 int status;
5552
Trond Myklebust9d12b212012-01-17 22:04:25 -05005553 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005554 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005555 dprintk("--> %s\n", __func__);
5556 task = rpc_run_task(&task_setup);
5557
5558 if (IS_ERR(task))
5559 status = PTR_ERR(task);
5560 else {
5561 status = task->tk_status;
5562 rpc_put_task(task);
5563 }
5564 dprintk("<-- %s return %d\n", __func__, status);
5565
5566 return status;
5567}
5568
Andy Adamsonfc931582009-04-01 09:22:31 -04005569/*
5570 * Initialize the values to be used by the client in CREATE_SESSION
5571 * If nfs4_init_session set the fore channel request and response sizes,
5572 * use them.
5573 *
5574 * Set the back channel max_resp_sz_cached to zero to force the client to
5575 * always set csa_cachethis to FALSE because the current implementation
5576 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5577 */
5578static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5579{
5580 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005581 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5582 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005583
5584 if (mxrqst_sz == 0)
5585 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5586 if (mxresp_sz == 0)
5587 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5588 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005589 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5590 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005591 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005592 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005593
5594 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005595 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005596 __func__,
5597 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005598 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005599
5600 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005601 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5602 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5603 args->bc_attrs.max_resp_sz_cached = 0;
5604 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5605 args->bc_attrs.max_reqs = 1;
5606
5607 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5608 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5609 __func__,
5610 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5611 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5612 args->bc_attrs.max_reqs);
5613}
5614
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005615static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005616{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005617 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5618 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5619
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005620 if (rcvd->max_resp_sz > sent->max_resp_sz)
5621 return -EINVAL;
5622 /*
5623 * Our requested max_ops is the minimum we need; we're not
5624 * prepared to break up compounds into smaller pieces than that.
5625 * So, no point even trying to continue if the server won't
5626 * cooperate:
5627 */
5628 if (rcvd->max_ops < sent->max_ops)
5629 return -EINVAL;
5630 if (rcvd->max_reqs == 0)
5631 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005632 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5633 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005634 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005635}
5636
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005637static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5638{
5639 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5640 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005641
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005642 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5643 return -EINVAL;
5644 if (rcvd->max_resp_sz < sent->max_resp_sz)
5645 return -EINVAL;
5646 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5647 return -EINVAL;
5648 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005649 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005650 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005651 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005652 return -EINVAL;
5653 return 0;
5654}
Andy Adamson8d353012009-04-01 09:22:32 -04005655
Andy Adamson8d353012009-04-01 09:22:32 -04005656static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5657 struct nfs4_session *session)
5658{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005659 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005660
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005661 ret = nfs4_verify_fore_channel_attrs(args, session);
5662 if (ret)
5663 return ret;
5664 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005665}
5666
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005667static int _nfs4_proc_create_session(struct nfs_client *clp,
5668 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005669{
5670 struct nfs4_session *session = clp->cl_session;
5671 struct nfs41_create_session_args args = {
5672 .client = clp,
5673 .cb_program = NFS4_CALLBACK,
5674 };
5675 struct nfs41_create_session_res res = {
5676 .client = clp,
5677 };
5678 struct rpc_message msg = {
5679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5680 .rpc_argp = &args,
5681 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005682 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005683 };
5684 int status;
5685
5686 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005687 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005688
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005689 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005690
Trond Myklebust43095d32012-11-20 11:13:12 -05005691 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005692 /* Verify the session's negotiated channel_attrs values */
5693 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005694 /* Increment the clientid slot sequence id */
5695 clp->cl_seqid++;
5696 }
5697
5698 return status;
5699}
5700
5701/*
5702 * Issues a CREATE_SESSION operation to the server.
5703 * It is the responsibility of the caller to verify the session is
5704 * expired before calling this routine.
5705 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005706int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005707{
5708 int status;
5709 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005710 struct nfs4_session *session = clp->cl_session;
5711
5712 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5713
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005714 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005715 if (status)
5716 goto out;
5717
Andy Adamsonaacd5532011-11-09 13:58:21 -05005718 /* Init or reset the session slot tables */
5719 status = nfs4_setup_session_slot_tables(session);
5720 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005721 if (status)
5722 goto out;
5723
5724 ptr = (unsigned *)&session->sess_id.data[0];
5725 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5726 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005727out:
5728 dprintk("<-- %s\n", __func__);
5729 return status;
5730}
5731
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005732/*
5733 * Issue the over-the-wire RPC DESTROY_SESSION.
5734 * The caller must serialize access to this routine.
5735 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005736int nfs4_proc_destroy_session(struct nfs4_session *session,
5737 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005738{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005739 struct rpc_message msg = {
5740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5741 .rpc_argp = session,
5742 .rpc_cred = cred,
5743 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005744 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005745
5746 dprintk("--> nfs4_proc_destroy_session\n");
5747
5748 /* session is still being setup */
5749 if (session->clp->cl_cons_state != NFS_CS_READY)
5750 return status;
5751
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005752 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005753
5754 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005755 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005756 "Session has been destroyed regardless...\n", status);
5757
5758 dprintk("<-- nfs4_proc_destroy_session\n");
5759 return status;
5760}
5761
Trond Myklebust7b38c362012-05-23 13:23:31 -04005762/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005763 * Renew the cl_session lease.
5764 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005765struct nfs4_sequence_data {
5766 struct nfs_client *clp;
5767 struct nfs4_sequence_args args;
5768 struct nfs4_sequence_res res;
5769};
5770
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005771static void nfs41_sequence_release(void *data)
5772{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005773 struct nfs4_sequence_data *calldata = data;
5774 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005775
Alexandros Batsakis71358402010-02-05 03:45:05 -08005776 if (atomic_read(&clp->cl_count) > 1)
5777 nfs4_schedule_state_renewal(clp);
5778 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005779 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005780}
5781
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005782static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5783{
5784 switch(task->tk_status) {
5785 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005786 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5787 return -EAGAIN;
5788 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005789 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005790 }
5791 return 0;
5792}
5793
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005794static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005795{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005796 struct nfs4_sequence_data *calldata = data;
5797 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005798
Trond Myklebust14516c32010-07-31 14:29:06 -04005799 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5800 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005801
5802 if (task->tk_status < 0) {
5803 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005804 if (atomic_read(&clp->cl_count) == 1)
5805 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005806
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005807 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5808 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005809 return;
5810 }
5811 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005812 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005813out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005814 dprintk("<-- %s\n", __func__);
5815}
5816
5817static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5818{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005819 struct nfs4_sequence_data *calldata = data;
5820 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005821 struct nfs4_sequence_args *args;
5822 struct nfs4_sequence_res *res;
5823
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005824 args = task->tk_msg.rpc_argp;
5825 res = task->tk_msg.rpc_resp;
5826
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005827 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005828}
5829
5830static const struct rpc_call_ops nfs41_sequence_ops = {
5831 .rpc_call_done = nfs41_sequence_call_done,
5832 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005833 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005834};
5835
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005836static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5837 struct rpc_cred *cred,
5838 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005839{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005840 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005841 struct rpc_message msg = {
5842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5843 .rpc_cred = cred,
5844 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005845 struct rpc_task_setup task_setup_data = {
5846 .rpc_client = clp->cl_rpcclient,
5847 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005848 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005849 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5850 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005851
Alexandros Batsakis71358402010-02-05 03:45:05 -08005852 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005853 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005854 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005855 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005856 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005857 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005858 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005859 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005860 if (is_privileged)
5861 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005862 msg.rpc_argp = &calldata->args;
5863 msg.rpc_resp = &calldata->res;
5864 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005865 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005866
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005867 return rpc_run_task(&task_setup_data);
5868}
5869
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005870static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005871{
5872 struct rpc_task *task;
5873 int ret = 0;
5874
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005875 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5876 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005877 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005878 if (IS_ERR(task))
5879 ret = PTR_ERR(task);
5880 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005881 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005882 dprintk("<-- %s status=%d\n", __func__, ret);
5883 return ret;
5884}
5885
5886static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5887{
5888 struct rpc_task *task;
5889 int ret;
5890
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005891 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005892 if (IS_ERR(task)) {
5893 ret = PTR_ERR(task);
5894 goto out;
5895 }
5896 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005897 if (!ret) {
5898 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5899
5900 if (task->tk_status == 0)
5901 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005902 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005903 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005904 rpc_put_task(task);
5905out:
5906 dprintk("<-- %s status=%d\n", __func__, ret);
5907 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005908}
5909
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005910struct nfs4_reclaim_complete_data {
5911 struct nfs_client *clp;
5912 struct nfs41_reclaim_complete_args arg;
5913 struct nfs41_reclaim_complete_res res;
5914};
5915
5916static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5917{
5918 struct nfs4_reclaim_complete_data *calldata = data;
5919
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005920 nfs41_setup_sequence(calldata->clp->cl_session,
5921 &calldata->arg.seq_args,
5922 &calldata->res.seq_res,
5923 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924}
5925
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005926static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5927{
5928 switch(task->tk_status) {
5929 case 0:
5930 case -NFS4ERR_COMPLETE_ALREADY:
5931 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5932 break;
5933 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005934 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005935 /* fall through */
5936 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005937 return -EAGAIN;
5938 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005939 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005940 }
5941 return 0;
5942}
5943
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005944static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5945{
5946 struct nfs4_reclaim_complete_data *calldata = data;
5947 struct nfs_client *clp = calldata->clp;
5948 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5949
5950 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005951 if (!nfs41_sequence_done(task, res))
5952 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005953
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005954 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5955 rpc_restart_call_prepare(task);
5956 return;
5957 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005958 dprintk("<-- %s\n", __func__);
5959}
5960
5961static void nfs4_free_reclaim_complete_data(void *data)
5962{
5963 struct nfs4_reclaim_complete_data *calldata = data;
5964
5965 kfree(calldata);
5966}
5967
5968static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5969 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5970 .rpc_call_done = nfs4_reclaim_complete_done,
5971 .rpc_release = nfs4_free_reclaim_complete_data,
5972};
5973
5974/*
5975 * Issue a global reclaim complete.
5976 */
5977static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5978{
5979 struct nfs4_reclaim_complete_data *calldata;
5980 struct rpc_task *task;
5981 struct rpc_message msg = {
5982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5983 };
5984 struct rpc_task_setup task_setup_data = {
5985 .rpc_client = clp->cl_rpcclient,
5986 .rpc_message = &msg,
5987 .callback_ops = &nfs4_reclaim_complete_call_ops,
5988 .flags = RPC_TASK_ASYNC,
5989 };
5990 int status = -ENOMEM;
5991
5992 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005993 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005994 if (calldata == NULL)
5995 goto out;
5996 calldata->clp = clp;
5997 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005998
Trond Myklebust9d12b212012-01-17 22:04:25 -05005999 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006000 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006001 msg.rpc_argp = &calldata->arg;
6002 msg.rpc_resp = &calldata->res;
6003 task_setup_data.callback_data = calldata;
6004 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006005 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006006 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006007 goto out;
6008 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006009 status = nfs4_wait_for_completion_rpc_task(task);
6010 if (status == 0)
6011 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006012 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006013 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006014out:
6015 dprintk("<-- %s status=%d\n", __func__, status);
6016 return status;
6017}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006018
6019static void
6020nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6021{
6022 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006023 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006024 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006025
6026 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006027 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6028 * right now covering the LAYOUTGET we are about to send.
6029 * However, that is not so catastrophic, and there seems
6030 * to be no way to prevent it completely.
6031 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006032 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006033 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006034 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006035 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6036 NFS_I(lgp->args.inode)->layout,
6037 lgp->args.ctx->state)) {
6038 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006039 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006040}
6041
6042static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6043{
6044 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006045 struct inode *inode = lgp->args.inode;
6046 struct nfs_server *server = NFS_SERVER(inode);
6047 struct pnfs_layout_hdr *lo;
6048 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006049
6050 dprintk("--> %s\n", __func__);
6051
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006052 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006053 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006054
6055 switch (task->tk_status) {
6056 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006057 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006058 case -NFS4ERR_LAYOUTTRYLATER:
6059 case -NFS4ERR_RECALLCONFLICT:
6060 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006061 break;
6062 case -NFS4ERR_EXPIRED:
6063 case -NFS4ERR_BAD_STATEID:
6064 spin_lock(&inode->i_lock);
6065 lo = NFS_I(inode)->layout;
6066 if (!lo || list_empty(&lo->plh_segs)) {
6067 spin_unlock(&inode->i_lock);
6068 /* If the open stateid was bad, then recover it. */
6069 state = lgp->args.ctx->state;
6070 } else {
6071 LIST_HEAD(head);
6072
6073 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6074 spin_unlock(&inode->i_lock);
6075 /* Mark the bad layout state as invalid, then
6076 * retry using the open stateid. */
6077 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006078 }
6079 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006080 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6081 rpc_restart_call_prepare(task);
6082out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006083 dprintk("<-- %s\n", __func__);
6084}
6085
Idan Kedar85541162012-08-02 11:47:10 +03006086static size_t max_response_pages(struct nfs_server *server)
6087{
6088 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6089 return nfs_page_array_len(0, max_resp_sz);
6090}
6091
6092static void nfs4_free_pages(struct page **pages, size_t size)
6093{
6094 int i;
6095
6096 if (!pages)
6097 return;
6098
6099 for (i = 0; i < size; i++) {
6100 if (!pages[i])
6101 break;
6102 __free_page(pages[i]);
6103 }
6104 kfree(pages);
6105}
6106
6107static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6108{
6109 struct page **pages;
6110 int i;
6111
6112 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6113 if (!pages) {
6114 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6115 return NULL;
6116 }
6117
6118 for (i = 0; i < size; i++) {
6119 pages[i] = alloc_page(gfp_flags);
6120 if (!pages[i]) {
6121 dprintk("%s: failed to allocate page\n", __func__);
6122 nfs4_free_pages(pages, size);
6123 return NULL;
6124 }
6125 }
6126
6127 return pages;
6128}
6129
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006130static void nfs4_layoutget_release(void *calldata)
6131{
6132 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006133 struct inode *inode = lgp->args.inode;
6134 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006135 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006136
6137 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006138 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006139 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006140 put_nfs_open_context(lgp->args.ctx);
6141 kfree(calldata);
6142 dprintk("<-- %s\n", __func__);
6143}
6144
6145static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6146 .rpc_call_prepare = nfs4_layoutget_prepare,
6147 .rpc_call_done = nfs4_layoutget_done,
6148 .rpc_release = nfs4_layoutget_release,
6149};
6150
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006151struct pnfs_layout_segment *
6152nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006153{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006154 struct inode *inode = lgp->args.inode;
6155 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006156 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006157 struct rpc_task *task;
6158 struct rpc_message msg = {
6159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6160 .rpc_argp = &lgp->args,
6161 .rpc_resp = &lgp->res,
6162 };
6163 struct rpc_task_setup task_setup_data = {
6164 .rpc_client = server->client,
6165 .rpc_message = &msg,
6166 .callback_ops = &nfs4_layoutget_call_ops,
6167 .callback_data = lgp,
6168 .flags = RPC_TASK_ASYNC,
6169 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006170 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006171 int status = 0;
6172
6173 dprintk("--> %s\n", __func__);
6174
Idan Kedar85541162012-08-02 11:47:10 +03006175 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6176 if (!lgp->args.layout.pages) {
6177 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006178 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006179 }
6180 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6181
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006182 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006183 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006184 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006185
6186 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6187 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6188
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006189 task = rpc_run_task(&task_setup_data);
6190 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006191 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006192 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006193 if (status == 0)
6194 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006195 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6196 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006197 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006198 rpc_put_task(task);
6199 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006200 if (status)
6201 return ERR_PTR(status);
6202 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006203}
6204
Benny Halevycbe82602011-05-22 19:52:37 +03006205static void
6206nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6207{
6208 struct nfs4_layoutreturn *lrp = calldata;
6209
6210 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006211 nfs41_setup_sequence(lrp->clp->cl_session,
6212 &lrp->args.seq_args,
6213 &lrp->res.seq_res,
6214 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006215}
6216
6217static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6218{
6219 struct nfs4_layoutreturn *lrp = calldata;
6220 struct nfs_server *server;
6221
6222 dprintk("--> %s\n", __func__);
6223
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006224 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006225 return;
6226
6227 server = NFS_SERVER(lrp->args.inode);
6228 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006229 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006230 return;
6231 }
Benny Halevycbe82602011-05-22 19:52:37 +03006232 dprintk("<-- %s\n", __func__);
6233}
6234
6235static void nfs4_layoutreturn_release(void *calldata)
6236{
6237 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006238 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006239
6240 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006241 spin_lock(&lo->plh_inode->i_lock);
6242 if (lrp->res.lrs_present)
6243 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6244 lo->plh_block_lgets--;
6245 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006246 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006247 kfree(calldata);
6248 dprintk("<-- %s\n", __func__);
6249}
6250
6251static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6252 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6253 .rpc_call_done = nfs4_layoutreturn_done,
6254 .rpc_release = nfs4_layoutreturn_release,
6255};
6256
6257int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6258{
6259 struct rpc_task *task;
6260 struct rpc_message msg = {
6261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6262 .rpc_argp = &lrp->args,
6263 .rpc_resp = &lrp->res,
6264 };
6265 struct rpc_task_setup task_setup_data = {
6266 .rpc_client = lrp->clp->cl_rpcclient,
6267 .rpc_message = &msg,
6268 .callback_ops = &nfs4_layoutreturn_call_ops,
6269 .callback_data = lrp,
6270 };
6271 int status;
6272
6273 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006274 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006275 task = rpc_run_task(&task_setup_data);
6276 if (IS_ERR(task))
6277 return PTR_ERR(task);
6278 status = task->tk_status;
6279 dprintk("<-- %s status=%d\n", __func__, status);
6280 rpc_put_task(task);
6281 return status;
6282}
6283
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006284/*
6285 * Retrieve the list of Data Server devices from the MDS.
6286 */
6287static int _nfs4_getdevicelist(struct nfs_server *server,
6288 const struct nfs_fh *fh,
6289 struct pnfs_devicelist *devlist)
6290{
6291 struct nfs4_getdevicelist_args args = {
6292 .fh = fh,
6293 .layoutclass = server->pnfs_curr_ld->id,
6294 };
6295 struct nfs4_getdevicelist_res res = {
6296 .devlist = devlist,
6297 };
6298 struct rpc_message msg = {
6299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6300 .rpc_argp = &args,
6301 .rpc_resp = &res,
6302 };
6303 int status;
6304
6305 dprintk("--> %s\n", __func__);
6306 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6307 &res.seq_res, 0);
6308 dprintk("<-- %s status=%d\n", __func__, status);
6309 return status;
6310}
6311
6312int nfs4_proc_getdevicelist(struct nfs_server *server,
6313 const struct nfs_fh *fh,
6314 struct pnfs_devicelist *devlist)
6315{
6316 struct nfs4_exception exception = { };
6317 int err;
6318
6319 do {
6320 err = nfs4_handle_exception(server,
6321 _nfs4_getdevicelist(server, fh, devlist),
6322 &exception);
6323 } while (exception.retry);
6324
6325 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6326 err, devlist->num_devs);
6327
6328 return err;
6329}
6330EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6331
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006332static int
6333_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6334{
6335 struct nfs4_getdeviceinfo_args args = {
6336 .pdev = pdev,
6337 };
6338 struct nfs4_getdeviceinfo_res res = {
6339 .pdev = pdev,
6340 };
6341 struct rpc_message msg = {
6342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6343 .rpc_argp = &args,
6344 .rpc_resp = &res,
6345 };
6346 int status;
6347
6348 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006349 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006350 dprintk("<-- %s status=%d\n", __func__, status);
6351
6352 return status;
6353}
6354
6355int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6356{
6357 struct nfs4_exception exception = { };
6358 int err;
6359
6360 do {
6361 err = nfs4_handle_exception(server,
6362 _nfs4_proc_getdeviceinfo(server, pdev),
6363 &exception);
6364 } while (exception.retry);
6365 return err;
6366}
6367EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6368
Andy Adamson863a3c62011-03-23 13:27:54 +00006369static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6370{
6371 struct nfs4_layoutcommit_data *data = calldata;
6372 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006373 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006374
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006375 nfs41_setup_sequence(session,
6376 &data->args.seq_args,
6377 &data->res.seq_res,
6378 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006379}
6380
6381static void
6382nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6383{
6384 struct nfs4_layoutcommit_data *data = calldata;
6385 struct nfs_server *server = NFS_SERVER(data->args.inode);
6386
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006387 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006388 return;
6389
6390 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006391 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6392 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6393 case -NFS4ERR_BADLAYOUT: /* no layout */
6394 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006395 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006396 break;
6397 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006398 nfs_post_op_update_inode_force_wcc(data->args.inode,
6399 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006400 break;
6401 default:
6402 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6403 rpc_restart_call_prepare(task);
6404 return;
6405 }
6406 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006407}
6408
6409static void nfs4_layoutcommit_release(void *calldata)
6410{
6411 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006412 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006413 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006414
Andy Adamsondb29c082011-07-30 20:52:38 -04006415 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006416 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006417 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6418 list_del_init(&lseg->pls_lc_list);
6419 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6420 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006421 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006422 }
Peng Tao92407e72011-10-23 20:21:17 -07006423
6424 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6425 smp_mb__after_clear_bit();
6426 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6427
Andy Adamson863a3c62011-03-23 13:27:54 +00006428 put_rpccred(data->cred);
6429 kfree(data);
6430}
6431
6432static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6433 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6434 .rpc_call_done = nfs4_layoutcommit_done,
6435 .rpc_release = nfs4_layoutcommit_release,
6436};
6437
6438int
Andy Adamsonef311532011-03-12 02:58:10 -05006439nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006440{
6441 struct rpc_message msg = {
6442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6443 .rpc_argp = &data->args,
6444 .rpc_resp = &data->res,
6445 .rpc_cred = data->cred,
6446 };
6447 struct rpc_task_setup task_setup_data = {
6448 .task = &data->task,
6449 .rpc_client = NFS_CLIENT(data->args.inode),
6450 .rpc_message = &msg,
6451 .callback_ops = &nfs4_layoutcommit_ops,
6452 .callback_data = data,
6453 .flags = RPC_TASK_ASYNC,
6454 };
6455 struct rpc_task *task;
6456 int status = 0;
6457
6458 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6459 "lbw: %llu inode %lu\n",
6460 data->task.tk_pid, sync,
6461 data->args.lastbytewritten,
6462 data->args.inode->i_ino);
6463
Trond Myklebust9d12b212012-01-17 22:04:25 -05006464 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006465 task = rpc_run_task(&task_setup_data);
6466 if (IS_ERR(task))
6467 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006468 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006469 goto out;
6470 status = nfs4_wait_for_completion_rpc_task(task);
6471 if (status != 0)
6472 goto out;
6473 status = task->tk_status;
6474out:
6475 dprintk("%s: status %d\n", __func__, status);
6476 rpc_put_task(task);
6477 return status;
6478}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006479
6480static int
6481_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6482 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6483{
6484 struct nfs41_secinfo_no_name_args args = {
6485 .style = SECINFO_STYLE_CURRENT_FH,
6486 };
6487 struct nfs4_secinfo_res res = {
6488 .flavors = flavors,
6489 };
6490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6492 .rpc_argp = &args,
6493 .rpc_resp = &res,
6494 };
6495 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6496}
6497
6498static int
6499nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6500 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6501{
6502 struct nfs4_exception exception = { };
6503 int err;
6504 do {
6505 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6506 switch (err) {
6507 case 0:
6508 case -NFS4ERR_WRONGSEC:
6509 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006510 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006511 default:
6512 err = nfs4_handle_exception(server, err, &exception);
6513 }
6514 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006515out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006516 return err;
6517}
6518
6519static int
6520nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6521 struct nfs_fsinfo *info)
6522{
6523 int err;
6524 struct page *page;
6525 rpc_authflavor_t flavor;
6526 struct nfs4_secinfo_flavors *flavors;
6527
6528 page = alloc_page(GFP_KERNEL);
6529 if (!page) {
6530 err = -ENOMEM;
6531 goto out;
6532 }
6533
6534 flavors = page_address(page);
6535 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6536
6537 /*
6538 * Fall back on "guess and check" method if
6539 * the server doesn't support SECINFO_NO_NAME
6540 */
6541 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6542 err = nfs4_find_root_sec(server, fhandle, info);
6543 goto out_freepage;
6544 }
6545 if (err)
6546 goto out_freepage;
6547
6548 flavor = nfs_find_best_sec(flavors);
6549 if (err == 0)
6550 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6551
6552out_freepage:
6553 put_page(page);
6554 if (err == -EACCES)
6555 return -EPERM;
6556out:
6557 return err;
6558}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006559
6560static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006561{
6562 int status;
6563 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006564 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006565 };
6566 struct nfs41_test_stateid_res res;
6567 struct rpc_message msg = {
6568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6569 .rpc_argp = &args,
6570 .rpc_resp = &res,
6571 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006572
Chuck Lever38527b12012-07-11 16:30:23 -04006573 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006574 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006575 nfs4_set_sequence_privileged(&args.seq_args);
6576 status = nfs4_call_sync_sequence(server->client, server, &msg,
6577 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006578 if (status != NFS_OK) {
6579 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006580 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006581 }
6582 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006583 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006584}
6585
Chuck Lever38527b12012-07-11 16:30:23 -04006586/**
6587 * nfs41_test_stateid - perform a TEST_STATEID operation
6588 *
6589 * @server: server / transport on which to perform the operation
6590 * @stateid: state ID to test
6591 *
6592 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6593 * Otherwise a negative NFS4ERR value is returned if the operation
6594 * failed or the state ID is not currently valid.
6595 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006596static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006597{
6598 struct nfs4_exception exception = { };
6599 int err;
6600 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006601 err = _nfs41_test_stateid(server, stateid);
6602 if (err != -NFS4ERR_DELAY)
6603 break;
6604 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006605 } while (exception.retry);
6606 return err;
6607}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006608
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006609static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006610{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006611 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006612 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006613 };
6614 struct nfs41_free_stateid_res res;
6615 struct rpc_message msg = {
6616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6617 .rpc_argp = &args,
6618 .rpc_resp = &res,
6619 };
Chuck Lever38527b12012-07-11 16:30:23 -04006620 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006621
Chuck Lever38527b12012-07-11 16:30:23 -04006622 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006623 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006624 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006625 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006626 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006627 dprintk("NFS reply free_stateid: %d\n", status);
6628 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006629}
6630
Chuck Lever38527b12012-07-11 16:30:23 -04006631/**
6632 * nfs41_free_stateid - perform a FREE_STATEID operation
6633 *
6634 * @server: server / transport on which to perform the operation
6635 * @stateid: state ID to release
6636 *
6637 * Returns NFS_OK if the server freed "stateid". Otherwise a
6638 * negative NFS4ERR value is returned.
6639 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006640static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006641{
6642 struct nfs4_exception exception = { };
6643 int err;
6644 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006645 err = _nfs4_free_stateid(server, stateid);
6646 if (err != -NFS4ERR_DELAY)
6647 break;
6648 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006649 } while (exception.retry);
6650 return err;
6651}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006652
6653static bool nfs41_match_stateid(const nfs4_stateid *s1,
6654 const nfs4_stateid *s2)
6655{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006656 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006657 return false;
6658
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006659 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006660 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006661 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006662 return true;
6663
6664 return false;
6665}
6666
Andy Adamson557134a2009-04-01 09:21:53 -04006667#endif /* CONFIG_NFS_V4_1 */
6668
Trond Myklebust36281ca2012-03-04 18:13:56 -05006669static bool nfs4_match_stateid(const nfs4_stateid *s1,
6670 const nfs4_stateid *s2)
6671{
Trond Myklebustf597c532012-03-04 18:13:56 -05006672 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006673}
6674
6675
Trond Myklebust17280172012-03-11 13:11:00 -04006676static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006677 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006678 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 .recover_open = nfs4_open_reclaim,
6680 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006681 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006682 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006683 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684};
6685
Andy Adamson591d71c2009-04-01 09:22:47 -04006686#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006687static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006688 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6689 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6690 .recover_open = nfs4_open_reclaim,
6691 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006692 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006693 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006694 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006695 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006696};
6697#endif /* CONFIG_NFS_V4_1 */
6698
Trond Myklebust17280172012-03-11 13:11:00 -04006699static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006700 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006701 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 .recover_open = nfs4_open_expired,
6703 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006704 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006705 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706};
6707
Andy Adamson591d71c2009-04-01 09:22:47 -04006708#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006709static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006710 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6711 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006712 .recover_open = nfs41_open_expired,
6713 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006714 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006715 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006716};
6717#endif /* CONFIG_NFS_V4_1 */
6718
Trond Myklebust17280172012-03-11 13:11:00 -04006719static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006720 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006721 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006722 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006723};
6724
6725#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006726static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006727 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006728 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006729 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006730};
6731#endif
6732
Trond Myklebust97dc1352010-06-16 09:52:26 -04006733static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6734 .minor_version = 0,
6735 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006736 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006737 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006738 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6739 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6740 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006741};
6742
6743#if defined(CONFIG_NFS_V4_1)
6744static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6745 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006746 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006747 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006748 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006749 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6750 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6751 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006752};
6753#endif
6754
Trond Myklebust97dc1352010-06-16 09:52:26 -04006755const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6756 [0] = &nfs_v4_0_minor_ops,
6757#if defined(CONFIG_NFS_V4_1)
6758 [1] = &nfs_v4_1_minor_ops,
6759#endif
6760};
6761
Bryan Schumaker73a79702012-07-16 16:39:12 -04006762const struct inode_operations nfs4_dir_inode_operations = {
6763 .create = nfs_create,
6764 .lookup = nfs_lookup,
6765 .atomic_open = nfs_atomic_open,
6766 .link = nfs_link,
6767 .unlink = nfs_unlink,
6768 .symlink = nfs_symlink,
6769 .mkdir = nfs_mkdir,
6770 .rmdir = nfs_rmdir,
6771 .mknod = nfs_mknod,
6772 .rename = nfs_rename,
6773 .permission = nfs_permission,
6774 .getattr = nfs_getattr,
6775 .setattr = nfs_setattr,
6776 .getxattr = generic_getxattr,
6777 .setxattr = generic_setxattr,
6778 .listxattr = generic_listxattr,
6779 .removexattr = generic_removexattr,
6780};
6781
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006782static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006783 .permission = nfs_permission,
6784 .getattr = nfs_getattr,
6785 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006786 .getxattr = generic_getxattr,
6787 .setxattr = generic_setxattr,
6788 .listxattr = generic_listxattr,
6789 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006790};
6791
David Howells509de812006-08-22 20:06:11 -04006792const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 .version = 4, /* protocol version */
6794 .dentry_ops = &nfs4_dentry_operations,
6795 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006796 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006797 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006799 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006800 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 .getattr = nfs4_proc_getattr,
6802 .setattr = nfs4_proc_setattr,
6803 .lookup = nfs4_proc_lookup,
6804 .access = nfs4_proc_access,
6805 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 .create = nfs4_proc_create,
6807 .remove = nfs4_proc_remove,
6808 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006809 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 .unlink_done = nfs4_proc_unlink_done,
6811 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006812 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006813 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006814 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 .link = nfs4_proc_link,
6816 .symlink = nfs4_proc_symlink,
6817 .mkdir = nfs4_proc_mkdir,
6818 .rmdir = nfs4_proc_remove,
6819 .readdir = nfs4_proc_readdir,
6820 .mknod = nfs4_proc_mknod,
6821 .statfs = nfs4_proc_statfs,
6822 .fsinfo = nfs4_proc_fsinfo,
6823 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006824 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 .decode_dirent = nfs4_decode_dirent,
6826 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006827 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006828 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006829 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006831 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006832 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006833 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006835 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006836 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006838 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006839 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006840 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006841 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006842 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006843 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006844 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04006845 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006846 .create_server = nfs4_create_server,
6847 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848};
6849
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006850static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6851 .prefix = XATTR_NAME_NFSV4_ACL,
6852 .list = nfs4_xattr_list_nfs4_acl,
6853 .get = nfs4_xattr_get_nfs4_acl,
6854 .set = nfs4_xattr_set_nfs4_acl,
6855};
6856
6857const struct xattr_handler *nfs4_xattr_handlers[] = {
6858 &nfs4_xattr_nfs4_acl_handler,
6859 NULL
6860};
6861
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862/*
6863 * Local variables:
6864 * c-basic-offset: 8
6865 * End:
6866 */