blob: 5d864fb3657885ff9eb8a043d880f2c1490f8d0e [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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebustb7391f42008-12-23 15:21:52 -0500899 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 return 1;
901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100904{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100906 case FMODE_WRITE:
907 state->n_wronly++;
908 break;
909 case FMODE_READ:
910 state->n_rdonly++;
911 break;
912 case FMODE_READ|FMODE_WRITE:
913 state->n_rdwr++;
914 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100916}
917
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400919{
920 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500921 nfs4_stateid_copy(&state->stateid, stateid);
922 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400924 case FMODE_READ:
925 set_bit(NFS_O_RDONLY_STATE, &state->flags);
926 break;
927 case FMODE_WRITE:
928 set_bit(NFS_O_WRONLY_STATE, &state->flags);
929 break;
930 case FMODE_READ|FMODE_WRITE:
931 set_bit(NFS_O_RDWR_STATE, &state->flags);
932 }
933}
934
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500935static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400936{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400937 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400940}
941
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942static 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 -0700943{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400944 /*
945 * Protect the call to nfs4_state_set_mode_locked and
946 * serialise the stateid update
947 */
948 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400949 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500950 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951 set_bit(NFS_DELEGATED_STATE, &state->flags);
952 }
953 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400955 write_sequnlock(&state->seqlock);
956 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700958 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static 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 -0500962{
963 struct nfs_inode *nfsi = NFS_I(state->inode);
964 struct nfs_delegation *deleg_cur;
965 int ret = 0;
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500968
969 rcu_read_lock();
970 deleg_cur = rcu_dereference(nfsi->delegation);
971 if (deleg_cur == NULL)
972 goto no_delegation;
973
974 spin_lock(&deleg_cur->lock);
975 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500977 goto no_delegation_unlock;
978
979 if (delegation == NULL)
980 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500981 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500982 goto no_delegation_unlock;
983
Trond Myklebustb7391f42008-12-23 15:21:52 -0500984 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500985 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500986 ret = 1;
987no_delegation_unlock:
988 spin_unlock(&deleg_cur->lock);
989no_delegation:
990 rcu_read_unlock();
991
992 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500994 ret = 1;
995 }
996
997 return ret;
998}
999
1000
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002{
1003 struct nfs_delegation *delegation;
1004
1005 rcu_read_lock();
1006 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008 rcu_read_unlock();
1009 return;
1010 }
1011 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001012 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013}
1014
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016{
1017 struct nfs4_state *state = opendata->state;
1018 struct nfs_inode *nfsi = NFS_I(state->inode);
1019 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001020 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022 nfs4_stateid stateid;
1023 int ret = -EAGAIN;
1024
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001027 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 if (can_open_cached(state, fmode, open_mode)) {
1029 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001031 goto out_return_state;
1032 }
1033 spin_unlock(&state->owner->so_lock);
1034 }
Trond Myklebust34310432008-12-23 15:21:38 -05001035 rcu_read_lock();
1036 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001037 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001038 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001040 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001042 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001044 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 if (ret != 0)
1046 goto out;
1047 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001048
1049 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001051 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053out:
1054 return ERR_PTR(ret);
1055out_return_state:
1056 atomic_inc(&state->count);
1057 return state;
1058}
1059
Andy Adamsone23008e2012-10-02 21:07:32 -04001060static void
1061nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1062{
1063 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1064 struct nfs_delegation *delegation;
1065 int delegation_flags = 0;
1066
1067 rcu_read_lock();
1068 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1069 if (delegation)
1070 delegation_flags = delegation->flags;
1071 rcu_read_unlock();
1072 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1073 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1074 "returning a delegation for "
1075 "OPEN(CLAIM_DELEGATE_CUR)\n",
1076 clp->cl_hostname);
1077 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1078 nfs_inode_set_delegation(state->inode,
1079 data->owner->so_cred,
1080 &data->o_res);
1081 else
1082 nfs_inode_reclaim_delegation(state->inode,
1083 data->owner->so_cred,
1084 &data->o_res);
1085}
1086
1087/*
1088 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1089 * and update the nfs4_state.
1090 */
1091static struct nfs4_state *
1092_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1093{
1094 struct inode *inode = data->state->inode;
1095 struct nfs4_state *state = data->state;
1096 int ret;
1097
1098 if (!data->rpc_done) {
1099 ret = data->rpc_status;
1100 goto err;
1101 }
1102
1103 ret = -ESTALE;
1104 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1105 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1106 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1107 goto err;
1108
1109 ret = -ENOMEM;
1110 state = nfs4_get_open_state(inode, data->owner);
1111 if (state == NULL)
1112 goto err;
1113
1114 ret = nfs_refresh_inode(inode, &data->f_attr);
1115 if (ret)
1116 goto err;
1117
1118 if (data->o_res.delegation_type != 0)
1119 nfs4_opendata_check_deleg(data, state);
1120 update_open_stateid(state, &data->o_res.stateid, NULL,
1121 data->o_arg.fmode);
1122
1123 return state;
1124err:
1125 return ERR_PTR(ret);
1126
1127}
1128
1129static struct nfs4_state *
1130_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131{
1132 struct inode *inode;
1133 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135
Trond Myklebustaac00a82007-07-05 19:02:21 -04001136 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001137 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001138 goto out;
1139 }
1140
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001141 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001143 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001146 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147 goto err;
1148 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149 state = nfs4_get_open_state(inode, data->owner);
1150 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001151 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001152 if (data->o_res.delegation_type != 0)
1153 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001154 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001156 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001157out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001158 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001159err_put_inode:
1160 iput(inode);
1161err:
1162 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001163}
1164
Andy Adamsone23008e2012-10-02 21:07:32 -04001165static struct nfs4_state *
1166nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1167{
1168 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1169 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1170 return _nfs4_opendata_to_nfs4_state(data);
1171}
1172
Trond Myklebust864472e2006-01-03 09:55:15 +01001173static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1174{
1175 struct nfs_inode *nfsi = NFS_I(state->inode);
1176 struct nfs_open_context *ctx;
1177
1178 spin_lock(&state->inode->i_lock);
1179 list_for_each_entry(ctx, &nfsi->open_files, list) {
1180 if (ctx->state != state)
1181 continue;
1182 get_nfs_open_context(ctx);
1183 spin_unlock(&state->inode->i_lock);
1184 return ctx;
1185 }
1186 spin_unlock(&state->inode->i_lock);
1187 return ERR_PTR(-ENOENT);
1188}
1189
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001190static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1191{
1192 struct nfs4_opendata *opendata;
1193
Al Viro3d4ff432011-06-22 18:40:12 -04001194 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001195 if (opendata == NULL)
1196 return ERR_PTR(-ENOMEM);
1197 opendata->state = state;
1198 atomic_inc(&state->count);
1199 return opendata;
1200}
1201
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001202static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001203{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 int ret;
1206
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001207 opendata->o_arg.open_flags = 0;
1208 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1210 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1211 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001212 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 if (ret != 0)
1214 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001216 if (IS_ERR(newstate))
1217 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001218 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 return 0;
1221}
1222
1223static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1224{
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 int ret;
1227
1228 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 smp_rmb();
1231 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001232 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 if (ret != 0)
1235 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 if (newstate != state)
1237 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 }
1239 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001240 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001241 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 if (ret != 0)
1243 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 if (newstate != state)
1245 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 }
1247 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001248 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001249 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 if (ret != 0)
1251 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 if (newstate != state)
1253 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 /*
1256 * We may have performed cached opens for all three recoveries.
1257 * Check if we need to update the current stateid.
1258 */
1259 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001260 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001261 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001263 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001264 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001266 return 0;
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/*
1270 * OPEN_RECLAIM:
1271 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001273static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001275 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001277 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int status;
1279
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001280 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1281 if (IS_ERR(opendata))
1282 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1284 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001285 rcu_read_lock();
1286 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001287 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001288 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001289 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001290 opendata->o_arg.u.delegation_type = delegation_type;
1291 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return status;
1294}
1295
Trond Myklebust539cd032007-06-05 11:46:42 -04001296static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 struct nfs_server *server = NFS_SERVER(state->inode);
1299 struct nfs4_exception exception = { };
1300 int err;
1301 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001303 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001304 break;
1305 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 } while (exception.retry);
1307 return err;
1308}
1309
Trond Myklebust864472e2006-01-03 09:55:15 +01001310static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1311{
1312 struct nfs_open_context *ctx;
1313 int ret;
1314
1315 ctx = nfs4_state_find_open_context(state);
1316 if (IS_ERR(ctx))
1317 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001318 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001319 put_nfs_open_context(ctx);
1320 return ret;
1321}
1322
Trond Myklebust13437e12007-07-06 15:10:43 -04001323static 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 -07001324{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001325 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001326 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001328 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1329 if (IS_ERR(opendata))
1330 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001331 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001332 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001334 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001335 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Trond Myklebust13437e12007-07-06 15:10:43 -04001338int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001341 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int err;
1343 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001344 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 switch (err) {
1346 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347 case -ENOENT:
1348 case -ESTALE:
1349 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001350 case -NFS4ERR_BADSESSION:
1351 case -NFS4ERR_BADSLOT:
1352 case -NFS4ERR_BAD_HIGH_SLOT:
1353 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1354 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001355 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 case -NFS4ERR_STALE_CLIENTID:
1358 case -NFS4ERR_STALE_STATEID:
1359 case -NFS4ERR_EXPIRED:
1360 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001361 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001362 goto out;
1363 case -ERESTARTSYS:
1364 /*
1365 * The show must go on: exit, but mark the
1366 * stateid as needing recovery.
1367 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001368 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001369 case -NFS4ERR_ADMIN_REVOKED:
1370 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001371 nfs_inode_find_state_and_recover(state->inode,
1372 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001373 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001374 case -ENOMEM:
1375 err = 0;
1376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 err = nfs4_handle_exception(server, err, &exception);
1379 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001380out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return err;
1382}
1383
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1385{
1386 struct nfs4_opendata *data = calldata;
1387
1388 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001389 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001390 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001391 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001392 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001393 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001394 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395}
1396
1397static void nfs4_open_confirm_release(void *calldata)
1398{
1399 struct nfs4_opendata *data = calldata;
1400 struct nfs4_state *state = NULL;
1401
1402 /* If this request hasn't been cancelled, do nothing */
1403 if (data->cancelled == 0)
1404 goto out_free;
1405 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001406 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001407 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001408 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001409 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001410 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001411out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001412 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001413}
1414
1415static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416 .rpc_call_done = nfs4_open_confirm_done,
1417 .rpc_release = nfs4_open_confirm_release,
1418};
1419
1420/*
1421 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1422 */
1423static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1424{
1425 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1426 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001427 struct rpc_message msg = {
1428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1429 .rpc_argp = &data->c_arg,
1430 .rpc_resp = &data->c_res,
1431 .rpc_cred = data->owner->so_cred,
1432 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001433 struct rpc_task_setup task_setup_data = {
1434 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001435 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001436 .callback_ops = &nfs4_open_confirm_ops,
1437 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001438 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 .flags = RPC_TASK_ASYNC,
1440 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 int status;
1442
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001443 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001444 data->rpc_done = 0;
1445 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001446 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001447 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001448 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001449 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001450 status = nfs4_wait_for_completion_rpc_task(task);
1451 if (status != 0) {
1452 data->cancelled = 1;
1453 smp_wmb();
1454 } else
1455 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001456 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return status;
1458}
1459
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 struct nfs4_opendata *data = calldata;
1463 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001464
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1466 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001467 /*
1468 * Check if we still need to send an OPEN call, or if we can use
1469 * a delegation instead.
1470 */
1471 if (data->state != NULL) {
1472 struct nfs_delegation *delegation;
1473
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001474 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001475 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001476 rcu_read_lock();
1477 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001478 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1479 can_open_delegated(delegation, data->o_arg.fmode))
1480 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001481 rcu_read_unlock();
1482 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001483 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001484 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001485 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001486 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001487 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001488 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1489 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001490 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001491 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001492 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001493 &data->o_res.seq_res,
1494 task) != 0)
1495 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001496 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001497unlock_no_action:
1498 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001499out_no_action:
1500 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001501 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001502}
1503
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504static void nfs4_open_done(struct rpc_task *task, void *calldata)
1505{
1506 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001509
Trond Myklebust14516c32010-07-31 14:29:06 -04001510 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1511 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001512
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001514 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1515 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001516 case S_IFREG:
1517 break;
1518 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001520 break;
1521 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 break;
1524 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001526 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001527 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001528 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001529 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1530 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001531 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001532 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535static void nfs4_open_release(void *calldata)
1536{
1537 struct nfs4_opendata *data = calldata;
1538 struct nfs4_state *state = NULL;
1539
1540 /* If this request hasn't been cancelled, do nothing */
1541 if (data->cancelled == 0)
1542 goto out_free;
1543 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001544 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 goto out_free;
1546 /* In case we need an open_confirm, no cleanup! */
1547 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1548 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001550 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001551 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001553 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554}
1555
1556static const struct rpc_call_ops nfs4_open_ops = {
1557 .rpc_call_prepare = nfs4_open_prepare,
1558 .rpc_call_done = nfs4_open_done,
1559 .rpc_release = nfs4_open_release,
1560};
1561
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001562static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563{
1564 struct inode *dir = data->dir->d_inode;
1565 struct nfs_server *server = NFS_SERVER(dir);
1566 struct nfs_openargs *o_arg = &data->o_arg;
1567 struct nfs_openres *o_res = &data->o_res;
1568 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001569 struct rpc_message msg = {
1570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1571 .rpc_argp = o_arg,
1572 .rpc_resp = o_res,
1573 .rpc_cred = data->owner->so_cred,
1574 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001575 struct rpc_task_setup task_setup_data = {
1576 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001577 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001578 .callback_ops = &nfs4_open_ops,
1579 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001580 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 .flags = RPC_TASK_ASYNC,
1582 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583 int status;
1584
Trond Myklebust9d12b212012-01-17 22:04:25 -05001585 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001586 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001587 data->rpc_done = 0;
1588 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001589 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001590 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001591 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001592 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001593 if (IS_ERR(task))
1594 return PTR_ERR(task);
1595 status = nfs4_wait_for_completion_rpc_task(task);
1596 if (status != 0) {
1597 data->cancelled = 1;
1598 smp_wmb();
1599 } else
1600 status = data->rpc_status;
1601 rpc_put_task(task);
1602
1603 return status;
1604}
1605
1606static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1607{
1608 struct inode *dir = data->dir->d_inode;
1609 struct nfs_openres *o_res = &data->o_res;
1610 int status;
1611
1612 status = nfs4_run_open_task(data, 1);
1613 if (status != 0 || !data->rpc_done)
1614 return status;
1615
Trond Myklebust6926afd2012-01-07 13:22:46 -05001616 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1617
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001618 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1619 status = _nfs4_proc_open_confirm(data);
1620 if (status != 0)
1621 return status;
1622 }
1623
1624 return status;
1625}
1626
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001627static int nfs4_opendata_access(struct rpc_cred *cred,
1628 struct nfs4_opendata *opendata,
1629 struct nfs4_state *state, fmode_t fmode)
1630{
1631 struct nfs_access_entry cache;
1632 u32 mask;
1633
1634 /* access call failed or for some reason the server doesn't
1635 * support any access modes -- defer access call until later */
1636 if (opendata->o_res.access_supported == 0)
1637 return 0;
1638
1639 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001640 /* don't check MAY_WRITE - a newly created file may not have
1641 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001642 if (fmode & FMODE_READ)
1643 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001644 if (fmode & FMODE_EXEC)
1645 mask |= MAY_EXEC;
1646
1647 cache.cred = cred;
1648 cache.jiffies = jiffies;
1649 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1650 nfs_access_add_cache(state->inode, &cache);
1651
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001652 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001653 return 0;
1654
1655 /* even though OPEN succeeded, access is denied. Close the file */
1656 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001657 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001658}
1659
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001660/*
1661 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1662 */
1663static int _nfs4_proc_open(struct nfs4_opendata *data)
1664{
1665 struct inode *dir = data->dir->d_inode;
1666 struct nfs_server *server = NFS_SERVER(dir);
1667 struct nfs_openargs *o_arg = &data->o_arg;
1668 struct nfs_openres *o_res = &data->o_res;
1669 int status;
1670
1671 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001672 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001674 if (status != 0) {
1675 if (status == -NFS4ERR_BADNAME &&
1676 !(o_arg->open_flags & O_CREAT))
1677 return -ENOENT;
1678 return status;
1679 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680
Trond Myklebust6926afd2012-01-07 13:22:46 -05001681 nfs_fattr_map_and_free_names(server, &data->f_attr);
1682
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001683 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001684 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001685 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1686 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001688 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001693 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Andy Adamsond83217c2011-03-01 01:34:17 +00001697static int nfs4_recover_expired_lease(struct nfs_server *server)
1698{
1699 return nfs4_client_recover_expired_lease(server->nfs_client);
1700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702/*
1703 * OPEN_EXPIRED:
1704 * reclaim state on the server after a network partition.
1705 * Assumes caller holds the appropriate lock
1706 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001707static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001709 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001710 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001712 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1713 if (IS_ERR(opendata))
1714 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001715 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001716 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001717 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001718 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001722static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001723{
Trond Myklebust539cd032007-06-05 11:46:42 -04001724 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001725 struct nfs4_exception exception = { };
1726 int err;
1727
1728 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001729 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001730 switch (err) {
1731 default:
1732 goto out;
1733 case -NFS4ERR_GRACE:
1734 case -NFS4ERR_DELAY:
1735 nfs4_handle_exception(server, err, &exception);
1736 err = 0;
1737 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001738 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001739out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001740 return err;
1741}
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001746 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 ctx = nfs4_state_find_open_context(state);
1749 if (IS_ERR(ctx))
1750 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001751 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 put_nfs_open_context(ctx);
1753 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001756#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001757static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001758{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001759 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001760 nfs4_stateid *stateid = &state->stateid;
1761 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001763 /* If a state reset has been done, test_stateid is unneeded */
1764 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1765 return;
1766
1767 status = nfs41_test_stateid(server, stateid);
1768 if (status != NFS_OK) {
1769 /* Free the stateid unless the server explicitly
1770 * informs us the stateid is unrecognized. */
1771 if (status != -NFS4ERR_BAD_STATEID)
1772 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001773 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001774
Bryan Schumaker69388672012-09-26 15:25:52 -04001775 write_seqlock(&state->seqlock);
1776 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1777 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001778 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1779 }
1780}
1781
1782/**
1783 * nfs41_check_open_stateid - possibly free an open stateid
1784 *
1785 * @state: NFSv4 state for an inode
1786 *
1787 * Returns NFS_OK if recovery for this stateid is now finished.
1788 * Otherwise a negative NFS4ERR value is returned.
1789 */
1790static int nfs41_check_open_stateid(struct nfs4_state *state)
1791{
1792 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001793 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001794 int status;
1795
1796 /* If a state reset has been done, test_stateid is unneeded */
1797 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1798 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1799 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1800 return -NFS4ERR_BAD_STATEID;
1801
1802 status = nfs41_test_stateid(server, stateid);
1803 if (status != NFS_OK) {
1804 /* Free the stateid unless the server explicitly
1805 * informs us the stateid is unrecognized. */
1806 if (status != -NFS4ERR_BAD_STATEID)
1807 nfs41_free_stateid(server, stateid);
1808
1809 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1810 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1811 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001812 }
1813 return status;
1814}
1815
1816static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1817{
Chuck Levereb64cf92012-07-11 16:30:05 -04001818 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001819
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001820 nfs41_clear_delegation_stateid(state);
1821 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001822 if (status != NFS_OK)
1823 status = nfs4_open_expired(sp, state);
1824 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001825}
1826#endif
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001829 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1830 * fields corresponding to attributes that were used to store the verifier.
1831 * Make sure we clobber those fields in the later setattr call
1832 */
1833static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1834{
1835 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1836 !(sattr->ia_valid & ATTR_ATIME_SET))
1837 sattr->ia_valid |= ATTR_ATIME;
1838
1839 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1840 !(sattr->ia_valid & ATTR_MTIME_SET))
1841 sattr->ia_valid |= ATTR_MTIME;
1842}
1843
1844/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
Andy Adamson82be4172012-05-23 05:02:35 -04001847static int _nfs4_do_open(struct inode *dir,
1848 struct dentry *dentry,
1849 fmode_t fmode,
1850 int flags,
1851 struct iattr *sattr,
1852 struct rpc_cred *cred,
1853 struct nfs4_state **res,
1854 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 struct nfs4_state_owner *sp;
1857 struct nfs4_state *state = NULL;
1858 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001859 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001860 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001864 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1865 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1867 goto out_err;
1868 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001869 status = nfs4_recover_expired_lease(server);
1870 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001871 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001872 if (dentry->d_inode != NULL)
1873 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001874 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001875 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001876 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001877 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Andy Adamson82be4172012-05-23 05:02:35 -04001879 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1880 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1881 if (!opendata->f_attr.mdsthreshold)
1882 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001883 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001884 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001885 if (dentry->d_inode != NULL)
1886 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001887
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001888 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001890 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001892 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001893 status = PTR_ERR(state);
1894 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001895 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001896 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001897 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001898
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001899 status = nfs4_opendata_access(cred, opendata, state, fmode);
1900 if (status != 0)
1901 goto err_opendata_put;
1902
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001903 if (opendata->o_arg.open_flags & O_EXCL) {
1904 nfs4_exclusive_attrset(opendata, sattr);
1905
1906 nfs_fattr_init(opendata->o_res.f_attr);
1907 status = nfs4_do_setattr(state->inode, cred,
1908 opendata->o_res.f_attr, sattr,
1909 state);
1910 if (status == 0)
1911 nfs_setattr_update_inode(state->inode, sattr);
1912 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1913 }
Andy Adamson82be4172012-05-23 05:02:35 -04001914
1915 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1916 *ctx_th = opendata->f_attr.mdsthreshold;
1917 else
1918 kfree(opendata->f_attr.mdsthreshold);
1919 opendata->f_attr.mdsthreshold = NULL;
1920
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001921 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 *res = state;
1924 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001925err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001926 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001927 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001928err_put_state_owner:
1929 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 *res = NULL;
1932 return status;
1933}
1934
1935
Andy Adamson82be4172012-05-23 05:02:35 -04001936static struct nfs4_state *nfs4_do_open(struct inode *dir,
1937 struct dentry *dentry,
1938 fmode_t fmode,
1939 int flags,
1940 struct iattr *sattr,
1941 struct rpc_cred *cred,
1942 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 struct nfs4_exception exception = { };
1945 struct nfs4_state *res;
1946 int status;
1947
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001948 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001950 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1951 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (status == 0)
1953 break;
1954 /* NOTE: BAD_SEQID means the server and client disagree about the
1955 * book-keeping w.r.t. state-changing operations
1956 * (OPEN/CLOSE/LOCK/LOCKU...)
1957 * It is actually a sign of a bug on the client or on the server.
1958 *
1959 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001960 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * have unhashed the old state_owner for us, and that we can
1962 * therefore safely retry using a new one. We should still warn
1963 * the user though...
1964 */
1965 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001966 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001967 " returned a bad sequence-id error!\n",
1968 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 exception.retry = 1;
1970 continue;
1971 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001972 /*
1973 * BAD_STATEID on OPEN means that the server cancelled our
1974 * state before it received the OPEN_CONFIRM.
1975 * Recover by retrying the request as per the discussion
1976 * on Page 181 of RFC3530.
1977 */
1978 if (status == -NFS4ERR_BAD_STATEID) {
1979 exception.retry = 1;
1980 continue;
1981 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001982 if (status == -EAGAIN) {
1983 /* We must have found a delegation */
1984 exception.retry = 1;
1985 continue;
1986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1988 status, &exception));
1989 } while (exception.retry);
1990 return res;
1991}
1992
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001993static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1994 struct nfs_fattr *fattr, struct iattr *sattr,
1995 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001997 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001999 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 .iap = sattr,
2001 .server = server,
2002 .bitmask = server->attr_bitmask,
2003 };
2004 struct nfs_setattrres res = {
2005 .fattr = fattr,
2006 .server = server,
2007 };
2008 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2010 .rpc_argp = &arg,
2011 .rpc_resp = &res,
2012 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002014 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002015 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Trond Myklebust0e574af2005-10-27 22:12:38 -04002017 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Trond Myklebust4fc87962012-03-08 17:42:01 -05002019 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002020 struct nfs_lockowner lockowner = {
2021 .l_owner = current->files,
2022 .l_pid = current->tgid,
2023 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002024 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002025 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002026 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2027 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002028 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002029 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002030 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Bryan Schumaker7c513052011-03-24 17:12:24 +00002032 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002033 if (status == 0 && state != NULL)
2034 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002035 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002038static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2039 struct nfs_fattr *fattr, struct iattr *sattr,
2040 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002042 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002043 struct nfs4_exception exception = {
2044 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002045 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002046 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int err;
2048 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002049 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2050 switch (err) {
2051 case -NFS4ERR_OPENMODE:
2052 if (state && !(state->state & FMODE_WRITE)) {
2053 err = -EBADF;
2054 if (sattr->ia_valid & ATTR_OPEN)
2055 err = -EACCES;
2056 goto out;
2057 }
2058 }
2059 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002061out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return err;
2063}
2064
2065struct nfs4_closedata {
2066 struct inode *inode;
2067 struct nfs4_state *state;
2068 struct nfs_closeargs arg;
2069 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002070 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002071 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002072 bool roc;
2073 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074};
2075
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002076static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002077{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002078 struct nfs4_closedata *calldata = data;
2079 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002080 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002081
Fred Isamanf7e89172011-01-06 11:36:32 +00002082 if (calldata->roc)
2083 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002084 nfs4_put_open_state(calldata->state);
2085 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002086 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002087 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002088 kfree(calldata);
2089}
2090
Trond Myklebust88069f72009-12-08 08:33:16 -05002091static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2092 fmode_t fmode)
2093{
2094 spin_lock(&state->owner->so_lock);
2095 if (!(fmode & FMODE_READ))
2096 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2097 if (!(fmode & FMODE_WRITE))
2098 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2099 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2100 spin_unlock(&state->owner->so_lock);
2101}
2102
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002103static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002105 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 struct nfs_server *server = NFS_SERVER(calldata->inode);
2108
Chuck Levera3ca5652012-03-01 17:00:40 -05002109 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002110 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /* hmm. we are done with the inode, and in the process of freeing
2113 * the state_owner. we keep this around to process errors
2114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 switch (task->tk_status) {
2116 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002117 if (calldata->roc)
2118 pnfs_roc_set_barrier(state->inode,
2119 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002120 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002121 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002122 nfs4_close_clear_stateid_flags(state,
2123 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 break;
2125 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002126 case -NFS4ERR_OLD_STATEID:
2127 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002129 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002132 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2133 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002135 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002136 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002137 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002140static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002142 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002143 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002144 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002145 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002146
Chuck Levera3ca5652012-03-01 17:00:40 -05002147 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002148 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002149 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002150
Trond Myklebust88069f72009-12-08 08:33:16 -05002151 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2152 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002153 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002154 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002155 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002156 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002157 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2158 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2159 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002160 }
2161 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002162 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2163 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2164 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002165 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002166 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002167 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002168
2169 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002170 /* Note: exit _without_ calling nfs4_close_done */
2171 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002172 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002173 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002174 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002175
Fred Isamanf7e89172011-01-06 11:36:32 +00002176 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002177 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002178 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002179 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002180 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002181 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002182
Trond Myklebust516a6af2005-10-27 22:12:41 -04002183 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002184 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002185 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002186 &calldata->arg.seq_args,
2187 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002188 task) != 0)
2189 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002190out:
2191 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002194static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002195 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002196 .rpc_call_done = nfs4_close_done,
2197 .rpc_release = nfs4_free_closedata,
2198};
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200/*
2201 * It is possible for data to be read/written from a mem-mapped file
2202 * after the sys_close call (which hits the vfs layer as a flush).
2203 * This means that we can't safely call nfsv4 close on a file until
2204 * the inode is cleared. This in turn means that we are not good
2205 * NFSv4 citizens - we do not indicate to the server to update the file's
2206 * share state even when we are done with one of the three share
2207 * stateid's in the inode.
2208 *
2209 * NOTE: Caller must be holding the sp->so_owner semaphore!
2210 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002211int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002213 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002215 struct nfs4_state_owner *sp = state->owner;
2216 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002217 struct rpc_message msg = {
2218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2219 .rpc_cred = state->owner->so_cred,
2220 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002221 struct rpc_task_setup task_setup_data = {
2222 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002223 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002224 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002225 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002226 .flags = RPC_TASK_ASYNC,
2227 };
Trond Myklebust95121352005-10-18 14:20:12 -07002228 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002230 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002232 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002233 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002234 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002236 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002237 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002239 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002240 if (calldata->arg.seqid == NULL)
2241 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002242 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002243 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002244 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002245 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002246 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002247 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002248 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002249
Trond Myklebust1174dd12010-12-21 10:52:24 -05002250 msg.rpc_argp = &calldata->arg;
2251 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002252 task_setup_data.callback_data = calldata;
2253 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002254 if (IS_ERR(task))
2255 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002256 status = 0;
2257 if (wait)
2258 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002259 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002260 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002261out_free_calldata:
2262 kfree(calldata);
2263out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002264 nfs4_put_open_state(state);
2265 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002266 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Trond Myklebust2b484292010-09-17 10:56:51 -04002269static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002270nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 struct nfs4_state *state;
2273
Trond Myklebust565277f2007-10-15 18:17:53 -04002274 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002275 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2276 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002277 if (IS_ERR(state))
2278 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002279 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002280 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Trond Myklebust1185a552009-12-03 15:54:02 -05002283static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002284{
2285 if (ctx->state == NULL)
2286 return;
2287 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002288 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002289 else
Al Viro643168c2011-06-22 18:20:23 -04002290 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002291}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2294{
Benny Halevy43652ad2009-04-01 09:21:54 -04002295 struct nfs4_server_caps_arg args = {
2296 .fhandle = fhandle,
2297 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 struct nfs4_server_caps_res res = {};
2299 struct rpc_message msg = {
2300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002301 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 .rpc_resp = &res,
2303 };
2304 int status;
2305
Bryan Schumaker7c513052011-03-24 17:12:24 +00002306 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (status == 0) {
2308 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002309 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2310 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2311 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2312 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2313 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2315 server->caps |= NFS_CAP_ACLS;
2316 if (res.has_links != 0)
2317 server->caps |= NFS_CAP_HARDLINKS;
2318 if (res.has_symlinks != 0)
2319 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002320 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2321 server->caps |= NFS_CAP_FILEID;
2322 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2323 server->caps |= NFS_CAP_MODE;
2324 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2325 server->caps |= NFS_CAP_NLINK;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2327 server->caps |= NFS_CAP_OWNER;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2329 server->caps |= NFS_CAP_OWNER_GROUP;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2331 server->caps |= NFS_CAP_ATIME;
2332 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2333 server->caps |= NFS_CAP_CTIME;
2334 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2335 server->caps |= NFS_CAP_MTIME;
2336
Trond Myklebusta65318b2009-03-11 14:10:28 -04002337 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2338 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2339 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002341 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return status;
2345}
2346
Trond Myklebust55a97592006-06-09 09:34:19 -04002347int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 struct nfs4_exception exception = { };
2350 int err;
2351 do {
2352 err = nfs4_handle_exception(server,
2353 _nfs4_server_capabilities(server, fhandle),
2354 &exception);
2355 } while (exception.retry);
2356 return err;
2357}
2358
2359static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2360 struct nfs_fsinfo *info)
2361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct nfs4_lookup_root_arg args = {
2363 .bitmask = nfs4_fattr_bitmap,
2364 };
2365 struct nfs4_lookup_res res = {
2366 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002367 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 .fh = fhandle,
2369 };
2370 struct rpc_message msg = {
2371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2372 .rpc_argp = &args,
2373 .rpc_resp = &res,
2374 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002375
Trond Myklebust0e574af2005-10-27 22:12:38 -04002376 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002377 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
2380static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2381 struct nfs_fsinfo *info)
2382{
2383 struct nfs4_exception exception = { };
2384 int err;
2385 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002386 err = _nfs4_lookup_root(server, fhandle, info);
2387 switch (err) {
2388 case 0:
2389 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002390 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002391 default:
2392 err = nfs4_handle_exception(server, err, &exception);
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002395out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return err;
2397}
2398
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002399static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2400 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2401{
2402 struct rpc_auth *auth;
2403 int ret;
2404
2405 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002406 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002407 ret = -EIO;
2408 goto out;
2409 }
2410 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002411out:
2412 return ret;
2413}
2414
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002415static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002416 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002418 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002419 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002421 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002422 if (len < 0)
2423 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002424
2425 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002426 /* AUTH_UNIX is the default flavor if none was specified,
2427 * thus has already been tried. */
2428 if (flav_array[i] == RPC_AUTH_UNIX)
2429 continue;
2430
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002431 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002432 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002433 continue;
2434 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002435 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002436 /*
2437 * -EACCESS could mean that the user doesn't have correct permissions
2438 * to access the mount. It could also mean that we tried to mount
2439 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2440 * existing mount programs don't handle -EACCES very well so it should
2441 * be mapped to -EPERM instead.
2442 */
2443 if (status == -EACCES)
2444 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002445 return status;
2446}
2447
2448/*
2449 * get the file handle for the "/" directory on the server
2450 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002451int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2452 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002453{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002454 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002455 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002456 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2457 /*
2458 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2459 * by nfs4_map_errors() as this function exits.
2460 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002461 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (status == 0)
2463 status = nfs4_server_capabilities(server, fhandle);
2464 if (status == 0)
2465 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002466 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Bryan Schumakerbae36242012-05-10 15:07:31 -04002469static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2470 struct nfs_fsinfo *info)
2471{
2472 int error;
2473 struct nfs_fattr *fattr = info->fattr;
2474
2475 error = nfs4_server_capabilities(server, mntfh);
2476 if (error < 0) {
2477 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2478 return error;
2479 }
2480
2481 error = nfs4_proc_getattr(server, mntfh, fattr);
2482 if (error < 0) {
2483 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2484 return error;
2485 }
2486
2487 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2488 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2489 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2490
2491 return error;
2492}
2493
Manoj Naik6b97fd32006-06-09 09:34:29 -04002494/*
2495 * Get locations and (maybe) other attributes of a referral.
2496 * Note that we'll actually follow the referral later when
2497 * we detect fsid mismatch in inode revalidation
2498 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002499static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2500 const struct qstr *name, struct nfs_fattr *fattr,
2501 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002502{
2503 int status = -ENOMEM;
2504 struct page *page = NULL;
2505 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002506
2507 page = alloc_page(GFP_KERNEL);
2508 if (page == NULL)
2509 goto out;
2510 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2511 if (locations == NULL)
2512 goto out;
2513
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002514 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002515 if (status != 0)
2516 goto out;
2517 /* Make sure server returned a different fsid for the referral */
2518 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002519 dprintk("%s: server did not return a different fsid for"
2520 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002521 status = -EIO;
2522 goto out;
2523 }
Andy Adamson533eb462011-06-13 18:25:56 -04002524 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2525 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002526
Andy Adamson533eb462011-06-13 18:25:56 -04002527 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002528 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002529 memset(fhandle, 0, sizeof(struct nfs_fh));
2530out:
2531 if (page)
2532 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002533 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002534 return status;
2535}
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2538{
2539 struct nfs4_getattr_arg args = {
2540 .fh = fhandle,
2541 .bitmask = server->attr_bitmask,
2542 };
2543 struct nfs4_getattr_res res = {
2544 .fattr = fattr,
2545 .server = server,
2546 };
2547 struct rpc_message msg = {
2548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2549 .rpc_argp = &args,
2550 .rpc_resp = &res,
2551 };
2552
Trond Myklebust0e574af2005-10-27 22:12:38 -04002553 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002554 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
2557static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2558{
2559 struct nfs4_exception exception = { };
2560 int err;
2561 do {
2562 err = nfs4_handle_exception(server,
2563 _nfs4_proc_getattr(server, fhandle, fattr),
2564 &exception);
2565 } while (exception.retry);
2566 return err;
2567}
2568
2569/*
2570 * The file is not closed if it is opened due to the a request to change
2571 * the size of the file. The open call will not be needed once the
2572 * VFS layer lookup-intents are implemented.
2573 *
2574 * Close is called when the inode is destroyed.
2575 * If we haven't opened the file for O_WRONLY, we
2576 * need to in the size_change case to obtain a stateid.
2577 *
2578 * Got race?
2579 * Because OPEN is always done by name in nfsv4, it is
2580 * possible that we opened a different file by the same
2581 * name. We can recognize this race condition, but we
2582 * can't do anything about it besides returning an error.
2583 *
2584 * This will be fixed with VFS changes (lookup-intent).
2585 */
2586static int
2587nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2588 struct iattr *sattr)
2589{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002590 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002591 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002592 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 int status;
2594
Benny Halevy8a1636c2010-07-14 15:43:57 -04002595 if (pnfs_ld_layoutret_on_setattr(inode))
2596 pnfs_return_layout(inode);
2597
Trond Myklebust0e574af2005-10-27 22:12:38 -04002598 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Andy Adamson26699402012-05-30 16:12:24 -04002600 /* Deal with open(O_TRUNC) */
2601 if (sattr->ia_valid & ATTR_OPEN)
2602 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2603
2604 /* Optimization: if the end result is no change, don't RPC */
2605 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2606 return 0;
2607
Trond Myklebustd5308382005-11-04 15:33:38 -05002608 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002609 if (sattr->ia_valid & ATTR_FILE) {
2610 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002611
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002612 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002613 if (ctx) {
2614 cred = ctx->cred;
2615 state = ctx->state;
2616 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002617 }
2618
2619 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002620 if (status == 0)
2621 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return status;
2623}
2624
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002625static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2626 const struct qstr *name, struct nfs_fh *fhandle,
2627 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002628{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002629 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002630 int status;
2631 struct nfs4_lookup_arg args = {
2632 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002633 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002634 .name = name,
2635 };
2636 struct nfs4_lookup_res res = {
2637 .server = server,
2638 .fattr = fattr,
2639 .fh = fhandle,
2640 };
2641 struct rpc_message msg = {
2642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2643 .rpc_argp = &args,
2644 .rpc_resp = &res,
2645 };
2646
2647 nfs_fattr_init(fattr);
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002650 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 dprintk("NFS reply lookup: %d\n", status);
2652 return status;
2653}
2654
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002655static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002656{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002657 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002658 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002659 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2660 fattr->nlink = 2;
2661}
2662
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002663static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2664 struct qstr *name, struct nfs_fh *fhandle,
2665 struct nfs_fattr *fattr)
2666{
2667 struct nfs4_exception exception = { };
2668 struct rpc_clnt *client = *clnt;
2669 int err;
2670 do {
2671 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2672 switch (err) {
2673 case -NFS4ERR_BADNAME:
2674 err = -ENOENT;
2675 goto out;
2676 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002677 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002678 goto out;
2679 case -NFS4ERR_WRONGSEC:
2680 err = -EPERM;
2681 if (client != *clnt)
2682 goto out;
2683
2684 client = nfs4_create_sec_client(client, dir, name);
2685 if (IS_ERR(client))
2686 return PTR_ERR(client);
2687
2688 exception.retry = 1;
2689 break;
2690 default:
2691 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2692 }
2693 } while (exception.retry);
2694
2695out:
2696 if (err == 0)
2697 *clnt = client;
2698 else if (client != *clnt)
2699 rpc_shutdown_client(client);
2700
2701 return err;
2702}
2703
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002704static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002705 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002707 int status;
2708 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002709
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002710 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2711 if (client != NFS_CLIENT(dir)) {
2712 rpc_shutdown_client(client);
2713 nfs_fixup_secinfo_attributes(fattr);
2714 }
2715 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002718struct rpc_clnt *
2719nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2720 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2721{
2722 int status;
2723 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2724
2725 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2726 if (status < 0) {
2727 rpc_shutdown_client(client);
2728 return ERR_PTR(status);
2729 }
2730 return client;
2731}
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2734{
Trond Myklebust76b32992007-08-10 17:45:11 -04002735 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 struct nfs4_accessargs args = {
2737 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002738 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002740 struct nfs4_accessres res = {
2741 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002742 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct rpc_message msg = {
2744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2745 .rpc_argp = &args,
2746 .rpc_resp = &res,
2747 .rpc_cred = entry->cred,
2748 };
2749 int mode = entry->mask;
2750 int status;
2751
2752 /*
2753 * Determine which access bits we want to ask for...
2754 */
2755 if (mode & MAY_READ)
2756 args.access |= NFS4_ACCESS_READ;
2757 if (S_ISDIR(inode->i_mode)) {
2758 if (mode & MAY_WRITE)
2759 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2760 if (mode & MAY_EXEC)
2761 args.access |= NFS4_ACCESS_LOOKUP;
2762 } else {
2763 if (mode & MAY_WRITE)
2764 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2765 if (mode & MAY_EXEC)
2766 args.access |= NFS4_ACCESS_EXECUTE;
2767 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002768
2769 res.fattr = nfs_alloc_fattr();
2770 if (res.fattr == NULL)
2771 return -ENOMEM;
2772
Bryan Schumaker7c513052011-03-24 17:12:24 +00002773 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002775 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002776 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002778 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return status;
2780}
2781
2782static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2783{
2784 struct nfs4_exception exception = { };
2785 int err;
2786 do {
2787 err = nfs4_handle_exception(NFS_SERVER(inode),
2788 _nfs4_proc_access(inode, entry),
2789 &exception);
2790 } while (exception.retry);
2791 return err;
2792}
2793
2794/*
2795 * TODO: For the time being, we don't try to get any attributes
2796 * along with any of the zero-copy operations READ, READDIR,
2797 * READLINK, WRITE.
2798 *
2799 * In the case of the first three, we want to put the GETATTR
2800 * after the read-type operation -- this is because it is hard
2801 * to predict the length of a GETATTR response in v4, and thus
2802 * align the READ data correctly. This means that the GETATTR
2803 * may end up partially falling into the page cache, and we should
2804 * shift it into the 'tail' of the xdr_buf before processing.
2805 * To do this efficiently, we need to know the total length
2806 * of data received, which doesn't seem to be available outside
2807 * of the RPC layer.
2808 *
2809 * In the case of WRITE, we also want to put the GETATTR after
2810 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002811 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 *
2813 * Both of these changes to the XDR layer would in fact be quite
2814 * minor, but I decided to leave them for a subsequent patch.
2815 */
2816static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2817 unsigned int pgbase, unsigned int pglen)
2818{
2819 struct nfs4_readlink args = {
2820 .fh = NFS_FH(inode),
2821 .pgbase = pgbase,
2822 .pglen = pglen,
2823 .pages = &page,
2824 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002825 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 struct rpc_message msg = {
2827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2828 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002829 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 };
2831
Bryan Schumaker7c513052011-03-24 17:12:24 +00002832 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 -07002833}
2834
2835static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2836 unsigned int pgbase, unsigned int pglen)
2837{
2838 struct nfs4_exception exception = { };
2839 int err;
2840 do {
2841 err = nfs4_handle_exception(NFS_SERVER(inode),
2842 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2843 &exception);
2844 } while (exception.retry);
2845 return err;
2846}
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002849 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851static int
2852nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002853 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002855 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 int status = 0;
2858
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002859 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2860 if (IS_ERR(ctx))
2861 return PTR_ERR(ctx);
2862
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002863 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002864 state = nfs4_do_open(dir, dentry, ctx->mode,
2865 flags, sattr, ctx->cred,
2866 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002867 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (IS_ERR(state)) {
2869 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002870 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002872 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002873 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002874 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002876 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return status;
2878}
2879
2880static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2881{
Trond Myklebust16e42952005-10-27 22:12:44 -04002882 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002883 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002885 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002887 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002888 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002889 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2892 .rpc_argp = &args,
2893 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002895 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002896
Bryan Schumaker7c513052011-03-24 17:12:24 +00002897 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002898 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002899 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return status;
2901}
2902
2903static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2904{
2905 struct nfs4_exception exception = { };
2906 int err;
2907 do {
2908 err = nfs4_handle_exception(NFS_SERVER(dir),
2909 _nfs4_proc_remove(dir, name),
2910 &exception);
2911 } while (exception.retry);
2912 return err;
2913}
2914
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002915static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002917 struct nfs_server *server = NFS_SERVER(dir);
2918 struct nfs_removeargs *args = msg->rpc_argp;
2919 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002921 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002923 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002926static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2927{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002928 nfs4_setup_sequence(NFS_SERVER(data->dir),
2929 &data->args.seq_args,
2930 &data->res.seq_res,
2931 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002934static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002936 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2937
Trond Myklebust14516c32010-07-31 14:29:06 -04002938 if (!nfs4_sequence_done(task, &res->seq_res))
2939 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002940 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002941 return 0;
2942 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002943 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
Jeff Laytond3d41522010-09-17 17:31:57 -04002946static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2947{
2948 struct nfs_server *server = NFS_SERVER(dir);
2949 struct nfs_renameargs *arg = msg->rpc_argp;
2950 struct nfs_renameres *res = msg->rpc_resp;
2951
2952 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002953 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002954 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002955}
2956
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002957static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2958{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002959 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2960 &data->args.seq_args,
2961 &data->res.seq_res,
2962 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002963}
2964
2965static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2966 struct inode *new_dir)
2967{
2968 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2969
2970 if (!nfs4_sequence_done(task, &res->seq_res))
2971 return 0;
2972 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2973 return 0;
2974
2975 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002976 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002977 return 1;
2978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2981 struct inode *new_dir, struct qstr *new_name)
2982{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002983 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002984 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 .old_dir = NFS_FH(old_dir),
2986 .new_dir = NFS_FH(new_dir),
2987 .old_name = old_name,
2988 .new_name = new_name,
2989 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002990 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002991 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct rpc_message msg = {
2994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2995 .rpc_argp = &arg,
2996 .rpc_resp = &res,
2997 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002998 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Bryan Schumaker7c513052011-03-24 17:12:24 +00003000 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 if (!status) {
3002 update_changeattr(old_dir, &res.old_cinfo);
3003 update_changeattr(new_dir, &res.new_cinfo);
3004 }
3005 return status;
3006}
3007
3008static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3009 struct inode *new_dir, struct qstr *new_name)
3010{
3011 struct nfs4_exception exception = { };
3012 int err;
3013 do {
3014 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3015 _nfs4_proc_rename(old_dir, old_name,
3016 new_dir, new_name),
3017 &exception);
3018 } while (exception.retry);
3019 return err;
3020}
3021
3022static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3023{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003024 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 struct nfs4_link_arg arg = {
3026 .fh = NFS_FH(inode),
3027 .dir_fh = NFS_FH(dir),
3028 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003029 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003031 struct nfs4_link_res res = {
3032 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003033 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 struct rpc_message msg = {
3035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3036 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003037 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003039 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Trond Myklebust136f2622010-04-16 16:22:49 -04003041 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003042 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003043 goto out;
3044
Bryan Schumaker7c513052011-03-24 17:12:24 +00003045 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003046 if (!status) {
3047 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003048 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003049 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003050out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003051 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return status;
3053}
3054
3055static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3056{
3057 struct nfs4_exception exception = { };
3058 int err;
3059 do {
3060 err = nfs4_handle_exception(NFS_SERVER(inode),
3061 _nfs4_proc_link(inode, dir, name),
3062 &exception);
3063 } while (exception.retry);
3064 return err;
3065}
3066
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003067struct nfs4_createdata {
3068 struct rpc_message msg;
3069 struct nfs4_create_arg arg;
3070 struct nfs4_create_res res;
3071 struct nfs_fh fh;
3072 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003073};
3074
3075static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3076 struct qstr *name, struct iattr *sattr, u32 ftype)
3077{
3078 struct nfs4_createdata *data;
3079
3080 data = kzalloc(sizeof(*data), GFP_KERNEL);
3081 if (data != NULL) {
3082 struct nfs_server *server = NFS_SERVER(dir);
3083
3084 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3085 data->msg.rpc_argp = &data->arg;
3086 data->msg.rpc_resp = &data->res;
3087 data->arg.dir_fh = NFS_FH(dir);
3088 data->arg.server = server;
3089 data->arg.name = name;
3090 data->arg.attrs = sattr;
3091 data->arg.ftype = ftype;
3092 data->arg.bitmask = server->attr_bitmask;
3093 data->res.server = server;
3094 data->res.fh = &data->fh;
3095 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003096 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003097 }
3098 return data;
3099}
3100
3101static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3102{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003103 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003104 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003105 if (status == 0) {
3106 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3108 }
3109 return status;
3110}
3111
3112static void nfs4_free_createdata(struct nfs4_createdata *data)
3113{
3114 kfree(data);
3115}
3116
Chuck Lever4f390c12006-08-22 20:06:22 -04003117static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003118 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003120 struct nfs4_createdata *data;
3121 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Chuck Lever94a6d752006-08-22 20:06:23 -04003123 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003124 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003125
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003126 status = -ENOMEM;
3127 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3128 if (data == NULL)
3129 goto out;
3130
3131 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3132 data->arg.u.symlink.pages = &page;
3133 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003135 status = nfs4_do_create(dir, dentry, data);
3136
3137 nfs4_free_createdata(data);
3138out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 return status;
3140}
3141
Chuck Lever4f390c12006-08-22 20:06:22 -04003142static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003143 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
3145 struct nfs4_exception exception = { };
3146 int err;
3147 do {
3148 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003149 _nfs4_proc_symlink(dir, dentry, page,
3150 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 &exception);
3152 } while (exception.retry);
3153 return err;
3154}
3155
3156static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3157 struct iattr *sattr)
3158{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003159 struct nfs4_createdata *data;
3160 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003162 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3163 if (data == NULL)
3164 goto out;
3165
3166 status = nfs4_do_create(dir, dentry, data);
3167
3168 nfs4_free_createdata(data);
3169out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 return status;
3171}
3172
3173static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3174 struct iattr *sattr)
3175{
3176 struct nfs4_exception exception = { };
3177 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003178
3179 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 do {
3181 err = nfs4_handle_exception(NFS_SERVER(dir),
3182 _nfs4_proc_mkdir(dir, dentry, sattr),
3183 &exception);
3184 } while (exception.retry);
3185 return err;
3186}
3187
3188static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003189 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 struct inode *dir = dentry->d_inode;
3192 struct nfs4_readdir_arg args = {
3193 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003194 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 .pgbase = 0,
3196 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003197 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003198 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 };
3200 struct nfs4_readdir_res res;
3201 struct rpc_message msg = {
3202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3203 .rpc_argp = &args,
3204 .rpc_resp = &res,
3205 .rpc_cred = cred,
3206 };
3207 int status;
3208
Harvey Harrison3110ff82008-05-02 13:42:44 -07003209 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003210 dentry->d_parent->d_name.name,
3211 dentry->d_name.name,
3212 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003213 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003215 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 -05003216 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003217 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003218 status += args.pgbase;
3219 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003220
3221 nfs_invalidate_atime(dir);
3222
Harvey Harrison3110ff82008-05-02 13:42:44 -07003223 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return status;
3225}
3226
3227static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003228 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 struct nfs4_exception exception = { };
3231 int err;
3232 do {
3233 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3234 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003235 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 &exception);
3237 } while (exception.retry);
3238 return err;
3239}
3240
3241static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3242 struct iattr *sattr, dev_t rdev)
3243{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003244 struct nfs4_createdata *data;
3245 int mode = sattr->ia_mode;
3246 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003248 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3249 if (data == NULL)
3250 goto out;
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003253 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003255 data->arg.ftype = NF4BLK;
3256 data->arg.u.device.specdata1 = MAJOR(rdev);
3257 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
3259 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003260 data->arg.ftype = NF4CHR;
3261 data->arg.u.device.specdata1 = MAJOR(rdev);
3262 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003263 } else if (!S_ISSOCK(mode)) {
3264 status = -EINVAL;
3265 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003268 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003269out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003270 nfs4_free_createdata(data);
3271out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return status;
3273}
3274
3275static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3276 struct iattr *sattr, dev_t rdev)
3277{
3278 struct nfs4_exception exception = { };
3279 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003280
3281 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 do {
3283 err = nfs4_handle_exception(NFS_SERVER(dir),
3284 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3285 &exception);
3286 } while (exception.retry);
3287 return err;
3288}
3289
3290static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3291 struct nfs_fsstat *fsstat)
3292{
3293 struct nfs4_statfs_arg args = {
3294 .fh = fhandle,
3295 .bitmask = server->attr_bitmask,
3296 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003297 struct nfs4_statfs_res res = {
3298 .fsstat = fsstat,
3299 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct rpc_message msg = {
3301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3302 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003303 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 };
3305
Trond Myklebust0e574af2005-10-27 22:12:38 -04003306 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003307 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309
3310static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3311{
3312 struct nfs4_exception exception = { };
3313 int err;
3314 do {
3315 err = nfs4_handle_exception(server,
3316 _nfs4_proc_statfs(server, fhandle, fsstat),
3317 &exception);
3318 } while (exception.retry);
3319 return err;
3320}
3321
3322static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3323 struct nfs_fsinfo *fsinfo)
3324{
3325 struct nfs4_fsinfo_arg args = {
3326 .fh = fhandle,
3327 .bitmask = server->attr_bitmask,
3328 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003329 struct nfs4_fsinfo_res res = {
3330 .fsinfo = fsinfo,
3331 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 struct rpc_message msg = {
3333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3334 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003335 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 };
3337
Bryan Schumaker7c513052011-03-24 17:12:24 +00003338 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339}
3340
3341static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3342{
3343 struct nfs4_exception exception = { };
3344 int err;
3345
3346 do {
3347 err = nfs4_handle_exception(server,
3348 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3349 &exception);
3350 } while (exception.retry);
3351 return err;
3352}
3353
3354static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3355{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003356 int error;
3357
Trond Myklebust0e574af2005-10-27 22:12:38 -04003358 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003359 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003360 if (error == 0) {
3361 /* block layout checks this! */
3362 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003363 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003364 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003365
3366 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
3369static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3370 struct nfs_pathconf *pathconf)
3371{
3372 struct nfs4_pathconf_arg args = {
3373 .fh = fhandle,
3374 .bitmask = server->attr_bitmask,
3375 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003376 struct nfs4_pathconf_res res = {
3377 .pathconf = pathconf,
3378 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 struct rpc_message msg = {
3380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3381 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003382 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 };
3384
3385 /* None of the pathconf attributes are mandatory to implement */
3386 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3387 memset(pathconf, 0, sizeof(*pathconf));
3388 return 0;
3389 }
3390
Trond Myklebust0e574af2005-10-27 22:12:38 -04003391 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003392 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
3395static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3396 struct nfs_pathconf *pathconf)
3397{
3398 struct nfs4_exception exception = { };
3399 int err;
3400
3401 do {
3402 err = nfs4_handle_exception(server,
3403 _nfs4_proc_pathconf(server, fhandle, pathconf),
3404 &exception);
3405 } while (exception.retry);
3406 return err;
3407}
3408
Benny Halevyd20581a2011-05-22 19:52:03 +03003409void __nfs4_read_done_cb(struct nfs_read_data *data)
3410{
Fred Isamancd841602012-04-20 14:47:44 -04003411 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003412}
3413
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003414static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
Fred Isamancd841602012-04-20 14:47:44 -04003416 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003418 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003419 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003420 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003422
Benny Halevyd20581a2011-05-22 19:52:03 +03003423 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003425 renew_lease(server, data->timestamp);
3426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003429static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3430{
3431
3432 dprintk("--> %s\n", __func__);
3433
3434 if (!nfs4_sequence_done(task, &data->res.seq_res))
3435 return -EAGAIN;
3436
Benny Halevyd20581a2011-05-22 19:52:03 +03003437 return data->read_done_cb ? data->read_done_cb(task, data) :
3438 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003439}
3440
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003441static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003444 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003445 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003446 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003449static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3450{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003451 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3452 &data->args.seq_args,
3453 &data->res.seq_res,
3454 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
Fred Isamanb029bc92011-03-03 15:13:42 +00003457static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Fred Isamancd841602012-04-20 14:47:44 -04003459 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003461 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003462 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003463 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003465 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003467 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003468 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
Fred Isamanb029bc92011-03-03 15:13:42 +00003472static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3473{
3474 if (!nfs4_sequence_done(task, &data->res.seq_res))
3475 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003476 return data->write_done_cb ? data->write_done_cb(task, data) :
3477 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003478}
3479
Trond Myklebust5a37f852012-04-28 14:55:16 -04003480static
3481bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003482{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003483 const struct nfs_pgio_header *hdr = data->header;
3484
3485 /* Don't request attributes for pNFS or O_DIRECT writes */
3486 if (data->ds_clp != NULL || hdr->dreq != NULL)
3487 return false;
3488 /* Otherwise, request attributes if and only if we don't hold
3489 * a delegation
3490 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003491 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003492}
Fred Isamana69aef12011-03-03 15:13:47 +00003493
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003494static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Fred Isamancd841602012-04-20 14:47:44 -04003496 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003497
Trond Myklebust5a37f852012-04-28 14:55:16 -04003498 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003499 data->args.bitmask = NULL;
3500 data->res.fattr = NULL;
3501 } else
3502 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003503
Fred Isamanb029bc92011-03-03 15:13:42 +00003504 if (!data->write_done_cb)
3505 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003506 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 data->timestamp = jiffies;
3508
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003509 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003510 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003513static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3514{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003515 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3516 &data->args.seq_args,
3517 &data->res.seq_res,
3518 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
Fred Isaman0b7c0152012-04-20 14:47:39 -04003521static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3522{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003523 nfs4_setup_sequence(NFS_SERVER(data->inode),
3524 &data->args.seq_args,
3525 &data->res.seq_res,
3526 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Fred Isaman0b7c0152012-04-20 14:47:39 -04003529static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003532
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003533 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003534 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003535 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
3539
Fred Isaman0b7c0152012-04-20 14:47:39 -04003540static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003541{
3542 if (!nfs4_sequence_done(task, &data->res.seq_res))
3543 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003544 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003545}
3546
Fred Isaman0b7c0152012-04-20 14:47:39 -04003547static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003549 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003550
Fred Isaman0b7c0152012-04-20 14:47:39 -04003551 if (data->commit_done_cb == NULL)
3552 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003553 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003554 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003555 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
3557
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003558struct nfs4_renewdata {
3559 struct nfs_client *client;
3560 unsigned long timestamp;
3561};
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563/*
3564 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3565 * standalone procedure for queueing an asynchronous RENEW.
3566 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003567static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003568{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003569 struct nfs4_renewdata *data = calldata;
3570 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003571
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003572 if (atomic_read(&clp->cl_count) > 1)
3573 nfs4_schedule_state_renewal(clp);
3574 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003575 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003576}
3577
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003578static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003580 struct nfs4_renewdata *data = calldata;
3581 struct nfs_client *clp = data->client;
3582 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003585 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003586 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3587 return;
3588 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003589 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003590 return;
3591 }
3592 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003594 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003597static const struct rpc_call_ops nfs4_renew_ops = {
3598 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003599 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003600};
3601
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003602static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
3604 struct rpc_message msg = {
3605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3606 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003607 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003609 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003611 if (renew_flags == 0)
3612 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003613 if (!atomic_inc_not_zero(&clp->cl_count))
3614 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003615 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003616 if (data == NULL)
3617 return -ENOMEM;
3618 data->client = clp;
3619 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003621 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003624static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 struct rpc_message msg = {
3627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3628 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003629 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 };
3631 unsigned long now = jiffies;
3632 int status;
3633
3634 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3635 if (status < 0)
3636 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003637 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return 0;
3639}
3640
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003641static inline int nfs4_server_supports_acls(struct nfs_server *server)
3642{
3643 return (server->caps & NFS_CAP_ACLS)
3644 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3645 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3646}
3647
Trond Myklebust21f498c2012-08-24 10:59:25 -04003648/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3649 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003650 * the stack.
3651 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003652#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003653
Neil Hormane9e3d722011-03-04 19:26:03 -05003654static int buf_to_pages_noslab(const void *buf, size_t buflen,
3655 struct page **pages, unsigned int *pgbase)
3656{
3657 struct page *newpage, **spages;
3658 int rc = 0;
3659 size_t len;
3660 spages = pages;
3661
3662 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003663 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003664 newpage = alloc_page(GFP_KERNEL);
3665
3666 if (newpage == NULL)
3667 goto unwind;
3668 memcpy(page_address(newpage), buf, len);
3669 buf += len;
3670 buflen -= len;
3671 *pages++ = newpage;
3672 rc++;
3673 } while (buflen != 0);
3674
3675 return rc;
3676
3677unwind:
3678 for(; rc > 0; rc--)
3679 __free_page(spages[rc-1]);
3680 return -ENOMEM;
3681}
3682
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003683struct nfs4_cached_acl {
3684 int cached;
3685 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003686 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003687};
3688
3689static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003690{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003691 struct nfs_inode *nfsi = NFS_I(inode);
3692
3693 spin_lock(&inode->i_lock);
3694 kfree(nfsi->nfs4_acl);
3695 nfsi->nfs4_acl = acl;
3696 spin_unlock(&inode->i_lock);
3697}
3698
3699static void nfs4_zap_acl_attr(struct inode *inode)
3700{
3701 nfs4_set_cached_acl(inode, NULL);
3702}
3703
3704static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3705{
3706 struct nfs_inode *nfsi = NFS_I(inode);
3707 struct nfs4_cached_acl *acl;
3708 int ret = -ENOENT;
3709
3710 spin_lock(&inode->i_lock);
3711 acl = nfsi->nfs4_acl;
3712 if (acl == NULL)
3713 goto out;
3714 if (buf == NULL) /* user is just asking for length */
3715 goto out_len;
3716 if (acl->cached == 0)
3717 goto out;
3718 ret = -ERANGE; /* see getxattr(2) man page */
3719 if (acl->len > buflen)
3720 goto out;
3721 memcpy(buf, acl->data, acl->len);
3722out_len:
3723 ret = acl->len;
3724out:
3725 spin_unlock(&inode->i_lock);
3726 return ret;
3727}
3728
Sachin Prabhu5794d212012-04-17 14:36:40 +01003729static 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 +00003730{
3731 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003732 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003733
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003734 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003735 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003736 if (acl == NULL)
3737 goto out;
3738 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003739 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740 } else {
3741 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3742 if (acl == NULL)
3743 goto out;
3744 acl->cached = 0;
3745 }
3746 acl->len = acl_len;
3747out:
3748 nfs4_set_cached_acl(inode, acl);
3749}
3750
Andy Adamsonbf118a32011-12-07 11:55:27 -05003751/*
3752 * The getxattr API returns the required buffer length when called with a
3753 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3754 * the required buf. On a NULL buf, we send a page of data to the server
3755 * guessing that the ACL request can be serviced by a page. If so, we cache
3756 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3757 * the cache. If not so, we throw away the page, and cache the required
3758 * length. The next getxattr call will then produce another round trip to
3759 * the server, this time with the input buf of the required size.
3760 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003761static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003762{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003763 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003764 struct nfs_getaclargs args = {
3765 .fh = NFS_FH(inode),
3766 .acl_pages = pages,
3767 .acl_len = buflen,
3768 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003769 struct nfs_getaclres res = {
3770 .acl_len = buflen,
3771 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003772 struct rpc_message msg = {
3773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3774 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003775 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003776 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003777 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3778 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003779
Andy Adamsonbf118a32011-12-07 11:55:27 -05003780 /* As long as we're doing a round trip to the server anyway,
3781 * let's be prepared for a page of acl data. */
3782 if (npages == 0)
3783 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003784 if (npages > ARRAY_SIZE(pages))
3785 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003786
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787 for (i = 0; i < npages; i++) {
3788 pages[i] = alloc_page(GFP_KERNEL);
3789 if (!pages[i])
3790 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003791 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003792
3793 /* for decoding across pages */
3794 res.acl_scratch = alloc_page(GFP_KERNEL);
3795 if (!res.acl_scratch)
3796 goto out_free;
3797
Andy Adamsonbf118a32011-12-07 11:55:27 -05003798 args.acl_len = npages * PAGE_SIZE;
3799 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003800
Peng Taode040be2012-01-10 22:42:47 +08003801 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003802 __func__, buf, buflen, npages, args.acl_len);
3803 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3804 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003805 if (ret)
3806 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003807
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003808 /* Handle the case where the passed-in buffer is too short */
3809 if (res.acl_flags & NFS4_ACL_TRUNC) {
3810 /* Did the user only issue a request for the acl length? */
3811 if (buf == NULL)
3812 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003813 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003814 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003816 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003817 if (buf) {
3818 if (res.acl_len > buflen) {
3819 ret = -ERANGE;
3820 goto out_free;
3821 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003822 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003823 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003824out_ok:
3825 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003826out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003827 for (i = 0; i < npages; i++)
3828 if (pages[i])
3829 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003830 if (res.acl_scratch)
3831 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003832 return ret;
3833}
3834
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003835static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3836{
3837 struct nfs4_exception exception = { };
3838 ssize_t ret;
3839 do {
3840 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3841 if (ret >= 0)
3842 break;
3843 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3844 } while (exception.retry);
3845 return ret;
3846}
3847
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003848static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3849{
3850 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003851 int ret;
3852
3853 if (!nfs4_server_supports_acls(server))
3854 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003855 ret = nfs_revalidate_inode(server, inode);
3856 if (ret < 0)
3857 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003858 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3859 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860 ret = nfs4_read_cached_acl(inode, buf, buflen);
3861 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003862 /* -ENOENT is returned if there is no ACL or if there is an ACL
3863 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003864 return ret;
3865 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003866}
3867
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003868static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003869{
3870 struct nfs_server *server = NFS_SERVER(inode);
3871 struct page *pages[NFS4ACL_MAXPAGES];
3872 struct nfs_setaclargs arg = {
3873 .fh = NFS_FH(inode),
3874 .acl_pages = pages,
3875 .acl_len = buflen,
3876 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003877 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003878 struct rpc_message msg = {
3879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3880 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003881 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003882 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003883 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003884 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003885
3886 if (!nfs4_server_supports_acls(server))
3887 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003888 if (npages > ARRAY_SIZE(pages))
3889 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003890 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3891 if (i < 0)
3892 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003893 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003894 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003895
3896 /*
3897 * Free each page after tx, so the only ref left is
3898 * held by the network stack
3899 */
3900 for (; i > 0; i--)
3901 put_page(pages[i-1]);
3902
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003903 /*
3904 * Acl update can result in inode attribute update.
3905 * so mark the attribute cache invalid.
3906 */
3907 spin_lock(&inode->i_lock);
3908 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3909 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003910 nfs_access_zap_cache(inode);
3911 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003912 return ret;
3913}
3914
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003915static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3916{
3917 struct nfs4_exception exception = { };
3918 int err;
3919 do {
3920 err = nfs4_handle_exception(NFS_SERVER(inode),
3921 __nfs4_proc_set_acl(inode, buf, buflen),
3922 &exception);
3923 } while (exception.retry);
3924 return err;
3925}
3926
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003928nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003930 struct nfs_client *clp = server->nfs_client;
3931
3932 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return 0;
3934 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003935 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003936 case -NFS4ERR_ADMIN_REVOKED:
3937 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003938 if (state == NULL)
3939 break;
3940 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003941 case -NFS4ERR_OPENMODE:
3942 if (state == NULL)
3943 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003944 nfs4_schedule_stateid_recovery(server, state);
3945 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003946 case -NFS4ERR_EXPIRED:
3947 if (state != NULL)
3948 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003950 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003951 nfs4_schedule_lease_recovery(clp);
3952 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003953#if defined(CONFIG_NFS_V4_1)
3954 case -NFS4ERR_BADSESSION:
3955 case -NFS4ERR_BADSLOT:
3956 case -NFS4ERR_BAD_HIGH_SLOT:
3957 case -NFS4ERR_DEADSESSION:
3958 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3959 case -NFS4ERR_SEQ_FALSE_RETRY:
3960 case -NFS4ERR_SEQ_MISORDERED:
3961 dprintk("%s ERROR %d, Reset session\n", __func__,
3962 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003963 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003964 task->tk_status = 0;
3965 return -EAGAIN;
3966#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003968 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003969 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3971 task->tk_status = 0;
3972 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003973 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 case -NFS4ERR_OLD_STATEID:
3975 task->tk_status = 0;
3976 return -EAGAIN;
3977 }
3978 task->tk_status = nfs4_map_errors(task->tk_status);
3979 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003980wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003981 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003982 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3983 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3984 task->tk_status = 0;
3985 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986}
3987
Chuck Leverf0920752012-05-21 22:45:41 -04003988static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3989 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003990{
3991 __be32 verf[2];
3992
Chuck Lever2c820d92012-05-21 22:45:33 -04003993 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3994 /* An impossible timestamp guarantees this value
3995 * will never match a generated boot time. */
3996 verf[0] = 0;
3997 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3998 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003999 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4000 verf[0] = (__be32)nn->boot_time.tv_sec;
4001 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004002 }
Chuck Levercd937102012-03-02 17:14:31 -05004003 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4004}
4005
Chuck Levere984a552012-09-14 17:24:21 -04004006static unsigned int
4007nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4008 char *buf, size_t len)
4009{
4010 unsigned int result;
4011
4012 rcu_read_lock();
4013 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4014 clp->cl_ipaddr,
4015 rpc_peeraddr2str(clp->cl_rpcclient,
4016 RPC_DISPLAY_ADDR),
4017 rpc_peeraddr2str(clp->cl_rpcclient,
4018 RPC_DISPLAY_PROTO));
4019 rcu_read_unlock();
4020 return result;
4021}
4022
4023static unsigned int
4024nfs4_init_uniform_client_string(const struct nfs_client *clp,
4025 char *buf, size_t len)
4026{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004027 char *nodename = clp->cl_rpcclient->cl_nodename;
4028
4029 if (nfs4_client_id_uniquifier[0] != '\0')
4030 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004031 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4032 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004033 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004034}
4035
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004036/**
4037 * nfs4_proc_setclientid - Negotiate client ID
4038 * @clp: state data structure
4039 * @program: RPC program for NFSv4 callback service
4040 * @port: IP port number for NFS4 callback service
4041 * @cred: RPC credential to use for this call
4042 * @res: where to place the result
4043 *
4044 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4045 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004046int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4047 unsigned short port, struct rpc_cred *cred,
4048 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
4050 nfs4_verifier sc_verifier;
4051 struct nfs4_setclientid setclientid = {
4052 .sc_verifier = &sc_verifier,
4053 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004054 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 };
4056 struct rpc_message msg = {
4057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4058 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004059 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004060 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004062 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Chuck Leverde734832012-07-11 16:30:50 -04004064 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004065 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004066 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4067 setclientid.sc_name_len =
4068 nfs4_init_uniform_client_string(clp,
4069 setclientid.sc_name,
4070 sizeof(setclientid.sc_name));
4071 else
4072 setclientid.sc_name_len =
4073 nfs4_init_nonuniform_client_string(clp,
4074 setclientid.sc_name,
4075 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004076 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004077 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004078 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004079 sizeof(setclientid.sc_netid),
4080 rpc_peeraddr2str(clp->cl_rpcclient,
4081 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004082 rcu_read_unlock();
4083 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004084 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 clp->cl_ipaddr, port >> 8, port & 255);
4086
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004087 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4088 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4089 setclientid.sc_name_len, setclientid.sc_name);
4090 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4091 dprintk("NFS reply setclientid: %d\n", status);
4092 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093}
4094
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004095/**
4096 * nfs4_proc_setclientid_confirm - Confirm client ID
4097 * @clp: state data structure
4098 * @res: result of a previous SETCLIENTID
4099 * @cred: RPC credential to use for this call
4100 *
4101 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4102 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004103int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004104 struct nfs4_setclientid_res *arg,
4105 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
4107 struct nfs_fsinfo fsinfo;
4108 struct rpc_message msg = {
4109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004110 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004112 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 };
4114 unsigned long now;
4115 int status;
4116
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004117 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4118 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4119 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004121 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 if (status == 0) {
4123 spin_lock(&clp->cl_lock);
4124 clp->cl_lease_time = fsinfo.lease_time * HZ;
4125 clp->cl_last_renewal = now;
4126 spin_unlock(&clp->cl_lock);
4127 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004128 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 return status;
4130}
4131
Trond Myklebustfe650402006-01-03 09:55:18 +01004132struct nfs4_delegreturndata {
4133 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004134 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004135 struct nfs_fh fh;
4136 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004137 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004138 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004139 int rpc_status;
4140};
4141
Trond Myklebustfe650402006-01-03 09:55:18 +01004142static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4143{
4144 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004145
Trond Myklebust14516c32010-07-31 14:29:06 -04004146 if (!nfs4_sequence_done(task, &data->res.seq_res))
4147 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004148
Ricardo Labiaga79708862009-12-07 09:23:21 -05004149 switch (task->tk_status) {
4150 case -NFS4ERR_STALE_STATEID:
4151 case -NFS4ERR_EXPIRED:
4152 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004153 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004154 break;
4155 default:
4156 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4157 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004158 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004159 return;
4160 }
4161 }
4162 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004163}
4164
4165static void nfs4_delegreturn_release(void *calldata)
4166{
Trond Myklebustfe650402006-01-03 09:55:18 +01004167 kfree(calldata);
4168}
4169
Andy Adamson938e1012009-04-01 09:22:28 -04004170#if defined(CONFIG_NFS_V4_1)
4171static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4172{
4173 struct nfs4_delegreturndata *d_data;
4174
4175 d_data = (struct nfs4_delegreturndata *)data;
4176
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004177 nfs4_setup_sequence(d_data->res.server,
4178 &d_data->args.seq_args,
4179 &d_data->res.seq_res,
4180 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004181}
4182#endif /* CONFIG_NFS_V4_1 */
4183
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004184static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004185#if defined(CONFIG_NFS_V4_1)
4186 .rpc_call_prepare = nfs4_delegreturn_prepare,
4187#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004188 .rpc_call_done = nfs4_delegreturn_done,
4189 .rpc_release = nfs4_delegreturn_release,
4190};
4191
Trond Myklebuste6f81072008-01-24 18:14:34 -05004192static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004193{
4194 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004195 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004196 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004197 struct rpc_message msg = {
4198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4199 .rpc_cred = cred,
4200 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004201 struct rpc_task_setup task_setup_data = {
4202 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004203 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004204 .callback_ops = &nfs4_delegreturn_ops,
4205 .flags = RPC_TASK_ASYNC,
4206 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004207 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004208
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004209 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004210 if (data == NULL)
4211 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004212 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004213 data->args.fhandle = &data->fh;
4214 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004215 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004216 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004217 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004218 data->res.fattr = &data->fattr;
4219 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004220 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004221 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004222 data->rpc_status = 0;
4223
Trond Myklebustc970aa82007-07-14 15:39:59 -04004224 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004225 msg.rpc_argp = &data->args;
4226 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004227 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004228 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004229 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004230 if (!issync)
4231 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004232 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004233 if (status != 0)
4234 goto out;
4235 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004236 if (status == 0)
4237 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4238 else
4239 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004240out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004241 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004242 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243}
4244
Trond Myklebuste6f81072008-01-24 18:14:34 -05004245int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
4247 struct nfs_server *server = NFS_SERVER(inode);
4248 struct nfs4_exception exception = { };
4249 int err;
4250 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004251 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 switch (err) {
4253 case -NFS4ERR_STALE_STATEID:
4254 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 case 0:
4256 return 0;
4257 }
4258 err = nfs4_handle_exception(server, err, &exception);
4259 } while (exception.retry);
4260 return err;
4261}
4262
4263#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4264#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4265
4266/*
4267 * sleep, with exponential backoff, and retry the LOCK operation.
4268 */
4269static unsigned long
4270nfs4_set_lock_task_retry(unsigned long timeout)
4271{
Jeff Laytond3103102011-12-01 22:44:39 +01004272 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 timeout <<= 1;
4274 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4275 return NFS4_LOCK_MAXTIMEOUT;
4276 return timeout;
4277}
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4280{
4281 struct inode *inode = state->inode;
4282 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004283 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004284 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004286 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004288 struct nfs_lockt_res res = {
4289 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 };
4291 struct rpc_message msg = {
4292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4293 .rpc_argp = &arg,
4294 .rpc_resp = &res,
4295 .rpc_cred = state->owner->so_cred,
4296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 struct nfs4_lock_state *lsp;
4298 int status;
4299
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004300 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004301 status = nfs4_set_lock_state(state, request);
4302 if (status != 0)
4303 goto out;
4304 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004305 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004306 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004307 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004308 switch (status) {
4309 case 0:
4310 request->fl_type = F_UNLCK;
4311 break;
4312 case -NFS4ERR_DENIED:
4313 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004315 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004316out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return status;
4318}
4319
4320static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4321{
4322 struct nfs4_exception exception = { };
4323 int err;
4324
4325 do {
4326 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4327 _nfs4_proc_getlk(state, cmd, request),
4328 &exception);
4329 } while (exception.retry);
4330 return err;
4331}
4332
4333static int do_vfs_lock(struct file *file, struct file_lock *fl)
4334{
4335 int res = 0;
4336 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4337 case FL_POSIX:
4338 res = posix_lock_file_wait(file, fl);
4339 break;
4340 case FL_FLOCK:
4341 res = flock_lock_file_wait(file, fl);
4342 break;
4343 default:
4344 BUG();
4345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 return res;
4347}
4348
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004349struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004350 struct nfs_locku_args arg;
4351 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004352 struct nfs4_lock_state *lsp;
4353 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004354 struct file_lock fl;
4355 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004356 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004357};
4358
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004359static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4360 struct nfs_open_context *ctx,
4361 struct nfs4_lock_state *lsp,
4362 struct nfs_seqid *seqid)
4363{
4364 struct nfs4_unlockdata *p;
4365 struct inode *inode = lsp->ls_state->inode;
4366
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004367 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004368 if (p == NULL)
4369 return NULL;
4370 p->arg.fh = NFS_FH(inode);
4371 p->arg.fl = &p->fl;
4372 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004373 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004374 p->arg.stateid = &lsp->ls_stateid;
4375 p->lsp = lsp;
4376 atomic_inc(&lsp->ls_count);
4377 /* Ensure we don't close file until we're done freeing locks! */
4378 p->ctx = get_nfs_open_context(ctx);
4379 memcpy(&p->fl, fl, sizeof(p->fl));
4380 p->server = NFS_SERVER(inode);
4381 return p;
4382}
4383
Trond Myklebust06f814a2006-01-03 09:55:07 +01004384static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004385{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004386 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004387 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004388 nfs4_put_lock_state(calldata->lsp);
4389 put_nfs_open_context(calldata->ctx);
4390 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391}
4392
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004393static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004394{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004395 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004396
Trond Myklebust14516c32010-07-31 14:29:06 -04004397 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4398 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004399 switch (task->tk_status) {
4400 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004401 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4402 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004403 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004404 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004405 case -NFS4ERR_BAD_STATEID:
4406 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004407 case -NFS4ERR_STALE_STATEID:
4408 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004409 break;
4410 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004411 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004412 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004413 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004414 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004415}
4416
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004417static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004418{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004419 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004421 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004422 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004423 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004424 /* Note: exit _without_ running nfs4_locku_done */
4425 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004426 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004427 return;
4428 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004429 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004430 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004431 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004432 &calldata->res.seq_res,
4433 task) != 0)
4434 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004437static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004438 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004439 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004440 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004441};
4442
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4444 struct nfs_open_context *ctx,
4445 struct nfs4_lock_state *lsp,
4446 struct nfs_seqid *seqid)
4447{
4448 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004449 struct rpc_message msg = {
4450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4451 .rpc_cred = ctx->cred,
4452 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004453 struct rpc_task_setup task_setup_data = {
4454 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004455 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004456 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004457 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004458 .flags = RPC_TASK_ASYNC,
4459 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004460
Frank Filz137d6ac2007-07-09 15:32:29 -07004461 /* Ensure this is an unlock - when canceling a lock, the
4462 * canceled lock is passed in, and it won't be an unlock.
4463 */
4464 fl->fl_type = F_UNLCK;
4465
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004466 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4467 if (data == NULL) {
4468 nfs_free_seqid(seqid);
4469 return ERR_PTR(-ENOMEM);
4470 }
4471
Trond Myklebust9d12b212012-01-17 22:04:25 -05004472 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004473 msg.rpc_argp = &data->arg;
4474 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004475 task_setup_data.callback_data = data;
4476 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477}
4478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4480{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004481 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004482 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004483 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004484 struct rpc_task *task;
4485 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004486 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Trond Myklebust9b073572006-06-29 16:38:34 -04004488 status = nfs4_set_lock_state(state, request);
4489 /* Unlock _before_ we do the RPC call */
4490 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004491 down_read(&nfsi->rwsem);
4492 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4493 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004494 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004495 }
4496 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004497 if (status != 0)
4498 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004499 /* Is this a delegated lock? */
4500 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004501 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004502 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004503 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004504 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004505 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004506 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004507 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508 status = PTR_ERR(task);
4509 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004510 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004511 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004512 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004513out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004514 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004515 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516}
4517
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518struct nfs4_lockdata {
4519 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004520 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004521 struct nfs4_lock_state *lsp;
4522 struct nfs_open_context *ctx;
4523 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004524 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 int rpc_status;
4526 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004527 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004528};
4529
4530static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004531 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4532 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533{
4534 struct nfs4_lockdata *p;
4535 struct inode *inode = lsp->ls_state->inode;
4536 struct nfs_server *server = NFS_SERVER(inode);
4537
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004538 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004539 if (p == NULL)
4540 return NULL;
4541
4542 p->arg.fh = NFS_FH(inode);
4543 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004544 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004545 if (p->arg.open_seqid == NULL)
4546 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004547 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004548 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004549 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004550 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004551 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004552 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004553 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004554 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004555 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004556 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557 atomic_inc(&lsp->ls_count);
4558 p->ctx = get_nfs_open_context(ctx);
4559 memcpy(&p->fl, fl, sizeof(p->fl));
4560 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004561out_free_seqid:
4562 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004563out_free:
4564 kfree(p);
4565 return NULL;
4566}
4567
4568static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4569{
4570 struct nfs4_lockdata *data = calldata;
4571 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Harvey Harrison3110ff82008-05-02 13:42:44 -07004573 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004574 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4575 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004576 /* Do we need to do an open_to_lock_owner? */
4577 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004578 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004579 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004580 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004581 data->arg.open_stateid = &state->stateid;
4582 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004583 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004584 } else
4585 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004586 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004587 if (nfs4_setup_sequence(data->server,
4588 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004589 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004590 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004591 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004592 nfs_release_seqid(data->arg.open_seqid);
4593out_release_lock_seqid:
4594 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004595 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004596}
4597
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4599{
4600 struct nfs4_lockdata *data = calldata;
4601
Harvey Harrison3110ff82008-05-02 13:42:44 -07004602 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004603
Trond Myklebust14516c32010-07-31 14:29:06 -04004604 if (!nfs4_sequence_done(task, &data->res.seq_res))
4605 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004606
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004607 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004608 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609 if (data->rpc_status == 0)
4610 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4611 else
4612 goto out;
4613 }
4614 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004615 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004616 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004617 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004618 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004620 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621}
4622
4623static void nfs4_lock_release(void *calldata)
4624{
4625 struct nfs4_lockdata *data = calldata;
4626
Harvey Harrison3110ff82008-05-02 13:42:44 -07004627 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004628 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004629 if (data->cancelled != 0) {
4630 struct rpc_task *task;
4631 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4632 data->arg.lock_seqid);
4633 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004634 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004635 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004636 } else
4637 nfs_free_seqid(data->arg.lock_seqid);
4638 nfs4_put_lock_state(data->lsp);
4639 put_nfs_open_context(data->ctx);
4640 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004641 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004642}
4643
4644static const struct rpc_call_ops nfs4_lock_ops = {
4645 .rpc_call_prepare = nfs4_lock_prepare,
4646 .rpc_call_done = nfs4_lock_done,
4647 .rpc_release = nfs4_lock_release,
4648};
4649
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004650static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4651{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004652 switch (error) {
4653 case -NFS4ERR_ADMIN_REVOKED:
4654 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004655 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004656 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004657 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004658 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004659 break;
4660 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004661 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004662 case -NFS4ERR_EXPIRED:
4663 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004664 };
4665}
4666
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004667static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668{
4669 struct nfs4_lockdata *data;
4670 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004671 struct rpc_message msg = {
4672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4673 .rpc_cred = state->owner->so_cred,
4674 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004675 struct rpc_task_setup task_setup_data = {
4676 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004677 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004678 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004679 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004680 .flags = RPC_TASK_ASYNC,
4681 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004682 int ret;
4683
Harvey Harrison3110ff82008-05-02 13:42:44 -07004684 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004685 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004686 fl->fl_u.nfs4_fl.owner,
4687 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688 if (data == NULL)
4689 return -ENOMEM;
4690 if (IS_SETLKW(cmd))
4691 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004692 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004693 msg.rpc_argp = &data->arg;
4694 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004695 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004696 if (recovery_type > NFS_LOCK_NEW) {
4697 if (recovery_type == NFS_LOCK_RECLAIM)
4698 data->arg.reclaim = NFS_LOCK_RECLAIM;
4699 nfs4_set_sequence_privileged(&data->arg.seq_args);
4700 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004701 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004702 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004703 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004704 ret = nfs4_wait_for_completion_rpc_task(task);
4705 if (ret == 0) {
4706 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004707 if (ret)
4708 nfs4_handle_setlk_error(data->server, data->lsp,
4709 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004710 } else
4711 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004712 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004713 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004714 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
4717static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4718{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004719 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004720 struct nfs4_exception exception = {
4721 .inode = state->inode,
4722 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004723 int err;
4724
4725 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004726 /* Cache the lock if possible... */
4727 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4728 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004729 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004730 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004731 break;
4732 nfs4_handle_exception(server, err, &exception);
4733 } while (exception.retry);
4734 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
4737static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4738{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004739 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004740 struct nfs4_exception exception = {
4741 .inode = state->inode,
4742 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004743 int err;
4744
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004745 err = nfs4_set_lock_state(state, request);
4746 if (err != 0)
4747 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004748 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004749 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4750 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004751 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004752 switch (err) {
4753 default:
4754 goto out;
4755 case -NFS4ERR_GRACE:
4756 case -NFS4ERR_DELAY:
4757 nfs4_handle_exception(server, err, &exception);
4758 err = 0;
4759 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004760 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004761out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004762 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763}
4764
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004765#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004766/**
4767 * nfs41_check_expired_locks - possibly free a lock stateid
4768 *
4769 * @state: NFSv4 state for an inode
4770 *
4771 * Returns NFS_OK if recovery for this stateid is now finished.
4772 * Otherwise a negative NFS4ERR value is returned.
4773 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004774static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004775{
Chuck Levereb64cf92012-07-11 16:30:05 -04004776 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004777 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004778 struct nfs_server *server = NFS_SERVER(state->inode);
4779
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004780 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004781 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004782 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4783 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004784 /* Free the stateid unless the server
4785 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004786 if (status != -NFS4ERR_BAD_STATEID)
4787 nfs41_free_stateid(server,
4788 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004789 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004790 ret = status;
4791 }
4792 }
4793 };
4794
4795 return ret;
4796}
4797
4798static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4799{
4800 int status = NFS_OK;
4801
4802 if (test_bit(LK_STATE_IN_USE, &state->flags))
4803 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004804 if (status != NFS_OK)
4805 status = nfs4_lock_expired(state, request);
4806 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004807}
4808#endif
4809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4811{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004812 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004813 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004814 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004816 if ((fl_flags & FL_POSIX) &&
4817 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4818 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004819 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004820 status = nfs4_set_lock_state(state, request);
4821 if (status != 0)
4822 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004823 request->fl_flags |= FL_ACCESS;
4824 status = do_vfs_lock(request->fl_file, request);
4825 if (status < 0)
4826 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004827 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004828 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004829 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004830 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004831 request->fl_flags = fl_flags & ~FL_SLEEP;
4832 status = do_vfs_lock(request->fl_file, request);
4833 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004834 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004835 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004836 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004837 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004838 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004839 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004840 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004841 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4842 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004843out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004844 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004845out:
4846 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 return status;
4848}
4849
4850static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4851{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004852 struct nfs4_exception exception = {
4853 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004854 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004855 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 int err;
4857
4858 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004859 err = _nfs4_proc_setlk(state, cmd, request);
4860 if (err == -NFS4ERR_DENIED)
4861 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004863 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 } while (exception.retry);
4865 return err;
4866}
4867
4868static int
4869nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4870{
4871 struct nfs_open_context *ctx;
4872 struct nfs4_state *state;
4873 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4874 int status;
4875
4876 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004877 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 state = ctx->state;
4879
4880 if (request->fl_start < 0 || request->fl_end < 0)
4881 return -EINVAL;
4882
Trond Myklebustd9531262009-07-21 19:22:38 -04004883 if (IS_GETLK(cmd)) {
4884 if (state != NULL)
4885 return nfs4_proc_getlk(state, F_GETLK, request);
4886 return 0;
4887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
4889 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4890 return -EINVAL;
4891
Trond Myklebustd9531262009-07-21 19:22:38 -04004892 if (request->fl_type == F_UNLCK) {
4893 if (state != NULL)
4894 return nfs4_proc_unlck(state, cmd, request);
4895 return 0;
4896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Trond Myklebustd9531262009-07-21 19:22:38 -04004898 if (state == NULL)
4899 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004900 /*
4901 * Don't rely on the VFS having checked the file open mode,
4902 * since it won't do this for flock() locks.
4903 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004904 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004905 case F_RDLCK:
4906 if (!(filp->f_mode & FMODE_READ))
4907 return -EBADF;
4908 break;
4909 case F_WRLCK:
4910 if (!(filp->f_mode & FMODE_WRITE))
4911 return -EBADF;
4912 }
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 do {
4915 status = nfs4_proc_setlk(state, cmd, request);
4916 if ((status != -EAGAIN) || IS_SETLK(cmd))
4917 break;
4918 timeout = nfs4_set_lock_task_retry(timeout);
4919 status = -ERESTARTSYS;
4920 if (signalled())
4921 break;
4922 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 return status;
4924}
4925
Trond Myklebust888e6942005-11-04 15:38:11 -05004926int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4927{
4928 struct nfs_server *server = NFS_SERVER(state->inode);
4929 struct nfs4_exception exception = { };
4930 int err;
4931
4932 err = nfs4_set_lock_state(state, fl);
4933 if (err != 0)
4934 goto out;
4935 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004936 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004937 switch (err) {
4938 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004939 printk(KERN_ERR "NFS: %s: unhandled error "
4940 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004941 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004942 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004943 goto out;
4944 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004945 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004946 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004947 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004948 nfs4_schedule_lease_recovery(server->nfs_client);
4949 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004950 case -NFS4ERR_BADSESSION:
4951 case -NFS4ERR_BADSLOT:
4952 case -NFS4ERR_BAD_HIGH_SLOT:
4953 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4954 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004955 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004956 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004957 case -ERESTARTSYS:
4958 /*
4959 * The show must go on: exit, but mark the
4960 * stateid as needing recovery.
4961 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004962 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004963 case -NFS4ERR_ADMIN_REVOKED:
4964 case -NFS4ERR_BAD_STATEID:
4965 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004966 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004967 err = 0;
4968 goto out;
4969 case -ENOMEM:
4970 case -NFS4ERR_DENIED:
4971 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4972 err = 0;
4973 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004974 case -NFS4ERR_DELAY:
4975 break;
4976 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004977 err = nfs4_handle_exception(server, err, &exception);
4978 } while (exception.retry);
4979out:
4980 return err;
4981}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004982
Trond Myklebustcf470c32012-03-07 13:49:12 -05004983struct nfs_release_lockowner_data {
4984 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004985 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004986 struct nfs_release_lockowner_args args;
4987};
4988
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004989static void nfs4_release_lockowner_release(void *calldata)
4990{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004991 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004992 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004993 kfree(calldata);
4994}
4995
Trond Myklebust17280172012-03-11 13:11:00 -04004996static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004997 .rpc_release = nfs4_release_lockowner_release,
4998};
4999
Trond Myklebustcf470c32012-03-07 13:49:12 -05005000int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005001{
5002 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005003 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005004 struct rpc_message msg = {
5005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5006 };
5007
5008 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005009 return -EINVAL;
5010 data = kmalloc(sizeof(*data), GFP_NOFS);
5011 if (!data)
5012 return -ENOMEM;
5013 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005014 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005015 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5016 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5017 data->args.lock_owner.s_dev = server->s_dev;
5018 msg.rpc_argp = &data->args;
5019 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5020 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005021}
5022
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005023#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5024
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005025static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5026 const void *buf, size_t buflen,
5027 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005028{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005029 if (strcmp(key, "") != 0)
5030 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005031
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005032 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005033}
5034
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005035static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5036 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005037{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005038 if (strcmp(key, "") != 0)
5039 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005040
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005041 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005042}
5043
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005044static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5045 size_t list_len, const char *name,
5046 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005047{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005048 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005049
J. Bruce Fields096455a2006-03-20 23:23:42 -05005050 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5051 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005052
5053 if (list && len <= list_len)
5054 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005055 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005056}
5057
Andy Adamson533eb462011-06-13 18:25:56 -04005058/*
5059 * nfs_fhget will use either the mounted_on_fileid or the fileid
5060 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005061static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5062{
Andy Adamson533eb462011-06-13 18:25:56 -04005063 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5064 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5065 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005066 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005067 return;
5068
5069 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005070 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005071 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5072 fattr->nlink = 2;
5073}
5074
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005075static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5076 const struct qstr *name,
5077 struct nfs4_fs_locations *fs_locations,
5078 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005079{
5080 struct nfs_server *server = NFS_SERVER(dir);
5081 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005082 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005083 };
5084 struct nfs4_fs_locations_arg args = {
5085 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005086 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005087 .page = page,
5088 .bitmask = bitmask,
5089 };
Benny Halevy22958462009-04-01 09:22:02 -04005090 struct nfs4_fs_locations_res res = {
5091 .fs_locations = fs_locations,
5092 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005093 struct rpc_message msg = {
5094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5095 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005096 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005097 };
5098 int status;
5099
Harvey Harrison3110ff82008-05-02 13:42:44 -07005100 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005101
5102 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5103 * is not supported */
5104 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5105 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5106 else
5107 bitmask[0] |= FATTR4_WORD0_FILEID;
5108
Trond Myklebustc228fd32007-01-13 02:28:11 -05005109 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005110 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005111 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005112 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005113 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005114 return status;
5115}
5116
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005117int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5118 const struct qstr *name,
5119 struct nfs4_fs_locations *fs_locations,
5120 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005121{
5122 struct nfs4_exception exception = { };
5123 int err;
5124 do {
5125 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005126 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005127 &exception);
5128 } while (exception.retry);
5129 return err;
5130}
5131
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005132static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5133{
5134 int status;
5135 struct nfs4_secinfo_arg args = {
5136 .dir_fh = NFS_FH(dir),
5137 .name = name,
5138 };
5139 struct nfs4_secinfo_res res = {
5140 .flavors = flavors,
5141 };
5142 struct rpc_message msg = {
5143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5144 .rpc_argp = &args,
5145 .rpc_resp = &res,
5146 };
5147
5148 dprintk("NFS call secinfo %s\n", name->name);
5149 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5150 dprintk("NFS reply secinfo: %d\n", status);
5151 return status;
5152}
5153
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005154int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5155 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005156{
5157 struct nfs4_exception exception = { };
5158 int err;
5159 do {
5160 err = nfs4_handle_exception(NFS_SERVER(dir),
5161 _nfs4_proc_secinfo(dir, name, flavors),
5162 &exception);
5163 } while (exception.retry);
5164 return err;
5165}
5166
Andy Adamson557134a2009-04-01 09:21:53 -04005167#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005168/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005169 * Check the exchange flags returned by the server for invalid flags, having
5170 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5171 * DS flags set.
5172 */
5173static int nfs4_check_cl_exchange_flags(u32 flags)
5174{
5175 if (flags & ~EXCHGID4_FLAG_MASK_R)
5176 goto out_inval;
5177 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5178 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5179 goto out_inval;
5180 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5181 goto out_inval;
5182 return NFS_OK;
5183out_inval:
5184 return -NFS4ERR_INVAL;
5185}
5186
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005187static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005188nfs41_same_server_scope(struct nfs41_server_scope *a,
5189 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005190{
5191 if (a->server_scope_sz == b->server_scope_sz &&
5192 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5193 return true;
5194
5195 return false;
5196}
5197
Andy Adamson357f54d2010-12-14 10:11:57 -05005198/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005199 * nfs4_proc_bind_conn_to_session()
5200 *
5201 * The 4.1 client currently uses the same TCP connection for the
5202 * fore and backchannel.
5203 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005204int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005205{
5206 int status;
5207 struct nfs41_bind_conn_to_session_res res;
5208 struct rpc_message msg = {
5209 .rpc_proc =
5210 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5211 .rpc_argp = clp,
5212 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005213 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005214 };
5215
5216 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005217
5218 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5219 if (unlikely(res.session == NULL)) {
5220 status = -ENOMEM;
5221 goto out;
5222 }
5223
5224 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5225 if (status == 0) {
5226 if (memcmp(res.session->sess_id.data,
5227 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5228 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5229 status = -EIO;
5230 goto out_session;
5231 }
5232 if (res.dir != NFS4_CDFS4_BOTH) {
5233 dprintk("NFS: %s: Unexpected direction from server\n",
5234 __func__);
5235 status = -EIO;
5236 goto out_session;
5237 }
5238 if (res.use_conn_in_rdma_mode) {
5239 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5240 __func__);
5241 status = -EIO;
5242 goto out_session;
5243 }
5244 }
5245out_session:
5246 kfree(res.session);
5247out:
5248 dprintk("<-- %s status= %d\n", __func__, status);
5249 return status;
5250}
5251
5252/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005253 * nfs4_proc_exchange_id()
5254 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005255 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5256 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005257 * Since the clientid has expired, all compounds using sessions
5258 * associated with the stale clientid will be returning
5259 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5260 * be in some phase of session reset.
5261 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005262int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005263{
5264 nfs4_verifier verifier;
5265 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005266 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005267 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005268 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005269 };
5270 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005271 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005272 };
5273 int status;
5274 struct rpc_message msg = {
5275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5276 .rpc_argp = &args,
5277 .rpc_resp = &res,
5278 .rpc_cred = cred,
5279 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005280
Chuck Leverf0920752012-05-21 22:45:41 -04005281 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005282 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5283 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005284 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5285 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5286 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005287
Chuck Leveracdeb692012-05-21 22:46:16 -04005288 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005289 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005290 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005291 status = -ENOMEM;
5292 goto out;
5293 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005294
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005295 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005296 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005297 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005298 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005299 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005300 }
5301
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005302 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005303 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005304 status = -ENOMEM;
5305 goto out_server_scope;
5306 }
5307
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005308 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005309 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005310 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005311
Chuck Lever177313f2012-05-21 22:44:58 -04005312 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005313 clp->cl_clientid = res.clientid;
5314 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5315 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5316 clp->cl_seqid = res.seqid;
5317
Chuck Leveracdeb692012-05-21 22:46:16 -04005318 kfree(clp->cl_serverowner);
5319 clp->cl_serverowner = res.server_owner;
5320 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005321
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005322 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005323 kfree(clp->cl_implid);
5324 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005325
Chuck Lever177313f2012-05-21 22:44:58 -04005326 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005327 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005328 res.server_scope)) {
5329 dprintk("%s: server_scope mismatch detected\n",
5330 __func__);
5331 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005332 kfree(clp->cl_serverscope);
5333 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005334 }
5335
Chuck Lever177313f2012-05-21 22:44:58 -04005336 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005337 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005338 goto out;
5339 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005340 } else
5341 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005342
Chuck Leveracdeb692012-05-21 22:46:16 -04005343out_server_owner:
5344 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005345out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005346 kfree(res.server_scope);
5347out:
Chuck Lever177313f2012-05-21 22:44:58 -04005348 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005349 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005350 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005351 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005352 clp->cl_implid->date.seconds,
5353 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005354 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005355 return status;
5356}
5357
Trond Myklebust66245532012-05-25 17:18:09 -04005358static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5359 struct rpc_cred *cred)
5360{
5361 struct rpc_message msg = {
5362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5363 .rpc_argp = clp,
5364 .rpc_cred = cred,
5365 };
5366 int status;
5367
5368 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5369 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005370 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005371 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5372 return status;
5373}
5374
5375static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5376 struct rpc_cred *cred)
5377{
5378 unsigned int loop;
5379 int ret;
5380
5381 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5382 ret = _nfs4_proc_destroy_clientid(clp, cred);
5383 switch (ret) {
5384 case -NFS4ERR_DELAY:
5385 case -NFS4ERR_CLIENTID_BUSY:
5386 ssleep(1);
5387 break;
5388 default:
5389 return ret;
5390 }
5391 }
5392 return 0;
5393}
5394
5395int nfs4_destroy_clientid(struct nfs_client *clp)
5396{
5397 struct rpc_cred *cred;
5398 int ret = 0;
5399
5400 if (clp->cl_mvops->minor_version < 1)
5401 goto out;
5402 if (clp->cl_exchange_flags == 0)
5403 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005404 if (clp->cl_preserve_clid)
5405 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005406 cred = nfs4_get_exchange_id_cred(clp);
5407 ret = nfs4_proc_destroy_clientid(clp, cred);
5408 if (cred)
5409 put_rpccred(cred);
5410 switch (ret) {
5411 case 0:
5412 case -NFS4ERR_STALE_CLIENTID:
5413 clp->cl_exchange_flags = 0;
5414 }
5415out:
5416 return ret;
5417}
5418
Andy Adamson2050f0c2009-04-01 09:22:30 -04005419struct nfs4_get_lease_time_data {
5420 struct nfs4_get_lease_time_args *args;
5421 struct nfs4_get_lease_time_res *res;
5422 struct nfs_client *clp;
5423};
5424
5425static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5426 void *calldata)
5427{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005428 struct nfs4_get_lease_time_data *data =
5429 (struct nfs4_get_lease_time_data *)calldata;
5430
5431 dprintk("--> %s\n", __func__);
5432 /* just setup sequence, do not trigger session recovery
5433 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005434 nfs41_setup_sequence(data->clp->cl_session,
5435 &data->args->la_seq_args,
5436 &data->res->lr_seq_res,
5437 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005438 dprintk("<-- %s\n", __func__);
5439}
5440
5441/*
5442 * Called from nfs4_state_manager thread for session setup, so don't recover
5443 * from sequence operation or clientid errors.
5444 */
5445static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5446{
5447 struct nfs4_get_lease_time_data *data =
5448 (struct nfs4_get_lease_time_data *)calldata;
5449
5450 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005451 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5452 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005453 switch (task->tk_status) {
5454 case -NFS4ERR_DELAY:
5455 case -NFS4ERR_GRACE:
5456 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5457 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5458 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005459 /* fall through */
5460 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005461 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005462 return;
5463 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005464 dprintk("<-- %s\n", __func__);
5465}
5466
Trond Myklebust17280172012-03-11 13:11:00 -04005467static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005468 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5469 .rpc_call_done = nfs4_get_lease_time_done,
5470};
5471
5472int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5473{
5474 struct rpc_task *task;
5475 struct nfs4_get_lease_time_args args;
5476 struct nfs4_get_lease_time_res res = {
5477 .lr_fsinfo = fsinfo,
5478 };
5479 struct nfs4_get_lease_time_data data = {
5480 .args = &args,
5481 .res = &res,
5482 .clp = clp,
5483 };
5484 struct rpc_message msg = {
5485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5486 .rpc_argp = &args,
5487 .rpc_resp = &res,
5488 };
5489 struct rpc_task_setup task_setup = {
5490 .rpc_client = clp->cl_rpcclient,
5491 .rpc_message = &msg,
5492 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005493 .callback_data = &data,
5494 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005495 };
5496 int status;
5497
Trond Myklebust9d12b212012-01-17 22:04:25 -05005498 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005499 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005500 dprintk("--> %s\n", __func__);
5501 task = rpc_run_task(&task_setup);
5502
5503 if (IS_ERR(task))
5504 status = PTR_ERR(task);
5505 else {
5506 status = task->tk_status;
5507 rpc_put_task(task);
5508 }
5509 dprintk("<-- %s return %d\n", __func__, status);
5510
5511 return status;
5512}
5513
Andy Adamsonfc931582009-04-01 09:22:31 -04005514/*
5515 * Initialize the values to be used by the client in CREATE_SESSION
5516 * If nfs4_init_session set the fore channel request and response sizes,
5517 * use them.
5518 *
5519 * Set the back channel max_resp_sz_cached to zero to force the client to
5520 * always set csa_cachethis to FALSE because the current implementation
5521 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5522 */
5523static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5524{
5525 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005526 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5527 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005528
5529 if (mxrqst_sz == 0)
5530 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5531 if (mxresp_sz == 0)
5532 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5533 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005534 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5535 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005536 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005537 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005538
5539 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005540 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005541 __func__,
5542 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005543 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005544
5545 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005546 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5547 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5548 args->bc_attrs.max_resp_sz_cached = 0;
5549 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5550 args->bc_attrs.max_reqs = 1;
5551
5552 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5553 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5554 __func__,
5555 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5556 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5557 args->bc_attrs.max_reqs);
5558}
5559
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005560static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005561{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005562 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5563 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5564
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005565 if (rcvd->max_resp_sz > sent->max_resp_sz)
5566 return -EINVAL;
5567 /*
5568 * Our requested max_ops is the minimum we need; we're not
5569 * prepared to break up compounds into smaller pieces than that.
5570 * So, no point even trying to continue if the server won't
5571 * cooperate:
5572 */
5573 if (rcvd->max_ops < sent->max_ops)
5574 return -EINVAL;
5575 if (rcvd->max_reqs == 0)
5576 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005577 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5578 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005579 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005580}
5581
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005582static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5583{
5584 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5585 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005586
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005587 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5588 return -EINVAL;
5589 if (rcvd->max_resp_sz < sent->max_resp_sz)
5590 return -EINVAL;
5591 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5592 return -EINVAL;
5593 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005594 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005595 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005596 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005597 return -EINVAL;
5598 return 0;
5599}
Andy Adamson8d353012009-04-01 09:22:32 -04005600
Andy Adamson8d353012009-04-01 09:22:32 -04005601static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5602 struct nfs4_session *session)
5603{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005604 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005605
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005606 ret = nfs4_verify_fore_channel_attrs(args, session);
5607 if (ret)
5608 return ret;
5609 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005610}
5611
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005612static int _nfs4_proc_create_session(struct nfs_client *clp,
5613 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005614{
5615 struct nfs4_session *session = clp->cl_session;
5616 struct nfs41_create_session_args args = {
5617 .client = clp,
5618 .cb_program = NFS4_CALLBACK,
5619 };
5620 struct nfs41_create_session_res res = {
5621 .client = clp,
5622 };
5623 struct rpc_message msg = {
5624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5625 .rpc_argp = &args,
5626 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005627 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005628 };
5629 int status;
5630
5631 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005632 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005633
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005634 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005635
Trond Myklebust43095d32012-11-20 11:13:12 -05005636 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005637 /* Verify the session's negotiated channel_attrs values */
5638 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005639 /* Increment the clientid slot sequence id */
5640 clp->cl_seqid++;
5641 }
5642
5643 return status;
5644}
5645
5646/*
5647 * Issues a CREATE_SESSION operation to the server.
5648 * It is the responsibility of the caller to verify the session is
5649 * expired before calling this routine.
5650 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005651int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005652{
5653 int status;
5654 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005655 struct nfs4_session *session = clp->cl_session;
5656
5657 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5658
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005659 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005660 if (status)
5661 goto out;
5662
Andy Adamsonaacd5532011-11-09 13:58:21 -05005663 /* Init or reset the session slot tables */
5664 status = nfs4_setup_session_slot_tables(session);
5665 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005666 if (status)
5667 goto out;
5668
5669 ptr = (unsigned *)&session->sess_id.data[0];
5670 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5671 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005672out:
5673 dprintk("<-- %s\n", __func__);
5674 return status;
5675}
5676
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005677/*
5678 * Issue the over-the-wire RPC DESTROY_SESSION.
5679 * The caller must serialize access to this routine.
5680 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005681int nfs4_proc_destroy_session(struct nfs4_session *session,
5682 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005683{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005684 struct rpc_message msg = {
5685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5686 .rpc_argp = session,
5687 .rpc_cred = cred,
5688 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005689 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005690
5691 dprintk("--> nfs4_proc_destroy_session\n");
5692
5693 /* session is still being setup */
5694 if (session->clp->cl_cons_state != NFS_CS_READY)
5695 return status;
5696
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005697 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005698
5699 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005700 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005701 "Session has been destroyed regardless...\n", status);
5702
5703 dprintk("<-- nfs4_proc_destroy_session\n");
5704 return status;
5705}
5706
Trond Myklebust7b38c362012-05-23 13:23:31 -04005707/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005708 * Renew the cl_session lease.
5709 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005710struct nfs4_sequence_data {
5711 struct nfs_client *clp;
5712 struct nfs4_sequence_args args;
5713 struct nfs4_sequence_res res;
5714};
5715
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005716static void nfs41_sequence_release(void *data)
5717{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005718 struct nfs4_sequence_data *calldata = data;
5719 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005720
Alexandros Batsakis71358402010-02-05 03:45:05 -08005721 if (atomic_read(&clp->cl_count) > 1)
5722 nfs4_schedule_state_renewal(clp);
5723 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005724 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005725}
5726
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005727static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5728{
5729 switch(task->tk_status) {
5730 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005731 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5732 return -EAGAIN;
5733 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005734 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005735 }
5736 return 0;
5737}
5738
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005739static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005740{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005741 struct nfs4_sequence_data *calldata = data;
5742 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005743
Trond Myklebust14516c32010-07-31 14:29:06 -04005744 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5745 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005746
5747 if (task->tk_status < 0) {
5748 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005749 if (atomic_read(&clp->cl_count) == 1)
5750 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005751
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005752 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5753 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005754 return;
5755 }
5756 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005757 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005758out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005759 dprintk("<-- %s\n", __func__);
5760}
5761
5762static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5763{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005764 struct nfs4_sequence_data *calldata = data;
5765 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005766 struct nfs4_sequence_args *args;
5767 struct nfs4_sequence_res *res;
5768
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005769 args = task->tk_msg.rpc_argp;
5770 res = task->tk_msg.rpc_resp;
5771
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005772 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005773}
5774
5775static const struct rpc_call_ops nfs41_sequence_ops = {
5776 .rpc_call_done = nfs41_sequence_call_done,
5777 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005778 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005779};
5780
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005781static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5782 struct rpc_cred *cred,
5783 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005784{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005785 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005786 struct rpc_message msg = {
5787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5788 .rpc_cred = cred,
5789 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005790 struct rpc_task_setup task_setup_data = {
5791 .rpc_client = clp->cl_rpcclient,
5792 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005793 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005794 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5795 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005796
Alexandros Batsakis71358402010-02-05 03:45:05 -08005797 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005798 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005799 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005800 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005801 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005802 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005803 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005804 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005805 if (is_privileged)
5806 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005807 msg.rpc_argp = &calldata->args;
5808 msg.rpc_resp = &calldata->res;
5809 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005810 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005811
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005812 return rpc_run_task(&task_setup_data);
5813}
5814
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005815static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005816{
5817 struct rpc_task *task;
5818 int ret = 0;
5819
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005820 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5821 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005822 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005823 if (IS_ERR(task))
5824 ret = PTR_ERR(task);
5825 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005826 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005827 dprintk("<-- %s status=%d\n", __func__, ret);
5828 return ret;
5829}
5830
5831static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5832{
5833 struct rpc_task *task;
5834 int ret;
5835
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005836 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005837 if (IS_ERR(task)) {
5838 ret = PTR_ERR(task);
5839 goto out;
5840 }
5841 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005842 if (!ret) {
5843 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5844
5845 if (task->tk_status == 0)
5846 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005847 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005848 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005849 rpc_put_task(task);
5850out:
5851 dprintk("<-- %s status=%d\n", __func__, ret);
5852 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005853}
5854
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005855struct nfs4_reclaim_complete_data {
5856 struct nfs_client *clp;
5857 struct nfs41_reclaim_complete_args arg;
5858 struct nfs41_reclaim_complete_res res;
5859};
5860
5861static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5862{
5863 struct nfs4_reclaim_complete_data *calldata = data;
5864
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005865 nfs41_setup_sequence(calldata->clp->cl_session,
5866 &calldata->arg.seq_args,
5867 &calldata->res.seq_res,
5868 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005869}
5870
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005871static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5872{
5873 switch(task->tk_status) {
5874 case 0:
5875 case -NFS4ERR_COMPLETE_ALREADY:
5876 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5877 break;
5878 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005879 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005880 /* fall through */
5881 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005882 return -EAGAIN;
5883 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005884 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005885 }
5886 return 0;
5887}
5888
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005889static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5890{
5891 struct nfs4_reclaim_complete_data *calldata = data;
5892 struct nfs_client *clp = calldata->clp;
5893 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5894
5895 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005896 if (!nfs41_sequence_done(task, res))
5897 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005898
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005899 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5900 rpc_restart_call_prepare(task);
5901 return;
5902 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005903 dprintk("<-- %s\n", __func__);
5904}
5905
5906static void nfs4_free_reclaim_complete_data(void *data)
5907{
5908 struct nfs4_reclaim_complete_data *calldata = data;
5909
5910 kfree(calldata);
5911}
5912
5913static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5914 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5915 .rpc_call_done = nfs4_reclaim_complete_done,
5916 .rpc_release = nfs4_free_reclaim_complete_data,
5917};
5918
5919/*
5920 * Issue a global reclaim complete.
5921 */
5922static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5923{
5924 struct nfs4_reclaim_complete_data *calldata;
5925 struct rpc_task *task;
5926 struct rpc_message msg = {
5927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5928 };
5929 struct rpc_task_setup task_setup_data = {
5930 .rpc_client = clp->cl_rpcclient,
5931 .rpc_message = &msg,
5932 .callback_ops = &nfs4_reclaim_complete_call_ops,
5933 .flags = RPC_TASK_ASYNC,
5934 };
5935 int status = -ENOMEM;
5936
5937 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005938 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005939 if (calldata == NULL)
5940 goto out;
5941 calldata->clp = clp;
5942 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005943
Trond Myklebust9d12b212012-01-17 22:04:25 -05005944 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005945 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005946 msg.rpc_argp = &calldata->arg;
5947 msg.rpc_resp = &calldata->res;
5948 task_setup_data.callback_data = calldata;
5949 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005950 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005951 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005952 goto out;
5953 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005954 status = nfs4_wait_for_completion_rpc_task(task);
5955 if (status == 0)
5956 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005957 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005958 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005959out:
5960 dprintk("<-- %s status=%d\n", __func__, status);
5961 return status;
5962}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005963
5964static void
5965nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5966{
5967 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005968 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005969 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005970
5971 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005972 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5973 * right now covering the LAYOUTGET we are about to send.
5974 * However, that is not so catastrophic, and there seems
5975 * to be no way to prevent it completely.
5976 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005977 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005978 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005979 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005980 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5981 NFS_I(lgp->args.inode)->layout,
5982 lgp->args.ctx->state)) {
5983 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005984 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005985}
5986
5987static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5988{
5989 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07005990 struct inode *inode = lgp->args.inode;
5991 struct nfs_server *server = NFS_SERVER(inode);
5992 struct pnfs_layout_hdr *lo;
5993 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005994
5995 dprintk("--> %s\n", __func__);
5996
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005997 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07005998 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005999
6000 switch (task->tk_status) {
6001 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006002 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006003 case -NFS4ERR_LAYOUTTRYLATER:
6004 case -NFS4ERR_RECALLCONFLICT:
6005 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006006 break;
6007 case -NFS4ERR_EXPIRED:
6008 case -NFS4ERR_BAD_STATEID:
6009 spin_lock(&inode->i_lock);
6010 lo = NFS_I(inode)->layout;
6011 if (!lo || list_empty(&lo->plh_segs)) {
6012 spin_unlock(&inode->i_lock);
6013 /* If the open stateid was bad, then recover it. */
6014 state = lgp->args.ctx->state;
6015 } else {
6016 LIST_HEAD(head);
6017
6018 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6019 spin_unlock(&inode->i_lock);
6020 /* Mark the bad layout state as invalid, then
6021 * retry using the open stateid. */
6022 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006023 }
6024 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006025 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6026 rpc_restart_call_prepare(task);
6027out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006028 dprintk("<-- %s\n", __func__);
6029}
6030
Idan Kedar85541162012-08-02 11:47:10 +03006031static size_t max_response_pages(struct nfs_server *server)
6032{
6033 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6034 return nfs_page_array_len(0, max_resp_sz);
6035}
6036
6037static void nfs4_free_pages(struct page **pages, size_t size)
6038{
6039 int i;
6040
6041 if (!pages)
6042 return;
6043
6044 for (i = 0; i < size; i++) {
6045 if (!pages[i])
6046 break;
6047 __free_page(pages[i]);
6048 }
6049 kfree(pages);
6050}
6051
6052static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6053{
6054 struct page **pages;
6055 int i;
6056
6057 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6058 if (!pages) {
6059 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6060 return NULL;
6061 }
6062
6063 for (i = 0; i < size; i++) {
6064 pages[i] = alloc_page(gfp_flags);
6065 if (!pages[i]) {
6066 dprintk("%s: failed to allocate page\n", __func__);
6067 nfs4_free_pages(pages, size);
6068 return NULL;
6069 }
6070 }
6071
6072 return pages;
6073}
6074
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006075static void nfs4_layoutget_release(void *calldata)
6076{
6077 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006078 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6079 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006080
6081 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006082 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006083 put_nfs_open_context(lgp->args.ctx);
6084 kfree(calldata);
6085 dprintk("<-- %s\n", __func__);
6086}
6087
6088static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6089 .rpc_call_prepare = nfs4_layoutget_prepare,
6090 .rpc_call_done = nfs4_layoutget_done,
6091 .rpc_release = nfs4_layoutget_release,
6092};
6093
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006094struct pnfs_layout_segment *
6095nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006096{
6097 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006098 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006099 struct rpc_task *task;
6100 struct rpc_message msg = {
6101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6102 .rpc_argp = &lgp->args,
6103 .rpc_resp = &lgp->res,
6104 };
6105 struct rpc_task_setup task_setup_data = {
6106 .rpc_client = server->client,
6107 .rpc_message = &msg,
6108 .callback_ops = &nfs4_layoutget_call_ops,
6109 .callback_data = lgp,
6110 .flags = RPC_TASK_ASYNC,
6111 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006112 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006113 int status = 0;
6114
6115 dprintk("--> %s\n", __func__);
6116
Idan Kedar85541162012-08-02 11:47:10 +03006117 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6118 if (!lgp->args.layout.pages) {
6119 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006120 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006121 }
6122 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6123
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006124 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006125 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006126 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006127 task = rpc_run_task(&task_setup_data);
6128 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006129 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006130 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006131 if (status == 0)
6132 status = task->tk_status;
6133 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006134 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006135 rpc_put_task(task);
6136 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006137 if (status)
6138 return ERR_PTR(status);
6139 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006140}
6141
Benny Halevycbe82602011-05-22 19:52:37 +03006142static void
6143nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6144{
6145 struct nfs4_layoutreturn *lrp = calldata;
6146
6147 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006148 nfs41_setup_sequence(lrp->clp->cl_session,
6149 &lrp->args.seq_args,
6150 &lrp->res.seq_res,
6151 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006152}
6153
6154static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6155{
6156 struct nfs4_layoutreturn *lrp = calldata;
6157 struct nfs_server *server;
6158
6159 dprintk("--> %s\n", __func__);
6160
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006161 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006162 return;
6163
6164 server = NFS_SERVER(lrp->args.inode);
6165 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006166 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006167 return;
6168 }
Benny Halevycbe82602011-05-22 19:52:37 +03006169 dprintk("<-- %s\n", __func__);
6170}
6171
6172static void nfs4_layoutreturn_release(void *calldata)
6173{
6174 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006175 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006176
6177 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006178 spin_lock(&lo->plh_inode->i_lock);
6179 if (lrp->res.lrs_present)
6180 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6181 lo->plh_block_lgets--;
6182 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006183 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006184 kfree(calldata);
6185 dprintk("<-- %s\n", __func__);
6186}
6187
6188static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6189 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6190 .rpc_call_done = nfs4_layoutreturn_done,
6191 .rpc_release = nfs4_layoutreturn_release,
6192};
6193
6194int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6195{
6196 struct rpc_task *task;
6197 struct rpc_message msg = {
6198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6199 .rpc_argp = &lrp->args,
6200 .rpc_resp = &lrp->res,
6201 };
6202 struct rpc_task_setup task_setup_data = {
6203 .rpc_client = lrp->clp->cl_rpcclient,
6204 .rpc_message = &msg,
6205 .callback_ops = &nfs4_layoutreturn_call_ops,
6206 .callback_data = lrp,
6207 };
6208 int status;
6209
6210 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006211 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006212 task = rpc_run_task(&task_setup_data);
6213 if (IS_ERR(task))
6214 return PTR_ERR(task);
6215 status = task->tk_status;
6216 dprintk("<-- %s status=%d\n", __func__, status);
6217 rpc_put_task(task);
6218 return status;
6219}
6220
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006221/*
6222 * Retrieve the list of Data Server devices from the MDS.
6223 */
6224static int _nfs4_getdevicelist(struct nfs_server *server,
6225 const struct nfs_fh *fh,
6226 struct pnfs_devicelist *devlist)
6227{
6228 struct nfs4_getdevicelist_args args = {
6229 .fh = fh,
6230 .layoutclass = server->pnfs_curr_ld->id,
6231 };
6232 struct nfs4_getdevicelist_res res = {
6233 .devlist = devlist,
6234 };
6235 struct rpc_message msg = {
6236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6237 .rpc_argp = &args,
6238 .rpc_resp = &res,
6239 };
6240 int status;
6241
6242 dprintk("--> %s\n", __func__);
6243 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6244 &res.seq_res, 0);
6245 dprintk("<-- %s status=%d\n", __func__, status);
6246 return status;
6247}
6248
6249int nfs4_proc_getdevicelist(struct nfs_server *server,
6250 const struct nfs_fh *fh,
6251 struct pnfs_devicelist *devlist)
6252{
6253 struct nfs4_exception exception = { };
6254 int err;
6255
6256 do {
6257 err = nfs4_handle_exception(server,
6258 _nfs4_getdevicelist(server, fh, devlist),
6259 &exception);
6260 } while (exception.retry);
6261
6262 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6263 err, devlist->num_devs);
6264
6265 return err;
6266}
6267EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6268
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006269static int
6270_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6271{
6272 struct nfs4_getdeviceinfo_args args = {
6273 .pdev = pdev,
6274 };
6275 struct nfs4_getdeviceinfo_res res = {
6276 .pdev = pdev,
6277 };
6278 struct rpc_message msg = {
6279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6280 .rpc_argp = &args,
6281 .rpc_resp = &res,
6282 };
6283 int status;
6284
6285 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006286 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006287 dprintk("<-- %s status=%d\n", __func__, status);
6288
6289 return status;
6290}
6291
6292int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6293{
6294 struct nfs4_exception exception = { };
6295 int err;
6296
6297 do {
6298 err = nfs4_handle_exception(server,
6299 _nfs4_proc_getdeviceinfo(server, pdev),
6300 &exception);
6301 } while (exception.retry);
6302 return err;
6303}
6304EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6305
Andy Adamson863a3c62011-03-23 13:27:54 +00006306static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6307{
6308 struct nfs4_layoutcommit_data *data = calldata;
6309 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006310 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006311
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006312 nfs41_setup_sequence(session,
6313 &data->args.seq_args,
6314 &data->res.seq_res,
6315 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006316}
6317
6318static void
6319nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6320{
6321 struct nfs4_layoutcommit_data *data = calldata;
6322 struct nfs_server *server = NFS_SERVER(data->args.inode);
6323
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006324 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006325 return;
6326
6327 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006328 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6329 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6330 case -NFS4ERR_BADLAYOUT: /* no layout */
6331 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006332 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006333 break;
6334 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006335 nfs_post_op_update_inode_force_wcc(data->args.inode,
6336 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006337 break;
6338 default:
6339 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6340 rpc_restart_call_prepare(task);
6341 return;
6342 }
6343 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006344}
6345
6346static void nfs4_layoutcommit_release(void *calldata)
6347{
6348 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006349 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006350 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006351
Andy Adamsondb29c082011-07-30 20:52:38 -04006352 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006353 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006354 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6355 list_del_init(&lseg->pls_lc_list);
6356 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6357 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006358 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006359 }
Peng Tao92407e72011-10-23 20:21:17 -07006360
6361 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6362 smp_mb__after_clear_bit();
6363 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6364
Andy Adamson863a3c62011-03-23 13:27:54 +00006365 put_rpccred(data->cred);
6366 kfree(data);
6367}
6368
6369static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6370 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6371 .rpc_call_done = nfs4_layoutcommit_done,
6372 .rpc_release = nfs4_layoutcommit_release,
6373};
6374
6375int
Andy Adamsonef311532011-03-12 02:58:10 -05006376nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006377{
6378 struct rpc_message msg = {
6379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6380 .rpc_argp = &data->args,
6381 .rpc_resp = &data->res,
6382 .rpc_cred = data->cred,
6383 };
6384 struct rpc_task_setup task_setup_data = {
6385 .task = &data->task,
6386 .rpc_client = NFS_CLIENT(data->args.inode),
6387 .rpc_message = &msg,
6388 .callback_ops = &nfs4_layoutcommit_ops,
6389 .callback_data = data,
6390 .flags = RPC_TASK_ASYNC,
6391 };
6392 struct rpc_task *task;
6393 int status = 0;
6394
6395 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6396 "lbw: %llu inode %lu\n",
6397 data->task.tk_pid, sync,
6398 data->args.lastbytewritten,
6399 data->args.inode->i_ino);
6400
Trond Myklebust9d12b212012-01-17 22:04:25 -05006401 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006402 task = rpc_run_task(&task_setup_data);
6403 if (IS_ERR(task))
6404 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006405 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006406 goto out;
6407 status = nfs4_wait_for_completion_rpc_task(task);
6408 if (status != 0)
6409 goto out;
6410 status = task->tk_status;
6411out:
6412 dprintk("%s: status %d\n", __func__, status);
6413 rpc_put_task(task);
6414 return status;
6415}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006416
6417static int
6418_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6419 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6420{
6421 struct nfs41_secinfo_no_name_args args = {
6422 .style = SECINFO_STYLE_CURRENT_FH,
6423 };
6424 struct nfs4_secinfo_res res = {
6425 .flavors = flavors,
6426 };
6427 struct rpc_message msg = {
6428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6429 .rpc_argp = &args,
6430 .rpc_resp = &res,
6431 };
6432 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6433}
6434
6435static int
6436nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6437 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6438{
6439 struct nfs4_exception exception = { };
6440 int err;
6441 do {
6442 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6443 switch (err) {
6444 case 0:
6445 case -NFS4ERR_WRONGSEC:
6446 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006447 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006448 default:
6449 err = nfs4_handle_exception(server, err, &exception);
6450 }
6451 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006452out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006453 return err;
6454}
6455
6456static int
6457nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6458 struct nfs_fsinfo *info)
6459{
6460 int err;
6461 struct page *page;
6462 rpc_authflavor_t flavor;
6463 struct nfs4_secinfo_flavors *flavors;
6464
6465 page = alloc_page(GFP_KERNEL);
6466 if (!page) {
6467 err = -ENOMEM;
6468 goto out;
6469 }
6470
6471 flavors = page_address(page);
6472 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6473
6474 /*
6475 * Fall back on "guess and check" method if
6476 * the server doesn't support SECINFO_NO_NAME
6477 */
6478 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6479 err = nfs4_find_root_sec(server, fhandle, info);
6480 goto out_freepage;
6481 }
6482 if (err)
6483 goto out_freepage;
6484
6485 flavor = nfs_find_best_sec(flavors);
6486 if (err == 0)
6487 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6488
6489out_freepage:
6490 put_page(page);
6491 if (err == -EACCES)
6492 return -EPERM;
6493out:
6494 return err;
6495}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006496
6497static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006498{
6499 int status;
6500 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006501 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006502 };
6503 struct nfs41_test_stateid_res res;
6504 struct rpc_message msg = {
6505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6506 .rpc_argp = &args,
6507 .rpc_resp = &res,
6508 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006509
Chuck Lever38527b12012-07-11 16:30:23 -04006510 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006511 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006512 nfs4_set_sequence_privileged(&args.seq_args);
6513 status = nfs4_call_sync_sequence(server->client, server, &msg,
6514 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006515 if (status != NFS_OK) {
6516 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006517 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006518 }
6519 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006520 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006521}
6522
Chuck Lever38527b12012-07-11 16:30:23 -04006523/**
6524 * nfs41_test_stateid - perform a TEST_STATEID operation
6525 *
6526 * @server: server / transport on which to perform the operation
6527 * @stateid: state ID to test
6528 *
6529 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6530 * Otherwise a negative NFS4ERR value is returned if the operation
6531 * failed or the state ID is not currently valid.
6532 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006533static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006534{
6535 struct nfs4_exception exception = { };
6536 int err;
6537 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006538 err = _nfs41_test_stateid(server, stateid);
6539 if (err != -NFS4ERR_DELAY)
6540 break;
6541 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006542 } while (exception.retry);
6543 return err;
6544}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006545
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006546static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006547{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006548 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006549 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006550 };
6551 struct nfs41_free_stateid_res res;
6552 struct rpc_message msg = {
6553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6554 .rpc_argp = &args,
6555 .rpc_resp = &res,
6556 };
Chuck Lever38527b12012-07-11 16:30:23 -04006557 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006558
Chuck Lever38527b12012-07-11 16:30:23 -04006559 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006560 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006561 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006562 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006563 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006564 dprintk("NFS reply free_stateid: %d\n", status);
6565 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006566}
6567
Chuck Lever38527b12012-07-11 16:30:23 -04006568/**
6569 * nfs41_free_stateid - perform a FREE_STATEID operation
6570 *
6571 * @server: server / transport on which to perform the operation
6572 * @stateid: state ID to release
6573 *
6574 * Returns NFS_OK if the server freed "stateid". Otherwise a
6575 * negative NFS4ERR value is returned.
6576 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006577static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006578{
6579 struct nfs4_exception exception = { };
6580 int err;
6581 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006582 err = _nfs4_free_stateid(server, stateid);
6583 if (err != -NFS4ERR_DELAY)
6584 break;
6585 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006586 } while (exception.retry);
6587 return err;
6588}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006589
6590static bool nfs41_match_stateid(const nfs4_stateid *s1,
6591 const nfs4_stateid *s2)
6592{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006593 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006594 return false;
6595
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006596 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006597 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006598 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006599 return true;
6600
6601 return false;
6602}
6603
Andy Adamson557134a2009-04-01 09:21:53 -04006604#endif /* CONFIG_NFS_V4_1 */
6605
Trond Myklebust36281ca2012-03-04 18:13:56 -05006606static bool nfs4_match_stateid(const nfs4_stateid *s1,
6607 const nfs4_stateid *s2)
6608{
Trond Myklebustf597c532012-03-04 18:13:56 -05006609 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006610}
6611
6612
Trond Myklebust17280172012-03-11 13:11:00 -04006613static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006614 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006615 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 .recover_open = nfs4_open_reclaim,
6617 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006618 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006619 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006620 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621};
6622
Andy Adamson591d71c2009-04-01 09:22:47 -04006623#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006624static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006625 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6626 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6627 .recover_open = nfs4_open_reclaim,
6628 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006629 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006630 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006631 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006632 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006633};
6634#endif /* CONFIG_NFS_V4_1 */
6635
Trond Myklebust17280172012-03-11 13:11:00 -04006636static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006637 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006638 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 .recover_open = nfs4_open_expired,
6640 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006641 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006642 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643};
6644
Andy Adamson591d71c2009-04-01 09:22:47 -04006645#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006646static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006647 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6648 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006649 .recover_open = nfs41_open_expired,
6650 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006651 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006652 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006653};
6654#endif /* CONFIG_NFS_V4_1 */
6655
Trond Myklebust17280172012-03-11 13:11:00 -04006656static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006657 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006658 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006659 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006660};
6661
6662#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006663static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006664 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006665 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006666 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006667};
6668#endif
6669
Trond Myklebust97dc1352010-06-16 09:52:26 -04006670static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6671 .minor_version = 0,
6672 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006673 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006674 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006675 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6676 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6677 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006678};
6679
6680#if defined(CONFIG_NFS_V4_1)
6681static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6682 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006683 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006684 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006685 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006686 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6687 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6688 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006689};
6690#endif
6691
Trond Myklebust97dc1352010-06-16 09:52:26 -04006692const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6693 [0] = &nfs_v4_0_minor_ops,
6694#if defined(CONFIG_NFS_V4_1)
6695 [1] = &nfs_v4_1_minor_ops,
6696#endif
6697};
6698
Bryan Schumaker73a79702012-07-16 16:39:12 -04006699const struct inode_operations nfs4_dir_inode_operations = {
6700 .create = nfs_create,
6701 .lookup = nfs_lookup,
6702 .atomic_open = nfs_atomic_open,
6703 .link = nfs_link,
6704 .unlink = nfs_unlink,
6705 .symlink = nfs_symlink,
6706 .mkdir = nfs_mkdir,
6707 .rmdir = nfs_rmdir,
6708 .mknod = nfs_mknod,
6709 .rename = nfs_rename,
6710 .permission = nfs_permission,
6711 .getattr = nfs_getattr,
6712 .setattr = nfs_setattr,
6713 .getxattr = generic_getxattr,
6714 .setxattr = generic_setxattr,
6715 .listxattr = generic_listxattr,
6716 .removexattr = generic_removexattr,
6717};
6718
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006719static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006720 .permission = nfs_permission,
6721 .getattr = nfs_getattr,
6722 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006723 .getxattr = generic_getxattr,
6724 .setxattr = generic_setxattr,
6725 .listxattr = generic_listxattr,
6726 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006727};
6728
David Howells509de812006-08-22 20:06:11 -04006729const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 .version = 4, /* protocol version */
6731 .dentry_ops = &nfs4_dentry_operations,
6732 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006733 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006734 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006736 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006737 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 .getattr = nfs4_proc_getattr,
6739 .setattr = nfs4_proc_setattr,
6740 .lookup = nfs4_proc_lookup,
6741 .access = nfs4_proc_access,
6742 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 .create = nfs4_proc_create,
6744 .remove = nfs4_proc_remove,
6745 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006746 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 .unlink_done = nfs4_proc_unlink_done,
6748 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006749 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006750 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006751 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 .link = nfs4_proc_link,
6753 .symlink = nfs4_proc_symlink,
6754 .mkdir = nfs4_proc_mkdir,
6755 .rmdir = nfs4_proc_remove,
6756 .readdir = nfs4_proc_readdir,
6757 .mknod = nfs4_proc_mknod,
6758 .statfs = nfs4_proc_statfs,
6759 .fsinfo = nfs4_proc_fsinfo,
6760 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006761 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 .decode_dirent = nfs4_decode_dirent,
6763 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006764 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006765 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006766 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006768 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006769 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006770 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006772 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006773 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006775 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006776 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006777 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006778 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006779 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006780 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006781 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006782 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006783 .create_server = nfs4_create_server,
6784 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785};
6786
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006787static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6788 .prefix = XATTR_NAME_NFSV4_ACL,
6789 .list = nfs4_xattr_list_nfs4_acl,
6790 .get = nfs4_xattr_get_nfs4_acl,
6791 .set = nfs4_xattr_set_nfs4_acl,
6792};
6793
6794const struct xattr_handler *nfs4_xattr_handlers[] = {
6795 &nfs4_xattr_nfs4_acl_handler,
6796 NULL
6797};
6798
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799/*
6800 * Local variables:
6801 * c-basic-offset: 8
6802 * End:
6803 */