blob: 87525eb60bd8e2fb37248fbf9fdb2dc887b2c6dc [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"
67
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:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond Myklebust961a8282012-01-17 22:57:37 -0500382bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
383{
384 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
385 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386}
387
Trond Myklebustd185a332010-06-16 09:52:25 -0400388static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400389{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500390 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400391 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500392 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400393
Benny Halevydfb4f3092010-09-24 09:17:01 -0400394 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400395 /* just wake up the next guy waiting since
396 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500397 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500398 return;
Andy Adamson13615872009-04-01 09:22:17 -0400399 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500400 tbl = res->sr_slot->table;
401 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500402
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500403 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500404 /* Be nice to the server: try to ensure that the last transmitted
405 * value for highest_user_slotid <= target_highest_slotid
406 */
407 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
408 send_new_highest_used_slotid = true;
409
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500410 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500411
412 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
413 send_new_highest_used_slotid = false;
414 if (!nfs4_session_draining(session)) {
415 if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
416 nfs4_set_task_privileged, NULL) != NULL)
417 send_new_highest_used_slotid = false;
418 }
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500419 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400420 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500421 if (send_new_highest_used_slotid)
422 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400423}
424
Trond Myklebust14516c32010-07-31 14:29:06 -0400425static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500427 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500428 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400429 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400430 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431
432 /*
433 * sr_status remains 1 if an RPC level error occurred. The server
434 * may or may not have processed the sequence operation..
435 * Proceed as if the server received and processed the sequence
436 * operation.
437 */
438 if (res->sr_status == 1)
439 res->sr_status = NFS_OK;
440
Bryan Schumaker468f8612011-04-18 15:57:32 -0400441 /* don't increment the sequence number if the task wasn't sent */
442 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 goto out;
444
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500446 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500447
Andy Adamson691daf32009-12-04 15:55:39 -0500448 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 switch (res->sr_status) {
450 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400451 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500452 ++slot->seq_nr;
453 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500454 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400455 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500456 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500459 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 break;
461 case -NFS4ERR_DELAY:
462 /* The server detected a resend of the RPC call and
463 * returned NFS4ERR_DELAY as per Section 2.10.6.2
464 * of RFC5661.
465 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500466 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400467 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500468 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500469 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 goto out_retry;
471 default:
472 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500473 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400474 }
475out:
476 /* The session may be reset by one of the error handlers. */
477 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400478 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 return 1;
480out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400481 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 goto out;
483 rpc_delay(task, NFS4_POLL_RETRY_MAX);
484 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485}
486
Trond Myklebust14516c32010-07-31 14:29:06 -0400487static int nfs4_sequence_done(struct rpc_task *task,
488 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400489{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500490 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 return 1;
492 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400493}
494
Trond Myklebust9d12b212012-01-17 22:04:25 -0500495static void nfs41_init_sequence(struct nfs4_sequence_args *args,
496 struct nfs4_sequence_res *res, int cache_reply)
497{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500498 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500499 args->sa_cache_this = 0;
500 if (cache_reply)
501 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500502 res->sr_slot = NULL;
503}
504
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000505int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 struct rpc_task *task)
509{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 struct nfs4_slot *slot;
511 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512
513 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400514 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400515 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400516 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400517
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 tbl = &session->fc_slot_table;
519
Trond Myklebust69d206b2012-11-22 13:21:02 -0500520 task->tk_timeout = 0;
521
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400523 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800524 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500525 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500526 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400527 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400528 }
529
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800530 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
531 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800532 dprintk("%s enforce FIFO order\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400533 goto out_sleep;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800534 }
535
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500536 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500537 if (IS_ERR(slot)) {
538 /* If out of memory, try again in 1/4 second */
539 if (slot == ERR_PTR(-ENOMEM))
540 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400541 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400542 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543 }
544 spin_unlock(&tbl->slot_tbl_lock);
545
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800546 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500547
548 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500550 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
551 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400552
Benny Halevydfb4f3092010-09-24 09:17:01 -0400553 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400554 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 /*
556 * sr_status is only set in decode_sequence, and so will remain
557 * set to 1 if an rpc level failure occurs.
558 */
559 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400560out_success:
561 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400563out_sleep:
564 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
565 spin_unlock(&tbl->slot_tbl_lock);
566 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000568EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569
Trond Myklebust035168a2010-06-16 09:52:26 -0400570int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571 struct nfs4_sequence_args *args,
572 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 struct rpc_task *task)
574{
Trond Myklebust035168a2010-06-16 09:52:26 -0400575 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 int ret = 0;
577
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400578 if (session == NULL) {
579 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400580 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400581 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400582
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500583 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400584 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500585 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400586
Trond Myklebust9d12b212012-01-17 22:04:25 -0500587 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588out:
589 dprintk("<-- %s status=%d\n", __func__, ret);
590 return ret;
591}
592
593struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400594 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595 struct nfs4_sequence_args *seq_args;
596 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597};
598
599static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
600{
601 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400602 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603
Trond Myklebust035168a2010-06-16 09:52:26 -0400604 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
605
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400606 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607}
608
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800609static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
610{
611 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
612 nfs41_call_sync_prepare(task, calldata);
613}
614
Andy Adamson69ab40c2009-04-01 09:22:19 -0400615static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
616{
617 struct nfs41_call_sync_data *data = calldata;
618
Trond Myklebust14516c32010-07-31 14:29:06 -0400619 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400620}
621
Trond Myklebust17280172012-03-11 13:11:00 -0400622static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400624 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625};
626
Trond Myklebust17280172012-03-11 13:11:00 -0400627static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800628 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
629 .rpc_call_done = nfs41_call_sync_done,
630};
631
Bryan Schumaker7c513052011-03-24 17:12:24 +0000632static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
633 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 struct rpc_message *msg,
635 struct nfs4_sequence_args *args,
636 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800637 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638{
639 int ret;
640 struct rpc_task *task;
641 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400642 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643 .seq_args = args,
644 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 };
646 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000647 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648 .rpc_message = msg,
649 .callback_ops = &nfs41_call_sync_ops,
650 .callback_data = &data
651 };
652
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800653 if (privileged)
654 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655 task = rpc_run_task(&task_setup);
656 if (IS_ERR(task))
657 ret = PTR_ERR(task);
658 else {
659 ret = task->tk_status;
660 rpc_put_task(task);
661 }
662 return ret;
663}
664
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400665static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000666int _nfs4_call_sync_session(struct rpc_clnt *clnt,
667 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400668 struct rpc_message *msg,
669 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400670 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400671{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500672 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400673}
674
Trond Myklebustdf896452010-06-16 09:52:26 -0400675#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500676static inline
677void nfs41_init_sequence(struct nfs4_sequence_args *args,
678 struct nfs4_sequence_res *res, int cache_reply)
679{
680}
681
Trond Myklebust14516c32010-07-31 14:29:06 -0400682static int nfs4_sequence_done(struct rpc_task *task,
683 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400684{
Trond Myklebust14516c32010-07-31 14:29:06 -0400685 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400686}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400687#endif /* CONFIG_NFS_V4_1 */
688
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400689static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000690int _nfs4_call_sync(struct rpc_clnt *clnt,
691 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400692 struct rpc_message *msg,
693 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400694 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400695{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000696 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400697}
698
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400699static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000700int nfs4_call_sync(struct rpc_clnt *clnt,
701 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000702 struct rpc_message *msg,
703 struct nfs4_sequence_args *args,
704 struct nfs4_sequence_res *res,
705 int cache_reply)
706{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400707 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000708 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400709 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000710}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711
Trond Myklebust38478b22006-05-25 01:40:57 -0400712static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Trond Myklebust38478b22006-05-25 01:40:57 -0400714 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Trond Myklebust38478b22006-05-25 01:40:57 -0400716 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400717 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700718 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400719 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700720 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400721 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400725 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100726 struct nfs_openargs o_arg;
727 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100728 struct nfs_open_confirmargs c_arg;
729 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500730 struct nfs4_string owner_name;
731 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100732 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100733 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400734 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100735 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400736 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100737 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100738 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400739 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100740 int rpc_status;
741 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742};
743
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400744
745static void nfs4_init_opendata_res(struct nfs4_opendata *p)
746{
747 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400748 p->o_res.seqid = p->o_arg.seqid;
749 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400750 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400751 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400752 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500753 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400754}
755
Al Viro82a2c1b2011-06-22 18:30:55 -0400756static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500757 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400758 const struct iattr *attrs,
759 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760{
Al Viro82a2c1b2011-06-22 18:30:55 -0400761 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762 struct inode *dir = parent->d_inode;
763 struct nfs_server *server = NFS_SERVER(dir);
764 struct nfs4_opendata *p;
765
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400766 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 if (p == NULL)
768 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400769 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 if (p->o_arg.seqid == NULL)
771 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400772 nfs_sb_active(dentry->d_sb);
773 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774 p->dir = parent;
775 p->owner = sp;
776 atomic_inc(&sp->so_count);
777 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500778 p->o_arg.open_flags = flags;
779 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700780 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
781 * will return permission denied for all bits until close */
782 if (!(flags & O_EXCL)) {
783 /* ask server to check for all possible rights as results
784 * are cached */
785 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
786 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700787 }
David Howells7539bba2006-08-22 20:06:09 -0400788 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400789 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
790 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400791 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792 p->o_arg.server = server;
793 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400794 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500796 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500797 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400798
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 p->o_arg.u.attrs = &p->attrs;
800 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500801
802 verf[0] = jiffies;
803 verf[1] = current->pid;
804 memcpy(p->o_arg.u.verifier.data, verf,
805 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100807 p->c_arg.fh = &p->o_res.fh;
808 p->c_arg.stateid = &p->o_res.stateid;
809 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400810 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 return p;
813err_free:
814 kfree(p);
815err:
816 dput(parent);
817 return NULL;
818}
819
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400820static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400822 struct nfs4_opendata *p = container_of(kref,
823 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400824 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400825
826 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827 if (p->state != NULL)
828 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400829 nfs4_put_state_owner(p->owner);
830 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400831 dput(p->dentry);
832 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500833 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400834 kfree(p);
835}
836
837static void nfs4_opendata_put(struct nfs4_opendata *p)
838{
839 if (p != NULL)
840 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841}
842
Trond Myklebust06f814a2006-01-03 09:55:07 +0100843static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
844{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100845 int ret;
846
Trond Myklebust06f814a2006-01-03 09:55:07 +0100847 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100848 return ret;
849}
850
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400852{
853 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854
Trond Myklebust536e43d2012-01-17 22:04:26 -0500855 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856 goto out;
857 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400858 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500859 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
860 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400861 break;
862 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500863 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
864 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400865 break;
866 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500867 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
868 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400869 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871 return ret;
872}
873
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500874static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400875{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500876 if (delegation == NULL)
877 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400879 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500880 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400881 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500882 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400883 return 1;
884}
885
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100887{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100889 case FMODE_WRITE:
890 state->n_wronly++;
891 break;
892 case FMODE_READ:
893 state->n_rdonly++;
894 break;
895 case FMODE_READ|FMODE_WRITE:
896 state->n_rdwr++;
897 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100899}
900
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400902{
903 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500904 nfs4_stateid_copy(&state->stateid, stateid);
905 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400907 case FMODE_READ:
908 set_bit(NFS_O_RDONLY_STATE, &state->flags);
909 break;
910 case FMODE_WRITE:
911 set_bit(NFS_O_WRONLY_STATE, &state->flags);
912 break;
913 case FMODE_READ|FMODE_WRITE:
914 set_bit(NFS_O_RDWR_STATE, &state->flags);
915 }
916}
917
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400919{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400920 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400922 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400923}
924
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925static 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 -0700926{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400927 /*
928 * Protect the call to nfs4_state_set_mode_locked and
929 * serialise the stateid update
930 */
931 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400932 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500933 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400934 set_bit(NFS_DELEGATED_STATE, &state->flags);
935 }
936 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400938 write_sequnlock(&state->seqlock);
939 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700941 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static 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 -0500945{
946 struct nfs_inode *nfsi = NFS_I(state->inode);
947 struct nfs_delegation *deleg_cur;
948 int ret = 0;
949
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500951
952 rcu_read_lock();
953 deleg_cur = rcu_dereference(nfsi->delegation);
954 if (deleg_cur == NULL)
955 goto no_delegation;
956
957 spin_lock(&deleg_cur->lock);
958 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500960 goto no_delegation_unlock;
961
962 if (delegation == NULL)
963 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500964 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500965 goto no_delegation_unlock;
966
Trond Myklebustb7391f42008-12-23 15:21:52 -0500967 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500969 ret = 1;
970no_delegation_unlock:
971 spin_unlock(&deleg_cur->lock);
972no_delegation:
973 rcu_read_unlock();
974
975 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500977 ret = 1;
978 }
979
980 return ret;
981}
982
983
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400985{
986 struct nfs_delegation *delegation;
987
988 rcu_read_lock();
989 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400991 rcu_read_unlock();
992 return;
993 }
994 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400995 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400996}
997
Trond Myklebust6ee41262007-07-08 14:11:36 -0400998static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400999{
1000 struct nfs4_state *state = opendata->state;
1001 struct nfs_inode *nfsi = NFS_I(state->inode);
1002 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001003 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005 nfs4_stateid stateid;
1006 int ret = -EAGAIN;
1007
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001010 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 if (can_open_cached(state, fmode, open_mode)) {
1012 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001013 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014 goto out_return_state;
1015 }
1016 spin_unlock(&state->owner->so_lock);
1017 }
Trond Myklebust34310432008-12-23 15:21:38 -05001018 rcu_read_lock();
1019 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001020 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001021 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001022 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001023 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001025 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001027 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 if (ret != 0)
1029 goto out;
1030 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001031
1032 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001034 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001035 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001036out:
1037 return ERR_PTR(ret);
1038out_return_state:
1039 atomic_inc(&state->count);
1040 return state;
1041}
1042
Andy Adamsone23008e2012-10-02 21:07:32 -04001043static void
1044nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1045{
1046 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1047 struct nfs_delegation *delegation;
1048 int delegation_flags = 0;
1049
1050 rcu_read_lock();
1051 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1052 if (delegation)
1053 delegation_flags = delegation->flags;
1054 rcu_read_unlock();
1055 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1056 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1057 "returning a delegation for "
1058 "OPEN(CLAIM_DELEGATE_CUR)\n",
1059 clp->cl_hostname);
1060 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1061 nfs_inode_set_delegation(state->inode,
1062 data->owner->so_cred,
1063 &data->o_res);
1064 else
1065 nfs_inode_reclaim_delegation(state->inode,
1066 data->owner->so_cred,
1067 &data->o_res);
1068}
1069
1070/*
1071 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1072 * and update the nfs4_state.
1073 */
1074static struct nfs4_state *
1075_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1076{
1077 struct inode *inode = data->state->inode;
1078 struct nfs4_state *state = data->state;
1079 int ret;
1080
1081 if (!data->rpc_done) {
1082 ret = data->rpc_status;
1083 goto err;
1084 }
1085
1086 ret = -ESTALE;
1087 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1088 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1089 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1090 goto err;
1091
1092 ret = -ENOMEM;
1093 state = nfs4_get_open_state(inode, data->owner);
1094 if (state == NULL)
1095 goto err;
1096
1097 ret = nfs_refresh_inode(inode, &data->f_attr);
1098 if (ret)
1099 goto err;
1100
1101 if (data->o_res.delegation_type != 0)
1102 nfs4_opendata_check_deleg(data, state);
1103 update_open_stateid(state, &data->o_res.stateid, NULL,
1104 data->o_arg.fmode);
1105
1106 return state;
1107err:
1108 return ERR_PTR(ret);
1109
1110}
1111
1112static struct nfs4_state *
1113_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114{
1115 struct inode *inode;
1116 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001118
Trond Myklebustaac00a82007-07-05 19:02:21 -04001119 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001120 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 goto out;
1122 }
1123
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001124 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001125 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001126 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001129 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 goto err;
1131 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001132 state = nfs4_get_open_state(inode, data->owner);
1133 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001135 if (data->o_res.delegation_type != 0)
1136 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001137 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001138 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001139 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001140out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001142err_put_inode:
1143 iput(inode);
1144err:
1145 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001146}
1147
Andy Adamsone23008e2012-10-02 21:07:32 -04001148static struct nfs4_state *
1149nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1150{
1151 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1152 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1153 return _nfs4_opendata_to_nfs4_state(data);
1154}
1155
Trond Myklebust864472e2006-01-03 09:55:15 +01001156static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1157{
1158 struct nfs_inode *nfsi = NFS_I(state->inode);
1159 struct nfs_open_context *ctx;
1160
1161 spin_lock(&state->inode->i_lock);
1162 list_for_each_entry(ctx, &nfsi->open_files, list) {
1163 if (ctx->state != state)
1164 continue;
1165 get_nfs_open_context(ctx);
1166 spin_unlock(&state->inode->i_lock);
1167 return ctx;
1168 }
1169 spin_unlock(&state->inode->i_lock);
1170 return ERR_PTR(-ENOENT);
1171}
1172
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001173static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1174{
1175 struct nfs4_opendata *opendata;
1176
Al Viro3d4ff432011-06-22 18:40:12 -04001177 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001178 if (opendata == NULL)
1179 return ERR_PTR(-ENOMEM);
1180 opendata->state = state;
1181 atomic_inc(&state->count);
1182 return opendata;
1183}
1184
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001186{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 int ret;
1189
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001190 opendata->o_arg.open_flags = 0;
1191 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001192 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1193 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1194 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001195 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 if (ret != 0)
1197 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001198 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001199 if (IS_ERR(newstate))
1200 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001201 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001202 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 return 0;
1204}
1205
1206static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1207{
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 int ret;
1210
1211 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 smp_rmb();
1214 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001215 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001217 if (ret != 0)
1218 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 if (newstate != state)
1220 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001221 }
1222 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001223 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001224 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 if (ret != 0)
1226 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001227 if (newstate != state)
1228 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 }
1230 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001231 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 if (ret != 0)
1234 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001235 if (newstate != state)
1236 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001238 /*
1239 * We may have performed cached opens for all three recoveries.
1240 * Check if we need to update the current stateid.
1241 */
1242 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001243 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001244 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001246 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001247 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001248 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 return 0;
1250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252/*
1253 * OPEN_RECLAIM:
1254 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001256static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001258 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001259 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001260 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int status;
1262
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001263 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1264 if (IS_ERR(opendata))
1265 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001266 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1267 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 rcu_read_lock();
1269 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001270 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001271 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001272 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001273 opendata->o_arg.u.delegation_type = delegation_type;
1274 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return status;
1277}
1278
Trond Myklebust539cd032007-06-05 11:46:42 -04001279static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct nfs_server *server = NFS_SERVER(state->inode);
1282 struct nfs4_exception exception = { };
1283 int err;
1284 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001285 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001286 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001287 break;
1288 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 } while (exception.retry);
1290 return err;
1291}
1292
Trond Myklebust864472e2006-01-03 09:55:15 +01001293static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1294{
1295 struct nfs_open_context *ctx;
1296 int ret;
1297
1298 ctx = nfs4_state_find_open_context(state);
1299 if (IS_ERR(ctx))
1300 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001301 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 put_nfs_open_context(ctx);
1303 return ret;
1304}
1305
Trond Myklebust13437e12007-07-06 15:10:43 -04001306static 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 -07001307{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001309 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001311 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1312 if (IS_ERR(opendata))
1313 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001314 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001315 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001316 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001317 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Trond Myklebust13437e12007-07-06 15:10:43 -04001321int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001324 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int err;
1326 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001327 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 switch (err) {
1329 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001330 case -ENOENT:
1331 case -ESTALE:
1332 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001333 case -NFS4ERR_BADSESSION:
1334 case -NFS4ERR_BADSLOT:
1335 case -NFS4ERR_BAD_HIGH_SLOT:
1336 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1337 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001338 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001339 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 case -NFS4ERR_STALE_CLIENTID:
1341 case -NFS4ERR_STALE_STATEID:
1342 case -NFS4ERR_EXPIRED:
1343 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001344 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001345 goto out;
1346 case -ERESTARTSYS:
1347 /*
1348 * The show must go on: exit, but mark the
1349 * stateid as needing recovery.
1350 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001351 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001352 case -NFS4ERR_ADMIN_REVOKED:
1353 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001354 nfs_inode_find_state_and_recover(state->inode,
1355 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001356 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001357 case -EKEYEXPIRED:
1358 /*
1359 * User RPCSEC_GSS context has expired.
1360 * We cannot recover this stateid now, so
1361 * skip it and allow recovery thread to
1362 * proceed.
1363 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001364 case -ENOMEM:
1365 err = 0;
1366 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 err = nfs4_handle_exception(server, err, &exception);
1369 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001370out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return err;
1372}
1373
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001374static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1375{
1376 struct nfs4_opendata *data = calldata;
1377
1378 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001379 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001380 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001381 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001382 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001383 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001384 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001385}
1386
1387static void nfs4_open_confirm_release(void *calldata)
1388{
1389 struct nfs4_opendata *data = calldata;
1390 struct nfs4_state *state = NULL;
1391
1392 /* If this request hasn't been cancelled, do nothing */
1393 if (data->cancelled == 0)
1394 goto out_free;
1395 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001396 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001397 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001399 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001400 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001402 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403}
1404
1405static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001406 .rpc_call_done = nfs4_open_confirm_done,
1407 .rpc_release = nfs4_open_confirm_release,
1408};
1409
1410/*
1411 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1412 */
1413static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1414{
1415 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1416 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001417 struct rpc_message msg = {
1418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1419 .rpc_argp = &data->c_arg,
1420 .rpc_resp = &data->c_res,
1421 .rpc_cred = data->owner->so_cred,
1422 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001423 struct rpc_task_setup task_setup_data = {
1424 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001425 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001426 .callback_ops = &nfs4_open_confirm_ops,
1427 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001428 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001429 .flags = RPC_TASK_ASYNC,
1430 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int status;
1432
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001433 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001434 data->rpc_done = 0;
1435 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001436 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001437 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001438 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001439 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001440 status = nfs4_wait_for_completion_rpc_task(task);
1441 if (status != 0) {
1442 data->cancelled = 1;
1443 smp_wmb();
1444 } else
1445 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001446 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return status;
1448}
1449
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 struct nfs4_opendata *data = calldata;
1453 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001454
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001455 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1456 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001457 /*
1458 * Check if we still need to send an OPEN call, or if we can use
1459 * a delegation instead.
1460 */
1461 if (data->state != NULL) {
1462 struct nfs_delegation *delegation;
1463
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001464 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001465 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001466 rcu_read_lock();
1467 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001468 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1469 can_open_delegated(delegation, data->o_arg.fmode))
1470 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001471 rcu_read_unlock();
1472 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001473 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001474 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001475 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001476 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001477 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001478 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1479 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001480 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001481 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001482 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001483 &data->o_res.seq_res,
1484 task) != 0)
1485 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001486 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001487unlock_no_action:
1488 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001489out_no_action:
1490 task->tk_action = NULL;
1491
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001494static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1495{
1496 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1497 nfs4_open_prepare(task, calldata);
1498}
1499
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001500static void nfs4_open_done(struct rpc_task *task, void *calldata)
1501{
1502 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001505
Trond Myklebust14516c32010-07-31 14:29:06 -04001506 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1507 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001508
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001510 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1511 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001512 case S_IFREG:
1513 break;
1514 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001516 break;
1517 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519 break;
1520 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001522 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001524 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001525 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1526 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001527 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001528 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531static void nfs4_open_release(void *calldata)
1532{
1533 struct nfs4_opendata *data = calldata;
1534 struct nfs4_state *state = NULL;
1535
1536 /* If this request hasn't been cancelled, do nothing */
1537 if (data->cancelled == 0)
1538 goto out_free;
1539 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001540 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541 goto out_free;
1542 /* In case we need an open_confirm, no cleanup! */
1543 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1544 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001546 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001547 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001549 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550}
1551
1552static const struct rpc_call_ops nfs4_open_ops = {
1553 .rpc_call_prepare = nfs4_open_prepare,
1554 .rpc_call_done = nfs4_open_done,
1555 .rpc_release = nfs4_open_release,
1556};
1557
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001558static const struct rpc_call_ops nfs4_recover_open_ops = {
1559 .rpc_call_prepare = nfs4_recover_open_prepare,
1560 .rpc_call_done = nfs4_open_done,
1561 .rpc_release = nfs4_open_release,
1562};
1563
1564static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001565{
1566 struct inode *dir = data->dir->d_inode;
1567 struct nfs_server *server = NFS_SERVER(dir);
1568 struct nfs_openargs *o_arg = &data->o_arg;
1569 struct nfs_openres *o_res = &data->o_res;
1570 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001571 struct rpc_message msg = {
1572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1573 .rpc_argp = o_arg,
1574 .rpc_resp = o_res,
1575 .rpc_cred = data->owner->so_cred,
1576 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001577 struct rpc_task_setup task_setup_data = {
1578 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001579 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001580 .callback_ops = &nfs4_open_ops,
1581 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001582 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001583 .flags = RPC_TASK_ASYNC,
1584 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001585 int status;
1586
Trond Myklebust9d12b212012-01-17 22:04:25 -05001587 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001588 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001589 data->rpc_done = 0;
1590 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001591 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001592 if (isrecover)
1593 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001594 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001595 if (IS_ERR(task))
1596 return PTR_ERR(task);
1597 status = nfs4_wait_for_completion_rpc_task(task);
1598 if (status != 0) {
1599 data->cancelled = 1;
1600 smp_wmb();
1601 } else
1602 status = data->rpc_status;
1603 rpc_put_task(task);
1604
1605 return status;
1606}
1607
1608static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1609{
1610 struct inode *dir = data->dir->d_inode;
1611 struct nfs_openres *o_res = &data->o_res;
1612 int status;
1613
1614 status = nfs4_run_open_task(data, 1);
1615 if (status != 0 || !data->rpc_done)
1616 return status;
1617
Trond Myklebust6926afd2012-01-07 13:22:46 -05001618 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1619
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001620 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1621 status = _nfs4_proc_open_confirm(data);
1622 if (status != 0)
1623 return status;
1624 }
1625
1626 return status;
1627}
1628
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001629static int nfs4_opendata_access(struct rpc_cred *cred,
1630 struct nfs4_opendata *opendata,
1631 struct nfs4_state *state, fmode_t fmode)
1632{
1633 struct nfs_access_entry cache;
1634 u32 mask;
1635
1636 /* access call failed or for some reason the server doesn't
1637 * support any access modes -- defer access call until later */
1638 if (opendata->o_res.access_supported == 0)
1639 return 0;
1640
1641 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001642 /* don't check MAY_WRITE - a newly created file may not have
1643 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001644 if (fmode & FMODE_READ)
1645 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001646 if (fmode & FMODE_EXEC)
1647 mask |= MAY_EXEC;
1648
1649 cache.cred = cred;
1650 cache.jiffies = jiffies;
1651 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1652 nfs_access_add_cache(state->inode, &cache);
1653
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001654 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001655 return 0;
1656
1657 /* even though OPEN succeeded, access is denied. Close the file */
1658 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001659 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001660}
1661
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001662/*
1663 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1664 */
1665static int _nfs4_proc_open(struct nfs4_opendata *data)
1666{
1667 struct inode *dir = data->dir->d_inode;
1668 struct nfs_server *server = NFS_SERVER(dir);
1669 struct nfs_openargs *o_arg = &data->o_arg;
1670 struct nfs_openres *o_res = &data->o_res;
1671 int status;
1672
1673 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001674 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001675 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001676 if (status != 0) {
1677 if (status == -NFS4ERR_BADNAME &&
1678 !(o_arg->open_flags & O_CREAT))
1679 return -ENOENT;
1680 return status;
1681 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682
Trond Myklebust6926afd2012-01-07 13:22:46 -05001683 nfs_fattr_map_and_free_names(server, &data->f_attr);
1684
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001685 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001686 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001687 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1688 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001690 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001692 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001695 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Andy Adamsond83217c2011-03-01 01:34:17 +00001699static int nfs4_recover_expired_lease(struct nfs_server *server)
1700{
1701 return nfs4_client_recover_expired_lease(server->nfs_client);
1702}
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704/*
1705 * OPEN_EXPIRED:
1706 * reclaim state on the server after a network partition.
1707 * Assumes caller holds the appropriate lock
1708 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001709static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001711 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001712 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001714 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1715 if (IS_ERR(opendata))
1716 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001717 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001718 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001719 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001720 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001724static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001725{
Trond Myklebust539cd032007-06-05 11:46:42 -04001726 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727 struct nfs4_exception exception = { };
1728 int err;
1729
1730 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001731 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001732 switch (err) {
1733 default:
1734 goto out;
1735 case -NFS4ERR_GRACE:
1736 case -NFS4ERR_DELAY:
1737 nfs4_handle_exception(server, err, &exception);
1738 err = 0;
1739 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001740 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001741out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001742 return err;
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Trond Myklebust864472e2006-01-03 09:55:15 +01001750 ctx = nfs4_state_find_open_context(state);
1751 if (IS_ERR(ctx))
1752 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001753 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001754 put_nfs_open_context(ctx);
1755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001758#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001759static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001761 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001762 nfs4_stateid *stateid = &state->stateid;
1763 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001764
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001765 /* If a state reset has been done, test_stateid is unneeded */
1766 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1767 return;
1768
1769 status = nfs41_test_stateid(server, stateid);
1770 if (status != NFS_OK) {
1771 /* Free the stateid unless the server explicitly
1772 * informs us the stateid is unrecognized. */
1773 if (status != -NFS4ERR_BAD_STATEID)
1774 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001775 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001776
Bryan Schumaker69388672012-09-26 15:25:52 -04001777 write_seqlock(&state->seqlock);
1778 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1779 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001780 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1781 }
1782}
1783
1784/**
1785 * nfs41_check_open_stateid - possibly free an open stateid
1786 *
1787 * @state: NFSv4 state for an inode
1788 *
1789 * Returns NFS_OK if recovery for this stateid is now finished.
1790 * Otherwise a negative NFS4ERR value is returned.
1791 */
1792static int nfs41_check_open_stateid(struct nfs4_state *state)
1793{
1794 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001795 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001796 int status;
1797
1798 /* If a state reset has been done, test_stateid is unneeded */
1799 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1800 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1801 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1802 return -NFS4ERR_BAD_STATEID;
1803
1804 status = nfs41_test_stateid(server, stateid);
1805 if (status != NFS_OK) {
1806 /* Free the stateid unless the server explicitly
1807 * informs us the stateid is unrecognized. */
1808 if (status != -NFS4ERR_BAD_STATEID)
1809 nfs41_free_stateid(server, stateid);
1810
1811 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1812 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1813 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001814 }
1815 return status;
1816}
1817
1818static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1819{
Chuck Levereb64cf92012-07-11 16:30:05 -04001820 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001821
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001822 nfs41_clear_delegation_stateid(state);
1823 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001824 if (status != NFS_OK)
1825 status = nfs4_open_expired(sp, state);
1826 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001827}
1828#endif
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001831 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1832 * fields corresponding to attributes that were used to store the verifier.
1833 * Make sure we clobber those fields in the later setattr call
1834 */
1835static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1836{
1837 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1838 !(sattr->ia_valid & ATTR_ATIME_SET))
1839 sattr->ia_valid |= ATTR_ATIME;
1840
1841 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1842 !(sattr->ia_valid & ATTR_MTIME_SET))
1843 sattr->ia_valid |= ATTR_MTIME;
1844}
1845
1846/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 */
Andy Adamson82be4172012-05-23 05:02:35 -04001849static int _nfs4_do_open(struct inode *dir,
1850 struct dentry *dentry,
1851 fmode_t fmode,
1852 int flags,
1853 struct iattr *sattr,
1854 struct rpc_cred *cred,
1855 struct nfs4_state **res,
1856 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 struct nfs4_state_owner *sp;
1859 struct nfs4_state *state = NULL;
1860 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001861 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001862 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001866 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1867 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1869 goto out_err;
1870 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001871 status = nfs4_recover_expired_lease(server);
1872 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001873 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001874 if (dentry->d_inode != NULL)
1875 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001876 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001877 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001878 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001879 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Andy Adamson82be4172012-05-23 05:02:35 -04001881 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1882 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1883 if (!opendata->f_attr.mdsthreshold)
1884 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001885 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001886 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001887 if (dentry->d_inode != NULL)
1888 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001889
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001890 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001892 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001894 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001895 status = PTR_ERR(state);
1896 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001897 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001898 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001899 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001900
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001901 status = nfs4_opendata_access(cred, opendata, state, fmode);
1902 if (status != 0)
1903 goto err_opendata_put;
1904
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001905 if (opendata->o_arg.open_flags & O_EXCL) {
1906 nfs4_exclusive_attrset(opendata, sattr);
1907
1908 nfs_fattr_init(opendata->o_res.f_attr);
1909 status = nfs4_do_setattr(state->inode, cred,
1910 opendata->o_res.f_attr, sattr,
1911 state);
1912 if (status == 0)
1913 nfs_setattr_update_inode(state->inode, sattr);
1914 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1915 }
Andy Adamson82be4172012-05-23 05:02:35 -04001916
1917 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1918 *ctx_th = opendata->f_attr.mdsthreshold;
1919 else
1920 kfree(opendata->f_attr.mdsthreshold);
1921 opendata->f_attr.mdsthreshold = NULL;
1922
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001923 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 *res = state;
1926 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001927err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001928 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001929 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001930err_put_state_owner:
1931 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 *res = NULL;
1934 return status;
1935}
1936
1937
Andy Adamson82be4172012-05-23 05:02:35 -04001938static struct nfs4_state *nfs4_do_open(struct inode *dir,
1939 struct dentry *dentry,
1940 fmode_t fmode,
1941 int flags,
1942 struct iattr *sattr,
1943 struct rpc_cred *cred,
1944 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 struct nfs4_exception exception = { };
1947 struct nfs4_state *res;
1948 int status;
1949
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001950 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001952 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1953 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (status == 0)
1955 break;
1956 /* NOTE: BAD_SEQID means the server and client disagree about the
1957 * book-keeping w.r.t. state-changing operations
1958 * (OPEN/CLOSE/LOCK/LOCKU...)
1959 * It is actually a sign of a bug on the client or on the server.
1960 *
1961 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001962 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * have unhashed the old state_owner for us, and that we can
1964 * therefore safely retry using a new one. We should still warn
1965 * the user though...
1966 */
1967 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001968 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001969 " returned a bad sequence-id error!\n",
1970 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 exception.retry = 1;
1972 continue;
1973 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001974 /*
1975 * BAD_STATEID on OPEN means that the server cancelled our
1976 * state before it received the OPEN_CONFIRM.
1977 * Recover by retrying the request as per the discussion
1978 * on Page 181 of RFC3530.
1979 */
1980 if (status == -NFS4ERR_BAD_STATEID) {
1981 exception.retry = 1;
1982 continue;
1983 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001984 if (status == -EAGAIN) {
1985 /* We must have found a delegation */
1986 exception.retry = 1;
1987 continue;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1990 status, &exception));
1991 } while (exception.retry);
1992 return res;
1993}
1994
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001995static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1996 struct nfs_fattr *fattr, struct iattr *sattr,
1997 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001999 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002001 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 .iap = sattr,
2003 .server = server,
2004 .bitmask = server->attr_bitmask,
2005 };
2006 struct nfs_setattrres res = {
2007 .fattr = fattr,
2008 .server = server,
2009 };
2010 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2012 .rpc_argp = &arg,
2013 .rpc_resp = &res,
2014 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002016 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002017 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Trond Myklebust0e574af2005-10-27 22:12:38 -04002019 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Trond Myklebust4fc87962012-03-08 17:42:01 -05002021 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002022 struct nfs_lockowner lockowner = {
2023 .l_owner = current->files,
2024 .l_pid = current->tgid,
2025 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002026 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002027 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002028 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2029 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002030 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002031 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002032 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Bryan Schumaker7c513052011-03-24 17:12:24 +00002034 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002035 if (status == 0 && state != NULL)
2036 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002037 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002040static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2041 struct nfs_fattr *fattr, struct iattr *sattr,
2042 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002044 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002045 struct nfs4_exception exception = {
2046 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002047 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 int err;
2050 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002051 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2052 switch (err) {
2053 case -NFS4ERR_OPENMODE:
2054 if (state && !(state->state & FMODE_WRITE)) {
2055 err = -EBADF;
2056 if (sattr->ia_valid & ATTR_OPEN)
2057 err = -EACCES;
2058 goto out;
2059 }
2060 }
2061 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002063out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return err;
2065}
2066
2067struct nfs4_closedata {
2068 struct inode *inode;
2069 struct nfs4_state *state;
2070 struct nfs_closeargs arg;
2071 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002072 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002073 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002074 bool roc;
2075 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076};
2077
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002078static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002079{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002080 struct nfs4_closedata *calldata = data;
2081 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002082 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002083
Fred Isamanf7e89172011-01-06 11:36:32 +00002084 if (calldata->roc)
2085 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002086 nfs4_put_open_state(calldata->state);
2087 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002088 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002089 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002090 kfree(calldata);
2091}
2092
Trond Myklebust88069f72009-12-08 08:33:16 -05002093static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2094 fmode_t fmode)
2095{
2096 spin_lock(&state->owner->so_lock);
2097 if (!(fmode & FMODE_READ))
2098 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2099 if (!(fmode & FMODE_WRITE))
2100 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2101 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2102 spin_unlock(&state->owner->so_lock);
2103}
2104
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002105static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002107 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct nfs_server *server = NFS_SERVER(calldata->inode);
2110
Chuck Levera3ca5652012-03-01 17:00:40 -05002111 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002112 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2113 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /* hmm. we are done with the inode, and in the process of freeing
2115 * the state_owner. we keep this around to process errors
2116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 switch (task->tk_status) {
2118 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002119 if (calldata->roc)
2120 pnfs_roc_set_barrier(state->inode,
2121 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002122 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002123 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002124 nfs4_close_clear_stateid_flags(state,
2125 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 break;
2127 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002128 case -NFS4ERR_OLD_STATEID:
2129 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002131 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002132 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002134 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2135 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002137 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002138 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002139 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002142static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002144 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002145 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002146 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002147 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002148
Chuck Levera3ca5652012-03-01 17:00:40 -05002149 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002150 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002151 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002152
Trond Myklebust88069f72009-12-08 08:33:16 -05002153 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2154 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002155 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002156 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002157 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002158 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002159 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2160 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2161 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002162 }
2163 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002164 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2165 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2166 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002167 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002168 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002169 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002170
2171 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002172 /* Note: exit _without_ calling nfs4_close_done */
2173 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002174 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002175 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002176
Fred Isamanf7e89172011-01-06 11:36:32 +00002177 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002178 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002179 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002180 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002181 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002182 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002183
Trond Myklebust516a6af2005-10-27 22:12:41 -04002184 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002185 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002186 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002187 &calldata->arg.seq_args,
2188 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002189 task) != 0)
2190 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002191out:
2192 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002195static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002196 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002197 .rpc_call_done = nfs4_close_done,
2198 .rpc_release = nfs4_free_closedata,
2199};
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201/*
2202 * It is possible for data to be read/written from a mem-mapped file
2203 * after the sys_close call (which hits the vfs layer as a flush).
2204 * This means that we can't safely call nfsv4 close on a file until
2205 * the inode is cleared. This in turn means that we are not good
2206 * NFSv4 citizens - we do not indicate to the server to update the file's
2207 * share state even when we are done with one of the three share
2208 * stateid's in the inode.
2209 *
2210 * NOTE: Caller must be holding the sp->so_owner semaphore!
2211 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002212int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002214 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002216 struct nfs4_state_owner *sp = state->owner;
2217 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002218 struct rpc_message msg = {
2219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2220 .rpc_cred = state->owner->so_cred,
2221 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002222 struct rpc_task_setup task_setup_data = {
2223 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002224 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002225 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002226 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002227 .flags = RPC_TASK_ASYNC,
2228 };
Trond Myklebust95121352005-10-18 14:20:12 -07002229 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002231 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002233 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002234 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002235 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002237 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002238 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002240 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002241 if (calldata->arg.seqid == NULL)
2242 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002243 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002244 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002245 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002246 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002247 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002248 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002249 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002250
Trond Myklebust1174dd12010-12-21 10:52:24 -05002251 msg.rpc_argp = &calldata->arg;
2252 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002253 task_setup_data.callback_data = calldata;
2254 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002255 if (IS_ERR(task))
2256 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002257 status = 0;
2258 if (wait)
2259 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002260 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002261 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002262out_free_calldata:
2263 kfree(calldata);
2264out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002265 nfs4_put_open_state(state);
2266 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002267 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
Trond Myklebust2b484292010-09-17 10:56:51 -04002270static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002271nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct nfs4_state *state;
2274
Trond Myklebust565277f2007-10-15 18:17:53 -04002275 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002276 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2277 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002278 if (IS_ERR(state))
2279 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002280 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002281 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Trond Myklebust1185a552009-12-03 15:54:02 -05002284static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002285{
2286 if (ctx->state == NULL)
2287 return;
2288 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002289 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002290 else
Al Viro643168c2011-06-22 18:20:23 -04002291 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002292}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2295{
Benny Halevy43652ad2009-04-01 09:21:54 -04002296 struct nfs4_server_caps_arg args = {
2297 .fhandle = fhandle,
2298 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 struct nfs4_server_caps_res res = {};
2300 struct rpc_message msg = {
2301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002302 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 .rpc_resp = &res,
2304 };
2305 int status;
2306
Bryan Schumaker7c513052011-03-24 17:12:24 +00002307 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (status == 0) {
2309 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002310 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2311 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2312 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2313 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2314 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2316 server->caps |= NFS_CAP_ACLS;
2317 if (res.has_links != 0)
2318 server->caps |= NFS_CAP_HARDLINKS;
2319 if (res.has_symlinks != 0)
2320 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002321 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2322 server->caps |= NFS_CAP_FILEID;
2323 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2324 server->caps |= NFS_CAP_MODE;
2325 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2326 server->caps |= NFS_CAP_NLINK;
2327 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2328 server->caps |= NFS_CAP_OWNER;
2329 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2330 server->caps |= NFS_CAP_OWNER_GROUP;
2331 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2332 server->caps |= NFS_CAP_ATIME;
2333 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2334 server->caps |= NFS_CAP_CTIME;
2335 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2336 server->caps |= NFS_CAP_MTIME;
2337
Trond Myklebusta65318b2009-03-11 14:10:28 -04002338 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2339 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2340 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002342 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return status;
2346}
2347
Trond Myklebust55a97592006-06-09 09:34:19 -04002348int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 struct nfs4_exception exception = { };
2351 int err;
2352 do {
2353 err = nfs4_handle_exception(server,
2354 _nfs4_server_capabilities(server, fhandle),
2355 &exception);
2356 } while (exception.retry);
2357 return err;
2358}
2359
2360static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2361 struct nfs_fsinfo *info)
2362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 struct nfs4_lookup_root_arg args = {
2364 .bitmask = nfs4_fattr_bitmap,
2365 };
2366 struct nfs4_lookup_res res = {
2367 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002368 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 .fh = fhandle,
2370 };
2371 struct rpc_message msg = {
2372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2373 .rpc_argp = &args,
2374 .rpc_resp = &res,
2375 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002376
Trond Myklebust0e574af2005-10-27 22:12:38 -04002377 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002378 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
2381static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2382 struct nfs_fsinfo *info)
2383{
2384 struct nfs4_exception exception = { };
2385 int err;
2386 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002387 err = _nfs4_lookup_root(server, fhandle, info);
2388 switch (err) {
2389 case 0:
2390 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002391 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002392 default:
2393 err = nfs4_handle_exception(server, err, &exception);
2394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002396out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return err;
2398}
2399
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002400static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2401 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2402{
2403 struct rpc_auth *auth;
2404 int ret;
2405
2406 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002407 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002408 ret = -EIO;
2409 goto out;
2410 }
2411 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002412out:
2413 return ret;
2414}
2415
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002416static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002417 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002419 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002420 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002422 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002423 if (len < 0)
2424 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002425
2426 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002427 /* AUTH_UNIX is the default flavor if none was specified,
2428 * thus has already been tried. */
2429 if (flav_array[i] == RPC_AUTH_UNIX)
2430 continue;
2431
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002432 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002433 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002434 continue;
2435 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002436 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002437 /*
2438 * -EACCESS could mean that the user doesn't have correct permissions
2439 * to access the mount. It could also mean that we tried to mount
2440 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2441 * existing mount programs don't handle -EACCES very well so it should
2442 * be mapped to -EPERM instead.
2443 */
2444 if (status == -EACCES)
2445 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002446 return status;
2447}
2448
2449/*
2450 * get the file handle for the "/" directory on the server
2451 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002452int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2453 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002454{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002455 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002456 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002457 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2458 /*
2459 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2460 * by nfs4_map_errors() as this function exits.
2461 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002462 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (status == 0)
2464 status = nfs4_server_capabilities(server, fhandle);
2465 if (status == 0)
2466 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002467 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Bryan Schumakerbae36242012-05-10 15:07:31 -04002470static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2471 struct nfs_fsinfo *info)
2472{
2473 int error;
2474 struct nfs_fattr *fattr = info->fattr;
2475
2476 error = nfs4_server_capabilities(server, mntfh);
2477 if (error < 0) {
2478 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2479 return error;
2480 }
2481
2482 error = nfs4_proc_getattr(server, mntfh, fattr);
2483 if (error < 0) {
2484 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2485 return error;
2486 }
2487
2488 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2489 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2490 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2491
2492 return error;
2493}
2494
Manoj Naik6b97fd32006-06-09 09:34:29 -04002495/*
2496 * Get locations and (maybe) other attributes of a referral.
2497 * Note that we'll actually follow the referral later when
2498 * we detect fsid mismatch in inode revalidation
2499 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002500static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2501 const struct qstr *name, struct nfs_fattr *fattr,
2502 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002503{
2504 int status = -ENOMEM;
2505 struct page *page = NULL;
2506 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002507
2508 page = alloc_page(GFP_KERNEL);
2509 if (page == NULL)
2510 goto out;
2511 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2512 if (locations == NULL)
2513 goto out;
2514
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002515 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002516 if (status != 0)
2517 goto out;
2518 /* Make sure server returned a different fsid for the referral */
2519 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002520 dprintk("%s: server did not return a different fsid for"
2521 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002522 status = -EIO;
2523 goto out;
2524 }
Andy Adamson533eb462011-06-13 18:25:56 -04002525 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2526 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002527
Andy Adamson533eb462011-06-13 18:25:56 -04002528 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002529 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002530 memset(fhandle, 0, sizeof(struct nfs_fh));
2531out:
2532 if (page)
2533 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002534 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002535 return status;
2536}
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2539{
2540 struct nfs4_getattr_arg args = {
2541 .fh = fhandle,
2542 .bitmask = server->attr_bitmask,
2543 };
2544 struct nfs4_getattr_res res = {
2545 .fattr = fattr,
2546 .server = server,
2547 };
2548 struct rpc_message msg = {
2549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2550 .rpc_argp = &args,
2551 .rpc_resp = &res,
2552 };
2553
Trond Myklebust0e574af2005-10-27 22:12:38 -04002554 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002555 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
2558static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2559{
2560 struct nfs4_exception exception = { };
2561 int err;
2562 do {
2563 err = nfs4_handle_exception(server,
2564 _nfs4_proc_getattr(server, fhandle, fattr),
2565 &exception);
2566 } while (exception.retry);
2567 return err;
2568}
2569
2570/*
2571 * The file is not closed if it is opened due to the a request to change
2572 * the size of the file. The open call will not be needed once the
2573 * VFS layer lookup-intents are implemented.
2574 *
2575 * Close is called when the inode is destroyed.
2576 * If we haven't opened the file for O_WRONLY, we
2577 * need to in the size_change case to obtain a stateid.
2578 *
2579 * Got race?
2580 * Because OPEN is always done by name in nfsv4, it is
2581 * possible that we opened a different file by the same
2582 * name. We can recognize this race condition, but we
2583 * can't do anything about it besides returning an error.
2584 *
2585 * This will be fixed with VFS changes (lookup-intent).
2586 */
2587static int
2588nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2589 struct iattr *sattr)
2590{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002591 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002592 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002593 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 int status;
2595
Benny Halevy8a1636c2010-07-14 15:43:57 -04002596 if (pnfs_ld_layoutret_on_setattr(inode))
2597 pnfs_return_layout(inode);
2598
Trond Myklebust0e574af2005-10-27 22:12:38 -04002599 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Andy Adamson26699402012-05-30 16:12:24 -04002601 /* Deal with open(O_TRUNC) */
2602 if (sattr->ia_valid & ATTR_OPEN)
2603 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2604
2605 /* Optimization: if the end result is no change, don't RPC */
2606 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2607 return 0;
2608
Trond Myklebustd5308382005-11-04 15:33:38 -05002609 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002610 if (sattr->ia_valid & ATTR_FILE) {
2611 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002612
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002613 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002614 if (ctx) {
2615 cred = ctx->cred;
2616 state = ctx->state;
2617 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002618 }
2619
2620 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002621 if (status == 0)
2622 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return status;
2624}
2625
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002626static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2627 const struct qstr *name, struct nfs_fh *fhandle,
2628 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002629{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002630 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002631 int status;
2632 struct nfs4_lookup_arg args = {
2633 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002634 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002635 .name = name,
2636 };
2637 struct nfs4_lookup_res res = {
2638 .server = server,
2639 .fattr = fattr,
2640 .fh = fhandle,
2641 };
2642 struct rpc_message msg = {
2643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2644 .rpc_argp = &args,
2645 .rpc_resp = &res,
2646 };
2647
2648 nfs_fattr_init(fattr);
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002651 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 dprintk("NFS reply lookup: %d\n", status);
2653 return status;
2654}
2655
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002656static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002657{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002658 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002659 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002660 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2661 fattr->nlink = 2;
2662}
2663
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002664static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2665 struct qstr *name, struct nfs_fh *fhandle,
2666 struct nfs_fattr *fattr)
2667{
2668 struct nfs4_exception exception = { };
2669 struct rpc_clnt *client = *clnt;
2670 int err;
2671 do {
2672 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2673 switch (err) {
2674 case -NFS4ERR_BADNAME:
2675 err = -ENOENT;
2676 goto out;
2677 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002678 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002679 goto out;
2680 case -NFS4ERR_WRONGSEC:
2681 err = -EPERM;
2682 if (client != *clnt)
2683 goto out;
2684
2685 client = nfs4_create_sec_client(client, dir, name);
2686 if (IS_ERR(client))
2687 return PTR_ERR(client);
2688
2689 exception.retry = 1;
2690 break;
2691 default:
2692 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2693 }
2694 } while (exception.retry);
2695
2696out:
2697 if (err == 0)
2698 *clnt = client;
2699 else if (client != *clnt)
2700 rpc_shutdown_client(client);
2701
2702 return err;
2703}
2704
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002705static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002706 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002708 int status;
2709 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002710
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002711 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2712 if (client != NFS_CLIENT(dir)) {
2713 rpc_shutdown_client(client);
2714 nfs_fixup_secinfo_attributes(fattr);
2715 }
2716 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002719struct rpc_clnt *
2720nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2721 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2722{
2723 int status;
2724 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2725
2726 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2727 if (status < 0) {
2728 rpc_shutdown_client(client);
2729 return ERR_PTR(status);
2730 }
2731 return client;
2732}
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2735{
Trond Myklebust76b32992007-08-10 17:45:11 -04002736 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct nfs4_accessargs args = {
2738 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002739 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002741 struct nfs4_accessres res = {
2742 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002743 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 struct rpc_message msg = {
2745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2746 .rpc_argp = &args,
2747 .rpc_resp = &res,
2748 .rpc_cred = entry->cred,
2749 };
2750 int mode = entry->mask;
2751 int status;
2752
2753 /*
2754 * Determine which access bits we want to ask for...
2755 */
2756 if (mode & MAY_READ)
2757 args.access |= NFS4_ACCESS_READ;
2758 if (S_ISDIR(inode->i_mode)) {
2759 if (mode & MAY_WRITE)
2760 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2761 if (mode & MAY_EXEC)
2762 args.access |= NFS4_ACCESS_LOOKUP;
2763 } else {
2764 if (mode & MAY_WRITE)
2765 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2766 if (mode & MAY_EXEC)
2767 args.access |= NFS4_ACCESS_EXECUTE;
2768 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002769
2770 res.fattr = nfs_alloc_fattr();
2771 if (res.fattr == NULL)
2772 return -ENOMEM;
2773
Bryan Schumaker7c513052011-03-24 17:12:24 +00002774 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002776 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002777 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002779 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return status;
2781}
2782
2783static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2784{
2785 struct nfs4_exception exception = { };
2786 int err;
2787 do {
2788 err = nfs4_handle_exception(NFS_SERVER(inode),
2789 _nfs4_proc_access(inode, entry),
2790 &exception);
2791 } while (exception.retry);
2792 return err;
2793}
2794
2795/*
2796 * TODO: For the time being, we don't try to get any attributes
2797 * along with any of the zero-copy operations READ, READDIR,
2798 * READLINK, WRITE.
2799 *
2800 * In the case of the first three, we want to put the GETATTR
2801 * after the read-type operation -- this is because it is hard
2802 * to predict the length of a GETATTR response in v4, and thus
2803 * align the READ data correctly. This means that the GETATTR
2804 * may end up partially falling into the page cache, and we should
2805 * shift it into the 'tail' of the xdr_buf before processing.
2806 * To do this efficiently, we need to know the total length
2807 * of data received, which doesn't seem to be available outside
2808 * of the RPC layer.
2809 *
2810 * In the case of WRITE, we also want to put the GETATTR after
2811 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002812 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 *
2814 * Both of these changes to the XDR layer would in fact be quite
2815 * minor, but I decided to leave them for a subsequent patch.
2816 */
2817static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2818 unsigned int pgbase, unsigned int pglen)
2819{
2820 struct nfs4_readlink args = {
2821 .fh = NFS_FH(inode),
2822 .pgbase = pgbase,
2823 .pglen = pglen,
2824 .pages = &page,
2825 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002826 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 struct rpc_message msg = {
2828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2829 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002830 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 };
2832
Bryan Schumaker7c513052011-03-24 17:12:24 +00002833 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 -07002834}
2835
2836static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2837 unsigned int pgbase, unsigned int pglen)
2838{
2839 struct nfs4_exception exception = { };
2840 int err;
2841 do {
2842 err = nfs4_handle_exception(NFS_SERVER(inode),
2843 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2844 &exception);
2845 } while (exception.retry);
2846 return err;
2847}
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002850 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852static int
2853nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002854 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 int status = 0;
2859
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002860 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2861 if (IS_ERR(ctx))
2862 return PTR_ERR(ctx);
2863
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002864 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002865 state = nfs4_do_open(dir, dentry, ctx->mode,
2866 flags, sattr, ctx->cred,
2867 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002868 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (IS_ERR(state)) {
2870 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002873 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002874 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002875 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002877 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return status;
2879}
2880
2881static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2882{
Trond Myklebust16e42952005-10-27 22:12:44 -04002883 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002884 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002886 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002888 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002889 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002890 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2893 .rpc_argp = &args,
2894 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002896 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002897
Bryan Schumaker7c513052011-03-24 17:12:24 +00002898 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002899 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002900 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902}
2903
2904static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(NFS_SERVER(dir),
2910 _nfs4_proc_remove(dir, name),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002916static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002918 struct nfs_server *server = NFS_SERVER(dir);
2919 struct nfs_removeargs *args = msg->rpc_argp;
2920 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002922 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002924 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002927static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2928{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002929 nfs4_setup_sequence(NFS_SERVER(data->dir),
2930 &data->args.seq_args,
2931 &data->res.seq_res,
2932 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933}
2934
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002935static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002937 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2938
Trond Myklebust14516c32010-07-31 14:29:06 -04002939 if (!nfs4_sequence_done(task, &res->seq_res))
2940 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002941 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002942 return 0;
2943 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002944 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
Jeff Laytond3d41522010-09-17 17:31:57 -04002947static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2948{
2949 struct nfs_server *server = NFS_SERVER(dir);
2950 struct nfs_renameargs *arg = msg->rpc_argp;
2951 struct nfs_renameres *res = msg->rpc_resp;
2952
2953 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002954 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002955 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002956}
2957
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002958static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2959{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002960 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2961 &data->args.seq_args,
2962 &data->res.seq_res,
2963 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002964}
2965
2966static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2967 struct inode *new_dir)
2968{
2969 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2970
2971 if (!nfs4_sequence_done(task, &res->seq_res))
2972 return 0;
2973 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2974 return 0;
2975
2976 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002977 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002978 return 1;
2979}
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2982 struct inode *new_dir, struct qstr *new_name)
2983{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002984 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002985 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 .old_dir = NFS_FH(old_dir),
2987 .new_dir = NFS_FH(new_dir),
2988 .old_name = old_name,
2989 .new_name = new_name,
2990 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002991 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002992 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002993 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 struct rpc_message msg = {
2995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2996 .rpc_argp = &arg,
2997 .rpc_resp = &res,
2998 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002999 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Bryan Schumaker7c513052011-03-24 17:12:24 +00003001 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (!status) {
3003 update_changeattr(old_dir, &res.old_cinfo);
3004 update_changeattr(new_dir, &res.new_cinfo);
3005 }
3006 return status;
3007}
3008
3009static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3010 struct inode *new_dir, struct qstr *new_name)
3011{
3012 struct nfs4_exception exception = { };
3013 int err;
3014 do {
3015 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3016 _nfs4_proc_rename(old_dir, old_name,
3017 new_dir, new_name),
3018 &exception);
3019 } while (exception.retry);
3020 return err;
3021}
3022
3023static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3024{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003025 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 struct nfs4_link_arg arg = {
3027 .fh = NFS_FH(inode),
3028 .dir_fh = NFS_FH(dir),
3029 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003030 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003032 struct nfs4_link_res res = {
3033 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct rpc_message msg = {
3036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3037 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003038 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003040 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Trond Myklebust136f2622010-04-16 16:22:49 -04003042 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003043 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003044 goto out;
3045
Bryan Schumaker7c513052011-03-24 17:12:24 +00003046 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003047 if (!status) {
3048 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003049 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003050 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003051out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003052 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return status;
3054}
3055
3056static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3057{
3058 struct nfs4_exception exception = { };
3059 int err;
3060 do {
3061 err = nfs4_handle_exception(NFS_SERVER(inode),
3062 _nfs4_proc_link(inode, dir, name),
3063 &exception);
3064 } while (exception.retry);
3065 return err;
3066}
3067
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003068struct nfs4_createdata {
3069 struct rpc_message msg;
3070 struct nfs4_create_arg arg;
3071 struct nfs4_create_res res;
3072 struct nfs_fh fh;
3073 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003074};
3075
3076static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3077 struct qstr *name, struct iattr *sattr, u32 ftype)
3078{
3079 struct nfs4_createdata *data;
3080
3081 data = kzalloc(sizeof(*data), GFP_KERNEL);
3082 if (data != NULL) {
3083 struct nfs_server *server = NFS_SERVER(dir);
3084
3085 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3086 data->msg.rpc_argp = &data->arg;
3087 data->msg.rpc_resp = &data->res;
3088 data->arg.dir_fh = NFS_FH(dir);
3089 data->arg.server = server;
3090 data->arg.name = name;
3091 data->arg.attrs = sattr;
3092 data->arg.ftype = ftype;
3093 data->arg.bitmask = server->attr_bitmask;
3094 data->res.server = server;
3095 data->res.fh = &data->fh;
3096 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003097 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003098 }
3099 return data;
3100}
3101
3102static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3103{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003104 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003105 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003106 if (status == 0) {
3107 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003108 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3109 }
3110 return status;
3111}
3112
3113static void nfs4_free_createdata(struct nfs4_createdata *data)
3114{
3115 kfree(data);
3116}
3117
Chuck Lever4f390c12006-08-22 20:06:22 -04003118static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003119 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003121 struct nfs4_createdata *data;
3122 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Chuck Lever94a6d752006-08-22 20:06:23 -04003124 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003125 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003126
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 status = -ENOMEM;
3128 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3129 if (data == NULL)
3130 goto out;
3131
3132 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3133 data->arg.u.symlink.pages = &page;
3134 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003136 status = nfs4_do_create(dir, dentry, data);
3137
3138 nfs4_free_createdata(data);
3139out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return status;
3141}
3142
Chuck Lever4f390c12006-08-22 20:06:22 -04003143static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003144 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
3146 struct nfs4_exception exception = { };
3147 int err;
3148 do {
3149 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003150 _nfs4_proc_symlink(dir, dentry, page,
3151 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 &exception);
3153 } while (exception.retry);
3154 return err;
3155}
3156
3157static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3158 struct iattr *sattr)
3159{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003160 struct nfs4_createdata *data;
3161 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003163 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3164 if (data == NULL)
3165 goto out;
3166
3167 status = nfs4_do_create(dir, dentry, data);
3168
3169 nfs4_free_createdata(data);
3170out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return status;
3172}
3173
3174static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3175 struct iattr *sattr)
3176{
3177 struct nfs4_exception exception = { };
3178 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003179
3180 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 do {
3182 err = nfs4_handle_exception(NFS_SERVER(dir),
3183 _nfs4_proc_mkdir(dir, dentry, sattr),
3184 &exception);
3185 } while (exception.retry);
3186 return err;
3187}
3188
3189static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003190 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
3192 struct inode *dir = dentry->d_inode;
3193 struct nfs4_readdir_arg args = {
3194 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003195 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 .pgbase = 0,
3197 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003198 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003199 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 };
3201 struct nfs4_readdir_res res;
3202 struct rpc_message msg = {
3203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3204 .rpc_argp = &args,
3205 .rpc_resp = &res,
3206 .rpc_cred = cred,
3207 };
3208 int status;
3209
Harvey Harrison3110ff82008-05-02 13:42:44 -07003210 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003211 dentry->d_parent->d_name.name,
3212 dentry->d_name.name,
3213 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003214 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003216 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 -05003217 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003218 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003219 status += args.pgbase;
3220 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003221
3222 nfs_invalidate_atime(dir);
3223
Harvey Harrison3110ff82008-05-02 13:42:44 -07003224 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return status;
3226}
3227
3228static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003229 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 struct nfs4_exception exception = { };
3232 int err;
3233 do {
3234 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3235 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003236 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 &exception);
3238 } while (exception.retry);
3239 return err;
3240}
3241
3242static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3243 struct iattr *sattr, dev_t rdev)
3244{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003245 struct nfs4_createdata *data;
3246 int mode = sattr->ia_mode;
3247 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003249 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3250 if (data == NULL)
3251 goto out;
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003254 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003256 data->arg.ftype = NF4BLK;
3257 data->arg.u.device.specdata1 = MAJOR(rdev);
3258 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 }
3260 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003261 data->arg.ftype = NF4CHR;
3262 data->arg.u.device.specdata1 = MAJOR(rdev);
3263 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003264 } else if (!S_ISSOCK(mode)) {
3265 status = -EINVAL;
3266 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003269 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003270out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003271 nfs4_free_createdata(data);
3272out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return status;
3274}
3275
3276static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3277 struct iattr *sattr, dev_t rdev)
3278{
3279 struct nfs4_exception exception = { };
3280 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003281
3282 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 do {
3284 err = nfs4_handle_exception(NFS_SERVER(dir),
3285 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3286 &exception);
3287 } while (exception.retry);
3288 return err;
3289}
3290
3291static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3292 struct nfs_fsstat *fsstat)
3293{
3294 struct nfs4_statfs_arg args = {
3295 .fh = fhandle,
3296 .bitmask = server->attr_bitmask,
3297 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003298 struct nfs4_statfs_res res = {
3299 .fsstat = fsstat,
3300 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 struct rpc_message msg = {
3302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3303 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003304 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 };
3306
Trond Myklebust0e574af2005-10-27 22:12:38 -04003307 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003308 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
3311static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3312{
3313 struct nfs4_exception exception = { };
3314 int err;
3315 do {
3316 err = nfs4_handle_exception(server,
3317 _nfs4_proc_statfs(server, fhandle, fsstat),
3318 &exception);
3319 } while (exception.retry);
3320 return err;
3321}
3322
3323static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3324 struct nfs_fsinfo *fsinfo)
3325{
3326 struct nfs4_fsinfo_arg args = {
3327 .fh = fhandle,
3328 .bitmask = server->attr_bitmask,
3329 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003330 struct nfs4_fsinfo_res res = {
3331 .fsinfo = fsinfo,
3332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 struct rpc_message msg = {
3334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3335 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003336 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 };
3338
Bryan Schumaker7c513052011-03-24 17:12:24 +00003339 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340}
3341
3342static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3343{
3344 struct nfs4_exception exception = { };
3345 int err;
3346
3347 do {
3348 err = nfs4_handle_exception(server,
3349 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3350 &exception);
3351 } while (exception.retry);
3352 return err;
3353}
3354
3355static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3356{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003357 int error;
3358
Trond Myklebust0e574af2005-10-27 22:12:38 -04003359 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003360 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003361 if (error == 0) {
3362 /* block layout checks this! */
3363 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003364 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003365 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003366
3367 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
3370static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3371 struct nfs_pathconf *pathconf)
3372{
3373 struct nfs4_pathconf_arg args = {
3374 .fh = fhandle,
3375 .bitmask = server->attr_bitmask,
3376 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003377 struct nfs4_pathconf_res res = {
3378 .pathconf = pathconf,
3379 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 struct rpc_message msg = {
3381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3382 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003383 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 };
3385
3386 /* None of the pathconf attributes are mandatory to implement */
3387 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3388 memset(pathconf, 0, sizeof(*pathconf));
3389 return 0;
3390 }
3391
Trond Myklebust0e574af2005-10-27 22:12:38 -04003392 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003393 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
3396static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3397 struct nfs_pathconf *pathconf)
3398{
3399 struct nfs4_exception exception = { };
3400 int err;
3401
3402 do {
3403 err = nfs4_handle_exception(server,
3404 _nfs4_proc_pathconf(server, fhandle, pathconf),
3405 &exception);
3406 } while (exception.retry);
3407 return err;
3408}
3409
Benny Halevyd20581a2011-05-22 19:52:03 +03003410void __nfs4_read_done_cb(struct nfs_read_data *data)
3411{
Fred Isamancd841602012-04-20 14:47:44 -04003412 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003413}
3414
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003415static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
Fred Isamancd841602012-04-20 14:47:44 -04003417 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003419 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003420 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003421 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003423
Benny Halevyd20581a2011-05-22 19:52:03 +03003424 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003426 renew_lease(server, data->timestamp);
3427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003430static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3431{
3432
3433 dprintk("--> %s\n", __func__);
3434
3435 if (!nfs4_sequence_done(task, &data->res.seq_res))
3436 return -EAGAIN;
3437
Benny Halevyd20581a2011-05-22 19:52:03 +03003438 return data->read_done_cb ? data->read_done_cb(task, data) :
3439 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003440}
3441
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003442static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003445 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003446 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003447 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003450static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3451{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003452 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3453 &data->args.seq_args,
3454 &data->res.seq_res,
3455 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456}
3457
Fred Isamanb029bc92011-03-03 15:13:42 +00003458static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
Fred Isamancd841602012-04-20 14:47:44 -04003460 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003462 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003463 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003464 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003466 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003468 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003469 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Fred Isamanb029bc92011-03-03 15:13:42 +00003473static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3474{
3475 if (!nfs4_sequence_done(task, &data->res.seq_res))
3476 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003477 return data->write_done_cb ? data->write_done_cb(task, data) :
3478 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003479}
3480
Trond Myklebust5a37f852012-04-28 14:55:16 -04003481static
3482bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003483{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003484 const struct nfs_pgio_header *hdr = data->header;
3485
3486 /* Don't request attributes for pNFS or O_DIRECT writes */
3487 if (data->ds_clp != NULL || hdr->dreq != NULL)
3488 return false;
3489 /* Otherwise, request attributes if and only if we don't hold
3490 * a delegation
3491 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003492 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003493}
Fred Isamana69aef12011-03-03 15:13:47 +00003494
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003495static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
Fred Isamancd841602012-04-20 14:47:44 -04003497 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003498
Trond Myklebust5a37f852012-04-28 14:55:16 -04003499 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003500 data->args.bitmask = NULL;
3501 data->res.fattr = NULL;
3502 } else
3503 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003504
Fred Isamanb029bc92011-03-03 15:13:42 +00003505 if (!data->write_done_cb)
3506 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003507 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 data->timestamp = jiffies;
3509
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003510 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003511 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003514static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3515{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003516 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3517 &data->args.seq_args,
3518 &data->res.seq_res,
3519 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520}
3521
Fred Isaman0b7c0152012-04-20 14:47:39 -04003522static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3523{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003524 nfs4_setup_sequence(NFS_SERVER(data->inode),
3525 &data->args.seq_args,
3526 &data->res.seq_res,
3527 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
Fred Isaman0b7c0152012-04-20 14:47:39 -04003530static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003533
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003534 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003535 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003536 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539}
3540
Fred Isaman0b7c0152012-04-20 14:47:39 -04003541static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003542{
3543 if (!nfs4_sequence_done(task, &data->res.seq_res))
3544 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003545 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003546}
3547
Fred Isaman0b7c0152012-04-20 14:47:39 -04003548static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003550 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003551
Fred Isaman0b7c0152012-04-20 14:47:39 -04003552 if (data->commit_done_cb == NULL)
3553 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003554 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003555 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003556 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557}
3558
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003559struct nfs4_renewdata {
3560 struct nfs_client *client;
3561 unsigned long timestamp;
3562};
3563
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564/*
3565 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3566 * standalone procedure for queueing an asynchronous RENEW.
3567 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003568static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003569{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003570 struct nfs4_renewdata *data = calldata;
3571 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003572
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003573 if (atomic_read(&clp->cl_count) > 1)
3574 nfs4_schedule_state_renewal(clp);
3575 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003576 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003577}
3578
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003579static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003581 struct nfs4_renewdata *data = calldata;
3582 struct nfs_client *clp = data->client;
3583 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
3585 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003586 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003587 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3588 return;
3589 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003590 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003591 return;
3592 }
3593 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003595 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
3597
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003598static const struct rpc_call_ops nfs4_renew_ops = {
3599 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003600 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003601};
3602
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003603static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 struct rpc_message msg = {
3606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3607 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003608 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003610 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003612 if (renew_flags == 0)
3613 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003614 if (!atomic_inc_not_zero(&clp->cl_count))
3615 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003616 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003617 if (data == NULL)
3618 return -ENOMEM;
3619 data->client = clp;
3620 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003622 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623}
3624
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003625static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
3627 struct rpc_message msg = {
3628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3629 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003630 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 };
3632 unsigned long now = jiffies;
3633 int status;
3634
3635 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3636 if (status < 0)
3637 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003638 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return 0;
3640}
3641
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003642static inline int nfs4_server_supports_acls(struct nfs_server *server)
3643{
3644 return (server->caps & NFS_CAP_ACLS)
3645 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3646 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3647}
3648
Trond Myklebust21f498c2012-08-24 10:59:25 -04003649/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3650 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003651 * the stack.
3652 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003653#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003654
Neil Hormane9e3d722011-03-04 19:26:03 -05003655static int buf_to_pages_noslab(const void *buf, size_t buflen,
3656 struct page **pages, unsigned int *pgbase)
3657{
3658 struct page *newpage, **spages;
3659 int rc = 0;
3660 size_t len;
3661 spages = pages;
3662
3663 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003664 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003665 newpage = alloc_page(GFP_KERNEL);
3666
3667 if (newpage == NULL)
3668 goto unwind;
3669 memcpy(page_address(newpage), buf, len);
3670 buf += len;
3671 buflen -= len;
3672 *pages++ = newpage;
3673 rc++;
3674 } while (buflen != 0);
3675
3676 return rc;
3677
3678unwind:
3679 for(; rc > 0; rc--)
3680 __free_page(spages[rc-1]);
3681 return -ENOMEM;
3682}
3683
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003684struct nfs4_cached_acl {
3685 int cached;
3686 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003687 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003688};
3689
3690static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003691{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003692 struct nfs_inode *nfsi = NFS_I(inode);
3693
3694 spin_lock(&inode->i_lock);
3695 kfree(nfsi->nfs4_acl);
3696 nfsi->nfs4_acl = acl;
3697 spin_unlock(&inode->i_lock);
3698}
3699
3700static void nfs4_zap_acl_attr(struct inode *inode)
3701{
3702 nfs4_set_cached_acl(inode, NULL);
3703}
3704
3705static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3706{
3707 struct nfs_inode *nfsi = NFS_I(inode);
3708 struct nfs4_cached_acl *acl;
3709 int ret = -ENOENT;
3710
3711 spin_lock(&inode->i_lock);
3712 acl = nfsi->nfs4_acl;
3713 if (acl == NULL)
3714 goto out;
3715 if (buf == NULL) /* user is just asking for length */
3716 goto out_len;
3717 if (acl->cached == 0)
3718 goto out;
3719 ret = -ERANGE; /* see getxattr(2) man page */
3720 if (acl->len > buflen)
3721 goto out;
3722 memcpy(buf, acl->data, acl->len);
3723out_len:
3724 ret = acl->len;
3725out:
3726 spin_unlock(&inode->i_lock);
3727 return ret;
3728}
3729
Sachin Prabhu5794d212012-04-17 14:36:40 +01003730static 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 +00003731{
3732 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003733 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003734
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003735 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003736 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003737 if (acl == NULL)
3738 goto out;
3739 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003740 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003741 } else {
3742 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3743 if (acl == NULL)
3744 goto out;
3745 acl->cached = 0;
3746 }
3747 acl->len = acl_len;
3748out:
3749 nfs4_set_cached_acl(inode, acl);
3750}
3751
Andy Adamsonbf118a32011-12-07 11:55:27 -05003752/*
3753 * The getxattr API returns the required buffer length when called with a
3754 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3755 * the required buf. On a NULL buf, we send a page of data to the server
3756 * guessing that the ACL request can be serviced by a page. If so, we cache
3757 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3758 * the cache. If not so, we throw away the page, and cache the required
3759 * length. The next getxattr call will then produce another round trip to
3760 * the server, this time with the input buf of the required size.
3761 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003762static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003763{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003764 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003765 struct nfs_getaclargs args = {
3766 .fh = NFS_FH(inode),
3767 .acl_pages = pages,
3768 .acl_len = buflen,
3769 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003770 struct nfs_getaclres res = {
3771 .acl_len = buflen,
3772 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003773 struct rpc_message msg = {
3774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3775 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003776 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003777 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003778 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3779 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003780
Andy Adamsonbf118a32011-12-07 11:55:27 -05003781 /* As long as we're doing a round trip to the server anyway,
3782 * let's be prepared for a page of acl data. */
3783 if (npages == 0)
3784 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003785 if (npages > ARRAY_SIZE(pages))
3786 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003787
Andy Adamsonbf118a32011-12-07 11:55:27 -05003788 for (i = 0; i < npages; i++) {
3789 pages[i] = alloc_page(GFP_KERNEL);
3790 if (!pages[i])
3791 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003792 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003793
3794 /* for decoding across pages */
3795 res.acl_scratch = alloc_page(GFP_KERNEL);
3796 if (!res.acl_scratch)
3797 goto out_free;
3798
Andy Adamsonbf118a32011-12-07 11:55:27 -05003799 args.acl_len = npages * PAGE_SIZE;
3800 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003801
Peng Taode040be2012-01-10 22:42:47 +08003802 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003803 __func__, buf, buflen, npages, args.acl_len);
3804 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3805 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003806 if (ret)
3807 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003808
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003809 /* Handle the case where the passed-in buffer is too short */
3810 if (res.acl_flags & NFS4_ACL_TRUNC) {
3811 /* Did the user only issue a request for the acl length? */
3812 if (buf == NULL)
3813 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003814 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003815 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003817 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3818 if (buf)
3819 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3820out_ok:
3821 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003822out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003823 for (i = 0; i < npages; i++)
3824 if (pages[i])
3825 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003826 if (res.acl_scratch)
3827 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828 return ret;
3829}
3830
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003831static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3832{
3833 struct nfs4_exception exception = { };
3834 ssize_t ret;
3835 do {
3836 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3837 if (ret >= 0)
3838 break;
3839 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3840 } while (exception.retry);
3841 return ret;
3842}
3843
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003844static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3845{
3846 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003847 int ret;
3848
3849 if (!nfs4_server_supports_acls(server))
3850 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003851 ret = nfs_revalidate_inode(server, inode);
3852 if (ret < 0)
3853 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003854 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3855 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003856 ret = nfs4_read_cached_acl(inode, buf, buflen);
3857 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003858 /* -ENOENT is returned if there is no ACL or if there is an ACL
3859 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860 return ret;
3861 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003862}
3863
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003864static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003865{
3866 struct nfs_server *server = NFS_SERVER(inode);
3867 struct page *pages[NFS4ACL_MAXPAGES];
3868 struct nfs_setaclargs arg = {
3869 .fh = NFS_FH(inode),
3870 .acl_pages = pages,
3871 .acl_len = buflen,
3872 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003873 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003874 struct rpc_message msg = {
3875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3876 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003877 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003878 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003879 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003880 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003881
3882 if (!nfs4_server_supports_acls(server))
3883 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003884 if (npages > ARRAY_SIZE(pages))
3885 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003886 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3887 if (i < 0)
3888 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003889 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003890 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003891
3892 /*
3893 * Free each page after tx, so the only ref left is
3894 * held by the network stack
3895 */
3896 for (; i > 0; i--)
3897 put_page(pages[i-1]);
3898
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003899 /*
3900 * Acl update can result in inode attribute update.
3901 * so mark the attribute cache invalid.
3902 */
3903 spin_lock(&inode->i_lock);
3904 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3905 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003906 nfs_access_zap_cache(inode);
3907 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003908 return ret;
3909}
3910
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003911static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3912{
3913 struct nfs4_exception exception = { };
3914 int err;
3915 do {
3916 err = nfs4_handle_exception(NFS_SERVER(inode),
3917 __nfs4_proc_set_acl(inode, buf, buflen),
3918 &exception);
3919 } while (exception.retry);
3920 return err;
3921}
3922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003924nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003926 struct nfs_client *clp = server->nfs_client;
3927
3928 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return 0;
3930 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003931 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003932 case -NFS4ERR_ADMIN_REVOKED:
3933 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003934 if (state == NULL)
3935 break;
3936 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003937 case -NFS4ERR_OPENMODE:
3938 if (state == NULL)
3939 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003940 nfs4_schedule_stateid_recovery(server, state);
3941 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003942 case -NFS4ERR_EXPIRED:
3943 if (state != NULL)
3944 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003946 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003947 nfs4_schedule_lease_recovery(clp);
3948 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003949#if defined(CONFIG_NFS_V4_1)
3950 case -NFS4ERR_BADSESSION:
3951 case -NFS4ERR_BADSLOT:
3952 case -NFS4ERR_BAD_HIGH_SLOT:
3953 case -NFS4ERR_DEADSESSION:
3954 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3955 case -NFS4ERR_SEQ_FALSE_RETRY:
3956 case -NFS4ERR_SEQ_MISORDERED:
3957 dprintk("%s ERROR %d, Reset session\n", __func__,
3958 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003959 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003960 task->tk_status = 0;
3961 return -EAGAIN;
3962#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003964 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003965 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003966 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3968 task->tk_status = 0;
3969 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003970 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 case -NFS4ERR_OLD_STATEID:
3972 task->tk_status = 0;
3973 return -EAGAIN;
3974 }
3975 task->tk_status = nfs4_map_errors(task->tk_status);
3976 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003977wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003978 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003979 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3980 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3981 task->tk_status = 0;
3982 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
3984
Chuck Leverf0920752012-05-21 22:45:41 -04003985static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3986 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003987{
3988 __be32 verf[2];
3989
Chuck Lever2c820d92012-05-21 22:45:33 -04003990 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3991 /* An impossible timestamp guarantees this value
3992 * will never match a generated boot time. */
3993 verf[0] = 0;
3994 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3995 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003996 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3997 verf[0] = (__be32)nn->boot_time.tv_sec;
3998 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003999 }
Chuck Levercd937102012-03-02 17:14:31 -05004000 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4001}
4002
Chuck Levere984a552012-09-14 17:24:21 -04004003static unsigned int
4004nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4005 char *buf, size_t len)
4006{
4007 unsigned int result;
4008
4009 rcu_read_lock();
4010 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4011 clp->cl_ipaddr,
4012 rpc_peeraddr2str(clp->cl_rpcclient,
4013 RPC_DISPLAY_ADDR),
4014 rpc_peeraddr2str(clp->cl_rpcclient,
4015 RPC_DISPLAY_PROTO));
4016 rcu_read_unlock();
4017 return result;
4018}
4019
4020static unsigned int
4021nfs4_init_uniform_client_string(const struct nfs_client *clp,
4022 char *buf, size_t len)
4023{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004024 char *nodename = clp->cl_rpcclient->cl_nodename;
4025
4026 if (nfs4_client_id_uniquifier[0] != '\0')
4027 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004028 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4029 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004030 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004031}
4032
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004033/**
4034 * nfs4_proc_setclientid - Negotiate client ID
4035 * @clp: state data structure
4036 * @program: RPC program for NFSv4 callback service
4037 * @port: IP port number for NFS4 callback service
4038 * @cred: RPC credential to use for this call
4039 * @res: where to place the result
4040 *
4041 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4042 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004043int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4044 unsigned short port, struct rpc_cred *cred,
4045 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046{
4047 nfs4_verifier sc_verifier;
4048 struct nfs4_setclientid setclientid = {
4049 .sc_verifier = &sc_verifier,
4050 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004051 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 };
4053 struct rpc_message msg = {
4054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4055 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004056 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004057 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004059 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Chuck Leverde734832012-07-11 16:30:50 -04004061 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004062 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004063 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4064 setclientid.sc_name_len =
4065 nfs4_init_uniform_client_string(clp,
4066 setclientid.sc_name,
4067 sizeof(setclientid.sc_name));
4068 else
4069 setclientid.sc_name_len =
4070 nfs4_init_nonuniform_client_string(clp,
4071 setclientid.sc_name,
4072 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004073 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004074 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004075 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004076 sizeof(setclientid.sc_netid),
4077 rpc_peeraddr2str(clp->cl_rpcclient,
4078 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004079 rcu_read_unlock();
4080 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004081 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 clp->cl_ipaddr, port >> 8, port & 255);
4083
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004084 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4085 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4086 setclientid.sc_name_len, setclientid.sc_name);
4087 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4088 dprintk("NFS reply setclientid: %d\n", status);
4089 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004092/**
4093 * nfs4_proc_setclientid_confirm - Confirm client ID
4094 * @clp: state data structure
4095 * @res: result of a previous SETCLIENTID
4096 * @cred: RPC credential to use for this call
4097 *
4098 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4099 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004100int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004101 struct nfs4_setclientid_res *arg,
4102 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
4104 struct nfs_fsinfo fsinfo;
4105 struct rpc_message msg = {
4106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004107 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004109 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 };
4111 unsigned long now;
4112 int status;
4113
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004114 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4115 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4116 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004118 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 if (status == 0) {
4120 spin_lock(&clp->cl_lock);
4121 clp->cl_lease_time = fsinfo.lease_time * HZ;
4122 clp->cl_last_renewal = now;
4123 spin_unlock(&clp->cl_lock);
4124 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004125 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 return status;
4127}
4128
Trond Myklebustfe650402006-01-03 09:55:18 +01004129struct nfs4_delegreturndata {
4130 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004131 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004132 struct nfs_fh fh;
4133 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004134 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004135 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004136 int rpc_status;
4137};
4138
Trond Myklebustfe650402006-01-03 09:55:18 +01004139static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4140{
4141 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004142
Trond Myklebust14516c32010-07-31 14:29:06 -04004143 if (!nfs4_sequence_done(task, &data->res.seq_res))
4144 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004145
Ricardo Labiaga79708862009-12-07 09:23:21 -05004146 switch (task->tk_status) {
4147 case -NFS4ERR_STALE_STATEID:
4148 case -NFS4ERR_EXPIRED:
4149 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004150 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004151 break;
4152 default:
4153 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4154 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004155 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004156 return;
4157 }
4158 }
4159 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004160}
4161
4162static void nfs4_delegreturn_release(void *calldata)
4163{
Trond Myklebustfe650402006-01-03 09:55:18 +01004164 kfree(calldata);
4165}
4166
Andy Adamson938e1012009-04-01 09:22:28 -04004167#if defined(CONFIG_NFS_V4_1)
4168static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4169{
4170 struct nfs4_delegreturndata *d_data;
4171
4172 d_data = (struct nfs4_delegreturndata *)data;
4173
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004174 nfs4_setup_sequence(d_data->res.server,
4175 &d_data->args.seq_args,
4176 &d_data->res.seq_res,
4177 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004178}
4179#endif /* CONFIG_NFS_V4_1 */
4180
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004181static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004182#if defined(CONFIG_NFS_V4_1)
4183 .rpc_call_prepare = nfs4_delegreturn_prepare,
4184#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004185 .rpc_call_done = nfs4_delegreturn_done,
4186 .rpc_release = nfs4_delegreturn_release,
4187};
4188
Trond Myklebuste6f81072008-01-24 18:14:34 -05004189static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004190{
4191 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004192 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004193 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004194 struct rpc_message msg = {
4195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4196 .rpc_cred = cred,
4197 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004198 struct rpc_task_setup task_setup_data = {
4199 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004200 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004201 .callback_ops = &nfs4_delegreturn_ops,
4202 .flags = RPC_TASK_ASYNC,
4203 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004204 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004205
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004206 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004207 if (data == NULL)
4208 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004209 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004210 data->args.fhandle = &data->fh;
4211 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004212 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004213 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004214 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004215 data->res.fattr = &data->fattr;
4216 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004217 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004218 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004219 data->rpc_status = 0;
4220
Trond Myklebustc970aa82007-07-14 15:39:59 -04004221 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004222 msg.rpc_argp = &data->args;
4223 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004224 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004225 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004226 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004227 if (!issync)
4228 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004229 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004230 if (status != 0)
4231 goto out;
4232 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004233 if (status == 0)
4234 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4235 else
4236 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004237out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004238 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004239 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
Trond Myklebuste6f81072008-01-24 18:14:34 -05004242int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
4244 struct nfs_server *server = NFS_SERVER(inode);
4245 struct nfs4_exception exception = { };
4246 int err;
4247 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004248 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 switch (err) {
4250 case -NFS4ERR_STALE_STATEID:
4251 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 case 0:
4253 return 0;
4254 }
4255 err = nfs4_handle_exception(server, err, &exception);
4256 } while (exception.retry);
4257 return err;
4258}
4259
4260#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4261#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4262
4263/*
4264 * sleep, with exponential backoff, and retry the LOCK operation.
4265 */
4266static unsigned long
4267nfs4_set_lock_task_retry(unsigned long timeout)
4268{
Jeff Laytond3103102011-12-01 22:44:39 +01004269 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 timeout <<= 1;
4271 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4272 return NFS4_LOCK_MAXTIMEOUT;
4273 return timeout;
4274}
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4277{
4278 struct inode *inode = state->inode;
4279 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004280 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004281 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004283 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004285 struct nfs_lockt_res res = {
4286 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 };
4288 struct rpc_message msg = {
4289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4290 .rpc_argp = &arg,
4291 .rpc_resp = &res,
4292 .rpc_cred = state->owner->so_cred,
4293 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 struct nfs4_lock_state *lsp;
4295 int status;
4296
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004297 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004298 status = nfs4_set_lock_state(state, request);
4299 if (status != 0)
4300 goto out;
4301 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004302 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004303 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004304 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004305 switch (status) {
4306 case 0:
4307 request->fl_type = F_UNLCK;
4308 break;
4309 case -NFS4ERR_DENIED:
4310 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004312 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004313out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return status;
4315}
4316
4317static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4318{
4319 struct nfs4_exception exception = { };
4320 int err;
4321
4322 do {
4323 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4324 _nfs4_proc_getlk(state, cmd, request),
4325 &exception);
4326 } while (exception.retry);
4327 return err;
4328}
4329
4330static int do_vfs_lock(struct file *file, struct file_lock *fl)
4331{
4332 int res = 0;
4333 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4334 case FL_POSIX:
4335 res = posix_lock_file_wait(file, fl);
4336 break;
4337 case FL_FLOCK:
4338 res = flock_lock_file_wait(file, fl);
4339 break;
4340 default:
4341 BUG();
4342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return res;
4344}
4345
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004346struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004347 struct nfs_locku_args arg;
4348 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004349 struct nfs4_lock_state *lsp;
4350 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004351 struct file_lock fl;
4352 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004353 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004354};
4355
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004356static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4357 struct nfs_open_context *ctx,
4358 struct nfs4_lock_state *lsp,
4359 struct nfs_seqid *seqid)
4360{
4361 struct nfs4_unlockdata *p;
4362 struct inode *inode = lsp->ls_state->inode;
4363
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004364 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004365 if (p == NULL)
4366 return NULL;
4367 p->arg.fh = NFS_FH(inode);
4368 p->arg.fl = &p->fl;
4369 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004370 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004371 p->arg.stateid = &lsp->ls_stateid;
4372 p->lsp = lsp;
4373 atomic_inc(&lsp->ls_count);
4374 /* Ensure we don't close file until we're done freeing locks! */
4375 p->ctx = get_nfs_open_context(ctx);
4376 memcpy(&p->fl, fl, sizeof(p->fl));
4377 p->server = NFS_SERVER(inode);
4378 return p;
4379}
4380
Trond Myklebust06f814a2006-01-03 09:55:07 +01004381static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004382{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004383 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004384 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004385 nfs4_put_lock_state(calldata->lsp);
4386 put_nfs_open_context(calldata->ctx);
4387 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004388}
4389
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004390static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004392 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393
Trond Myklebust14516c32010-07-31 14:29:06 -04004394 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4395 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004396 switch (task->tk_status) {
4397 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004398 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4399 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004400 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004401 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004402 case -NFS4ERR_BAD_STATEID:
4403 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004404 case -NFS4ERR_STALE_STATEID:
4405 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 break;
4407 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004408 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004409 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004410 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004411 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004412}
4413
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004414static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004415{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004416 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004418 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004419 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004420 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004421 /* Note: exit _without_ running nfs4_locku_done */
4422 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004423 return;
4424 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004425 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004426 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004427 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004428 &calldata->res.seq_res,
4429 task) != 0)
4430 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
4432
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004433static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004434 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004435 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004436 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004437};
4438
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004439static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4440 struct nfs_open_context *ctx,
4441 struct nfs4_lock_state *lsp,
4442 struct nfs_seqid *seqid)
4443{
4444 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004445 struct rpc_message msg = {
4446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4447 .rpc_cred = ctx->cred,
4448 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004449 struct rpc_task_setup task_setup_data = {
4450 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004451 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004452 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004453 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004454 .flags = RPC_TASK_ASYNC,
4455 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004456
Frank Filz137d6ac2007-07-09 15:32:29 -07004457 /* Ensure this is an unlock - when canceling a lock, the
4458 * canceled lock is passed in, and it won't be an unlock.
4459 */
4460 fl->fl_type = F_UNLCK;
4461
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004462 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4463 if (data == NULL) {
4464 nfs_free_seqid(seqid);
4465 return ERR_PTR(-ENOMEM);
4466 }
4467
Trond Myklebust9d12b212012-01-17 22:04:25 -05004468 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004469 msg.rpc_argp = &data->arg;
4470 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004471 task_setup_data.callback_data = data;
4472 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004473}
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4476{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004477 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004478 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004479 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004480 struct rpc_task *task;
4481 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004482 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Trond Myklebust9b073572006-06-29 16:38:34 -04004484 status = nfs4_set_lock_state(state, request);
4485 /* Unlock _before_ we do the RPC call */
4486 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004487 down_read(&nfsi->rwsem);
4488 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4489 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004490 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004491 }
4492 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004493 if (status != 0)
4494 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004495 /* Is this a delegated lock? */
4496 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004497 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004498 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004499 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004500 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004501 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004502 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004503 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004504 status = PTR_ERR(task);
4505 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004506 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004507 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004508 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004509out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004510 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004511 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512}
4513
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514struct nfs4_lockdata {
4515 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004516 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517 struct nfs4_lock_state *lsp;
4518 struct nfs_open_context *ctx;
4519 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004520 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004521 int rpc_status;
4522 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004523 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524};
4525
4526static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004527 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4528 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529{
4530 struct nfs4_lockdata *p;
4531 struct inode *inode = lsp->ls_state->inode;
4532 struct nfs_server *server = NFS_SERVER(inode);
4533
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004534 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535 if (p == NULL)
4536 return NULL;
4537
4538 p->arg.fh = NFS_FH(inode);
4539 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004540 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004541 if (p->arg.open_seqid == NULL)
4542 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004543 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004544 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004545 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004546 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004547 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004548 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004549 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004550 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004551 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004552 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004553 atomic_inc(&lsp->ls_count);
4554 p->ctx = get_nfs_open_context(ctx);
4555 memcpy(&p->fl, fl, sizeof(p->fl));
4556 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004557out_free_seqid:
4558 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004559out_free:
4560 kfree(p);
4561 return NULL;
4562}
4563
4564static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4565{
4566 struct nfs4_lockdata *data = calldata;
4567 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Harvey Harrison3110ff82008-05-02 13:42:44 -07004569 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004570 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4571 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004572 /* Do we need to do an open_to_lock_owner? */
4573 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004574 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004575 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004576 data->arg.open_stateid = &state->stateid;
4577 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004578 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004579 } else
4580 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004581 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004582 if (nfs4_setup_sequence(data->server,
4583 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004584 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004585 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004586 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004587 nfs_release_seqid(data->arg.open_seqid);
4588out_release_lock_seqid:
4589 nfs_release_seqid(data->arg.lock_seqid);
4590 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591}
4592
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004593static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4594{
4595 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4596 nfs4_lock_prepare(task, calldata);
4597}
4598
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004599static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4600{
4601 struct nfs4_lockdata *data = calldata;
4602
Harvey Harrison3110ff82008-05-02 13:42:44 -07004603 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004604
Trond Myklebust14516c32010-07-31 14:29:06 -04004605 if (!nfs4_sequence_done(task, &data->res.seq_res))
4606 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004607
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004608 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610 if (data->rpc_status == 0)
4611 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4612 else
4613 goto out;
4614 }
4615 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004616 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004617 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004618 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004620out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004621 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622}
4623
4624static void nfs4_lock_release(void *calldata)
4625{
4626 struct nfs4_lockdata *data = calldata;
4627
Harvey Harrison3110ff82008-05-02 13:42:44 -07004628 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004629 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004630 if (data->cancelled != 0) {
4631 struct rpc_task *task;
4632 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4633 data->arg.lock_seqid);
4634 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004635 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004636 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004637 } else
4638 nfs_free_seqid(data->arg.lock_seqid);
4639 nfs4_put_lock_state(data->lsp);
4640 put_nfs_open_context(data->ctx);
4641 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004642 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643}
4644
4645static const struct rpc_call_ops nfs4_lock_ops = {
4646 .rpc_call_prepare = nfs4_lock_prepare,
4647 .rpc_call_done = nfs4_lock_done,
4648 .rpc_release = nfs4_lock_release,
4649};
4650
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004651static const struct rpc_call_ops nfs4_recover_lock_ops = {
4652 .rpc_call_prepare = nfs4_recover_lock_prepare,
4653 .rpc_call_done = nfs4_lock_done,
4654 .rpc_release = nfs4_lock_release,
4655};
4656
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004657static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4658{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004659 switch (error) {
4660 case -NFS4ERR_ADMIN_REVOKED:
4661 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004662 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004663 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004664 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004665 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004666 break;
4667 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004668 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004669 case -NFS4ERR_EXPIRED:
4670 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004671 };
4672}
4673
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004674static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675{
4676 struct nfs4_lockdata *data;
4677 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004678 struct rpc_message msg = {
4679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4680 .rpc_cred = state->owner->so_cred,
4681 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004682 struct rpc_task_setup task_setup_data = {
4683 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004684 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004685 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004686 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004687 .flags = RPC_TASK_ASYNC,
4688 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004689 int ret;
4690
Harvey Harrison3110ff82008-05-02 13:42:44 -07004691 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004692 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004693 fl->fl_u.nfs4_fl.owner,
4694 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695 if (data == NULL)
4696 return -ENOMEM;
4697 if (IS_SETLKW(cmd))
4698 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004699 if (recovery_type > NFS_LOCK_NEW) {
4700 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004701 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004702 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4703 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004704 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004705 msg.rpc_argp = &data->arg;
4706 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004707 task_setup_data.callback_data = data;
4708 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004709 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004710 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004711 ret = nfs4_wait_for_completion_rpc_task(task);
4712 if (ret == 0) {
4713 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004714 if (ret)
4715 nfs4_handle_setlk_error(data->server, data->lsp,
4716 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717 } else
4718 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004719 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004720 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
4724static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4725{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004726 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004727 struct nfs4_exception exception = {
4728 .inode = state->inode,
4729 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004730 int err;
4731
4732 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004733 /* Cache the lock if possible... */
4734 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4735 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004736 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004737 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004738 break;
4739 nfs4_handle_exception(server, err, &exception);
4740 } while (exception.retry);
4741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743
4744static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4745{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004746 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004747 struct nfs4_exception exception = {
4748 .inode = state->inode,
4749 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004750 int err;
4751
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004752 err = nfs4_set_lock_state(state, request);
4753 if (err != 0)
4754 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004755 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004756 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4757 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004758 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004759 switch (err) {
4760 default:
4761 goto out;
4762 case -NFS4ERR_GRACE:
4763 case -NFS4ERR_DELAY:
4764 nfs4_handle_exception(server, err, &exception);
4765 err = 0;
4766 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004767 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004768out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004769 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004772#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004773/**
4774 * nfs41_check_expired_locks - possibly free a lock stateid
4775 *
4776 * @state: NFSv4 state for an inode
4777 *
4778 * Returns NFS_OK if recovery for this stateid is now finished.
4779 * Otherwise a negative NFS4ERR value is returned.
4780 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004781static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004782{
Chuck Levereb64cf92012-07-11 16:30:05 -04004783 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004784 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004785 struct nfs_server *server = NFS_SERVER(state->inode);
4786
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004787 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004788 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004789 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4790 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004791 /* Free the stateid unless the server
4792 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004793 if (status != -NFS4ERR_BAD_STATEID)
4794 nfs41_free_stateid(server,
4795 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004796 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004797 ret = status;
4798 }
4799 }
4800 };
4801
4802 return ret;
4803}
4804
4805static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4806{
4807 int status = NFS_OK;
4808
4809 if (test_bit(LK_STATE_IN_USE, &state->flags))
4810 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004811 if (status != NFS_OK)
4812 status = nfs4_lock_expired(state, request);
4813 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004814}
4815#endif
4816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4818{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004819 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004820 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004821 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004823 if ((fl_flags & FL_POSIX) &&
4824 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4825 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004826 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004827 status = nfs4_set_lock_state(state, request);
4828 if (status != 0)
4829 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004830 request->fl_flags |= FL_ACCESS;
4831 status = do_vfs_lock(request->fl_file, request);
4832 if (status < 0)
4833 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004834 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004835 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004836 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004837 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004838 request->fl_flags = fl_flags & ~FL_SLEEP;
4839 status = do_vfs_lock(request->fl_file, request);
4840 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004841 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004842 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004843 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004844 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004845 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004846 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004847 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004848 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4849 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004850out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004851 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004852out:
4853 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 return status;
4855}
4856
4857static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4858{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004859 struct nfs4_exception exception = {
4860 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004861 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004862 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 int err;
4864
4865 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004866 err = _nfs4_proc_setlk(state, cmd, request);
4867 if (err == -NFS4ERR_DENIED)
4868 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004870 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 } while (exception.retry);
4872 return err;
4873}
4874
4875static int
4876nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4877{
4878 struct nfs_open_context *ctx;
4879 struct nfs4_state *state;
4880 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4881 int status;
4882
4883 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004884 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 state = ctx->state;
4886
4887 if (request->fl_start < 0 || request->fl_end < 0)
4888 return -EINVAL;
4889
Trond Myklebustd9531262009-07-21 19:22:38 -04004890 if (IS_GETLK(cmd)) {
4891 if (state != NULL)
4892 return nfs4_proc_getlk(state, F_GETLK, request);
4893 return 0;
4894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4897 return -EINVAL;
4898
Trond Myklebustd9531262009-07-21 19:22:38 -04004899 if (request->fl_type == F_UNLCK) {
4900 if (state != NULL)
4901 return nfs4_proc_unlck(state, cmd, request);
4902 return 0;
4903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Trond Myklebustd9531262009-07-21 19:22:38 -04004905 if (state == NULL)
4906 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004907 /*
4908 * Don't rely on the VFS having checked the file open mode,
4909 * since it won't do this for flock() locks.
4910 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004911 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004912 case F_RDLCK:
4913 if (!(filp->f_mode & FMODE_READ))
4914 return -EBADF;
4915 break;
4916 case F_WRLCK:
4917 if (!(filp->f_mode & FMODE_WRITE))
4918 return -EBADF;
4919 }
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 do {
4922 status = nfs4_proc_setlk(state, cmd, request);
4923 if ((status != -EAGAIN) || IS_SETLK(cmd))
4924 break;
4925 timeout = nfs4_set_lock_task_retry(timeout);
4926 status = -ERESTARTSYS;
4927 if (signalled())
4928 break;
4929 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 return status;
4931}
4932
Trond Myklebust888e6942005-11-04 15:38:11 -05004933int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4934{
4935 struct nfs_server *server = NFS_SERVER(state->inode);
4936 struct nfs4_exception exception = { };
4937 int err;
4938
4939 err = nfs4_set_lock_state(state, fl);
4940 if (err != 0)
4941 goto out;
4942 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004943 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004944 switch (err) {
4945 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004946 printk(KERN_ERR "NFS: %s: unhandled error "
4947 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004948 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004949 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004950 goto out;
4951 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004952 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004953 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004954 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004955 nfs4_schedule_lease_recovery(server->nfs_client);
4956 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004957 case -NFS4ERR_BADSESSION:
4958 case -NFS4ERR_BADSLOT:
4959 case -NFS4ERR_BAD_HIGH_SLOT:
4960 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4961 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004962 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004963 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004964 case -ERESTARTSYS:
4965 /*
4966 * The show must go on: exit, but mark the
4967 * stateid as needing recovery.
4968 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004969 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004970 case -NFS4ERR_ADMIN_REVOKED:
4971 case -NFS4ERR_BAD_STATEID:
4972 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004973 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004974 err = 0;
4975 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004976 case -EKEYEXPIRED:
4977 /*
4978 * User RPCSEC_GSS context has expired.
4979 * We cannot recover this stateid now, so
4980 * skip it and allow recovery thread to
4981 * proceed.
4982 */
4983 err = 0;
4984 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004985 case -ENOMEM:
4986 case -NFS4ERR_DENIED:
4987 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4988 err = 0;
4989 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004990 case -NFS4ERR_DELAY:
4991 break;
4992 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004993 err = nfs4_handle_exception(server, err, &exception);
4994 } while (exception.retry);
4995out:
4996 return err;
4997}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004998
Trond Myklebustcf470c32012-03-07 13:49:12 -05004999struct nfs_release_lockowner_data {
5000 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005001 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005002 struct nfs_release_lockowner_args args;
5003};
5004
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005005static void nfs4_release_lockowner_release(void *calldata)
5006{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005007 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005008 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005009 kfree(calldata);
5010}
5011
Trond Myklebust17280172012-03-11 13:11:00 -04005012static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005013 .rpc_release = nfs4_release_lockowner_release,
5014};
5015
Trond Myklebustcf470c32012-03-07 13:49:12 -05005016int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005017{
5018 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005019 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005020 struct rpc_message msg = {
5021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5022 };
5023
5024 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005025 return -EINVAL;
5026 data = kmalloc(sizeof(*data), GFP_NOFS);
5027 if (!data)
5028 return -ENOMEM;
5029 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005030 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005031 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5032 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5033 data->args.lock_owner.s_dev = server->s_dev;
5034 msg.rpc_argp = &data->args;
5035 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5036 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005037}
5038
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005039#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5040
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005041static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5042 const void *buf, size_t buflen,
5043 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005044{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005045 if (strcmp(key, "") != 0)
5046 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005047
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005048 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005049}
5050
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005051static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5052 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005053{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005054 if (strcmp(key, "") != 0)
5055 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005056
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005057 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005058}
5059
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005060static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5061 size_t list_len, const char *name,
5062 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005063{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005064 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005065
J. Bruce Fields096455a2006-03-20 23:23:42 -05005066 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5067 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005068
5069 if (list && len <= list_len)
5070 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005071 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005072}
5073
Andy Adamson533eb462011-06-13 18:25:56 -04005074/*
5075 * nfs_fhget will use either the mounted_on_fileid or the fileid
5076 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005077static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5078{
Andy Adamson533eb462011-06-13 18:25:56 -04005079 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5080 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5081 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005082 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005083 return;
5084
5085 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005086 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005087 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5088 fattr->nlink = 2;
5089}
5090
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005091static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5092 const struct qstr *name,
5093 struct nfs4_fs_locations *fs_locations,
5094 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005095{
5096 struct nfs_server *server = NFS_SERVER(dir);
5097 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005098 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005099 };
5100 struct nfs4_fs_locations_arg args = {
5101 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005102 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005103 .page = page,
5104 .bitmask = bitmask,
5105 };
Benny Halevy22958462009-04-01 09:22:02 -04005106 struct nfs4_fs_locations_res res = {
5107 .fs_locations = fs_locations,
5108 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005109 struct rpc_message msg = {
5110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5111 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005112 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005113 };
5114 int status;
5115
Harvey Harrison3110ff82008-05-02 13:42:44 -07005116 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005117
5118 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5119 * is not supported */
5120 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5121 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5122 else
5123 bitmask[0] |= FATTR4_WORD0_FILEID;
5124
Trond Myklebustc228fd32007-01-13 02:28:11 -05005125 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005126 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005127 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005128 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005129 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005130 return status;
5131}
5132
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005133int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5134 const struct qstr *name,
5135 struct nfs4_fs_locations *fs_locations,
5136 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005137{
5138 struct nfs4_exception exception = { };
5139 int err;
5140 do {
5141 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005142 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005143 &exception);
5144 } while (exception.retry);
5145 return err;
5146}
5147
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005148static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5149{
5150 int status;
5151 struct nfs4_secinfo_arg args = {
5152 .dir_fh = NFS_FH(dir),
5153 .name = name,
5154 };
5155 struct nfs4_secinfo_res res = {
5156 .flavors = flavors,
5157 };
5158 struct rpc_message msg = {
5159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5160 .rpc_argp = &args,
5161 .rpc_resp = &res,
5162 };
5163
5164 dprintk("NFS call secinfo %s\n", name->name);
5165 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5166 dprintk("NFS reply secinfo: %d\n", status);
5167 return status;
5168}
5169
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005170int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5171 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005172{
5173 struct nfs4_exception exception = { };
5174 int err;
5175 do {
5176 err = nfs4_handle_exception(NFS_SERVER(dir),
5177 _nfs4_proc_secinfo(dir, name, flavors),
5178 &exception);
5179 } while (exception.retry);
5180 return err;
5181}
5182
Andy Adamson557134a2009-04-01 09:21:53 -04005183#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005184/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005185 * Check the exchange flags returned by the server for invalid flags, having
5186 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5187 * DS flags set.
5188 */
5189static int nfs4_check_cl_exchange_flags(u32 flags)
5190{
5191 if (flags & ~EXCHGID4_FLAG_MASK_R)
5192 goto out_inval;
5193 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5194 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5195 goto out_inval;
5196 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5197 goto out_inval;
5198 return NFS_OK;
5199out_inval:
5200 return -NFS4ERR_INVAL;
5201}
5202
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005203static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005204nfs41_same_server_scope(struct nfs41_server_scope *a,
5205 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005206{
5207 if (a->server_scope_sz == b->server_scope_sz &&
5208 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5209 return true;
5210
5211 return false;
5212}
5213
Andy Adamson357f54d2010-12-14 10:11:57 -05005214/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005215 * nfs4_proc_bind_conn_to_session()
5216 *
5217 * The 4.1 client currently uses the same TCP connection for the
5218 * fore and backchannel.
5219 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005220int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005221{
5222 int status;
5223 struct nfs41_bind_conn_to_session_res res;
5224 struct rpc_message msg = {
5225 .rpc_proc =
5226 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5227 .rpc_argp = clp,
5228 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005229 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005230 };
5231
5232 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005233
5234 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5235 if (unlikely(res.session == NULL)) {
5236 status = -ENOMEM;
5237 goto out;
5238 }
5239
5240 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5241 if (status == 0) {
5242 if (memcmp(res.session->sess_id.data,
5243 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5244 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5245 status = -EIO;
5246 goto out_session;
5247 }
5248 if (res.dir != NFS4_CDFS4_BOTH) {
5249 dprintk("NFS: %s: Unexpected direction from server\n",
5250 __func__);
5251 status = -EIO;
5252 goto out_session;
5253 }
5254 if (res.use_conn_in_rdma_mode) {
5255 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5256 __func__);
5257 status = -EIO;
5258 goto out_session;
5259 }
5260 }
5261out_session:
5262 kfree(res.session);
5263out:
5264 dprintk("<-- %s status= %d\n", __func__, status);
5265 return status;
5266}
5267
5268/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005269 * nfs4_proc_exchange_id()
5270 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005271 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5272 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005273 * Since the clientid has expired, all compounds using sessions
5274 * associated with the stale clientid will be returning
5275 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5276 * be in some phase of session reset.
5277 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005278int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005279{
5280 nfs4_verifier verifier;
5281 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005282 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005283 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005284 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005285 };
5286 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005287 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005288 };
5289 int status;
5290 struct rpc_message msg = {
5291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5292 .rpc_argp = &args,
5293 .rpc_resp = &res,
5294 .rpc_cred = cred,
5295 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005296
Chuck Leverf0920752012-05-21 22:45:41 -04005297 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005298 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5299 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005300 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5301 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5302 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005303
Chuck Leveracdeb692012-05-21 22:46:16 -04005304 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005305 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005306 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005307 status = -ENOMEM;
5308 goto out;
5309 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005310
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005311 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005312 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005313 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005314 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005315 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005316 }
5317
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005318 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005319 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005320 status = -ENOMEM;
5321 goto out_server_scope;
5322 }
5323
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005324 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005325 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005326 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005327
Chuck Lever177313f2012-05-21 22:44:58 -04005328 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005329 clp->cl_clientid = res.clientid;
5330 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5331 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5332 clp->cl_seqid = res.seqid;
5333
Chuck Leveracdeb692012-05-21 22:46:16 -04005334 kfree(clp->cl_serverowner);
5335 clp->cl_serverowner = res.server_owner;
5336 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005337
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005338 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005339 kfree(clp->cl_implid);
5340 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005341
Chuck Lever177313f2012-05-21 22:44:58 -04005342 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005343 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005344 res.server_scope)) {
5345 dprintk("%s: server_scope mismatch detected\n",
5346 __func__);
5347 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005348 kfree(clp->cl_serverscope);
5349 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005350 }
5351
Chuck Lever177313f2012-05-21 22:44:58 -04005352 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005353 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005354 goto out;
5355 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005356 } else
5357 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005358
Chuck Leveracdeb692012-05-21 22:46:16 -04005359out_server_owner:
5360 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005361out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005362 kfree(res.server_scope);
5363out:
Chuck Lever177313f2012-05-21 22:44:58 -04005364 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005365 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005366 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005367 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005368 clp->cl_implid->date.seconds,
5369 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005370 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005371 return status;
5372}
5373
Trond Myklebust66245532012-05-25 17:18:09 -04005374static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5375 struct rpc_cred *cred)
5376{
5377 struct rpc_message msg = {
5378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5379 .rpc_argp = clp,
5380 .rpc_cred = cred,
5381 };
5382 int status;
5383
5384 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5385 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005386 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005387 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5388 return status;
5389}
5390
5391static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5392 struct rpc_cred *cred)
5393{
5394 unsigned int loop;
5395 int ret;
5396
5397 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5398 ret = _nfs4_proc_destroy_clientid(clp, cred);
5399 switch (ret) {
5400 case -NFS4ERR_DELAY:
5401 case -NFS4ERR_CLIENTID_BUSY:
5402 ssleep(1);
5403 break;
5404 default:
5405 return ret;
5406 }
5407 }
5408 return 0;
5409}
5410
5411int nfs4_destroy_clientid(struct nfs_client *clp)
5412{
5413 struct rpc_cred *cred;
5414 int ret = 0;
5415
5416 if (clp->cl_mvops->minor_version < 1)
5417 goto out;
5418 if (clp->cl_exchange_flags == 0)
5419 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005420 if (clp->cl_preserve_clid)
5421 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005422 cred = nfs4_get_exchange_id_cred(clp);
5423 ret = nfs4_proc_destroy_clientid(clp, cred);
5424 if (cred)
5425 put_rpccred(cred);
5426 switch (ret) {
5427 case 0:
5428 case -NFS4ERR_STALE_CLIENTID:
5429 clp->cl_exchange_flags = 0;
5430 }
5431out:
5432 return ret;
5433}
5434
Andy Adamson2050f0c2009-04-01 09:22:30 -04005435struct nfs4_get_lease_time_data {
5436 struct nfs4_get_lease_time_args *args;
5437 struct nfs4_get_lease_time_res *res;
5438 struct nfs_client *clp;
5439};
5440
5441static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5442 void *calldata)
5443{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005444 struct nfs4_get_lease_time_data *data =
5445 (struct nfs4_get_lease_time_data *)calldata;
5446
5447 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005448 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005449 /* just setup sequence, do not trigger session recovery
5450 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005451 nfs41_setup_sequence(data->clp->cl_session,
5452 &data->args->la_seq_args,
5453 &data->res->lr_seq_res,
5454 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005455 dprintk("<-- %s\n", __func__);
5456}
5457
5458/*
5459 * Called from nfs4_state_manager thread for session setup, so don't recover
5460 * from sequence operation or clientid errors.
5461 */
5462static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5463{
5464 struct nfs4_get_lease_time_data *data =
5465 (struct nfs4_get_lease_time_data *)calldata;
5466
5467 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005468 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5469 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005470 switch (task->tk_status) {
5471 case -NFS4ERR_DELAY:
5472 case -NFS4ERR_GRACE:
5473 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5474 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5475 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005476 /* fall through */
5477 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005478 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005479 return;
5480 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005481 dprintk("<-- %s\n", __func__);
5482}
5483
Trond Myklebust17280172012-03-11 13:11:00 -04005484static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005485 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5486 .rpc_call_done = nfs4_get_lease_time_done,
5487};
5488
5489int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5490{
5491 struct rpc_task *task;
5492 struct nfs4_get_lease_time_args args;
5493 struct nfs4_get_lease_time_res res = {
5494 .lr_fsinfo = fsinfo,
5495 };
5496 struct nfs4_get_lease_time_data data = {
5497 .args = &args,
5498 .res = &res,
5499 .clp = clp,
5500 };
5501 struct rpc_message msg = {
5502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5503 .rpc_argp = &args,
5504 .rpc_resp = &res,
5505 };
5506 struct rpc_task_setup task_setup = {
5507 .rpc_client = clp->cl_rpcclient,
5508 .rpc_message = &msg,
5509 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005510 .callback_data = &data,
5511 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005512 };
5513 int status;
5514
Trond Myklebust9d12b212012-01-17 22:04:25 -05005515 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005516 dprintk("--> %s\n", __func__);
5517 task = rpc_run_task(&task_setup);
5518
5519 if (IS_ERR(task))
5520 status = PTR_ERR(task);
5521 else {
5522 status = task->tk_status;
5523 rpc_put_task(task);
5524 }
5525 dprintk("<-- %s return %d\n", __func__, status);
5526
5527 return status;
5528}
5529
Andy Adamsonfc931582009-04-01 09:22:31 -04005530/*
5531 * Initialize the values to be used by the client in CREATE_SESSION
5532 * If nfs4_init_session set the fore channel request and response sizes,
5533 * use them.
5534 *
5535 * Set the back channel max_resp_sz_cached to zero to force the client to
5536 * always set csa_cachethis to FALSE because the current implementation
5537 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5538 */
5539static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5540{
5541 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005542 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5543 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005544
5545 if (mxrqst_sz == 0)
5546 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5547 if (mxresp_sz == 0)
5548 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5549 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005550 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5551 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005552 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005553 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005554
5555 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005556 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005557 __func__,
5558 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005559 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005560
5561 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005562 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5563 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5564 args->bc_attrs.max_resp_sz_cached = 0;
5565 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5566 args->bc_attrs.max_reqs = 1;
5567
5568 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5569 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5570 __func__,
5571 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5572 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5573 args->bc_attrs.max_reqs);
5574}
5575
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005576static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005577{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005578 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5579 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5580
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005581 if (rcvd->max_resp_sz > sent->max_resp_sz)
5582 return -EINVAL;
5583 /*
5584 * Our requested max_ops is the minimum we need; we're not
5585 * prepared to break up compounds into smaller pieces than that.
5586 * So, no point even trying to continue if the server won't
5587 * cooperate:
5588 */
5589 if (rcvd->max_ops < sent->max_ops)
5590 return -EINVAL;
5591 if (rcvd->max_reqs == 0)
5592 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005593 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5594 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005595 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005596}
5597
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005598static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5599{
5600 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5601 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005602
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005603 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5604 return -EINVAL;
5605 if (rcvd->max_resp_sz < sent->max_resp_sz)
5606 return -EINVAL;
5607 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5608 return -EINVAL;
5609 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005610 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005611 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005612 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005613 return -EINVAL;
5614 return 0;
5615}
Andy Adamson8d353012009-04-01 09:22:32 -04005616
Andy Adamson8d353012009-04-01 09:22:32 -04005617static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5618 struct nfs4_session *session)
5619{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005620 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005621
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005622 ret = nfs4_verify_fore_channel_attrs(args, session);
5623 if (ret)
5624 return ret;
5625 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005626}
5627
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005628static int _nfs4_proc_create_session(struct nfs_client *clp,
5629 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005630{
5631 struct nfs4_session *session = clp->cl_session;
5632 struct nfs41_create_session_args args = {
5633 .client = clp,
5634 .cb_program = NFS4_CALLBACK,
5635 };
5636 struct nfs41_create_session_res res = {
5637 .client = clp,
5638 };
5639 struct rpc_message msg = {
5640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5641 .rpc_argp = &args,
5642 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005643 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005644 };
5645 int status;
5646
5647 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005648 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005649
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005650 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005651
Trond Myklebust43095d32012-11-20 11:13:12 -05005652 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005653 /* Verify the session's negotiated channel_attrs values */
5654 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005655 /* Increment the clientid slot sequence id */
5656 clp->cl_seqid++;
5657 }
5658
5659 return status;
5660}
5661
5662/*
5663 * Issues a CREATE_SESSION operation to the server.
5664 * It is the responsibility of the caller to verify the session is
5665 * expired before calling this routine.
5666 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005667int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005668{
5669 int status;
5670 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005671 struct nfs4_session *session = clp->cl_session;
5672
5673 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5674
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005675 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005676 if (status)
5677 goto out;
5678
Andy Adamsonaacd5532011-11-09 13:58:21 -05005679 /* Init or reset the session slot tables */
5680 status = nfs4_setup_session_slot_tables(session);
5681 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005682 if (status)
5683 goto out;
5684
5685 ptr = (unsigned *)&session->sess_id.data[0];
5686 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5687 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005688out:
5689 dprintk("<-- %s\n", __func__);
5690 return status;
5691}
5692
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005693/*
5694 * Issue the over-the-wire RPC DESTROY_SESSION.
5695 * The caller must serialize access to this routine.
5696 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005697int nfs4_proc_destroy_session(struct nfs4_session *session,
5698 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005699{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005700 struct rpc_message msg = {
5701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5702 .rpc_argp = session,
5703 .rpc_cred = cred,
5704 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005705 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005706
5707 dprintk("--> nfs4_proc_destroy_session\n");
5708
5709 /* session is still being setup */
5710 if (session->clp->cl_cons_state != NFS_CS_READY)
5711 return status;
5712
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005713 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005714
5715 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005716 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005717 "Session has been destroyed regardless...\n", status);
5718
5719 dprintk("<-- nfs4_proc_destroy_session\n");
5720 return status;
5721}
5722
Trond Myklebust7b38c362012-05-23 13:23:31 -04005723/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005724 * Renew the cl_session lease.
5725 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005726struct nfs4_sequence_data {
5727 struct nfs_client *clp;
5728 struct nfs4_sequence_args args;
5729 struct nfs4_sequence_res res;
5730};
5731
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005732static void nfs41_sequence_release(void *data)
5733{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005734 struct nfs4_sequence_data *calldata = data;
5735 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005736
Alexandros Batsakis71358402010-02-05 03:45:05 -08005737 if (atomic_read(&clp->cl_count) > 1)
5738 nfs4_schedule_state_renewal(clp);
5739 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005740 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005741}
5742
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005743static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5744{
5745 switch(task->tk_status) {
5746 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005747 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5748 return -EAGAIN;
5749 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005750 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005751 }
5752 return 0;
5753}
5754
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005755static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005756{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005757 struct nfs4_sequence_data *calldata = data;
5758 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005759
Trond Myklebust14516c32010-07-31 14:29:06 -04005760 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5761 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762
5763 if (task->tk_status < 0) {
5764 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005765 if (atomic_read(&clp->cl_count) == 1)
5766 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005767
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005768 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5769 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005770 return;
5771 }
5772 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005773 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005774out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005775 dprintk("<-- %s\n", __func__);
5776}
5777
5778static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5779{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005780 struct nfs4_sequence_data *calldata = data;
5781 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005782 struct nfs4_sequence_args *args;
5783 struct nfs4_sequence_res *res;
5784
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005785 args = task->tk_msg.rpc_argp;
5786 res = task->tk_msg.rpc_resp;
5787
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005788 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005789}
5790
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005791static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5792{
5793 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5794 nfs41_sequence_prepare(task, data);
5795}
5796
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005797static const struct rpc_call_ops nfs41_sequence_ops = {
5798 .rpc_call_done = nfs41_sequence_call_done,
5799 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005800 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005801};
5802
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005803static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5804 .rpc_call_done = nfs41_sequence_call_done,
5805 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5806 .rpc_release = nfs41_sequence_release,
5807};
5808
5809static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5810 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005811{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005812 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005813 struct rpc_message msg = {
5814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5815 .rpc_cred = cred,
5816 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005817 struct rpc_task_setup task_setup_data = {
5818 .rpc_client = clp->cl_rpcclient,
5819 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005820 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005821 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5822 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005823
Alexandros Batsakis71358402010-02-05 03:45:05 -08005824 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005825 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005826 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005827 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005828 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005829 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005830 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005831 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005832 msg.rpc_argp = &calldata->args;
5833 msg.rpc_resp = &calldata->res;
5834 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005835 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005836
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005837 return rpc_run_task(&task_setup_data);
5838}
5839
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005840static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005841{
5842 struct rpc_task *task;
5843 int ret = 0;
5844
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005845 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5846 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005847 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005848 if (IS_ERR(task))
5849 ret = PTR_ERR(task);
5850 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005851 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005852 dprintk("<-- %s status=%d\n", __func__, ret);
5853 return ret;
5854}
5855
5856static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5857{
5858 struct rpc_task *task;
5859 int ret;
5860
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005861 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005862 if (IS_ERR(task)) {
5863 ret = PTR_ERR(task);
5864 goto out;
5865 }
5866 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005867 if (!ret) {
5868 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5869
5870 if (task->tk_status == 0)
5871 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005872 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005873 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005874 rpc_put_task(task);
5875out:
5876 dprintk("<-- %s status=%d\n", __func__, ret);
5877 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005878}
5879
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005880struct nfs4_reclaim_complete_data {
5881 struct nfs_client *clp;
5882 struct nfs41_reclaim_complete_args arg;
5883 struct nfs41_reclaim_complete_res res;
5884};
5885
5886static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5887{
5888 struct nfs4_reclaim_complete_data *calldata = data;
5889
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005890 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005891 nfs41_setup_sequence(calldata->clp->cl_session,
5892 &calldata->arg.seq_args,
5893 &calldata->res.seq_res,
5894 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005895}
5896
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005897static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5898{
5899 switch(task->tk_status) {
5900 case 0:
5901 case -NFS4ERR_COMPLETE_ALREADY:
5902 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5903 break;
5904 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005905 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005906 /* fall through */
5907 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005908 return -EAGAIN;
5909 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005910 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005911 }
5912 return 0;
5913}
5914
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005915static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5916{
5917 struct nfs4_reclaim_complete_data *calldata = data;
5918 struct nfs_client *clp = calldata->clp;
5919 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5920
5921 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005922 if (!nfs41_sequence_done(task, res))
5923 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005925 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5926 rpc_restart_call_prepare(task);
5927 return;
5928 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005929 dprintk("<-- %s\n", __func__);
5930}
5931
5932static void nfs4_free_reclaim_complete_data(void *data)
5933{
5934 struct nfs4_reclaim_complete_data *calldata = data;
5935
5936 kfree(calldata);
5937}
5938
5939static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5940 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5941 .rpc_call_done = nfs4_reclaim_complete_done,
5942 .rpc_release = nfs4_free_reclaim_complete_data,
5943};
5944
5945/*
5946 * Issue a global reclaim complete.
5947 */
5948static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5949{
5950 struct nfs4_reclaim_complete_data *calldata;
5951 struct rpc_task *task;
5952 struct rpc_message msg = {
5953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5954 };
5955 struct rpc_task_setup task_setup_data = {
5956 .rpc_client = clp->cl_rpcclient,
5957 .rpc_message = &msg,
5958 .callback_ops = &nfs4_reclaim_complete_call_ops,
5959 .flags = RPC_TASK_ASYNC,
5960 };
5961 int status = -ENOMEM;
5962
5963 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005964 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005965 if (calldata == NULL)
5966 goto out;
5967 calldata->clp = clp;
5968 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005969
Trond Myklebust9d12b212012-01-17 22:04:25 -05005970 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005971 msg.rpc_argp = &calldata->arg;
5972 msg.rpc_resp = &calldata->res;
5973 task_setup_data.callback_data = calldata;
5974 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005975 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005976 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005977 goto out;
5978 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005979 status = nfs4_wait_for_completion_rpc_task(task);
5980 if (status == 0)
5981 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005982 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005983 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005984out:
5985 dprintk("<-- %s status=%d\n", __func__, status);
5986 return status;
5987}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005988
5989static void
5990nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5991{
5992 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005993 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005994 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005995
5996 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005997 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5998 * right now covering the LAYOUTGET we are about to send.
5999 * However, that is not so catastrophic, and there seems
6000 * to be no way to prevent it completely.
6001 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006002 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006003 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006004 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006005 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6006 NFS_I(lgp->args.inode)->layout,
6007 lgp->args.ctx->state)) {
6008 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006009 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006010}
6011
6012static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6013{
6014 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006015 struct inode *inode = lgp->args.inode;
6016 struct nfs_server *server = NFS_SERVER(inode);
6017 struct pnfs_layout_hdr *lo;
6018 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006019
6020 dprintk("--> %s\n", __func__);
6021
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006022 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006023 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006024
6025 switch (task->tk_status) {
6026 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006027 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006028 case -NFS4ERR_LAYOUTTRYLATER:
6029 case -NFS4ERR_RECALLCONFLICT:
6030 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006031 break;
6032 case -NFS4ERR_EXPIRED:
6033 case -NFS4ERR_BAD_STATEID:
6034 spin_lock(&inode->i_lock);
6035 lo = NFS_I(inode)->layout;
6036 if (!lo || list_empty(&lo->plh_segs)) {
6037 spin_unlock(&inode->i_lock);
6038 /* If the open stateid was bad, then recover it. */
6039 state = lgp->args.ctx->state;
6040 } else {
6041 LIST_HEAD(head);
6042
6043 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6044 spin_unlock(&inode->i_lock);
6045 /* Mark the bad layout state as invalid, then
6046 * retry using the open stateid. */
6047 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006048 }
6049 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006050 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6051 rpc_restart_call_prepare(task);
6052out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006053 dprintk("<-- %s\n", __func__);
6054}
6055
Idan Kedar85541162012-08-02 11:47:10 +03006056static size_t max_response_pages(struct nfs_server *server)
6057{
6058 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6059 return nfs_page_array_len(0, max_resp_sz);
6060}
6061
6062static void nfs4_free_pages(struct page **pages, size_t size)
6063{
6064 int i;
6065
6066 if (!pages)
6067 return;
6068
6069 for (i = 0; i < size; i++) {
6070 if (!pages[i])
6071 break;
6072 __free_page(pages[i]);
6073 }
6074 kfree(pages);
6075}
6076
6077static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6078{
6079 struct page **pages;
6080 int i;
6081
6082 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6083 if (!pages) {
6084 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6085 return NULL;
6086 }
6087
6088 for (i = 0; i < size; i++) {
6089 pages[i] = alloc_page(gfp_flags);
6090 if (!pages[i]) {
6091 dprintk("%s: failed to allocate page\n", __func__);
6092 nfs4_free_pages(pages, size);
6093 return NULL;
6094 }
6095 }
6096
6097 return pages;
6098}
6099
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006100static void nfs4_layoutget_release(void *calldata)
6101{
6102 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006103 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6104 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006105
6106 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006107 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006108 put_nfs_open_context(lgp->args.ctx);
6109 kfree(calldata);
6110 dprintk("<-- %s\n", __func__);
6111}
6112
6113static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6114 .rpc_call_prepare = nfs4_layoutget_prepare,
6115 .rpc_call_done = nfs4_layoutget_done,
6116 .rpc_release = nfs4_layoutget_release,
6117};
6118
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006119struct pnfs_layout_segment *
6120nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006121{
6122 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006123 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006124 struct rpc_task *task;
6125 struct rpc_message msg = {
6126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6127 .rpc_argp = &lgp->args,
6128 .rpc_resp = &lgp->res,
6129 };
6130 struct rpc_task_setup task_setup_data = {
6131 .rpc_client = server->client,
6132 .rpc_message = &msg,
6133 .callback_ops = &nfs4_layoutget_call_ops,
6134 .callback_data = lgp,
6135 .flags = RPC_TASK_ASYNC,
6136 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006137 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006138 int status = 0;
6139
6140 dprintk("--> %s\n", __func__);
6141
Idan Kedar85541162012-08-02 11:47:10 +03006142 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6143 if (!lgp->args.layout.pages) {
6144 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006145 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006146 }
6147 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6148
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006149 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006150 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006151 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006152 task = rpc_run_task(&task_setup_data);
6153 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006154 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006155 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006156 if (status == 0)
6157 status = task->tk_status;
6158 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006159 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006160 rpc_put_task(task);
6161 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006162 if (status)
6163 return ERR_PTR(status);
6164 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006165}
6166
Benny Halevycbe82602011-05-22 19:52:37 +03006167static void
6168nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6169{
6170 struct nfs4_layoutreturn *lrp = calldata;
6171
6172 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006173 nfs41_setup_sequence(lrp->clp->cl_session,
6174 &lrp->args.seq_args,
6175 &lrp->res.seq_res,
6176 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006177}
6178
6179static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6180{
6181 struct nfs4_layoutreturn *lrp = calldata;
6182 struct nfs_server *server;
6183
6184 dprintk("--> %s\n", __func__);
6185
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006186 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006187 return;
6188
6189 server = NFS_SERVER(lrp->args.inode);
6190 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006191 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006192 return;
6193 }
Benny Halevycbe82602011-05-22 19:52:37 +03006194 dprintk("<-- %s\n", __func__);
6195}
6196
6197static void nfs4_layoutreturn_release(void *calldata)
6198{
6199 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006200 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006201
6202 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006203 spin_lock(&lo->plh_inode->i_lock);
6204 if (lrp->res.lrs_present)
6205 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6206 lo->plh_block_lgets--;
6207 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006208 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006209 kfree(calldata);
6210 dprintk("<-- %s\n", __func__);
6211}
6212
6213static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6214 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6215 .rpc_call_done = nfs4_layoutreturn_done,
6216 .rpc_release = nfs4_layoutreturn_release,
6217};
6218
6219int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6220{
6221 struct rpc_task *task;
6222 struct rpc_message msg = {
6223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6224 .rpc_argp = &lrp->args,
6225 .rpc_resp = &lrp->res,
6226 };
6227 struct rpc_task_setup task_setup_data = {
6228 .rpc_client = lrp->clp->cl_rpcclient,
6229 .rpc_message = &msg,
6230 .callback_ops = &nfs4_layoutreturn_call_ops,
6231 .callback_data = lrp,
6232 };
6233 int status;
6234
6235 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006236 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006237 task = rpc_run_task(&task_setup_data);
6238 if (IS_ERR(task))
6239 return PTR_ERR(task);
6240 status = task->tk_status;
6241 dprintk("<-- %s status=%d\n", __func__, status);
6242 rpc_put_task(task);
6243 return status;
6244}
6245
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006246/*
6247 * Retrieve the list of Data Server devices from the MDS.
6248 */
6249static int _nfs4_getdevicelist(struct nfs_server *server,
6250 const struct nfs_fh *fh,
6251 struct pnfs_devicelist *devlist)
6252{
6253 struct nfs4_getdevicelist_args args = {
6254 .fh = fh,
6255 .layoutclass = server->pnfs_curr_ld->id,
6256 };
6257 struct nfs4_getdevicelist_res res = {
6258 .devlist = devlist,
6259 };
6260 struct rpc_message msg = {
6261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6262 .rpc_argp = &args,
6263 .rpc_resp = &res,
6264 };
6265 int status;
6266
6267 dprintk("--> %s\n", __func__);
6268 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6269 &res.seq_res, 0);
6270 dprintk("<-- %s status=%d\n", __func__, status);
6271 return status;
6272}
6273
6274int nfs4_proc_getdevicelist(struct nfs_server *server,
6275 const struct nfs_fh *fh,
6276 struct pnfs_devicelist *devlist)
6277{
6278 struct nfs4_exception exception = { };
6279 int err;
6280
6281 do {
6282 err = nfs4_handle_exception(server,
6283 _nfs4_getdevicelist(server, fh, devlist),
6284 &exception);
6285 } while (exception.retry);
6286
6287 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6288 err, devlist->num_devs);
6289
6290 return err;
6291}
6292EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6293
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006294static int
6295_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6296{
6297 struct nfs4_getdeviceinfo_args args = {
6298 .pdev = pdev,
6299 };
6300 struct nfs4_getdeviceinfo_res res = {
6301 .pdev = pdev,
6302 };
6303 struct rpc_message msg = {
6304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6305 .rpc_argp = &args,
6306 .rpc_resp = &res,
6307 };
6308 int status;
6309
6310 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006311 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006312 dprintk("<-- %s status=%d\n", __func__, status);
6313
6314 return status;
6315}
6316
6317int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6318{
6319 struct nfs4_exception exception = { };
6320 int err;
6321
6322 do {
6323 err = nfs4_handle_exception(server,
6324 _nfs4_proc_getdeviceinfo(server, pdev),
6325 &exception);
6326 } while (exception.retry);
6327 return err;
6328}
6329EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6330
Andy Adamson863a3c62011-03-23 13:27:54 +00006331static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6332{
6333 struct nfs4_layoutcommit_data *data = calldata;
6334 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006335 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006336
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006337 nfs41_setup_sequence(session,
6338 &data->args.seq_args,
6339 &data->res.seq_res,
6340 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006341}
6342
6343static void
6344nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6345{
6346 struct nfs4_layoutcommit_data *data = calldata;
6347 struct nfs_server *server = NFS_SERVER(data->args.inode);
6348
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006349 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006350 return;
6351
6352 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006353 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6354 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6355 case -NFS4ERR_BADLAYOUT: /* no layout */
6356 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006357 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006358 break;
6359 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006360 nfs_post_op_update_inode_force_wcc(data->args.inode,
6361 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006362 break;
6363 default:
6364 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6365 rpc_restart_call_prepare(task);
6366 return;
6367 }
6368 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006369}
6370
6371static void nfs4_layoutcommit_release(void *calldata)
6372{
6373 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006374 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006375 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006376
Andy Adamsondb29c082011-07-30 20:52:38 -04006377 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006378 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006379 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6380 list_del_init(&lseg->pls_lc_list);
6381 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6382 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006383 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006384 }
Peng Tao92407e72011-10-23 20:21:17 -07006385
6386 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6387 smp_mb__after_clear_bit();
6388 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6389
Andy Adamson863a3c62011-03-23 13:27:54 +00006390 put_rpccred(data->cred);
6391 kfree(data);
6392}
6393
6394static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6395 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6396 .rpc_call_done = nfs4_layoutcommit_done,
6397 .rpc_release = nfs4_layoutcommit_release,
6398};
6399
6400int
Andy Adamsonef311532011-03-12 02:58:10 -05006401nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006402{
6403 struct rpc_message msg = {
6404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6405 .rpc_argp = &data->args,
6406 .rpc_resp = &data->res,
6407 .rpc_cred = data->cred,
6408 };
6409 struct rpc_task_setup task_setup_data = {
6410 .task = &data->task,
6411 .rpc_client = NFS_CLIENT(data->args.inode),
6412 .rpc_message = &msg,
6413 .callback_ops = &nfs4_layoutcommit_ops,
6414 .callback_data = data,
6415 .flags = RPC_TASK_ASYNC,
6416 };
6417 struct rpc_task *task;
6418 int status = 0;
6419
6420 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6421 "lbw: %llu inode %lu\n",
6422 data->task.tk_pid, sync,
6423 data->args.lastbytewritten,
6424 data->args.inode->i_ino);
6425
Trond Myklebust9d12b212012-01-17 22:04:25 -05006426 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006427 task = rpc_run_task(&task_setup_data);
6428 if (IS_ERR(task))
6429 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006430 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006431 goto out;
6432 status = nfs4_wait_for_completion_rpc_task(task);
6433 if (status != 0)
6434 goto out;
6435 status = task->tk_status;
6436out:
6437 dprintk("%s: status %d\n", __func__, status);
6438 rpc_put_task(task);
6439 return status;
6440}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006441
6442static int
6443_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6444 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6445{
6446 struct nfs41_secinfo_no_name_args args = {
6447 .style = SECINFO_STYLE_CURRENT_FH,
6448 };
6449 struct nfs4_secinfo_res res = {
6450 .flavors = flavors,
6451 };
6452 struct rpc_message msg = {
6453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6454 .rpc_argp = &args,
6455 .rpc_resp = &res,
6456 };
6457 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6458}
6459
6460static int
6461nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6462 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6463{
6464 struct nfs4_exception exception = { };
6465 int err;
6466 do {
6467 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6468 switch (err) {
6469 case 0:
6470 case -NFS4ERR_WRONGSEC:
6471 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006472 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006473 default:
6474 err = nfs4_handle_exception(server, err, &exception);
6475 }
6476 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006477out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006478 return err;
6479}
6480
6481static int
6482nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6483 struct nfs_fsinfo *info)
6484{
6485 int err;
6486 struct page *page;
6487 rpc_authflavor_t flavor;
6488 struct nfs4_secinfo_flavors *flavors;
6489
6490 page = alloc_page(GFP_KERNEL);
6491 if (!page) {
6492 err = -ENOMEM;
6493 goto out;
6494 }
6495
6496 flavors = page_address(page);
6497 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6498
6499 /*
6500 * Fall back on "guess and check" method if
6501 * the server doesn't support SECINFO_NO_NAME
6502 */
6503 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6504 err = nfs4_find_root_sec(server, fhandle, info);
6505 goto out_freepage;
6506 }
6507 if (err)
6508 goto out_freepage;
6509
6510 flavor = nfs_find_best_sec(flavors);
6511 if (err == 0)
6512 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6513
6514out_freepage:
6515 put_page(page);
6516 if (err == -EACCES)
6517 return -EPERM;
6518out:
6519 return err;
6520}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006521
6522static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006523{
6524 int status;
6525 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006526 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006527 };
6528 struct nfs41_test_stateid_res res;
6529 struct rpc_message msg = {
6530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6531 .rpc_argp = &args,
6532 .rpc_resp = &res,
6533 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006534
Chuck Lever38527b12012-07-11 16:30:23 -04006535 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006536 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006537 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006538 if (status != NFS_OK) {
6539 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006540 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006541 }
6542 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006543 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006544}
6545
Chuck Lever38527b12012-07-11 16:30:23 -04006546/**
6547 * nfs41_test_stateid - perform a TEST_STATEID operation
6548 *
6549 * @server: server / transport on which to perform the operation
6550 * @stateid: state ID to test
6551 *
6552 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6553 * Otherwise a negative NFS4ERR value is returned if the operation
6554 * failed or the state ID is not currently valid.
6555 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006556static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006557{
6558 struct nfs4_exception exception = { };
6559 int err;
6560 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006561 err = _nfs41_test_stateid(server, stateid);
6562 if (err != -NFS4ERR_DELAY)
6563 break;
6564 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006565 } while (exception.retry);
6566 return err;
6567}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006568
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006569static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006570{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006571 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006572 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006573 };
6574 struct nfs41_free_stateid_res res;
6575 struct rpc_message msg = {
6576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6577 .rpc_argp = &args,
6578 .rpc_resp = &res,
6579 };
Chuck Lever38527b12012-07-11 16:30:23 -04006580 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006581
Chuck Lever38527b12012-07-11 16:30:23 -04006582 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006583 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006584 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006585 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006586 dprintk("NFS reply free_stateid: %d\n", status);
6587 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006588}
6589
Chuck Lever38527b12012-07-11 16:30:23 -04006590/**
6591 * nfs41_free_stateid - perform a FREE_STATEID operation
6592 *
6593 * @server: server / transport on which to perform the operation
6594 * @stateid: state ID to release
6595 *
6596 * Returns NFS_OK if the server freed "stateid". Otherwise a
6597 * negative NFS4ERR value is returned.
6598 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006599static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006600{
6601 struct nfs4_exception exception = { };
6602 int err;
6603 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006604 err = _nfs4_free_stateid(server, stateid);
6605 if (err != -NFS4ERR_DELAY)
6606 break;
6607 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006608 } while (exception.retry);
6609 return err;
6610}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006611
6612static bool nfs41_match_stateid(const nfs4_stateid *s1,
6613 const nfs4_stateid *s2)
6614{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006615 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006616 return false;
6617
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006618 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006619 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006620 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006621 return true;
6622
6623 return false;
6624}
6625
Andy Adamson557134a2009-04-01 09:21:53 -04006626#endif /* CONFIG_NFS_V4_1 */
6627
Trond Myklebust36281ca2012-03-04 18:13:56 -05006628static bool nfs4_match_stateid(const nfs4_stateid *s1,
6629 const nfs4_stateid *s2)
6630{
Trond Myklebustf597c532012-03-04 18:13:56 -05006631 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006632}
6633
6634
Trond Myklebust17280172012-03-11 13:11:00 -04006635static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006636 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006637 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 .recover_open = nfs4_open_reclaim,
6639 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006640 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006641 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006642 .detect_trunking = nfs40_discover_server_trunking,
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_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006647 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6648 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6649 .recover_open = nfs4_open_reclaim,
6650 .recover_lock = nfs4_lock_reclaim,
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,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006653 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006654 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006655};
6656#endif /* CONFIG_NFS_V4_1 */
6657
Trond Myklebust17280172012-03-11 13:11:00 -04006658static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006659 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006660 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 .recover_open = nfs4_open_expired,
6662 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006663 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006664 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665};
6666
Andy Adamson591d71c2009-04-01 09:22:47 -04006667#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006668static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006669 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6670 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006671 .recover_open = nfs41_open_expired,
6672 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006673 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006674 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006675};
6676#endif /* CONFIG_NFS_V4_1 */
6677
Trond Myklebust17280172012-03-11 13:11:00 -04006678static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006679 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006680 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006681 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006682};
6683
6684#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006685static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006686 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006687 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006688 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006689};
6690#endif
6691
Trond Myklebust97dc1352010-06-16 09:52:26 -04006692static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6693 .minor_version = 0,
6694 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006695 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006696 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006697 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6698 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6699 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006700};
6701
6702#if defined(CONFIG_NFS_V4_1)
6703static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6704 .minor_version = 1,
6705 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006706 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006707 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006708 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6709 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6710 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006711};
6712#endif
6713
Trond Myklebust97dc1352010-06-16 09:52:26 -04006714const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6715 [0] = &nfs_v4_0_minor_ops,
6716#if defined(CONFIG_NFS_V4_1)
6717 [1] = &nfs_v4_1_minor_ops,
6718#endif
6719};
6720
Bryan Schumaker73a79702012-07-16 16:39:12 -04006721const struct inode_operations nfs4_dir_inode_operations = {
6722 .create = nfs_create,
6723 .lookup = nfs_lookup,
6724 .atomic_open = nfs_atomic_open,
6725 .link = nfs_link,
6726 .unlink = nfs_unlink,
6727 .symlink = nfs_symlink,
6728 .mkdir = nfs_mkdir,
6729 .rmdir = nfs_rmdir,
6730 .mknod = nfs_mknod,
6731 .rename = nfs_rename,
6732 .permission = nfs_permission,
6733 .getattr = nfs_getattr,
6734 .setattr = nfs_setattr,
6735 .getxattr = generic_getxattr,
6736 .setxattr = generic_setxattr,
6737 .listxattr = generic_listxattr,
6738 .removexattr = generic_removexattr,
6739};
6740
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006741static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006742 .permission = nfs_permission,
6743 .getattr = nfs_getattr,
6744 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006745 .getxattr = generic_getxattr,
6746 .setxattr = generic_setxattr,
6747 .listxattr = generic_listxattr,
6748 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006749};
6750
David Howells509de812006-08-22 20:06:11 -04006751const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 .version = 4, /* protocol version */
6753 .dentry_ops = &nfs4_dentry_operations,
6754 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006755 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006756 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006758 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006759 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 .getattr = nfs4_proc_getattr,
6761 .setattr = nfs4_proc_setattr,
6762 .lookup = nfs4_proc_lookup,
6763 .access = nfs4_proc_access,
6764 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 .create = nfs4_proc_create,
6766 .remove = nfs4_proc_remove,
6767 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006768 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 .unlink_done = nfs4_proc_unlink_done,
6770 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006771 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006772 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006773 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 .link = nfs4_proc_link,
6775 .symlink = nfs4_proc_symlink,
6776 .mkdir = nfs4_proc_mkdir,
6777 .rmdir = nfs4_proc_remove,
6778 .readdir = nfs4_proc_readdir,
6779 .mknod = nfs4_proc_mknod,
6780 .statfs = nfs4_proc_statfs,
6781 .fsinfo = nfs4_proc_fsinfo,
6782 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006783 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 .decode_dirent = nfs4_decode_dirent,
6785 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006786 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006787 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006788 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006790 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006791 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006792 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006794 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006795 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006797 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006798 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006799 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006800 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006801 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006802 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006803 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006804 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006805 .create_server = nfs4_create_server,
6806 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807};
6808
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006809static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6810 .prefix = XATTR_NAME_NFSV4_ACL,
6811 .list = nfs4_xattr_list_nfs4_acl,
6812 .get = nfs4_xattr_get_nfs4_acl,
6813 .set = nfs4_xattr_set_nfs4_acl,
6814};
6815
6816const struct xattr_handler *nfs4_xattr_handlers[] = {
6817 &nfs4_xattr_nfs4_acl_handler,
6818 NULL
6819};
6820
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821/*
6822 * Local variables:
6823 * c-basic-offset: 8
6824 * End:
6825 */