blob: 4b1635ce658d5276877e93596835636c5c015a90 [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 Myklebustd185a332010-06-16 09:52:25 -0400382static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400383{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500384 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400385 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500386 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400387
Benny Halevydfb4f3092010-09-24 09:17:01 -0400388 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400389 /* just wake up the next guy waiting since
390 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500391 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500392 return;
Andy Adamson13615872009-04-01 09:22:17 -0400393 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500394 tbl = res->sr_slot->table;
395 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500396
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500397 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500398 /* Be nice to the server: try to ensure that the last transmitted
399 * value for highest_user_slotid <= target_highest_slotid
400 */
401 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
402 send_new_highest_used_slotid = true;
403
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500404 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500405
406 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
407 send_new_highest_used_slotid = false;
408 if (!nfs4_session_draining(session)) {
Trond Myklebust275e7e22012-11-01 17:07:07 -0400409 if (rpc_wake_up_next(&tbl->slot_tbl_waitq) != NULL)
Trond Myklebustc10e4492012-11-26 16:16:54 -0500410 send_new_highest_used_slotid = false;
411 }
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500412 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400413 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500414 if (send_new_highest_used_slotid)
415 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400416}
417
Trond Myklebust14516c32010-07-31 14:29:06 -0400418static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400419{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500420 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500421 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400422 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400423 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400424
425 /*
426 * sr_status remains 1 if an RPC level error occurred. The server
427 * may or may not have processed the sequence operation..
428 * Proceed as if the server received and processed the sequence
429 * operation.
430 */
431 if (res->sr_status == 1)
432 res->sr_status = NFS_OK;
433
Bryan Schumaker468f8612011-04-18 15:57:32 -0400434 /* don't increment the sequence number if the task wasn't sent */
435 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400436 goto out;
437
Trond Myklebust933602e2012-11-16 12:12:38 -0500438 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500439 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500440
Andy Adamson691daf32009-12-04 15:55:39 -0500441 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400442 switch (res->sr_status) {
443 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 ++slot->seq_nr;
446 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500447 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400448 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500449 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500450 if (res->sr_status_flags != 0)
451 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500452 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400453 break;
454 case -NFS4ERR_DELAY:
455 /* The server detected a resend of the RPC call and
456 * returned NFS4ERR_DELAY as per Section 2.10.6.2
457 * of RFC5661.
458 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500459 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400460 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500461 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500462 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 goto out_retry;
464 default:
465 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500466 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400467 }
468out:
469 /* The session may be reset by one of the error handlers. */
470 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400471 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400472 return 1;
473out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400474 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400475 goto out;
476 rpc_delay(task, NFS4_POLL_RETRY_MAX);
477 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400478}
479
Trond Myklebust14516c32010-07-31 14:29:06 -0400480static int nfs4_sequence_done(struct rpc_task *task,
481 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400482{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500483 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400484 return 1;
485 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400486}
487
Trond Myklebust9d12b212012-01-17 22:04:25 -0500488static void nfs41_init_sequence(struct nfs4_sequence_args *args,
489 struct nfs4_sequence_res *res, int cache_reply)
490{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500491 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500492 args->sa_cache_this = 0;
493 if (cache_reply)
494 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500495 res->sr_slot = NULL;
496}
497
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000498int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400499 struct nfs4_sequence_args *args,
500 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400501 struct rpc_task *task)
502{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400503 struct nfs4_slot *slot;
504 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400505
506 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400507 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400508 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400509 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400510
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400511 tbl = &session->fc_slot_table;
512
Trond Myklebust69d206b2012-11-22 13:21:02 -0500513 task->tk_timeout = 0;
514
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400515 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400516 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800517 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500518 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500519 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400520 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400521 }
522
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500523 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500524 if (IS_ERR(slot)) {
525 /* If out of memory, try again in 1/4 second */
526 if (slot == ERR_PTR(-ENOMEM))
527 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400528 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400529 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400530 }
531 spin_unlock(&tbl->slot_tbl_lock);
532
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800533 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500534
535 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400536
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500537 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
538 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400539
Benny Halevydfb4f3092010-09-24 09:17:01 -0400540 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400541 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400542 /*
543 * sr_status is only set in decode_sequence, and so will remain
544 * set to 1 if an rpc level failure occurs.
545 */
546 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400547out_success:
548 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400549 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400550out_sleep:
551 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
552 spin_unlock(&tbl->slot_tbl_lock);
553 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000555EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400556
Trond Myklebust035168a2010-06-16 09:52:26 -0400557int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400558 struct nfs4_sequence_args *args,
559 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 struct rpc_task *task)
561{
Trond Myklebust035168a2010-06-16 09:52:26 -0400562 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563 int ret = 0;
564
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400565 if (session == NULL) {
566 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400568 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400569
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500570 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400571 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500572 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400573
Trond Myklebust9d12b212012-01-17 22:04:25 -0500574 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575out:
576 dprintk("<-- %s status=%d\n", __func__, ret);
577 return ret;
578}
579
580struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400581 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400582 struct nfs4_sequence_args *seq_args;
583 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400584};
585
586static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
587{
588 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400589 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590
Trond Myklebust035168a2010-06-16 09:52:26 -0400591 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
592
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400593 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400594}
595
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800596static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
597{
598 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
599 nfs41_call_sync_prepare(task, calldata);
600}
601
Andy Adamson69ab40c2009-04-01 09:22:19 -0400602static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
603{
604 struct nfs41_call_sync_data *data = calldata;
605
Trond Myklebust14516c32010-07-31 14:29:06 -0400606 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400607}
608
Trond Myklebust17280172012-03-11 13:11:00 -0400609static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400611 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612};
613
Trond Myklebust17280172012-03-11 13:11:00 -0400614static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800615 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
616 .rpc_call_done = nfs41_call_sync_done,
617};
618
Bryan Schumaker7c513052011-03-24 17:12:24 +0000619static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
620 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 struct rpc_message *msg,
622 struct nfs4_sequence_args *args,
623 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800624 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625{
626 int ret;
627 struct rpc_task *task;
628 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400629 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 .seq_args = args,
631 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 };
633 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000634 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400635 .rpc_message = msg,
636 .callback_ops = &nfs41_call_sync_ops,
637 .callback_data = &data
638 };
639
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800640 if (privileged)
641 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642 task = rpc_run_task(&task_setup);
643 if (IS_ERR(task))
644 ret = PTR_ERR(task);
645 else {
646 ret = task->tk_status;
647 rpc_put_task(task);
648 }
649 return ret;
650}
651
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400652static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000653int _nfs4_call_sync_session(struct rpc_clnt *clnt,
654 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400655 struct rpc_message *msg,
656 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400657 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400658{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500659 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400660}
661
Trond Myklebustdf896452010-06-16 09:52:26 -0400662#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500663static inline
664void nfs41_init_sequence(struct nfs4_sequence_args *args,
665 struct nfs4_sequence_res *res, int cache_reply)
666{
667}
668
Trond Myklebust14516c32010-07-31 14:29:06 -0400669static int nfs4_sequence_done(struct rpc_task *task,
670 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400671{
Trond Myklebust14516c32010-07-31 14:29:06 -0400672 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400673}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400674#endif /* CONFIG_NFS_V4_1 */
675
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400676static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000677int _nfs4_call_sync(struct rpc_clnt *clnt,
678 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400679 struct rpc_message *msg,
680 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400681 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400682{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000683 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400684}
685
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400686static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000687int nfs4_call_sync(struct rpc_clnt *clnt,
688 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000689 struct rpc_message *msg,
690 struct nfs4_sequence_args *args,
691 struct nfs4_sequence_res *res,
692 int cache_reply)
693{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400694 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000695 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400696 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000697}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400698
Trond Myklebust38478b22006-05-25 01:40:57 -0400699static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Trond Myklebust38478b22006-05-25 01:40:57 -0400701 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Trond Myklebust38478b22006-05-25 01:40:57 -0400703 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400704 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700705 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400706 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700707 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400708 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400712 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713 struct nfs_openargs o_arg;
714 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100715 struct nfs_open_confirmargs c_arg;
716 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500717 struct nfs4_string owner_name;
718 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100719 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400721 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400723 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100725 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400726 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100727 int rpc_status;
728 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729};
730
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400731
732static void nfs4_init_opendata_res(struct nfs4_opendata *p)
733{
734 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400735 p->o_res.seqid = p->o_arg.seqid;
736 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400737 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400738 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400739 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500740 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400741}
742
Al Viro82a2c1b2011-06-22 18:30:55 -0400743static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500744 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400745 const struct iattr *attrs,
746 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747{
Al Viro82a2c1b2011-06-22 18:30:55 -0400748 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct inode *dir = parent->d_inode;
750 struct nfs_server *server = NFS_SERVER(dir);
751 struct nfs4_opendata *p;
752
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400753 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 if (p == NULL)
755 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400756 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 if (p->o_arg.seqid == NULL)
758 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400759 nfs_sb_active(dentry->d_sb);
760 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 p->dir = parent;
762 p->owner = sp;
763 atomic_inc(&sp->so_count);
764 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500765 p->o_arg.open_flags = flags;
766 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700767 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
768 * will return permission denied for all bits until close */
769 if (!(flags & O_EXCL)) {
770 /* ask server to check for all possible rights as results
771 * are cached */
772 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
773 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700774 }
David Howells7539bba2006-08-22 20:06:09 -0400775 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400776 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
777 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400778 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 p->o_arg.server = server;
780 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400781 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500783 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500784 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400785
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 p->o_arg.u.attrs = &p->attrs;
787 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500788
789 verf[0] = jiffies;
790 verf[1] = current->pid;
791 memcpy(p->o_arg.u.verifier.data, verf,
792 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100794 p->c_arg.fh = &p->o_res.fh;
795 p->c_arg.stateid = &p->o_res.stateid;
796 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400797 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400798 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 return p;
800err_free:
801 kfree(p);
802err:
803 dput(parent);
804 return NULL;
805}
806
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400807static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400809 struct nfs4_opendata *p = container_of(kref,
810 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400811 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400812
813 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400814 if (p->state != NULL)
815 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400816 nfs4_put_state_owner(p->owner);
817 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 dput(p->dentry);
819 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500820 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400821 kfree(p);
822}
823
824static void nfs4_opendata_put(struct nfs4_opendata *p)
825{
826 if (p != NULL)
827 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828}
829
Trond Myklebust06f814a2006-01-03 09:55:07 +0100830static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
831{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100832 int ret;
833
Trond Myklebust06f814a2006-01-03 09:55:07 +0100834 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100835 return ret;
836}
837
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839{
840 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841
Trond Myklebust536e43d2012-01-17 22:04:26 -0500842 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500843 goto out;
844 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400845 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500846 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
847 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400848 break;
849 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500850 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
851 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400852 break;
853 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500854 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
855 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400856 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400858 return ret;
859}
860
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400862{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500863 if (delegation == NULL)
864 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400866 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500867 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400868 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500869 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400870 return 1;
871}
872
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100874{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100876 case FMODE_WRITE:
877 state->n_wronly++;
878 break;
879 case FMODE_READ:
880 state->n_rdonly++;
881 break;
882 case FMODE_READ|FMODE_WRITE:
883 state->n_rdwr++;
884 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400889{
890 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500891 nfs4_stateid_copy(&state->stateid, stateid);
892 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400894 case FMODE_READ:
895 set_bit(NFS_O_RDONLY_STATE, &state->flags);
896 break;
897 case FMODE_WRITE:
898 set_bit(NFS_O_WRONLY_STATE, &state->flags);
899 break;
900 case FMODE_READ|FMODE_WRITE:
901 set_bit(NFS_O_RDWR_STATE, &state->flags);
902 }
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400906{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400907 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400909 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400910}
911
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912static 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 -0700913{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400914 /*
915 * Protect the call to nfs4_state_set_mode_locked and
916 * serialise the stateid update
917 */
918 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400919 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500920 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400921 set_bit(NFS_DELEGATED_STATE, &state->flags);
922 }
923 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400925 write_sequnlock(&state->seqlock);
926 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700928 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931static 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 -0500932{
933 struct nfs_inode *nfsi = NFS_I(state->inode);
934 struct nfs_delegation *deleg_cur;
935 int ret = 0;
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500938
939 rcu_read_lock();
940 deleg_cur = rcu_dereference(nfsi->delegation);
941 if (deleg_cur == NULL)
942 goto no_delegation;
943
944 spin_lock(&deleg_cur->lock);
945 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500947 goto no_delegation_unlock;
948
949 if (delegation == NULL)
950 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500951 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500952 goto no_delegation_unlock;
953
Trond Myklebustb7391f42008-12-23 15:21:52 -0500954 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500956 ret = 1;
957no_delegation_unlock:
958 spin_unlock(&deleg_cur->lock);
959no_delegation:
960 rcu_read_unlock();
961
962 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500964 ret = 1;
965 }
966
967 return ret;
968}
969
970
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972{
973 struct nfs_delegation *delegation;
974
975 rcu_read_lock();
976 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 rcu_read_unlock();
979 return;
980 }
981 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400982 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400983}
984
Trond Myklebust6ee41262007-07-08 14:11:36 -0400985static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986{
987 struct nfs4_state *state = opendata->state;
988 struct nfs_inode *nfsi = NFS_I(state->inode);
989 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500990 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400992 nfs4_stateid stateid;
993 int ret = -EAGAIN;
994
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400997 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 if (can_open_cached(state, fmode, open_mode)) {
999 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001000 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001 goto out_return_state;
1002 }
1003 spin_unlock(&state->owner->so_lock);
1004 }
Trond Myklebust34310432008-12-23 15:21:38 -05001005 rcu_read_lock();
1006 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001007 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001008 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001009 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001010 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001012 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001014 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015 if (ret != 0)
1016 goto out;
1017 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001018
1019 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001021 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023out:
1024 return ERR_PTR(ret);
1025out_return_state:
1026 atomic_inc(&state->count);
1027 return state;
1028}
1029
Andy Adamsone23008e2012-10-02 21:07:32 -04001030static void
1031nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1032{
1033 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1034 struct nfs_delegation *delegation;
1035 int delegation_flags = 0;
1036
1037 rcu_read_lock();
1038 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1039 if (delegation)
1040 delegation_flags = delegation->flags;
1041 rcu_read_unlock();
1042 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1043 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1044 "returning a delegation for "
1045 "OPEN(CLAIM_DELEGATE_CUR)\n",
1046 clp->cl_hostname);
1047 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1048 nfs_inode_set_delegation(state->inode,
1049 data->owner->so_cred,
1050 &data->o_res);
1051 else
1052 nfs_inode_reclaim_delegation(state->inode,
1053 data->owner->so_cred,
1054 &data->o_res);
1055}
1056
1057/*
1058 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1059 * and update the nfs4_state.
1060 */
1061static struct nfs4_state *
1062_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1063{
1064 struct inode *inode = data->state->inode;
1065 struct nfs4_state *state = data->state;
1066 int ret;
1067
1068 if (!data->rpc_done) {
1069 ret = data->rpc_status;
1070 goto err;
1071 }
1072
1073 ret = -ESTALE;
1074 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1075 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1076 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1077 goto err;
1078
1079 ret = -ENOMEM;
1080 state = nfs4_get_open_state(inode, data->owner);
1081 if (state == NULL)
1082 goto err;
1083
1084 ret = nfs_refresh_inode(inode, &data->f_attr);
1085 if (ret)
1086 goto err;
1087
1088 if (data->o_res.delegation_type != 0)
1089 nfs4_opendata_check_deleg(data, state);
1090 update_open_stateid(state, &data->o_res.stateid, NULL,
1091 data->o_arg.fmode);
1092
1093 return state;
1094err:
1095 return ERR_PTR(ret);
1096
1097}
1098
1099static struct nfs4_state *
1100_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101{
1102 struct inode *inode;
1103 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001104 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001107 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108 goto out;
1109 }
1110
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001115 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001116 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 goto err;
1118 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001119 state = nfs4_get_open_state(inode, data->owner);
1120 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001121 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001122 if (data->o_res.delegation_type != 0)
1123 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001124 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001126 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001129err_put_inode:
1130 iput(inode);
1131err:
1132 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001133}
1134
Andy Adamsone23008e2012-10-02 21:07:32 -04001135static struct nfs4_state *
1136nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1137{
1138 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1139 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1140 return _nfs4_opendata_to_nfs4_state(data);
1141}
1142
Trond Myklebust864472e2006-01-03 09:55:15 +01001143static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1144{
1145 struct nfs_inode *nfsi = NFS_I(state->inode);
1146 struct nfs_open_context *ctx;
1147
1148 spin_lock(&state->inode->i_lock);
1149 list_for_each_entry(ctx, &nfsi->open_files, list) {
1150 if (ctx->state != state)
1151 continue;
1152 get_nfs_open_context(ctx);
1153 spin_unlock(&state->inode->i_lock);
1154 return ctx;
1155 }
1156 spin_unlock(&state->inode->i_lock);
1157 return ERR_PTR(-ENOENT);
1158}
1159
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001160static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1161{
1162 struct nfs4_opendata *opendata;
1163
Al Viro3d4ff432011-06-22 18:40:12 -04001164 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001165 if (opendata == NULL)
1166 return ERR_PTR(-ENOMEM);
1167 opendata->state = state;
1168 atomic_inc(&state->count);
1169 return opendata;
1170}
1171
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001173{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001174 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 int ret;
1176
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 opendata->o_arg.open_flags = 0;
1178 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1180 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1181 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001182 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 if (ret != 0)
1184 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001186 if (IS_ERR(newstate))
1187 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001188 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 return 0;
1191}
1192
1193static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1194{
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 int ret;
1197
1198 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 smp_rmb();
1201 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001202 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001203 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 if (ret != 0)
1205 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001206 if (newstate != state)
1207 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 }
1209 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001210 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 if (ret != 0)
1213 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 if (newstate != state)
1215 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 }
1217 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001218 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 if (ret != 0)
1221 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 if (newstate != state)
1223 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 /*
1226 * We may have performed cached opens for all three recoveries.
1227 * Check if we need to update the current stateid.
1228 */
1229 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001230 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001231 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001232 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001233 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001234 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 return 0;
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/*
1240 * OPEN_RECLAIM:
1241 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001243static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 int status;
1249
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001250 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1251 if (IS_ERR(opendata))
1252 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1254 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 rcu_read_lock();
1256 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001257 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001258 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001259 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 opendata->o_arg.u.delegation_type = delegation_type;
1261 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001262 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return status;
1264}
1265
Trond Myklebust539cd032007-06-05 11:46:42 -04001266static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct nfs_server *server = NFS_SERVER(state->inode);
1269 struct nfs4_exception exception = { };
1270 int err;
1271 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001272 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001273 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001274 break;
1275 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 } while (exception.retry);
1277 return err;
1278}
1279
Trond Myklebust864472e2006-01-03 09:55:15 +01001280static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1281{
1282 struct nfs_open_context *ctx;
1283 int ret;
1284
1285 ctx = nfs4_state_find_open_context(state);
1286 if (IS_ERR(ctx))
1287 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001288 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 put_nfs_open_context(ctx);
1290 return ret;
1291}
1292
Trond Myklebust13437e12007-07-06 15:10:43 -04001293static 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 -07001294{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001295 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001298 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1299 if (IS_ERR(opendata))
1300 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001302 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Trond Myklebust13437e12007-07-06 15:10:43 -04001308int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001311 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int err;
1313 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001314 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 switch (err) {
1316 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001317 case -ENOENT:
1318 case -ESTALE:
1319 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001320 case -NFS4ERR_BADSESSION:
1321 case -NFS4ERR_BADSLOT:
1322 case -NFS4ERR_BAD_HIGH_SLOT:
1323 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1324 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001325 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001326 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case -NFS4ERR_STALE_CLIENTID:
1328 case -NFS4ERR_STALE_STATEID:
1329 case -NFS4ERR_EXPIRED:
1330 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001331 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001332 goto out;
1333 case -ERESTARTSYS:
1334 /*
1335 * The show must go on: exit, but mark the
1336 * stateid as needing recovery.
1337 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001338 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001339 case -NFS4ERR_ADMIN_REVOKED:
1340 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001341 nfs_inode_find_state_and_recover(state->inode,
1342 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001343 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001344 case -EKEYEXPIRED:
1345 /*
1346 * User RPCSEC_GSS context has expired.
1347 * We cannot recover this stateid now, so
1348 * skip it and allow recovery thread to
1349 * proceed.
1350 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001351 case -ENOMEM:
1352 err = 0;
1353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 err = nfs4_handle_exception(server, err, &exception);
1356 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001357out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return err;
1359}
1360
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1362{
1363 struct nfs4_opendata *data = calldata;
1364
1365 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001366 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001367 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001368 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001369 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001370 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001371 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001372}
1373
1374static void nfs4_open_confirm_release(void *calldata)
1375{
1376 struct nfs4_opendata *data = calldata;
1377 struct nfs4_state *state = NULL;
1378
1379 /* If this request hasn't been cancelled, do nothing */
1380 if (data->cancelled == 0)
1381 goto out_free;
1382 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001383 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001385 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001386 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001387 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001388out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001389 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390}
1391
1392static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 .rpc_call_done = nfs4_open_confirm_done,
1394 .rpc_release = nfs4_open_confirm_release,
1395};
1396
1397/*
1398 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1399 */
1400static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1401{
1402 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1403 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404 struct rpc_message msg = {
1405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1406 .rpc_argp = &data->c_arg,
1407 .rpc_resp = &data->c_res,
1408 .rpc_cred = data->owner->so_cred,
1409 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001410 struct rpc_task_setup task_setup_data = {
1411 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001413 .callback_ops = &nfs4_open_confirm_ops,
1414 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001415 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 .flags = RPC_TASK_ASYNC,
1417 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int status;
1419
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001420 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001421 data->rpc_done = 0;
1422 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001423 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001425 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001427 status = nfs4_wait_for_completion_rpc_task(task);
1428 if (status != 0) {
1429 data->cancelled = 1;
1430 smp_wmb();
1431 } else
1432 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001433 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return status;
1435}
1436
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 struct nfs4_opendata *data = calldata;
1440 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001441
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1443 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001444 /*
1445 * Check if we still need to send an OPEN call, or if we can use
1446 * a delegation instead.
1447 */
1448 if (data->state != NULL) {
1449 struct nfs_delegation *delegation;
1450
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001451 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001452 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001453 rcu_read_lock();
1454 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001455 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1456 can_open_delegated(delegation, data->o_arg.fmode))
1457 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001458 rcu_read_unlock();
1459 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001460 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001461 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001462 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001463 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001464 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001465 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1466 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001467 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001468 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001469 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001470 &data->o_res.seq_res,
1471 task) != 0)
1472 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001473 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001474unlock_no_action:
1475 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001476out_no_action:
1477 task->tk_action = NULL;
1478
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001481static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1482{
1483 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1484 nfs4_open_prepare(task, calldata);
1485}
1486
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487static void nfs4_open_done(struct rpc_task *task, void *calldata)
1488{
1489 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001492
Trond Myklebust14516c32010-07-31 14:29:06 -04001493 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1494 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001497 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1498 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001499 case S_IFREG:
1500 break;
1501 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001502 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001503 break;
1504 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001506 break;
1507 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001509 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001510 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001511 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001512 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1513 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001514 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001515 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001517
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518static void nfs4_open_release(void *calldata)
1519{
1520 struct nfs4_opendata *data = calldata;
1521 struct nfs4_state *state = NULL;
1522
1523 /* If this request hasn't been cancelled, do nothing */
1524 if (data->cancelled == 0)
1525 goto out_free;
1526 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001527 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 goto out_free;
1529 /* In case we need an open_confirm, no cleanup! */
1530 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1531 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001533 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001534 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001536 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537}
1538
1539static const struct rpc_call_ops nfs4_open_ops = {
1540 .rpc_call_prepare = nfs4_open_prepare,
1541 .rpc_call_done = nfs4_open_done,
1542 .rpc_release = nfs4_open_release,
1543};
1544
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001545static const struct rpc_call_ops nfs4_recover_open_ops = {
1546 .rpc_call_prepare = nfs4_recover_open_prepare,
1547 .rpc_call_done = nfs4_open_done,
1548 .rpc_release = nfs4_open_release,
1549};
1550
1551static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552{
1553 struct inode *dir = data->dir->d_inode;
1554 struct nfs_server *server = NFS_SERVER(dir);
1555 struct nfs_openargs *o_arg = &data->o_arg;
1556 struct nfs_openres *o_res = &data->o_res;
1557 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001558 struct rpc_message msg = {
1559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1560 .rpc_argp = o_arg,
1561 .rpc_resp = o_res,
1562 .rpc_cred = data->owner->so_cred,
1563 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001564 struct rpc_task_setup task_setup_data = {
1565 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001566 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001567 .callback_ops = &nfs4_open_ops,
1568 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001569 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001570 .flags = RPC_TASK_ASYNC,
1571 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001572 int status;
1573
Trond Myklebust9d12b212012-01-17 22:04:25 -05001574 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001575 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001576 data->rpc_done = 0;
1577 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001578 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001579 if (isrecover)
1580 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001582 if (IS_ERR(task))
1583 return PTR_ERR(task);
1584 status = nfs4_wait_for_completion_rpc_task(task);
1585 if (status != 0) {
1586 data->cancelled = 1;
1587 smp_wmb();
1588 } else
1589 status = data->rpc_status;
1590 rpc_put_task(task);
1591
1592 return status;
1593}
1594
1595static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1596{
1597 struct inode *dir = data->dir->d_inode;
1598 struct nfs_openres *o_res = &data->o_res;
1599 int status;
1600
1601 status = nfs4_run_open_task(data, 1);
1602 if (status != 0 || !data->rpc_done)
1603 return status;
1604
Trond Myklebust6926afd2012-01-07 13:22:46 -05001605 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1606
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001607 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1608 status = _nfs4_proc_open_confirm(data);
1609 if (status != 0)
1610 return status;
1611 }
1612
1613 return status;
1614}
1615
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001616static int nfs4_opendata_access(struct rpc_cred *cred,
1617 struct nfs4_opendata *opendata,
1618 struct nfs4_state *state, fmode_t fmode)
1619{
1620 struct nfs_access_entry cache;
1621 u32 mask;
1622
1623 /* access call failed or for some reason the server doesn't
1624 * support any access modes -- defer access call until later */
1625 if (opendata->o_res.access_supported == 0)
1626 return 0;
1627
1628 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001629 /* don't check MAY_WRITE - a newly created file may not have
1630 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001631 if (fmode & FMODE_READ)
1632 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001633 if (fmode & FMODE_EXEC)
1634 mask |= MAY_EXEC;
1635
1636 cache.cred = cred;
1637 cache.jiffies = jiffies;
1638 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1639 nfs_access_add_cache(state->inode, &cache);
1640
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001641 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001642 return 0;
1643
1644 /* even though OPEN succeeded, access is denied. Close the file */
1645 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001646 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001647}
1648
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001649/*
1650 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1651 */
1652static int _nfs4_proc_open(struct nfs4_opendata *data)
1653{
1654 struct inode *dir = data->dir->d_inode;
1655 struct nfs_server *server = NFS_SERVER(dir);
1656 struct nfs_openargs *o_arg = &data->o_arg;
1657 struct nfs_openres *o_res = &data->o_res;
1658 int status;
1659
1660 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001661 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001663 if (status != 0) {
1664 if (status == -NFS4ERR_BADNAME &&
1665 !(o_arg->open_flags & O_CREAT))
1666 return -ENOENT;
1667 return status;
1668 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669
Trond Myklebust6926afd2012-01-07 13:22:46 -05001670 nfs_fattr_map_and_free_names(server, &data->f_attr);
1671
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001672 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001673 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001674 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1675 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001677 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001682 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Andy Adamsond83217c2011-03-01 01:34:17 +00001686static int nfs4_recover_expired_lease(struct nfs_server *server)
1687{
1688 return nfs4_client_recover_expired_lease(server->nfs_client);
1689}
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691/*
1692 * OPEN_EXPIRED:
1693 * reclaim state on the server after a network partition.
1694 * Assumes caller holds the appropriate lock
1695 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001696static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001698 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001699 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001701 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1702 if (IS_ERR(opendata))
1703 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001704 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001705 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001706 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001707 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001708 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001711static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001712{
Trond Myklebust539cd032007-06-05 11:46:42 -04001713 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001714 struct nfs4_exception exception = { };
1715 int err;
1716
1717 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001718 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001719 switch (err) {
1720 default:
1721 goto out;
1722 case -NFS4ERR_GRACE:
1723 case -NFS4ERR_DELAY:
1724 nfs4_handle_exception(server, err, &exception);
1725 err = 0;
1726 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001728out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729 return err;
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001735 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Trond Myklebust864472e2006-01-03 09:55:15 +01001737 ctx = nfs4_state_find_open_context(state);
1738 if (IS_ERR(ctx))
1739 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001740 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001741 put_nfs_open_context(ctx);
1742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001745#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001746static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001747{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001748 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001749 nfs4_stateid *stateid = &state->stateid;
1750 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001751
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001752 /* If a state reset has been done, test_stateid is unneeded */
1753 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1754 return;
1755
1756 status = nfs41_test_stateid(server, stateid);
1757 if (status != NFS_OK) {
1758 /* Free the stateid unless the server explicitly
1759 * informs us the stateid is unrecognized. */
1760 if (status != -NFS4ERR_BAD_STATEID)
1761 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001762 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001763
Bryan Schumaker69388672012-09-26 15:25:52 -04001764 write_seqlock(&state->seqlock);
1765 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1766 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001767 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1768 }
1769}
1770
1771/**
1772 * nfs41_check_open_stateid - possibly free an open stateid
1773 *
1774 * @state: NFSv4 state for an inode
1775 *
1776 * Returns NFS_OK if recovery for this stateid is now finished.
1777 * Otherwise a negative NFS4ERR value is returned.
1778 */
1779static int nfs41_check_open_stateid(struct nfs4_state *state)
1780{
1781 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001782 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001783 int status;
1784
1785 /* If a state reset has been done, test_stateid is unneeded */
1786 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1787 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1788 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1789 return -NFS4ERR_BAD_STATEID;
1790
1791 status = nfs41_test_stateid(server, stateid);
1792 if (status != NFS_OK) {
1793 /* Free the stateid unless the server explicitly
1794 * informs us the stateid is unrecognized. */
1795 if (status != -NFS4ERR_BAD_STATEID)
1796 nfs41_free_stateid(server, stateid);
1797
1798 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1799 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1800 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001801 }
1802 return status;
1803}
1804
1805static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1806{
Chuck Levereb64cf92012-07-11 16:30:05 -04001807 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001808
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001809 nfs41_clear_delegation_stateid(state);
1810 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001811 if (status != NFS_OK)
1812 status = nfs4_open_expired(sp, state);
1813 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001814}
1815#endif
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001818 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1819 * fields corresponding to attributes that were used to store the verifier.
1820 * Make sure we clobber those fields in the later setattr call
1821 */
1822static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1823{
1824 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1825 !(sattr->ia_valid & ATTR_ATIME_SET))
1826 sattr->ia_valid |= ATTR_ATIME;
1827
1828 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1829 !(sattr->ia_valid & ATTR_MTIME_SET))
1830 sattr->ia_valid |= ATTR_MTIME;
1831}
1832
1833/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001834 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 */
Andy Adamson82be4172012-05-23 05:02:35 -04001836static int _nfs4_do_open(struct inode *dir,
1837 struct dentry *dentry,
1838 fmode_t fmode,
1839 int flags,
1840 struct iattr *sattr,
1841 struct rpc_cred *cred,
1842 struct nfs4_state **res,
1843 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 struct nfs4_state_owner *sp;
1846 struct nfs4_state *state = NULL;
1847 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001848 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001849 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001853 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1854 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1856 goto out_err;
1857 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001858 status = nfs4_recover_expired_lease(server);
1859 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001860 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001861 if (dentry->d_inode != NULL)
1862 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001863 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001864 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001865 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001866 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Andy Adamson82be4172012-05-23 05:02:35 -04001868 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1869 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1870 if (!opendata->f_attr.mdsthreshold)
1871 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001872 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001873 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001874 if (dentry->d_inode != NULL)
1875 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001876
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001877 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001879 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001881 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001882 status = PTR_ERR(state);
1883 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001884 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001885 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001886 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001887
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001888 status = nfs4_opendata_access(cred, opendata, state, fmode);
1889 if (status != 0)
1890 goto err_opendata_put;
1891
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001892 if (opendata->o_arg.open_flags & O_EXCL) {
1893 nfs4_exclusive_attrset(opendata, sattr);
1894
1895 nfs_fattr_init(opendata->o_res.f_attr);
1896 status = nfs4_do_setattr(state->inode, cred,
1897 opendata->o_res.f_attr, sattr,
1898 state);
1899 if (status == 0)
1900 nfs_setattr_update_inode(state->inode, sattr);
1901 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1902 }
Andy Adamson82be4172012-05-23 05:02:35 -04001903
1904 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1905 *ctx_th = opendata->f_attr.mdsthreshold;
1906 else
1907 kfree(opendata->f_attr.mdsthreshold);
1908 opendata->f_attr.mdsthreshold = NULL;
1909
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001910 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 *res = state;
1913 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001914err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001915 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001916 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001917err_put_state_owner:
1918 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 *res = NULL;
1921 return status;
1922}
1923
1924
Andy Adamson82be4172012-05-23 05:02:35 -04001925static struct nfs4_state *nfs4_do_open(struct inode *dir,
1926 struct dentry *dentry,
1927 fmode_t fmode,
1928 int flags,
1929 struct iattr *sattr,
1930 struct rpc_cred *cred,
1931 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 struct nfs4_exception exception = { };
1934 struct nfs4_state *res;
1935 int status;
1936
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001937 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001939 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1940 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (status == 0)
1942 break;
1943 /* NOTE: BAD_SEQID means the server and client disagree about the
1944 * book-keeping w.r.t. state-changing operations
1945 * (OPEN/CLOSE/LOCK/LOCKU...)
1946 * It is actually a sign of a bug on the client or on the server.
1947 *
1948 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001949 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 * have unhashed the old state_owner for us, and that we can
1951 * therefore safely retry using a new one. We should still warn
1952 * the user though...
1953 */
1954 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001955 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001956 " returned a bad sequence-id error!\n",
1957 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 exception.retry = 1;
1959 continue;
1960 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001961 /*
1962 * BAD_STATEID on OPEN means that the server cancelled our
1963 * state before it received the OPEN_CONFIRM.
1964 * Recover by retrying the request as per the discussion
1965 * on Page 181 of RFC3530.
1966 */
1967 if (status == -NFS4ERR_BAD_STATEID) {
1968 exception.retry = 1;
1969 continue;
1970 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001971 if (status == -EAGAIN) {
1972 /* We must have found a delegation */
1973 exception.retry = 1;
1974 continue;
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1977 status, &exception));
1978 } while (exception.retry);
1979 return res;
1980}
1981
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001982static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1983 struct nfs_fattr *fattr, struct iattr *sattr,
1984 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001986 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001988 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 .iap = sattr,
1990 .server = server,
1991 .bitmask = server->attr_bitmask,
1992 };
1993 struct nfs_setattrres res = {
1994 .fattr = fattr,
1995 .server = server,
1996 };
1997 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1999 .rpc_argp = &arg,
2000 .rpc_resp = &res,
2001 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002003 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002004 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Trond Myklebust0e574af2005-10-27 22:12:38 -04002006 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Trond Myklebust4fc87962012-03-08 17:42:01 -05002008 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002009 struct nfs_lockowner lockowner = {
2010 .l_owner = current->files,
2011 .l_pid = current->tgid,
2012 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002013 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002014 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002015 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2016 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002017 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002018 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002019 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Bryan Schumaker7c513052011-03-24 17:12:24 +00002021 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002022 if (status == 0 && state != NULL)
2023 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002024 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002027static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2028 struct nfs_fattr *fattr, struct iattr *sattr,
2029 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002031 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002032 struct nfs4_exception exception = {
2033 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002034 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002035 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int err;
2037 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002038 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2039 switch (err) {
2040 case -NFS4ERR_OPENMODE:
2041 if (state && !(state->state & FMODE_WRITE)) {
2042 err = -EBADF;
2043 if (sattr->ia_valid & ATTR_OPEN)
2044 err = -EACCES;
2045 goto out;
2046 }
2047 }
2048 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002050out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return err;
2052}
2053
2054struct nfs4_closedata {
2055 struct inode *inode;
2056 struct nfs4_state *state;
2057 struct nfs_closeargs arg;
2058 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002059 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002060 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002061 bool roc;
2062 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063};
2064
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002065static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002066{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002067 struct nfs4_closedata *calldata = data;
2068 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002069 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002070
Fred Isamanf7e89172011-01-06 11:36:32 +00002071 if (calldata->roc)
2072 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002073 nfs4_put_open_state(calldata->state);
2074 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002075 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002076 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002077 kfree(calldata);
2078}
2079
Trond Myklebust88069f72009-12-08 08:33:16 -05002080static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2081 fmode_t fmode)
2082{
2083 spin_lock(&state->owner->so_lock);
2084 if (!(fmode & FMODE_READ))
2085 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2086 if (!(fmode & FMODE_WRITE))
2087 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2088 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2089 spin_unlock(&state->owner->so_lock);
2090}
2091
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002092static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002094 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct nfs_server *server = NFS_SERVER(calldata->inode);
2097
Chuck Levera3ca5652012-03-01 17:00:40 -05002098 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002099 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* hmm. we are done with the inode, and in the process of freeing
2102 * the state_owner. we keep this around to process errors
2103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 switch (task->tk_status) {
2105 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002106 if (calldata->roc)
2107 pnfs_roc_set_barrier(state->inode,
2108 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002109 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002110 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002111 nfs4_close_clear_stateid_flags(state,
2112 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 break;
2114 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002115 case -NFS4ERR_OLD_STATEID:
2116 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002118 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002121 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2122 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002124 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002125 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002126 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002129static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002131 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002132 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002133 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002134 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002135
Chuck Levera3ca5652012-03-01 17:00:40 -05002136 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002137 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002138 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002139
Trond Myklebust88069f72009-12-08 08:33:16 -05002140 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2141 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002142 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002143 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002144 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002145 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002146 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2147 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2148 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002149 }
2150 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002151 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2152 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2153 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002154 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002155 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002156 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002157
2158 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002159 /* Note: exit _without_ calling nfs4_close_done */
2160 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002161 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002162 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002163
Fred Isamanf7e89172011-01-06 11:36:32 +00002164 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002165 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002166 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002167 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002168 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002169 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002170
Trond Myklebust516a6af2005-10-27 22:12:41 -04002171 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002172 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002173 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002174 &calldata->arg.seq_args,
2175 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002176 task) != 0)
2177 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002178out:
2179 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002182static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002183 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002184 .rpc_call_done = nfs4_close_done,
2185 .rpc_release = nfs4_free_closedata,
2186};
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/*
2189 * It is possible for data to be read/written from a mem-mapped file
2190 * after the sys_close call (which hits the vfs layer as a flush).
2191 * This means that we can't safely call nfsv4 close on a file until
2192 * the inode is cleared. This in turn means that we are not good
2193 * NFSv4 citizens - we do not indicate to the server to update the file's
2194 * share state even when we are done with one of the three share
2195 * stateid's in the inode.
2196 *
2197 * NOTE: Caller must be holding the sp->so_owner semaphore!
2198 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002199int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002201 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002203 struct nfs4_state_owner *sp = state->owner;
2204 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002205 struct rpc_message msg = {
2206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2207 .rpc_cred = state->owner->so_cred,
2208 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002209 struct rpc_task_setup task_setup_data = {
2210 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002211 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002212 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002213 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002214 .flags = RPC_TASK_ASYNC,
2215 };
Trond Myklebust95121352005-10-18 14:20:12 -07002216 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002218 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002220 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002221 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002222 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002224 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002225 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002227 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002228 if (calldata->arg.seqid == NULL)
2229 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002230 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002231 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002232 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002233 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002234 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002235 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002236 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002237
Trond Myklebust1174dd12010-12-21 10:52:24 -05002238 msg.rpc_argp = &calldata->arg;
2239 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002240 task_setup_data.callback_data = calldata;
2241 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002242 if (IS_ERR(task))
2243 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002244 status = 0;
2245 if (wait)
2246 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002247 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002248 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002249out_free_calldata:
2250 kfree(calldata);
2251out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002252 nfs4_put_open_state(state);
2253 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002254 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Trond Myklebust2b484292010-09-17 10:56:51 -04002257static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002258nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 struct nfs4_state *state;
2261
Trond Myklebust565277f2007-10-15 18:17:53 -04002262 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002263 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2264 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002265 if (IS_ERR(state))
2266 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002267 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002268 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Trond Myklebust1185a552009-12-03 15:54:02 -05002271static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002272{
2273 if (ctx->state == NULL)
2274 return;
2275 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002276 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002277 else
Al Viro643168c2011-06-22 18:20:23 -04002278 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002279}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2282{
Benny Halevy43652ad2009-04-01 09:21:54 -04002283 struct nfs4_server_caps_arg args = {
2284 .fhandle = fhandle,
2285 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 struct nfs4_server_caps_res res = {};
2287 struct rpc_message msg = {
2288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002289 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 .rpc_resp = &res,
2291 };
2292 int status;
2293
Bryan Schumaker7c513052011-03-24 17:12:24 +00002294 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (status == 0) {
2296 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002297 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2298 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2299 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2300 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2301 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2303 server->caps |= NFS_CAP_ACLS;
2304 if (res.has_links != 0)
2305 server->caps |= NFS_CAP_HARDLINKS;
2306 if (res.has_symlinks != 0)
2307 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002308 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2309 server->caps |= NFS_CAP_FILEID;
2310 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2311 server->caps |= NFS_CAP_MODE;
2312 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2313 server->caps |= NFS_CAP_NLINK;
2314 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2315 server->caps |= NFS_CAP_OWNER;
2316 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2317 server->caps |= NFS_CAP_OWNER_GROUP;
2318 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2319 server->caps |= NFS_CAP_ATIME;
2320 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2321 server->caps |= NFS_CAP_CTIME;
2322 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2323 server->caps |= NFS_CAP_MTIME;
2324
Trond Myklebusta65318b2009-03-11 14:10:28 -04002325 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2326 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2327 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002329 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return status;
2333}
2334
Trond Myklebust55a97592006-06-09 09:34:19 -04002335int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 struct nfs4_exception exception = { };
2338 int err;
2339 do {
2340 err = nfs4_handle_exception(server,
2341 _nfs4_server_capabilities(server, fhandle),
2342 &exception);
2343 } while (exception.retry);
2344 return err;
2345}
2346
2347static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2348 struct nfs_fsinfo *info)
2349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct nfs4_lookup_root_arg args = {
2351 .bitmask = nfs4_fattr_bitmap,
2352 };
2353 struct nfs4_lookup_res res = {
2354 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002355 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 .fh = fhandle,
2357 };
2358 struct rpc_message msg = {
2359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2360 .rpc_argp = &args,
2361 .rpc_resp = &res,
2362 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002363
Trond Myklebust0e574af2005-10-27 22:12:38 -04002364 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002365 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
2368static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2369 struct nfs_fsinfo *info)
2370{
2371 struct nfs4_exception exception = { };
2372 int err;
2373 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002374 err = _nfs4_lookup_root(server, fhandle, info);
2375 switch (err) {
2376 case 0:
2377 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002378 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002379 default:
2380 err = nfs4_handle_exception(server, err, &exception);
2381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002383out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return err;
2385}
2386
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002387static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2388 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2389{
2390 struct rpc_auth *auth;
2391 int ret;
2392
2393 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002394 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002395 ret = -EIO;
2396 goto out;
2397 }
2398 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002399out:
2400 return ret;
2401}
2402
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002403static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002404 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002406 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002407 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002409 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002410 if (len < 0)
2411 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002412
2413 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002414 /* AUTH_UNIX is the default flavor if none was specified,
2415 * thus has already been tried. */
2416 if (flav_array[i] == RPC_AUTH_UNIX)
2417 continue;
2418
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002419 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002420 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002421 continue;
2422 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002423 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002424 /*
2425 * -EACCESS could mean that the user doesn't have correct permissions
2426 * to access the mount. It could also mean that we tried to mount
2427 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2428 * existing mount programs don't handle -EACCES very well so it should
2429 * be mapped to -EPERM instead.
2430 */
2431 if (status == -EACCES)
2432 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002433 return status;
2434}
2435
2436/*
2437 * get the file handle for the "/" directory on the server
2438 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002439int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2440 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002441{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002442 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002443 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002444 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2445 /*
2446 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2447 * by nfs4_map_errors() as this function exits.
2448 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002449 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (status == 0)
2451 status = nfs4_server_capabilities(server, fhandle);
2452 if (status == 0)
2453 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002454 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Bryan Schumakerbae36242012-05-10 15:07:31 -04002457static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2458 struct nfs_fsinfo *info)
2459{
2460 int error;
2461 struct nfs_fattr *fattr = info->fattr;
2462
2463 error = nfs4_server_capabilities(server, mntfh);
2464 if (error < 0) {
2465 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2466 return error;
2467 }
2468
2469 error = nfs4_proc_getattr(server, mntfh, fattr);
2470 if (error < 0) {
2471 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2472 return error;
2473 }
2474
2475 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2476 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2477 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2478
2479 return error;
2480}
2481
Manoj Naik6b97fd32006-06-09 09:34:29 -04002482/*
2483 * Get locations and (maybe) other attributes of a referral.
2484 * Note that we'll actually follow the referral later when
2485 * we detect fsid mismatch in inode revalidation
2486 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002487static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2488 const struct qstr *name, struct nfs_fattr *fattr,
2489 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002490{
2491 int status = -ENOMEM;
2492 struct page *page = NULL;
2493 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002494
2495 page = alloc_page(GFP_KERNEL);
2496 if (page == NULL)
2497 goto out;
2498 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2499 if (locations == NULL)
2500 goto out;
2501
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002502 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002503 if (status != 0)
2504 goto out;
2505 /* Make sure server returned a different fsid for the referral */
2506 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002507 dprintk("%s: server did not return a different fsid for"
2508 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002509 status = -EIO;
2510 goto out;
2511 }
Andy Adamson533eb462011-06-13 18:25:56 -04002512 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2513 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002514
Andy Adamson533eb462011-06-13 18:25:56 -04002515 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002516 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002517 memset(fhandle, 0, sizeof(struct nfs_fh));
2518out:
2519 if (page)
2520 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002521 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002522 return status;
2523}
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2526{
2527 struct nfs4_getattr_arg args = {
2528 .fh = fhandle,
2529 .bitmask = server->attr_bitmask,
2530 };
2531 struct nfs4_getattr_res res = {
2532 .fattr = fattr,
2533 .server = server,
2534 };
2535 struct rpc_message msg = {
2536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2537 .rpc_argp = &args,
2538 .rpc_resp = &res,
2539 };
2540
Trond Myklebust0e574af2005-10-27 22:12:38 -04002541 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002542 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
2545static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2546{
2547 struct nfs4_exception exception = { };
2548 int err;
2549 do {
2550 err = nfs4_handle_exception(server,
2551 _nfs4_proc_getattr(server, fhandle, fattr),
2552 &exception);
2553 } while (exception.retry);
2554 return err;
2555}
2556
2557/*
2558 * The file is not closed if it is opened due to the a request to change
2559 * the size of the file. The open call will not be needed once the
2560 * VFS layer lookup-intents are implemented.
2561 *
2562 * Close is called when the inode is destroyed.
2563 * If we haven't opened the file for O_WRONLY, we
2564 * need to in the size_change case to obtain a stateid.
2565 *
2566 * Got race?
2567 * Because OPEN is always done by name in nfsv4, it is
2568 * possible that we opened a different file by the same
2569 * name. We can recognize this race condition, but we
2570 * can't do anything about it besides returning an error.
2571 *
2572 * This will be fixed with VFS changes (lookup-intent).
2573 */
2574static int
2575nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2576 struct iattr *sattr)
2577{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002578 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002579 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002580 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 int status;
2582
Benny Halevy8a1636c2010-07-14 15:43:57 -04002583 if (pnfs_ld_layoutret_on_setattr(inode))
2584 pnfs_return_layout(inode);
2585
Trond Myklebust0e574af2005-10-27 22:12:38 -04002586 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Andy Adamson26699402012-05-30 16:12:24 -04002588 /* Deal with open(O_TRUNC) */
2589 if (sattr->ia_valid & ATTR_OPEN)
2590 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2591
2592 /* Optimization: if the end result is no change, don't RPC */
2593 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2594 return 0;
2595
Trond Myklebustd5308382005-11-04 15:33:38 -05002596 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002597 if (sattr->ia_valid & ATTR_FILE) {
2598 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002599
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002600 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002601 if (ctx) {
2602 cred = ctx->cred;
2603 state = ctx->state;
2604 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002605 }
2606
2607 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002608 if (status == 0)
2609 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return status;
2611}
2612
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002613static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2614 const struct qstr *name, struct nfs_fh *fhandle,
2615 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002616{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002617 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002618 int status;
2619 struct nfs4_lookup_arg args = {
2620 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002621 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002622 .name = name,
2623 };
2624 struct nfs4_lookup_res res = {
2625 .server = server,
2626 .fattr = fattr,
2627 .fh = fhandle,
2628 };
2629 struct rpc_message msg = {
2630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2631 .rpc_argp = &args,
2632 .rpc_resp = &res,
2633 };
2634
2635 nfs_fattr_init(fattr);
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002638 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 dprintk("NFS reply lookup: %d\n", status);
2640 return status;
2641}
2642
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002643static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002644{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002645 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002646 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002647 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2648 fattr->nlink = 2;
2649}
2650
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002651static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2652 struct qstr *name, struct nfs_fh *fhandle,
2653 struct nfs_fattr *fattr)
2654{
2655 struct nfs4_exception exception = { };
2656 struct rpc_clnt *client = *clnt;
2657 int err;
2658 do {
2659 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2660 switch (err) {
2661 case -NFS4ERR_BADNAME:
2662 err = -ENOENT;
2663 goto out;
2664 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002665 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002666 goto out;
2667 case -NFS4ERR_WRONGSEC:
2668 err = -EPERM;
2669 if (client != *clnt)
2670 goto out;
2671
2672 client = nfs4_create_sec_client(client, dir, name);
2673 if (IS_ERR(client))
2674 return PTR_ERR(client);
2675
2676 exception.retry = 1;
2677 break;
2678 default:
2679 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2680 }
2681 } while (exception.retry);
2682
2683out:
2684 if (err == 0)
2685 *clnt = client;
2686 else if (client != *clnt)
2687 rpc_shutdown_client(client);
2688
2689 return err;
2690}
2691
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002692static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002693 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002695 int status;
2696 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002697
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002698 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2699 if (client != NFS_CLIENT(dir)) {
2700 rpc_shutdown_client(client);
2701 nfs_fixup_secinfo_attributes(fattr);
2702 }
2703 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002706struct rpc_clnt *
2707nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2708 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2709{
2710 int status;
2711 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2712
2713 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2714 if (status < 0) {
2715 rpc_shutdown_client(client);
2716 return ERR_PTR(status);
2717 }
2718 return client;
2719}
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2722{
Trond Myklebust76b32992007-08-10 17:45:11 -04002723 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 struct nfs4_accessargs args = {
2725 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002726 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002728 struct nfs4_accessres res = {
2729 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002730 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct rpc_message msg = {
2732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2733 .rpc_argp = &args,
2734 .rpc_resp = &res,
2735 .rpc_cred = entry->cred,
2736 };
2737 int mode = entry->mask;
2738 int status;
2739
2740 /*
2741 * Determine which access bits we want to ask for...
2742 */
2743 if (mode & MAY_READ)
2744 args.access |= NFS4_ACCESS_READ;
2745 if (S_ISDIR(inode->i_mode)) {
2746 if (mode & MAY_WRITE)
2747 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2748 if (mode & MAY_EXEC)
2749 args.access |= NFS4_ACCESS_LOOKUP;
2750 } else {
2751 if (mode & MAY_WRITE)
2752 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2753 if (mode & MAY_EXEC)
2754 args.access |= NFS4_ACCESS_EXECUTE;
2755 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002756
2757 res.fattr = nfs_alloc_fattr();
2758 if (res.fattr == NULL)
2759 return -ENOMEM;
2760
Bryan Schumaker7c513052011-03-24 17:12:24 +00002761 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002763 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002764 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002766 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return status;
2768}
2769
2770static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2771{
2772 struct nfs4_exception exception = { };
2773 int err;
2774 do {
2775 err = nfs4_handle_exception(NFS_SERVER(inode),
2776 _nfs4_proc_access(inode, entry),
2777 &exception);
2778 } while (exception.retry);
2779 return err;
2780}
2781
2782/*
2783 * TODO: For the time being, we don't try to get any attributes
2784 * along with any of the zero-copy operations READ, READDIR,
2785 * READLINK, WRITE.
2786 *
2787 * In the case of the first three, we want to put the GETATTR
2788 * after the read-type operation -- this is because it is hard
2789 * to predict the length of a GETATTR response in v4, and thus
2790 * align the READ data correctly. This means that the GETATTR
2791 * may end up partially falling into the page cache, and we should
2792 * shift it into the 'tail' of the xdr_buf before processing.
2793 * To do this efficiently, we need to know the total length
2794 * of data received, which doesn't seem to be available outside
2795 * of the RPC layer.
2796 *
2797 * In the case of WRITE, we also want to put the GETATTR after
2798 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002799 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 *
2801 * Both of these changes to the XDR layer would in fact be quite
2802 * minor, but I decided to leave them for a subsequent patch.
2803 */
2804static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2805 unsigned int pgbase, unsigned int pglen)
2806{
2807 struct nfs4_readlink args = {
2808 .fh = NFS_FH(inode),
2809 .pgbase = pgbase,
2810 .pglen = pglen,
2811 .pages = &page,
2812 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002813 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 struct rpc_message msg = {
2815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2816 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002817 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 };
2819
Bryan Schumaker7c513052011-03-24 17:12:24 +00002820 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 -07002821}
2822
2823static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2824 unsigned int pgbase, unsigned int pglen)
2825{
2826 struct nfs4_exception exception = { };
2827 int err;
2828 do {
2829 err = nfs4_handle_exception(NFS_SERVER(inode),
2830 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2831 &exception);
2832 } while (exception.retry);
2833 return err;
2834}
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002837 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839static int
2840nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002841 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002843 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 int status = 0;
2846
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002847 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2848 if (IS_ERR(ctx))
2849 return PTR_ERR(ctx);
2850
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002851 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002852 state = nfs4_do_open(dir, dentry, ctx->mode,
2853 flags, sattr, ctx->cred,
2854 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002855 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (IS_ERR(state)) {
2857 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002860 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002861 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002862 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002864 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return status;
2866}
2867
2868static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2869{
Trond Myklebust16e42952005-10-27 22:12:44 -04002870 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002871 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002873 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002875 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002876 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002877 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2880 .rpc_argp = &args,
2881 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002883 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002884
Bryan Schumaker7c513052011-03-24 17:12:24 +00002885 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002886 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002887 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return status;
2889}
2890
2891static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2892{
2893 struct nfs4_exception exception = { };
2894 int err;
2895 do {
2896 err = nfs4_handle_exception(NFS_SERVER(dir),
2897 _nfs4_proc_remove(dir, name),
2898 &exception);
2899 } while (exception.retry);
2900 return err;
2901}
2902
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002903static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002905 struct nfs_server *server = NFS_SERVER(dir);
2906 struct nfs_removeargs *args = msg->rpc_argp;
2907 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002909 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002911 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002914static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2915{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002916 nfs4_setup_sequence(NFS_SERVER(data->dir),
2917 &data->args.seq_args,
2918 &data->res.seq_res,
2919 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002922static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002924 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2925
Trond Myklebust14516c32010-07-31 14:29:06 -04002926 if (!nfs4_sequence_done(task, &res->seq_res))
2927 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002928 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002929 return 0;
2930 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002931 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Jeff Laytond3d41522010-09-17 17:31:57 -04002934static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2935{
2936 struct nfs_server *server = NFS_SERVER(dir);
2937 struct nfs_renameargs *arg = msg->rpc_argp;
2938 struct nfs_renameres *res = msg->rpc_resp;
2939
2940 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002941 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002942 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002943}
2944
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002945static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2946{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002947 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2948 &data->args.seq_args,
2949 &data->res.seq_res,
2950 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002951}
2952
2953static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2954 struct inode *new_dir)
2955{
2956 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2957
2958 if (!nfs4_sequence_done(task, &res->seq_res))
2959 return 0;
2960 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2961 return 0;
2962
2963 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002964 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002965 return 1;
2966}
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2969 struct inode *new_dir, struct qstr *new_name)
2970{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002971 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002972 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 .old_dir = NFS_FH(old_dir),
2974 .new_dir = NFS_FH(new_dir),
2975 .old_name = old_name,
2976 .new_name = new_name,
2977 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002978 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002979 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002980 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 struct rpc_message msg = {
2982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2983 .rpc_argp = &arg,
2984 .rpc_resp = &res,
2985 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002986 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Bryan Schumaker7c513052011-03-24 17:12:24 +00002988 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (!status) {
2990 update_changeattr(old_dir, &res.old_cinfo);
2991 update_changeattr(new_dir, &res.new_cinfo);
2992 }
2993 return status;
2994}
2995
2996static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2997 struct inode *new_dir, struct qstr *new_name)
2998{
2999 struct nfs4_exception exception = { };
3000 int err;
3001 do {
3002 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3003 _nfs4_proc_rename(old_dir, old_name,
3004 new_dir, new_name),
3005 &exception);
3006 } while (exception.retry);
3007 return err;
3008}
3009
3010static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3011{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003012 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct nfs4_link_arg arg = {
3014 .fh = NFS_FH(inode),
3015 .dir_fh = NFS_FH(dir),
3016 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003017 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003019 struct nfs4_link_res res = {
3020 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003021 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 struct rpc_message msg = {
3023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3024 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003025 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003027 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Trond Myklebust136f2622010-04-16 16:22:49 -04003029 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003030 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003031 goto out;
3032
Bryan Schumaker7c513052011-03-24 17:12:24 +00003033 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003034 if (!status) {
3035 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003036 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003037 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003038out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003039 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return status;
3041}
3042
3043static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3044{
3045 struct nfs4_exception exception = { };
3046 int err;
3047 do {
3048 err = nfs4_handle_exception(NFS_SERVER(inode),
3049 _nfs4_proc_link(inode, dir, name),
3050 &exception);
3051 } while (exception.retry);
3052 return err;
3053}
3054
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003055struct nfs4_createdata {
3056 struct rpc_message msg;
3057 struct nfs4_create_arg arg;
3058 struct nfs4_create_res res;
3059 struct nfs_fh fh;
3060 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003061};
3062
3063static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3064 struct qstr *name, struct iattr *sattr, u32 ftype)
3065{
3066 struct nfs4_createdata *data;
3067
3068 data = kzalloc(sizeof(*data), GFP_KERNEL);
3069 if (data != NULL) {
3070 struct nfs_server *server = NFS_SERVER(dir);
3071
3072 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3073 data->msg.rpc_argp = &data->arg;
3074 data->msg.rpc_resp = &data->res;
3075 data->arg.dir_fh = NFS_FH(dir);
3076 data->arg.server = server;
3077 data->arg.name = name;
3078 data->arg.attrs = sattr;
3079 data->arg.ftype = ftype;
3080 data->arg.bitmask = server->attr_bitmask;
3081 data->res.server = server;
3082 data->res.fh = &data->fh;
3083 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003084 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003085 }
3086 return data;
3087}
3088
3089static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3090{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003091 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003092 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003093 if (status == 0) {
3094 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003095 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3096 }
3097 return status;
3098}
3099
3100static void nfs4_free_createdata(struct nfs4_createdata *data)
3101{
3102 kfree(data);
3103}
3104
Chuck Lever4f390c12006-08-22 20:06:22 -04003105static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003106 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003108 struct nfs4_createdata *data;
3109 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Chuck Lever94a6d752006-08-22 20:06:23 -04003111 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003113
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003114 status = -ENOMEM;
3115 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3116 if (data == NULL)
3117 goto out;
3118
3119 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3120 data->arg.u.symlink.pages = &page;
3121 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003123 status = nfs4_do_create(dir, dentry, data);
3124
3125 nfs4_free_createdata(data);
3126out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return status;
3128}
3129
Chuck Lever4f390c12006-08-22 20:06:22 -04003130static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003131 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
3133 struct nfs4_exception exception = { };
3134 int err;
3135 do {
3136 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003137 _nfs4_proc_symlink(dir, dentry, page,
3138 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 &exception);
3140 } while (exception.retry);
3141 return err;
3142}
3143
3144static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3145 struct iattr *sattr)
3146{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003147 struct nfs4_createdata *data;
3148 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003150 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3151 if (data == NULL)
3152 goto out;
3153
3154 status = nfs4_do_create(dir, dentry, data);
3155
3156 nfs4_free_createdata(data);
3157out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 return status;
3159}
3160
3161static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3162 struct iattr *sattr)
3163{
3164 struct nfs4_exception exception = { };
3165 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003166
3167 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 do {
3169 err = nfs4_handle_exception(NFS_SERVER(dir),
3170 _nfs4_proc_mkdir(dir, dentry, sattr),
3171 &exception);
3172 } while (exception.retry);
3173 return err;
3174}
3175
3176static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003177 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 struct inode *dir = dentry->d_inode;
3180 struct nfs4_readdir_arg args = {
3181 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003182 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 .pgbase = 0,
3184 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003185 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003186 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 };
3188 struct nfs4_readdir_res res;
3189 struct rpc_message msg = {
3190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3191 .rpc_argp = &args,
3192 .rpc_resp = &res,
3193 .rpc_cred = cred,
3194 };
3195 int status;
3196
Harvey Harrison3110ff82008-05-02 13:42:44 -07003197 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003198 dentry->d_parent->d_name.name,
3199 dentry->d_name.name,
3200 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003201 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003203 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 -05003204 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003205 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003206 status += args.pgbase;
3207 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003208
3209 nfs_invalidate_atime(dir);
3210
Harvey Harrison3110ff82008-05-02 13:42:44 -07003211 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return status;
3213}
3214
3215static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003216 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
3218 struct nfs4_exception exception = { };
3219 int err;
3220 do {
3221 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3222 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003223 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 &exception);
3225 } while (exception.retry);
3226 return err;
3227}
3228
3229static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3230 struct iattr *sattr, dev_t rdev)
3231{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003232 struct nfs4_createdata *data;
3233 int mode = sattr->ia_mode;
3234 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003236 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3237 if (data == NULL)
3238 goto out;
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003241 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243 data->arg.ftype = NF4BLK;
3244 data->arg.u.device.specdata1 = MAJOR(rdev);
3245 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
3247 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003248 data->arg.ftype = NF4CHR;
3249 data->arg.u.device.specdata1 = MAJOR(rdev);
3250 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003251 } else if (!S_ISSOCK(mode)) {
3252 status = -EINVAL;
3253 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003256 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003257out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003258 nfs4_free_createdata(data);
3259out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return status;
3261}
3262
3263static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3264 struct iattr *sattr, dev_t rdev)
3265{
3266 struct nfs4_exception exception = { };
3267 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003268
3269 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 do {
3271 err = nfs4_handle_exception(NFS_SERVER(dir),
3272 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3273 &exception);
3274 } while (exception.retry);
3275 return err;
3276}
3277
3278static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3279 struct nfs_fsstat *fsstat)
3280{
3281 struct nfs4_statfs_arg args = {
3282 .fh = fhandle,
3283 .bitmask = server->attr_bitmask,
3284 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003285 struct nfs4_statfs_res res = {
3286 .fsstat = fsstat,
3287 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 struct rpc_message msg = {
3289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3290 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003291 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 };
3293
Trond Myklebust0e574af2005-10-27 22:12:38 -04003294 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003295 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
3298static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3299{
3300 struct nfs4_exception exception = { };
3301 int err;
3302 do {
3303 err = nfs4_handle_exception(server,
3304 _nfs4_proc_statfs(server, fhandle, fsstat),
3305 &exception);
3306 } while (exception.retry);
3307 return err;
3308}
3309
3310static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3311 struct nfs_fsinfo *fsinfo)
3312{
3313 struct nfs4_fsinfo_arg args = {
3314 .fh = fhandle,
3315 .bitmask = server->attr_bitmask,
3316 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003317 struct nfs4_fsinfo_res res = {
3318 .fsinfo = fsinfo,
3319 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 struct rpc_message msg = {
3321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3322 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003323 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 };
3325
Bryan Schumaker7c513052011-03-24 17:12:24 +00003326 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
3329static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3330{
3331 struct nfs4_exception exception = { };
3332 int err;
3333
3334 do {
3335 err = nfs4_handle_exception(server,
3336 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3337 &exception);
3338 } while (exception.retry);
3339 return err;
3340}
3341
3342static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3343{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003344 int error;
3345
Trond Myklebust0e574af2005-10-27 22:12:38 -04003346 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003347 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003348 if (error == 0) {
3349 /* block layout checks this! */
3350 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003351 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003352 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003353
3354 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
3357static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3358 struct nfs_pathconf *pathconf)
3359{
3360 struct nfs4_pathconf_arg args = {
3361 .fh = fhandle,
3362 .bitmask = server->attr_bitmask,
3363 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003364 struct nfs4_pathconf_res res = {
3365 .pathconf = pathconf,
3366 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 struct rpc_message msg = {
3368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3369 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003370 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 };
3372
3373 /* None of the pathconf attributes are mandatory to implement */
3374 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3375 memset(pathconf, 0, sizeof(*pathconf));
3376 return 0;
3377 }
3378
Trond Myklebust0e574af2005-10-27 22:12:38 -04003379 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003380 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
3383static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3384 struct nfs_pathconf *pathconf)
3385{
3386 struct nfs4_exception exception = { };
3387 int err;
3388
3389 do {
3390 err = nfs4_handle_exception(server,
3391 _nfs4_proc_pathconf(server, fhandle, pathconf),
3392 &exception);
3393 } while (exception.retry);
3394 return err;
3395}
3396
Benny Halevyd20581a2011-05-22 19:52:03 +03003397void __nfs4_read_done_cb(struct nfs_read_data *data)
3398{
Fred Isamancd841602012-04-20 14:47:44 -04003399 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003400}
3401
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003402static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Fred Isamancd841602012-04-20 14:47:44 -04003404 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003406 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003407 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003408 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003410
Benny Halevyd20581a2011-05-22 19:52:03 +03003411 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003413 renew_lease(server, data->timestamp);
3414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003417static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3418{
3419
3420 dprintk("--> %s\n", __func__);
3421
3422 if (!nfs4_sequence_done(task, &data->res.seq_res))
3423 return -EAGAIN;
3424
Benny Halevyd20581a2011-05-22 19:52:03 +03003425 return data->read_done_cb ? data->read_done_cb(task, data) :
3426 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003427}
3428
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003429static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003432 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003433 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003434 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003437static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3438{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003439 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3440 &data->args.seq_args,
3441 &data->res.seq_res,
3442 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443}
3444
Fred Isamanb029bc92011-03-03 15:13:42 +00003445static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Fred Isamancd841602012-04-20 14:47:44 -04003447 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003449 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003450 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003451 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003453 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003455 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003456 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Fred Isamanb029bc92011-03-03 15:13:42 +00003460static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3461{
3462 if (!nfs4_sequence_done(task, &data->res.seq_res))
3463 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003464 return data->write_done_cb ? data->write_done_cb(task, data) :
3465 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003466}
3467
Trond Myklebust5a37f852012-04-28 14:55:16 -04003468static
3469bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003470{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003471 const struct nfs_pgio_header *hdr = data->header;
3472
3473 /* Don't request attributes for pNFS or O_DIRECT writes */
3474 if (data->ds_clp != NULL || hdr->dreq != NULL)
3475 return false;
3476 /* Otherwise, request attributes if and only if we don't hold
3477 * a delegation
3478 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003479 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003480}
Fred Isamana69aef12011-03-03 15:13:47 +00003481
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003482static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Fred Isamancd841602012-04-20 14:47:44 -04003484 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003485
Trond Myklebust5a37f852012-04-28 14:55:16 -04003486 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003487 data->args.bitmask = NULL;
3488 data->res.fattr = NULL;
3489 } else
3490 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003491
Fred Isamanb029bc92011-03-03 15:13:42 +00003492 if (!data->write_done_cb)
3493 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003494 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 data->timestamp = jiffies;
3496
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003497 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003498 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003501static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3502{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003503 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3504 &data->args.seq_args,
3505 &data->res.seq_res,
3506 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
Fred Isaman0b7c0152012-04-20 14:47:39 -04003509static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3510{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003511 nfs4_setup_sequence(NFS_SERVER(data->inode),
3512 &data->args.seq_args,
3513 &data->res.seq_res,
3514 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Fred Isaman0b7c0152012-04-20 14:47:39 -04003517static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003520
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003521 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003522 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003523 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Fred Isaman0b7c0152012-04-20 14:47:39 -04003528static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003529{
3530 if (!nfs4_sequence_done(task, &data->res.seq_res))
3531 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003532 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003533}
3534
Fred Isaman0b7c0152012-04-20 14:47:39 -04003535static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003537 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003538
Fred Isaman0b7c0152012-04-20 14:47:39 -04003539 if (data->commit_done_cb == NULL)
3540 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003541 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003542 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003543 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003546struct nfs4_renewdata {
3547 struct nfs_client *client;
3548 unsigned long timestamp;
3549};
3550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551/*
3552 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3553 * standalone procedure for queueing an asynchronous RENEW.
3554 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003555static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003556{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003557 struct nfs4_renewdata *data = calldata;
3558 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003559
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003560 if (atomic_read(&clp->cl_count) > 1)
3561 nfs4_schedule_state_renewal(clp);
3562 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003563 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003564}
3565
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003566static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003568 struct nfs4_renewdata *data = calldata;
3569 struct nfs_client *clp = data->client;
3570 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003573 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003574 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3575 return;
3576 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003577 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003578 return;
3579 }
3580 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003582 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003585static const struct rpc_call_ops nfs4_renew_ops = {
3586 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003587 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003588};
3589
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003590static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
3592 struct rpc_message msg = {
3593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3594 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003595 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003597 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003599 if (renew_flags == 0)
3600 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003601 if (!atomic_inc_not_zero(&clp->cl_count))
3602 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003603 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003604 if (data == NULL)
3605 return -ENOMEM;
3606 data->client = clp;
3607 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003609 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003612static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 struct rpc_message msg = {
3615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3616 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003617 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 };
3619 unsigned long now = jiffies;
3620 int status;
3621
3622 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3623 if (status < 0)
3624 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003625 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 return 0;
3627}
3628
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003629static inline int nfs4_server_supports_acls(struct nfs_server *server)
3630{
3631 return (server->caps & NFS_CAP_ACLS)
3632 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3633 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3634}
3635
Trond Myklebust21f498c2012-08-24 10:59:25 -04003636/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3637 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003638 * the stack.
3639 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003640#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003641
Neil Hormane9e3d722011-03-04 19:26:03 -05003642static int buf_to_pages_noslab(const void *buf, size_t buflen,
3643 struct page **pages, unsigned int *pgbase)
3644{
3645 struct page *newpage, **spages;
3646 int rc = 0;
3647 size_t len;
3648 spages = pages;
3649
3650 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003651 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003652 newpage = alloc_page(GFP_KERNEL);
3653
3654 if (newpage == NULL)
3655 goto unwind;
3656 memcpy(page_address(newpage), buf, len);
3657 buf += len;
3658 buflen -= len;
3659 *pages++ = newpage;
3660 rc++;
3661 } while (buflen != 0);
3662
3663 return rc;
3664
3665unwind:
3666 for(; rc > 0; rc--)
3667 __free_page(spages[rc-1]);
3668 return -ENOMEM;
3669}
3670
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003671struct nfs4_cached_acl {
3672 int cached;
3673 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003674 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003675};
3676
3677static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003678{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003679 struct nfs_inode *nfsi = NFS_I(inode);
3680
3681 spin_lock(&inode->i_lock);
3682 kfree(nfsi->nfs4_acl);
3683 nfsi->nfs4_acl = acl;
3684 spin_unlock(&inode->i_lock);
3685}
3686
3687static void nfs4_zap_acl_attr(struct inode *inode)
3688{
3689 nfs4_set_cached_acl(inode, NULL);
3690}
3691
3692static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3693{
3694 struct nfs_inode *nfsi = NFS_I(inode);
3695 struct nfs4_cached_acl *acl;
3696 int ret = -ENOENT;
3697
3698 spin_lock(&inode->i_lock);
3699 acl = nfsi->nfs4_acl;
3700 if (acl == NULL)
3701 goto out;
3702 if (buf == NULL) /* user is just asking for length */
3703 goto out_len;
3704 if (acl->cached == 0)
3705 goto out;
3706 ret = -ERANGE; /* see getxattr(2) man page */
3707 if (acl->len > buflen)
3708 goto out;
3709 memcpy(buf, acl->data, acl->len);
3710out_len:
3711 ret = acl->len;
3712out:
3713 spin_unlock(&inode->i_lock);
3714 return ret;
3715}
3716
Sachin Prabhu5794d212012-04-17 14:36:40 +01003717static 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 +00003718{
3719 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003720 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003721
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003722 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003723 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003724 if (acl == NULL)
3725 goto out;
3726 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003727 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003728 } else {
3729 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3730 if (acl == NULL)
3731 goto out;
3732 acl->cached = 0;
3733 }
3734 acl->len = acl_len;
3735out:
3736 nfs4_set_cached_acl(inode, acl);
3737}
3738
Andy Adamsonbf118a32011-12-07 11:55:27 -05003739/*
3740 * The getxattr API returns the required buffer length when called with a
3741 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3742 * the required buf. On a NULL buf, we send a page of data to the server
3743 * guessing that the ACL request can be serviced by a page. If so, we cache
3744 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3745 * the cache. If not so, we throw away the page, and cache the required
3746 * length. The next getxattr call will then produce another round trip to
3747 * the server, this time with the input buf of the required size.
3748 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003749static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003750{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003751 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003752 struct nfs_getaclargs args = {
3753 .fh = NFS_FH(inode),
3754 .acl_pages = pages,
3755 .acl_len = buflen,
3756 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003757 struct nfs_getaclres res = {
3758 .acl_len = buflen,
3759 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003760 struct rpc_message msg = {
3761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3762 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003763 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003764 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003765 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3766 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003767
Andy Adamsonbf118a32011-12-07 11:55:27 -05003768 /* As long as we're doing a round trip to the server anyway,
3769 * let's be prepared for a page of acl data. */
3770 if (npages == 0)
3771 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003772 if (npages > ARRAY_SIZE(pages))
3773 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003774
Andy Adamsonbf118a32011-12-07 11:55:27 -05003775 for (i = 0; i < npages; i++) {
3776 pages[i] = alloc_page(GFP_KERNEL);
3777 if (!pages[i])
3778 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003779 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003780
3781 /* for decoding across pages */
3782 res.acl_scratch = alloc_page(GFP_KERNEL);
3783 if (!res.acl_scratch)
3784 goto out_free;
3785
Andy Adamsonbf118a32011-12-07 11:55:27 -05003786 args.acl_len = npages * PAGE_SIZE;
3787 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003788
Peng Taode040be2012-01-10 22:42:47 +08003789 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003790 __func__, buf, buflen, npages, args.acl_len);
3791 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3792 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003793 if (ret)
3794 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003795
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003796 /* Handle the case where the passed-in buffer is too short */
3797 if (res.acl_flags & NFS4_ACL_TRUNC) {
3798 /* Did the user only issue a request for the acl length? */
3799 if (buf == NULL)
3800 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003801 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003802 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003803 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003804 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3805 if (buf)
3806 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3807out_ok:
3808 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003809out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003810 for (i = 0; i < npages; i++)
3811 if (pages[i])
3812 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003813 if (res.acl_scratch)
3814 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 return ret;
3816}
3817
Trond Myklebust16b42892006-08-24 12:27:15 -04003818static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3819{
3820 struct nfs4_exception exception = { };
3821 ssize_t ret;
3822 do {
3823 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3824 if (ret >= 0)
3825 break;
3826 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3827 } while (exception.retry);
3828 return ret;
3829}
3830
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3832{
3833 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003834 int ret;
3835
3836 if (!nfs4_server_supports_acls(server))
3837 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003838 ret = nfs_revalidate_inode(server, inode);
3839 if (ret < 0)
3840 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003841 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3842 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003843 ret = nfs4_read_cached_acl(inode, buf, buflen);
3844 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003845 /* -ENOENT is returned if there is no ACL or if there is an ACL
3846 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003847 return ret;
3848 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003849}
3850
Trond Myklebust16b42892006-08-24 12:27:15 -04003851static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003852{
3853 struct nfs_server *server = NFS_SERVER(inode);
3854 struct page *pages[NFS4ACL_MAXPAGES];
3855 struct nfs_setaclargs arg = {
3856 .fh = NFS_FH(inode),
3857 .acl_pages = pages,
3858 .acl_len = buflen,
3859 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003860 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003861 struct rpc_message msg = {
3862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3863 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003864 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003865 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003866 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003867 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003868
3869 if (!nfs4_server_supports_acls(server))
3870 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003871 if (npages > ARRAY_SIZE(pages))
3872 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003873 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3874 if (i < 0)
3875 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003876 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003877 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003878
3879 /*
3880 * Free each page after tx, so the only ref left is
3881 * held by the network stack
3882 */
3883 for (; i > 0; i--)
3884 put_page(pages[i-1]);
3885
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003886 /*
3887 * Acl update can result in inode attribute update.
3888 * so mark the attribute cache invalid.
3889 */
3890 spin_lock(&inode->i_lock);
3891 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3892 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003893 nfs_access_zap_cache(inode);
3894 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003895 return ret;
3896}
3897
Trond Myklebust16b42892006-08-24 12:27:15 -04003898static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3899{
3900 struct nfs4_exception exception = { };
3901 int err;
3902 do {
3903 err = nfs4_handle_exception(NFS_SERVER(inode),
3904 __nfs4_proc_set_acl(inode, buf, buflen),
3905 &exception);
3906 } while (exception.retry);
3907 return err;
3908}
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003911nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003913 struct nfs_client *clp = server->nfs_client;
3914
3915 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 return 0;
3917 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003918 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003919 case -NFS4ERR_ADMIN_REVOKED:
3920 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003921 if (state == NULL)
3922 break;
3923 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003924 case -NFS4ERR_OPENMODE:
3925 if (state == NULL)
3926 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003927 nfs4_schedule_stateid_recovery(server, state);
3928 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003929 case -NFS4ERR_EXPIRED:
3930 if (state != NULL)
3931 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003933 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003934 nfs4_schedule_lease_recovery(clp);
3935 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003936#if defined(CONFIG_NFS_V4_1)
3937 case -NFS4ERR_BADSESSION:
3938 case -NFS4ERR_BADSLOT:
3939 case -NFS4ERR_BAD_HIGH_SLOT:
3940 case -NFS4ERR_DEADSESSION:
3941 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3942 case -NFS4ERR_SEQ_FALSE_RETRY:
3943 case -NFS4ERR_SEQ_MISORDERED:
3944 dprintk("%s ERROR %d, Reset session\n", __func__,
3945 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003946 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003947 task->tk_status = 0;
3948 return -EAGAIN;
3949#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003951 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003952 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003953 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3955 task->tk_status = 0;
3956 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003957 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 case -NFS4ERR_OLD_STATEID:
3959 task->tk_status = 0;
3960 return -EAGAIN;
3961 }
3962 task->tk_status = nfs4_map_errors(task->tk_status);
3963 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003964wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003965 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003966 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3967 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3968 task->tk_status = 0;
3969 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
Chuck Leverf0920752012-05-21 22:45:41 -04003972static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3973 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003974{
3975 __be32 verf[2];
3976
Chuck Lever2c820d92012-05-21 22:45:33 -04003977 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3978 /* An impossible timestamp guarantees this value
3979 * will never match a generated boot time. */
3980 verf[0] = 0;
3981 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3982 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003983 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3984 verf[0] = (__be32)nn->boot_time.tv_sec;
3985 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003986 }
Chuck Levercd937102012-03-02 17:14:31 -05003987 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3988}
3989
Chuck Levere984a552012-09-14 17:24:21 -04003990static unsigned int
3991nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
3992 char *buf, size_t len)
3993{
3994 unsigned int result;
3995
3996 rcu_read_lock();
3997 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
3998 clp->cl_ipaddr,
3999 rpc_peeraddr2str(clp->cl_rpcclient,
4000 RPC_DISPLAY_ADDR),
4001 rpc_peeraddr2str(clp->cl_rpcclient,
4002 RPC_DISPLAY_PROTO));
4003 rcu_read_unlock();
4004 return result;
4005}
4006
4007static unsigned int
4008nfs4_init_uniform_client_string(const struct nfs_client *clp,
4009 char *buf, size_t len)
4010{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004011 char *nodename = clp->cl_rpcclient->cl_nodename;
4012
4013 if (nfs4_client_id_uniquifier[0] != '\0')
4014 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004015 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4016 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004017 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004018}
4019
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004020/**
4021 * nfs4_proc_setclientid - Negotiate client ID
4022 * @clp: state data structure
4023 * @program: RPC program for NFSv4 callback service
4024 * @port: IP port number for NFS4 callback service
4025 * @cred: RPC credential to use for this call
4026 * @res: where to place the result
4027 *
4028 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4029 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004030int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4031 unsigned short port, struct rpc_cred *cred,
4032 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
4034 nfs4_verifier sc_verifier;
4035 struct nfs4_setclientid setclientid = {
4036 .sc_verifier = &sc_verifier,
4037 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004038 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 };
4040 struct rpc_message msg = {
4041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4042 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004043 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004044 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004046 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Chuck Leverde734832012-07-11 16:30:50 -04004048 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004049 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004050 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4051 setclientid.sc_name_len =
4052 nfs4_init_uniform_client_string(clp,
4053 setclientid.sc_name,
4054 sizeof(setclientid.sc_name));
4055 else
4056 setclientid.sc_name_len =
4057 nfs4_init_nonuniform_client_string(clp,
4058 setclientid.sc_name,
4059 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004060 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004061 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004062 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004063 sizeof(setclientid.sc_netid),
4064 rpc_peeraddr2str(clp->cl_rpcclient,
4065 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004066 rcu_read_unlock();
4067 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004068 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 clp->cl_ipaddr, port >> 8, port & 255);
4070
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004071 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4072 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4073 setclientid.sc_name_len, setclientid.sc_name);
4074 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4075 dprintk("NFS reply setclientid: %d\n", status);
4076 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004079/**
4080 * nfs4_proc_setclientid_confirm - Confirm client ID
4081 * @clp: state data structure
4082 * @res: result of a previous SETCLIENTID
4083 * @cred: RPC credential to use for this call
4084 *
4085 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4086 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004087int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004088 struct nfs4_setclientid_res *arg,
4089 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
4091 struct nfs_fsinfo fsinfo;
4092 struct rpc_message msg = {
4093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004094 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004096 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 };
4098 unsigned long now;
4099 int status;
4100
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004101 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4102 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4103 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004105 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 if (status == 0) {
4107 spin_lock(&clp->cl_lock);
4108 clp->cl_lease_time = fsinfo.lease_time * HZ;
4109 clp->cl_last_renewal = now;
4110 spin_unlock(&clp->cl_lock);
4111 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004112 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return status;
4114}
4115
Trond Myklebustfe650402006-01-03 09:55:18 +01004116struct nfs4_delegreturndata {
4117 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004118 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004119 struct nfs_fh fh;
4120 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004121 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004122 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004123 int rpc_status;
4124};
4125
Trond Myklebustfe650402006-01-03 09:55:18 +01004126static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4127{
4128 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004129
Trond Myklebust14516c32010-07-31 14:29:06 -04004130 if (!nfs4_sequence_done(task, &data->res.seq_res))
4131 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004132
Ricardo Labiaga79708862009-12-07 09:23:21 -05004133 switch (task->tk_status) {
4134 case -NFS4ERR_STALE_STATEID:
4135 case -NFS4ERR_EXPIRED:
4136 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004137 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004138 break;
4139 default:
4140 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4141 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004142 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004143 return;
4144 }
4145 }
4146 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004147}
4148
4149static void nfs4_delegreturn_release(void *calldata)
4150{
Trond Myklebustfe650402006-01-03 09:55:18 +01004151 kfree(calldata);
4152}
4153
Andy Adamson938e1012009-04-01 09:22:28 -04004154#if defined(CONFIG_NFS_V4_1)
4155static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4156{
4157 struct nfs4_delegreturndata *d_data;
4158
4159 d_data = (struct nfs4_delegreturndata *)data;
4160
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004161 nfs4_setup_sequence(d_data->res.server,
4162 &d_data->args.seq_args,
4163 &d_data->res.seq_res,
4164 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004165}
4166#endif /* CONFIG_NFS_V4_1 */
4167
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004168static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004169#if defined(CONFIG_NFS_V4_1)
4170 .rpc_call_prepare = nfs4_delegreturn_prepare,
4171#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004172 .rpc_call_done = nfs4_delegreturn_done,
4173 .rpc_release = nfs4_delegreturn_release,
4174};
4175
Trond Myklebuste6f81072008-01-24 18:14:34 -05004176static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004177{
4178 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004179 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004180 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004181 struct rpc_message msg = {
4182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4183 .rpc_cred = cred,
4184 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004185 struct rpc_task_setup task_setup_data = {
4186 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004187 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004188 .callback_ops = &nfs4_delegreturn_ops,
4189 .flags = RPC_TASK_ASYNC,
4190 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004191 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004192
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004193 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004194 if (data == NULL)
4195 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004196 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004197 data->args.fhandle = &data->fh;
4198 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004199 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004200 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004201 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004202 data->res.fattr = &data->fattr;
4203 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004204 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004205 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004206 data->rpc_status = 0;
4207
Trond Myklebustc970aa82007-07-14 15:39:59 -04004208 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004209 msg.rpc_argp = &data->args;
4210 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004211 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004212 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004213 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004214 if (!issync)
4215 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004216 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004217 if (status != 0)
4218 goto out;
4219 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004220 if (status == 0)
4221 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4222 else
4223 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004224out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004225 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004226 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227}
4228
Trond Myklebuste6f81072008-01-24 18:14:34 -05004229int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230{
4231 struct nfs_server *server = NFS_SERVER(inode);
4232 struct nfs4_exception exception = { };
4233 int err;
4234 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004235 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 switch (err) {
4237 case -NFS4ERR_STALE_STATEID:
4238 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 case 0:
4240 return 0;
4241 }
4242 err = nfs4_handle_exception(server, err, &exception);
4243 } while (exception.retry);
4244 return err;
4245}
4246
4247#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4248#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4249
4250/*
4251 * sleep, with exponential backoff, and retry the LOCK operation.
4252 */
4253static unsigned long
4254nfs4_set_lock_task_retry(unsigned long timeout)
4255{
Jeff Laytond3103102011-12-01 22:44:39 +01004256 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 timeout <<= 1;
4258 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4259 return NFS4_LOCK_MAXTIMEOUT;
4260 return timeout;
4261}
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4264{
4265 struct inode *inode = state->inode;
4266 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004267 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004268 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004270 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004272 struct nfs_lockt_res res = {
4273 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 };
4275 struct rpc_message msg = {
4276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4277 .rpc_argp = &arg,
4278 .rpc_resp = &res,
4279 .rpc_cred = state->owner->so_cred,
4280 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 struct nfs4_lock_state *lsp;
4282 int status;
4283
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004284 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004285 status = nfs4_set_lock_state(state, request);
4286 if (status != 0)
4287 goto out;
4288 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004289 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004290 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004291 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004292 switch (status) {
4293 case 0:
4294 request->fl_type = F_UNLCK;
4295 break;
4296 case -NFS4ERR_DENIED:
4297 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004299 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004300out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return status;
4302}
4303
4304static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4305{
4306 struct nfs4_exception exception = { };
4307 int err;
4308
4309 do {
4310 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4311 _nfs4_proc_getlk(state, cmd, request),
4312 &exception);
4313 } while (exception.retry);
4314 return err;
4315}
4316
4317static int do_vfs_lock(struct file *file, struct file_lock *fl)
4318{
4319 int res = 0;
4320 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4321 case FL_POSIX:
4322 res = posix_lock_file_wait(file, fl);
4323 break;
4324 case FL_FLOCK:
4325 res = flock_lock_file_wait(file, fl);
4326 break;
4327 default:
4328 BUG();
4329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return res;
4331}
4332
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004333struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004334 struct nfs_locku_args arg;
4335 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004336 struct nfs4_lock_state *lsp;
4337 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004338 struct file_lock fl;
4339 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004340 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004341};
4342
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004343static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4344 struct nfs_open_context *ctx,
4345 struct nfs4_lock_state *lsp,
4346 struct nfs_seqid *seqid)
4347{
4348 struct nfs4_unlockdata *p;
4349 struct inode *inode = lsp->ls_state->inode;
4350
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004351 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004352 if (p == NULL)
4353 return NULL;
4354 p->arg.fh = NFS_FH(inode);
4355 p->arg.fl = &p->fl;
4356 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004357 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004358 p->arg.stateid = &lsp->ls_stateid;
4359 p->lsp = lsp;
4360 atomic_inc(&lsp->ls_count);
4361 /* Ensure we don't close file until we're done freeing locks! */
4362 p->ctx = get_nfs_open_context(ctx);
4363 memcpy(&p->fl, fl, sizeof(p->fl));
4364 p->server = NFS_SERVER(inode);
4365 return p;
4366}
4367
Trond Myklebust06f814a2006-01-03 09:55:07 +01004368static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004369{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004370 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004371 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004372 nfs4_put_lock_state(calldata->lsp);
4373 put_nfs_open_context(calldata->ctx);
4374 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004375}
4376
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004377static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004378{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004379 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004380
Trond Myklebust14516c32010-07-31 14:29:06 -04004381 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4382 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004383 switch (task->tk_status) {
4384 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004385 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4386 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004387 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004388 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004389 case -NFS4ERR_BAD_STATEID:
4390 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391 case -NFS4ERR_STALE_STATEID:
4392 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393 break;
4394 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004395 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004396 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004397 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004398 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004399}
4400
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004401static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004402{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004403 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004405 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004407 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004408 /* Note: exit _without_ running nfs4_locku_done */
4409 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004410 return;
4411 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004412 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004413 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004414 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004415 &calldata->res.seq_res,
4416 task) != 0)
4417 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004420static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004421 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004422 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004423 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004424};
4425
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004426static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4427 struct nfs_open_context *ctx,
4428 struct nfs4_lock_state *lsp,
4429 struct nfs_seqid *seqid)
4430{
4431 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004432 struct rpc_message msg = {
4433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4434 .rpc_cred = ctx->cred,
4435 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004436 struct rpc_task_setup task_setup_data = {
4437 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004438 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004439 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004440 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004441 .flags = RPC_TASK_ASYNC,
4442 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443
Frank Filz137d6ac2007-07-09 15:32:29 -07004444 /* Ensure this is an unlock - when canceling a lock, the
4445 * canceled lock is passed in, and it won't be an unlock.
4446 */
4447 fl->fl_type = F_UNLCK;
4448
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4450 if (data == NULL) {
4451 nfs_free_seqid(seqid);
4452 return ERR_PTR(-ENOMEM);
4453 }
4454
Trond Myklebust9d12b212012-01-17 22:04:25 -05004455 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004456 msg.rpc_argp = &data->arg;
4457 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004458 task_setup_data.callback_data = data;
4459 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004460}
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4463{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004464 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004465 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004466 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004467 struct rpc_task *task;
4468 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004469 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Trond Myklebust9b073572006-06-29 16:38:34 -04004471 status = nfs4_set_lock_state(state, request);
4472 /* Unlock _before_ we do the RPC call */
4473 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004474 down_read(&nfsi->rwsem);
4475 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4476 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004477 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004478 }
4479 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004480 if (status != 0)
4481 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004482 /* Is this a delegated lock? */
4483 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004484 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004485 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004486 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004487 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004488 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004489 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004490 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004491 status = PTR_ERR(task);
4492 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004493 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004494 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004495 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004496out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004497 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004498 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499}
4500
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004501struct nfs4_lockdata {
4502 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004503 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004504 struct nfs4_lock_state *lsp;
4505 struct nfs_open_context *ctx;
4506 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004507 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508 int rpc_status;
4509 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004510 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004511};
4512
4513static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004514 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4515 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004516{
4517 struct nfs4_lockdata *p;
4518 struct inode *inode = lsp->ls_state->inode;
4519 struct nfs_server *server = NFS_SERVER(inode);
4520
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004521 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522 if (p == NULL)
4523 return NULL;
4524
4525 p->arg.fh = NFS_FH(inode);
4526 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004527 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004528 if (p->arg.open_seqid == NULL)
4529 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004530 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004531 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004532 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004534 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004535 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004536 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004537 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004539 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 atomic_inc(&lsp->ls_count);
4541 p->ctx = get_nfs_open_context(ctx);
4542 memcpy(&p->fl, fl, sizeof(p->fl));
4543 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004544out_free_seqid:
4545 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004546out_free:
4547 kfree(p);
4548 return NULL;
4549}
4550
4551static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4552{
4553 struct nfs4_lockdata *data = calldata;
4554 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Harvey Harrison3110ff82008-05-02 13:42:44 -07004556 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004557 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4558 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004559 /* Do we need to do an open_to_lock_owner? */
4560 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004561 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004562 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004563 data->arg.open_stateid = &state->stateid;
4564 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004565 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004566 } else
4567 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004568 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004569 if (nfs4_setup_sequence(data->server,
4570 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004571 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004572 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004573 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004574 nfs_release_seqid(data->arg.open_seqid);
4575out_release_lock_seqid:
4576 nfs_release_seqid(data->arg.lock_seqid);
4577 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578}
4579
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004580static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4581{
4582 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4583 nfs4_lock_prepare(task, calldata);
4584}
4585
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004586static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4587{
4588 struct nfs4_lockdata *data = calldata;
4589
Harvey Harrison3110ff82008-05-02 13:42:44 -07004590 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591
Trond Myklebust14516c32010-07-31 14:29:06 -04004592 if (!nfs4_sequence_done(task, &data->res.seq_res))
4593 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004594
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004596 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597 if (data->rpc_status == 0)
4598 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4599 else
4600 goto out;
4601 }
4602 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004603 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004604 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004605 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004607out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004608 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609}
4610
4611static void nfs4_lock_release(void *calldata)
4612{
4613 struct nfs4_lockdata *data = calldata;
4614
Harvey Harrison3110ff82008-05-02 13:42:44 -07004615 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004616 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004617 if (data->cancelled != 0) {
4618 struct rpc_task *task;
4619 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4620 data->arg.lock_seqid);
4621 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004622 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004623 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004624 } else
4625 nfs_free_seqid(data->arg.lock_seqid);
4626 nfs4_put_lock_state(data->lsp);
4627 put_nfs_open_context(data->ctx);
4628 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004629 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004630}
4631
4632static const struct rpc_call_ops nfs4_lock_ops = {
4633 .rpc_call_prepare = nfs4_lock_prepare,
4634 .rpc_call_done = nfs4_lock_done,
4635 .rpc_release = nfs4_lock_release,
4636};
4637
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004638static const struct rpc_call_ops nfs4_recover_lock_ops = {
4639 .rpc_call_prepare = nfs4_recover_lock_prepare,
4640 .rpc_call_done = nfs4_lock_done,
4641 .rpc_release = nfs4_lock_release,
4642};
4643
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004644static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4645{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004646 switch (error) {
4647 case -NFS4ERR_ADMIN_REVOKED:
4648 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004649 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004650 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004651 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004652 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004653 break;
4654 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004655 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004656 case -NFS4ERR_EXPIRED:
4657 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004658 };
4659}
4660
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004661static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004662{
4663 struct nfs4_lockdata *data;
4664 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004665 struct rpc_message msg = {
4666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4667 .rpc_cred = state->owner->so_cred,
4668 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004669 struct rpc_task_setup task_setup_data = {
4670 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004671 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004672 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004673 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004674 .flags = RPC_TASK_ASYNC,
4675 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004676 int ret;
4677
Harvey Harrison3110ff82008-05-02 13:42:44 -07004678 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004679 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004680 fl->fl_u.nfs4_fl.owner,
4681 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004682 if (data == NULL)
4683 return -ENOMEM;
4684 if (IS_SETLKW(cmd))
4685 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004686 if (recovery_type > NFS_LOCK_NEW) {
4687 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004688 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004689 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4690 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004691 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004692 msg.rpc_argp = &data->arg;
4693 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004694 task_setup_data.callback_data = data;
4695 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004696 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004697 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004698 ret = nfs4_wait_for_completion_rpc_task(task);
4699 if (ret == 0) {
4700 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004701 if (ret)
4702 nfs4_handle_setlk_error(data->server, data->lsp,
4703 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004704 } else
4705 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004706 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004707 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004708 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709}
4710
4711static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4712{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004713 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004714 struct nfs4_exception exception = {
4715 .inode = state->inode,
4716 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004717 int err;
4718
4719 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004720 /* Cache the lock if possible... */
4721 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4722 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004723 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004724 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004725 break;
4726 nfs4_handle_exception(server, err, &exception);
4727 } while (exception.retry);
4728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
4731static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4732{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004733 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004734 struct nfs4_exception exception = {
4735 .inode = state->inode,
4736 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004737 int err;
4738
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004739 err = nfs4_set_lock_state(state, request);
4740 if (err != 0)
4741 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004742 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004743 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4744 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004745 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004746 switch (err) {
4747 default:
4748 goto out;
4749 case -NFS4ERR_GRACE:
4750 case -NFS4ERR_DELAY:
4751 nfs4_handle_exception(server, err, &exception);
4752 err = 0;
4753 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004754 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004755out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757}
4758
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004759#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004760/**
4761 * nfs41_check_expired_locks - possibly free a lock stateid
4762 *
4763 * @state: NFSv4 state for an inode
4764 *
4765 * Returns NFS_OK if recovery for this stateid is now finished.
4766 * Otherwise a negative NFS4ERR value is returned.
4767 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004768static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004769{
Chuck Levereb64cf92012-07-11 16:30:05 -04004770 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004771 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004772 struct nfs_server *server = NFS_SERVER(state->inode);
4773
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004774 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004775 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004776 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4777 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004778 /* Free the stateid unless the server
4779 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004780 if (status != -NFS4ERR_BAD_STATEID)
4781 nfs41_free_stateid(server,
4782 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004783 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004784 ret = status;
4785 }
4786 }
4787 };
4788
4789 return ret;
4790}
4791
4792static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4793{
4794 int status = NFS_OK;
4795
4796 if (test_bit(LK_STATE_IN_USE, &state->flags))
4797 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004798 if (status != NFS_OK)
4799 status = nfs4_lock_expired(state, request);
4800 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004801}
4802#endif
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4805{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004806 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004807 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004808 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004810 if ((fl_flags & FL_POSIX) &&
4811 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4812 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004813 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004814 status = nfs4_set_lock_state(state, request);
4815 if (status != 0)
4816 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004817 request->fl_flags |= FL_ACCESS;
4818 status = do_vfs_lock(request->fl_file, request);
4819 if (status < 0)
4820 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004821 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004822 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004823 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004824 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004825 request->fl_flags = fl_flags & ~FL_SLEEP;
4826 status = do_vfs_lock(request->fl_file, request);
4827 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004828 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004829 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004830 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004831 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004832 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004833 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004834 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004835 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4836 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004837out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004838 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004839out:
4840 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 return status;
4842}
4843
4844static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4845{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004846 struct nfs4_exception exception = {
4847 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004848 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004849 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 int err;
4851
4852 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004853 err = _nfs4_proc_setlk(state, cmd, request);
4854 if (err == -NFS4ERR_DENIED)
4855 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004857 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 } while (exception.retry);
4859 return err;
4860}
4861
4862static int
4863nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4864{
4865 struct nfs_open_context *ctx;
4866 struct nfs4_state *state;
4867 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4868 int status;
4869
4870 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004871 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 state = ctx->state;
4873
4874 if (request->fl_start < 0 || request->fl_end < 0)
4875 return -EINVAL;
4876
Trond Myklebustd9531262009-07-21 19:22:38 -04004877 if (IS_GETLK(cmd)) {
4878 if (state != NULL)
4879 return nfs4_proc_getlk(state, F_GETLK, request);
4880 return 0;
4881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
4883 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4884 return -EINVAL;
4885
Trond Myklebustd9531262009-07-21 19:22:38 -04004886 if (request->fl_type == F_UNLCK) {
4887 if (state != NULL)
4888 return nfs4_proc_unlck(state, cmd, request);
4889 return 0;
4890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
Trond Myklebustd9531262009-07-21 19:22:38 -04004892 if (state == NULL)
4893 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004894 /*
4895 * Don't rely on the VFS having checked the file open mode,
4896 * since it won't do this for flock() locks.
4897 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004898 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004899 case F_RDLCK:
4900 if (!(filp->f_mode & FMODE_READ))
4901 return -EBADF;
4902 break;
4903 case F_WRLCK:
4904 if (!(filp->f_mode & FMODE_WRITE))
4905 return -EBADF;
4906 }
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 do {
4909 status = nfs4_proc_setlk(state, cmd, request);
4910 if ((status != -EAGAIN) || IS_SETLK(cmd))
4911 break;
4912 timeout = nfs4_set_lock_task_retry(timeout);
4913 status = -ERESTARTSYS;
4914 if (signalled())
4915 break;
4916 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 return status;
4918}
4919
Trond Myklebust888e6942005-11-04 15:38:11 -05004920int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4921{
4922 struct nfs_server *server = NFS_SERVER(state->inode);
4923 struct nfs4_exception exception = { };
4924 int err;
4925
4926 err = nfs4_set_lock_state(state, fl);
4927 if (err != 0)
4928 goto out;
4929 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004930 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004931 switch (err) {
4932 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004933 printk(KERN_ERR "NFS: %s: unhandled error "
4934 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004935 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004936 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004937 goto out;
4938 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004939 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004940 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004941 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004942 nfs4_schedule_lease_recovery(server->nfs_client);
4943 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004944 case -NFS4ERR_BADSESSION:
4945 case -NFS4ERR_BADSLOT:
4946 case -NFS4ERR_BAD_HIGH_SLOT:
4947 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4948 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004949 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004950 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004951 case -ERESTARTSYS:
4952 /*
4953 * The show must go on: exit, but mark the
4954 * stateid as needing recovery.
4955 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004956 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004957 case -NFS4ERR_ADMIN_REVOKED:
4958 case -NFS4ERR_BAD_STATEID:
4959 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004960 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004961 err = 0;
4962 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004963 case -EKEYEXPIRED:
4964 /*
4965 * User RPCSEC_GSS context has expired.
4966 * We cannot recover this stateid now, so
4967 * skip it and allow recovery thread to
4968 * proceed.
4969 */
4970 err = 0;
4971 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004972 case -ENOMEM:
4973 case -NFS4ERR_DENIED:
4974 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4975 err = 0;
4976 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004977 case -NFS4ERR_DELAY:
4978 break;
4979 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004980 err = nfs4_handle_exception(server, err, &exception);
4981 } while (exception.retry);
4982out:
4983 return err;
4984}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004985
Trond Myklebustcf470c32012-03-07 13:49:12 -05004986struct nfs_release_lockowner_data {
4987 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004988 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004989 struct nfs_release_lockowner_args args;
4990};
4991
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004992static void nfs4_release_lockowner_release(void *calldata)
4993{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004994 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004995 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004996 kfree(calldata);
4997}
4998
Trond Myklebust17280172012-03-11 13:11:00 -04004999static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005000 .rpc_release = nfs4_release_lockowner_release,
5001};
5002
Trond Myklebustcf470c32012-03-07 13:49:12 -05005003int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005004{
5005 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005006 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005007 struct rpc_message msg = {
5008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5009 };
5010
5011 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005012 return -EINVAL;
5013 data = kmalloc(sizeof(*data), GFP_NOFS);
5014 if (!data)
5015 return -ENOMEM;
5016 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005017 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005018 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5019 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5020 data->args.lock_owner.s_dev = server->s_dev;
5021 msg.rpc_argp = &data->args;
5022 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5023 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005024}
5025
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005026#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5027
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005028static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5029 const void *buf, size_t buflen,
5030 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005031{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005032 if (strcmp(key, "") != 0)
5033 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005034
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005035 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005036}
5037
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005038static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5039 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005040{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005041 if (strcmp(key, "") != 0)
5042 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005043
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005044 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005045}
5046
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005047static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5048 size_t list_len, const char *name,
5049 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005050{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005051 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005052
J. Bruce Fields096455a2006-03-20 23:23:42 -05005053 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5054 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005055
5056 if (list && len <= list_len)
5057 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005058 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005059}
5060
Andy Adamson533eb462011-06-13 18:25:56 -04005061/*
5062 * nfs_fhget will use either the mounted_on_fileid or the fileid
5063 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005064static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5065{
Andy Adamson533eb462011-06-13 18:25:56 -04005066 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5067 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5068 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005069 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005070 return;
5071
5072 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005073 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005074 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5075 fattr->nlink = 2;
5076}
5077
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005078static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5079 const struct qstr *name,
5080 struct nfs4_fs_locations *fs_locations,
5081 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005082{
5083 struct nfs_server *server = NFS_SERVER(dir);
5084 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005085 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005086 };
5087 struct nfs4_fs_locations_arg args = {
5088 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005089 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005090 .page = page,
5091 .bitmask = bitmask,
5092 };
Benny Halevy22958462009-04-01 09:22:02 -04005093 struct nfs4_fs_locations_res res = {
5094 .fs_locations = fs_locations,
5095 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005096 struct rpc_message msg = {
5097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5098 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005099 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005100 };
5101 int status;
5102
Harvey Harrison3110ff82008-05-02 13:42:44 -07005103 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005104
5105 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5106 * is not supported */
5107 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5108 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5109 else
5110 bitmask[0] |= FATTR4_WORD0_FILEID;
5111
Trond Myklebustc228fd32007-01-13 02:28:11 -05005112 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005113 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005114 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005115 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005116 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005117 return status;
5118}
5119
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005120int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5121 const struct qstr *name,
5122 struct nfs4_fs_locations *fs_locations,
5123 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005124{
5125 struct nfs4_exception exception = { };
5126 int err;
5127 do {
5128 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005129 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005130 &exception);
5131 } while (exception.retry);
5132 return err;
5133}
5134
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005135static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5136{
5137 int status;
5138 struct nfs4_secinfo_arg args = {
5139 .dir_fh = NFS_FH(dir),
5140 .name = name,
5141 };
5142 struct nfs4_secinfo_res res = {
5143 .flavors = flavors,
5144 };
5145 struct rpc_message msg = {
5146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5147 .rpc_argp = &args,
5148 .rpc_resp = &res,
5149 };
5150
5151 dprintk("NFS call secinfo %s\n", name->name);
5152 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5153 dprintk("NFS reply secinfo: %d\n", status);
5154 return status;
5155}
5156
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005157int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5158 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005159{
5160 struct nfs4_exception exception = { };
5161 int err;
5162 do {
5163 err = nfs4_handle_exception(NFS_SERVER(dir),
5164 _nfs4_proc_secinfo(dir, name, flavors),
5165 &exception);
5166 } while (exception.retry);
5167 return err;
5168}
5169
Andy Adamson557134a2009-04-01 09:21:53 -04005170#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005171/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005172 * Check the exchange flags returned by the server for invalid flags, having
5173 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5174 * DS flags set.
5175 */
5176static int nfs4_check_cl_exchange_flags(u32 flags)
5177{
5178 if (flags & ~EXCHGID4_FLAG_MASK_R)
5179 goto out_inval;
5180 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5181 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5182 goto out_inval;
5183 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5184 goto out_inval;
5185 return NFS_OK;
5186out_inval:
5187 return -NFS4ERR_INVAL;
5188}
5189
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005190static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005191nfs41_same_server_scope(struct nfs41_server_scope *a,
5192 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005193{
5194 if (a->server_scope_sz == b->server_scope_sz &&
5195 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5196 return true;
5197
5198 return false;
5199}
5200
Andy Adamson357f54d2010-12-14 10:11:57 -05005201/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005202 * nfs4_proc_bind_conn_to_session()
5203 *
5204 * The 4.1 client currently uses the same TCP connection for the
5205 * fore and backchannel.
5206 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005207int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005208{
5209 int status;
5210 struct nfs41_bind_conn_to_session_res res;
5211 struct rpc_message msg = {
5212 .rpc_proc =
5213 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5214 .rpc_argp = clp,
5215 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005216 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005217 };
5218
5219 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005220
5221 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5222 if (unlikely(res.session == NULL)) {
5223 status = -ENOMEM;
5224 goto out;
5225 }
5226
5227 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5228 if (status == 0) {
5229 if (memcmp(res.session->sess_id.data,
5230 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5231 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5232 status = -EIO;
5233 goto out_session;
5234 }
5235 if (res.dir != NFS4_CDFS4_BOTH) {
5236 dprintk("NFS: %s: Unexpected direction from server\n",
5237 __func__);
5238 status = -EIO;
5239 goto out_session;
5240 }
5241 if (res.use_conn_in_rdma_mode) {
5242 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5243 __func__);
5244 status = -EIO;
5245 goto out_session;
5246 }
5247 }
5248out_session:
5249 kfree(res.session);
5250out:
5251 dprintk("<-- %s status= %d\n", __func__, status);
5252 return status;
5253}
5254
5255/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005256 * nfs4_proc_exchange_id()
5257 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005258 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5259 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005260 * Since the clientid has expired, all compounds using sessions
5261 * associated with the stale clientid will be returning
5262 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5263 * be in some phase of session reset.
5264 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005265int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005266{
5267 nfs4_verifier verifier;
5268 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005269 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005270 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005271 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005272 };
5273 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005274 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005275 };
5276 int status;
5277 struct rpc_message msg = {
5278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5279 .rpc_argp = &args,
5280 .rpc_resp = &res,
5281 .rpc_cred = cred,
5282 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005283
Chuck Leverf0920752012-05-21 22:45:41 -04005284 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005285 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5286 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005287 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5288 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5289 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005290
Chuck Leveracdeb692012-05-21 22:46:16 -04005291 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005292 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005293 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005294 status = -ENOMEM;
5295 goto out;
5296 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005297
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005298 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005299 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005300 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005301 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005302 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005303 }
5304
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005305 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005306 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005307 status = -ENOMEM;
5308 goto out_server_scope;
5309 }
5310
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005311 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005312 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005313 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005314
Chuck Lever177313f2012-05-21 22:44:58 -04005315 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005316 clp->cl_clientid = res.clientid;
5317 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5318 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5319 clp->cl_seqid = res.seqid;
5320
Chuck Leveracdeb692012-05-21 22:46:16 -04005321 kfree(clp->cl_serverowner);
5322 clp->cl_serverowner = res.server_owner;
5323 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005324
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005325 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005326 kfree(clp->cl_implid);
5327 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005328
Chuck Lever177313f2012-05-21 22:44:58 -04005329 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005330 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005331 res.server_scope)) {
5332 dprintk("%s: server_scope mismatch detected\n",
5333 __func__);
5334 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005335 kfree(clp->cl_serverscope);
5336 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005337 }
5338
Chuck Lever177313f2012-05-21 22:44:58 -04005339 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005340 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005341 goto out;
5342 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005343 } else
5344 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005345
Chuck Leveracdeb692012-05-21 22:46:16 -04005346out_server_owner:
5347 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005348out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005349 kfree(res.server_scope);
5350out:
Chuck Lever177313f2012-05-21 22:44:58 -04005351 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005352 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005353 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005354 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005355 clp->cl_implid->date.seconds,
5356 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005357 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005358 return status;
5359}
5360
Trond Myklebust66245532012-05-25 17:18:09 -04005361static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5362 struct rpc_cred *cred)
5363{
5364 struct rpc_message msg = {
5365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5366 .rpc_argp = clp,
5367 .rpc_cred = cred,
5368 };
5369 int status;
5370
5371 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5372 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005373 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005374 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5375 return status;
5376}
5377
5378static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5379 struct rpc_cred *cred)
5380{
5381 unsigned int loop;
5382 int ret;
5383
5384 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5385 ret = _nfs4_proc_destroy_clientid(clp, cred);
5386 switch (ret) {
5387 case -NFS4ERR_DELAY:
5388 case -NFS4ERR_CLIENTID_BUSY:
5389 ssleep(1);
5390 break;
5391 default:
5392 return ret;
5393 }
5394 }
5395 return 0;
5396}
5397
5398int nfs4_destroy_clientid(struct nfs_client *clp)
5399{
5400 struct rpc_cred *cred;
5401 int ret = 0;
5402
5403 if (clp->cl_mvops->minor_version < 1)
5404 goto out;
5405 if (clp->cl_exchange_flags == 0)
5406 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005407 if (clp->cl_preserve_clid)
5408 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005409 cred = nfs4_get_exchange_id_cred(clp);
5410 ret = nfs4_proc_destroy_clientid(clp, cred);
5411 if (cred)
5412 put_rpccred(cred);
5413 switch (ret) {
5414 case 0:
5415 case -NFS4ERR_STALE_CLIENTID:
5416 clp->cl_exchange_flags = 0;
5417 }
5418out:
5419 return ret;
5420}
5421
Andy Adamson2050f0c2009-04-01 09:22:30 -04005422struct nfs4_get_lease_time_data {
5423 struct nfs4_get_lease_time_args *args;
5424 struct nfs4_get_lease_time_res *res;
5425 struct nfs_client *clp;
5426};
5427
5428static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5429 void *calldata)
5430{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005431 struct nfs4_get_lease_time_data *data =
5432 (struct nfs4_get_lease_time_data *)calldata;
5433
5434 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005435 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005436 /* just setup sequence, do not trigger session recovery
5437 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005438 nfs41_setup_sequence(data->clp->cl_session,
5439 &data->args->la_seq_args,
5440 &data->res->lr_seq_res,
5441 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005442 dprintk("<-- %s\n", __func__);
5443}
5444
5445/*
5446 * Called from nfs4_state_manager thread for session setup, so don't recover
5447 * from sequence operation or clientid errors.
5448 */
5449static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5450{
5451 struct nfs4_get_lease_time_data *data =
5452 (struct nfs4_get_lease_time_data *)calldata;
5453
5454 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005455 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5456 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005457 switch (task->tk_status) {
5458 case -NFS4ERR_DELAY:
5459 case -NFS4ERR_GRACE:
5460 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5461 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5462 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005463 /* fall through */
5464 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005465 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005466 return;
5467 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005468 dprintk("<-- %s\n", __func__);
5469}
5470
Trond Myklebust17280172012-03-11 13:11:00 -04005471static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005472 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5473 .rpc_call_done = nfs4_get_lease_time_done,
5474};
5475
5476int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5477{
5478 struct rpc_task *task;
5479 struct nfs4_get_lease_time_args args;
5480 struct nfs4_get_lease_time_res res = {
5481 .lr_fsinfo = fsinfo,
5482 };
5483 struct nfs4_get_lease_time_data data = {
5484 .args = &args,
5485 .res = &res,
5486 .clp = clp,
5487 };
5488 struct rpc_message msg = {
5489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5490 .rpc_argp = &args,
5491 .rpc_resp = &res,
5492 };
5493 struct rpc_task_setup task_setup = {
5494 .rpc_client = clp->cl_rpcclient,
5495 .rpc_message = &msg,
5496 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005497 .callback_data = &data,
5498 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005499 };
5500 int status;
5501
Trond Myklebust9d12b212012-01-17 22:04:25 -05005502 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005503 dprintk("--> %s\n", __func__);
5504 task = rpc_run_task(&task_setup);
5505
5506 if (IS_ERR(task))
5507 status = PTR_ERR(task);
5508 else {
5509 status = task->tk_status;
5510 rpc_put_task(task);
5511 }
5512 dprintk("<-- %s return %d\n", __func__, status);
5513
5514 return status;
5515}
5516
Andy Adamsonfc931582009-04-01 09:22:31 -04005517/*
5518 * Initialize the values to be used by the client in CREATE_SESSION
5519 * If nfs4_init_session set the fore channel request and response sizes,
5520 * use them.
5521 *
5522 * Set the back channel max_resp_sz_cached to zero to force the client to
5523 * always set csa_cachethis to FALSE because the current implementation
5524 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5525 */
5526static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5527{
5528 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005529 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5530 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005531
5532 if (mxrqst_sz == 0)
5533 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5534 if (mxresp_sz == 0)
5535 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5536 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005537 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5538 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005539 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005540 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005541
5542 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005543 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005544 __func__,
5545 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005546 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005547
5548 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005549 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5550 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5551 args->bc_attrs.max_resp_sz_cached = 0;
5552 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5553 args->bc_attrs.max_reqs = 1;
5554
5555 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5556 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5557 __func__,
5558 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5559 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5560 args->bc_attrs.max_reqs);
5561}
5562
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005563static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005564{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005565 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5566 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5567
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005568 if (rcvd->max_resp_sz > sent->max_resp_sz)
5569 return -EINVAL;
5570 /*
5571 * Our requested max_ops is the minimum we need; we're not
5572 * prepared to break up compounds into smaller pieces than that.
5573 * So, no point even trying to continue if the server won't
5574 * cooperate:
5575 */
5576 if (rcvd->max_ops < sent->max_ops)
5577 return -EINVAL;
5578 if (rcvd->max_reqs == 0)
5579 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005580 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5581 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005582 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005583}
5584
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005585static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5586{
5587 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5588 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005589
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005590 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5591 return -EINVAL;
5592 if (rcvd->max_resp_sz < sent->max_resp_sz)
5593 return -EINVAL;
5594 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5595 return -EINVAL;
5596 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005597 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005598 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005599 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005600 return -EINVAL;
5601 return 0;
5602}
Andy Adamson8d353012009-04-01 09:22:32 -04005603
Andy Adamson8d353012009-04-01 09:22:32 -04005604static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5605 struct nfs4_session *session)
5606{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005607 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005608
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005609 ret = nfs4_verify_fore_channel_attrs(args, session);
5610 if (ret)
5611 return ret;
5612 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005613}
5614
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005615static int _nfs4_proc_create_session(struct nfs_client *clp,
5616 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005617{
5618 struct nfs4_session *session = clp->cl_session;
5619 struct nfs41_create_session_args args = {
5620 .client = clp,
5621 .cb_program = NFS4_CALLBACK,
5622 };
5623 struct nfs41_create_session_res res = {
5624 .client = clp,
5625 };
5626 struct rpc_message msg = {
5627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5628 .rpc_argp = &args,
5629 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005630 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005631 };
5632 int status;
5633
5634 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005635 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005636
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005637 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005638
Trond Myklebust43095d32012-11-20 11:13:12 -05005639 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005640 /* Verify the session's negotiated channel_attrs values */
5641 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005642 /* Increment the clientid slot sequence id */
5643 clp->cl_seqid++;
5644 }
5645
5646 return status;
5647}
5648
5649/*
5650 * Issues a CREATE_SESSION operation to the server.
5651 * It is the responsibility of the caller to verify the session is
5652 * expired before calling this routine.
5653 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005654int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005655{
5656 int status;
5657 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005658 struct nfs4_session *session = clp->cl_session;
5659
5660 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5661
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005662 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005663 if (status)
5664 goto out;
5665
Andy Adamsonaacd5532011-11-09 13:58:21 -05005666 /* Init or reset the session slot tables */
5667 status = nfs4_setup_session_slot_tables(session);
5668 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005669 if (status)
5670 goto out;
5671
5672 ptr = (unsigned *)&session->sess_id.data[0];
5673 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5674 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005675out:
5676 dprintk("<-- %s\n", __func__);
5677 return status;
5678}
5679
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005680/*
5681 * Issue the over-the-wire RPC DESTROY_SESSION.
5682 * The caller must serialize access to this routine.
5683 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005684int nfs4_proc_destroy_session(struct nfs4_session *session,
5685 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005686{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005687 struct rpc_message msg = {
5688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5689 .rpc_argp = session,
5690 .rpc_cred = cred,
5691 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005692 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005693
5694 dprintk("--> nfs4_proc_destroy_session\n");
5695
5696 /* session is still being setup */
5697 if (session->clp->cl_cons_state != NFS_CS_READY)
5698 return status;
5699
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005700 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005701
5702 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005703 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005704 "Session has been destroyed regardless...\n", status);
5705
5706 dprintk("<-- nfs4_proc_destroy_session\n");
5707 return status;
5708}
5709
Trond Myklebust7b38c362012-05-23 13:23:31 -04005710/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005711 * Renew the cl_session lease.
5712 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005713struct nfs4_sequence_data {
5714 struct nfs_client *clp;
5715 struct nfs4_sequence_args args;
5716 struct nfs4_sequence_res res;
5717};
5718
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005719static void nfs41_sequence_release(void *data)
5720{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005721 struct nfs4_sequence_data *calldata = data;
5722 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005723
Alexandros Batsakis71358402010-02-05 03:45:05 -08005724 if (atomic_read(&clp->cl_count) > 1)
5725 nfs4_schedule_state_renewal(clp);
5726 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005727 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005728}
5729
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005730static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5731{
5732 switch(task->tk_status) {
5733 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005734 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5735 return -EAGAIN;
5736 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005737 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005738 }
5739 return 0;
5740}
5741
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005742static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005743{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005744 struct nfs4_sequence_data *calldata = data;
5745 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005746
Trond Myklebust14516c32010-07-31 14:29:06 -04005747 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5748 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005749
5750 if (task->tk_status < 0) {
5751 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005752 if (atomic_read(&clp->cl_count) == 1)
5753 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005754
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005755 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5756 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005757 return;
5758 }
5759 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005760 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005761out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762 dprintk("<-- %s\n", __func__);
5763}
5764
5765static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5766{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005767 struct nfs4_sequence_data *calldata = data;
5768 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005769 struct nfs4_sequence_args *args;
5770 struct nfs4_sequence_res *res;
5771
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005772 args = task->tk_msg.rpc_argp;
5773 res = task->tk_msg.rpc_resp;
5774
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005775 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005776}
5777
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005778static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5779{
5780 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5781 nfs41_sequence_prepare(task, data);
5782}
5783
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005784static const struct rpc_call_ops nfs41_sequence_ops = {
5785 .rpc_call_done = nfs41_sequence_call_done,
5786 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005787 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005788};
5789
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005790static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5791 .rpc_call_done = nfs41_sequence_call_done,
5792 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5793 .rpc_release = nfs41_sequence_release,
5794};
5795
5796static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5797 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005798{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005799 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005800 struct rpc_message msg = {
5801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5802 .rpc_cred = cred,
5803 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005804 struct rpc_task_setup task_setup_data = {
5805 .rpc_client = clp->cl_rpcclient,
5806 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005807 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005808 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5809 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005810
Alexandros Batsakis71358402010-02-05 03:45:05 -08005811 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005812 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005813 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005814 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005815 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005816 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005817 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005818 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005819 msg.rpc_argp = &calldata->args;
5820 msg.rpc_resp = &calldata->res;
5821 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005822 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005823
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005824 return rpc_run_task(&task_setup_data);
5825}
5826
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005827static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005828{
5829 struct rpc_task *task;
5830 int ret = 0;
5831
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005832 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5833 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005834 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005835 if (IS_ERR(task))
5836 ret = PTR_ERR(task);
5837 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005838 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005839 dprintk("<-- %s status=%d\n", __func__, ret);
5840 return ret;
5841}
5842
5843static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5844{
5845 struct rpc_task *task;
5846 int ret;
5847
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005848 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005849 if (IS_ERR(task)) {
5850 ret = PTR_ERR(task);
5851 goto out;
5852 }
5853 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005854 if (!ret) {
5855 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5856
5857 if (task->tk_status == 0)
5858 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005859 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005860 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005861 rpc_put_task(task);
5862out:
5863 dprintk("<-- %s status=%d\n", __func__, ret);
5864 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005865}
5866
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005867struct nfs4_reclaim_complete_data {
5868 struct nfs_client *clp;
5869 struct nfs41_reclaim_complete_args arg;
5870 struct nfs41_reclaim_complete_res res;
5871};
5872
5873static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5874{
5875 struct nfs4_reclaim_complete_data *calldata = data;
5876
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005877 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005878 nfs41_setup_sequence(calldata->clp->cl_session,
5879 &calldata->arg.seq_args,
5880 &calldata->res.seq_res,
5881 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005882}
5883
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005884static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5885{
5886 switch(task->tk_status) {
5887 case 0:
5888 case -NFS4ERR_COMPLETE_ALREADY:
5889 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5890 break;
5891 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005892 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005893 /* fall through */
5894 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005895 return -EAGAIN;
5896 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005897 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005898 }
5899 return 0;
5900}
5901
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005902static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5903{
5904 struct nfs4_reclaim_complete_data *calldata = data;
5905 struct nfs_client *clp = calldata->clp;
5906 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5907
5908 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005909 if (!nfs41_sequence_done(task, res))
5910 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005911
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005912 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5913 rpc_restart_call_prepare(task);
5914 return;
5915 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005916 dprintk("<-- %s\n", __func__);
5917}
5918
5919static void nfs4_free_reclaim_complete_data(void *data)
5920{
5921 struct nfs4_reclaim_complete_data *calldata = data;
5922
5923 kfree(calldata);
5924}
5925
5926static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5927 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5928 .rpc_call_done = nfs4_reclaim_complete_done,
5929 .rpc_release = nfs4_free_reclaim_complete_data,
5930};
5931
5932/*
5933 * Issue a global reclaim complete.
5934 */
5935static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5936{
5937 struct nfs4_reclaim_complete_data *calldata;
5938 struct rpc_task *task;
5939 struct rpc_message msg = {
5940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5941 };
5942 struct rpc_task_setup task_setup_data = {
5943 .rpc_client = clp->cl_rpcclient,
5944 .rpc_message = &msg,
5945 .callback_ops = &nfs4_reclaim_complete_call_ops,
5946 .flags = RPC_TASK_ASYNC,
5947 };
5948 int status = -ENOMEM;
5949
5950 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005951 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005952 if (calldata == NULL)
5953 goto out;
5954 calldata->clp = clp;
5955 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005956
Trond Myklebust9d12b212012-01-17 22:04:25 -05005957 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005958 msg.rpc_argp = &calldata->arg;
5959 msg.rpc_resp = &calldata->res;
5960 task_setup_data.callback_data = calldata;
5961 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005962 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005963 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005964 goto out;
5965 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005966 status = nfs4_wait_for_completion_rpc_task(task);
5967 if (status == 0)
5968 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005969 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005970 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005971out:
5972 dprintk("<-- %s status=%d\n", __func__, status);
5973 return status;
5974}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005975
5976static void
5977nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5978{
5979 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005980 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005981 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005982
5983 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005984 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5985 * right now covering the LAYOUTGET we are about to send.
5986 * However, that is not so catastrophic, and there seems
5987 * to be no way to prevent it completely.
5988 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005989 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005990 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005991 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005992 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5993 NFS_I(lgp->args.inode)->layout,
5994 lgp->args.ctx->state)) {
5995 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005996 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005997}
5998
5999static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6000{
6001 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006002 struct inode *inode = lgp->args.inode;
6003 struct nfs_server *server = NFS_SERVER(inode);
6004 struct pnfs_layout_hdr *lo;
6005 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006006
6007 dprintk("--> %s\n", __func__);
6008
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006009 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006010 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006011
6012 switch (task->tk_status) {
6013 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006014 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006015 case -NFS4ERR_LAYOUTTRYLATER:
6016 case -NFS4ERR_RECALLCONFLICT:
6017 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006018 break;
6019 case -NFS4ERR_EXPIRED:
6020 case -NFS4ERR_BAD_STATEID:
6021 spin_lock(&inode->i_lock);
6022 lo = NFS_I(inode)->layout;
6023 if (!lo || list_empty(&lo->plh_segs)) {
6024 spin_unlock(&inode->i_lock);
6025 /* If the open stateid was bad, then recover it. */
6026 state = lgp->args.ctx->state;
6027 } else {
6028 LIST_HEAD(head);
6029
6030 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6031 spin_unlock(&inode->i_lock);
6032 /* Mark the bad layout state as invalid, then
6033 * retry using the open stateid. */
6034 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006035 }
6036 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006037 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6038 rpc_restart_call_prepare(task);
6039out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006040 dprintk("<-- %s\n", __func__);
6041}
6042
Idan Kedar85541162012-08-02 11:47:10 +03006043static size_t max_response_pages(struct nfs_server *server)
6044{
6045 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6046 return nfs_page_array_len(0, max_resp_sz);
6047}
6048
6049static void nfs4_free_pages(struct page **pages, size_t size)
6050{
6051 int i;
6052
6053 if (!pages)
6054 return;
6055
6056 for (i = 0; i < size; i++) {
6057 if (!pages[i])
6058 break;
6059 __free_page(pages[i]);
6060 }
6061 kfree(pages);
6062}
6063
6064static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6065{
6066 struct page **pages;
6067 int i;
6068
6069 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6070 if (!pages) {
6071 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6072 return NULL;
6073 }
6074
6075 for (i = 0; i < size; i++) {
6076 pages[i] = alloc_page(gfp_flags);
6077 if (!pages[i]) {
6078 dprintk("%s: failed to allocate page\n", __func__);
6079 nfs4_free_pages(pages, size);
6080 return NULL;
6081 }
6082 }
6083
6084 return pages;
6085}
6086
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006087static void nfs4_layoutget_release(void *calldata)
6088{
6089 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006090 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6091 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006092
6093 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006094 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006095 put_nfs_open_context(lgp->args.ctx);
6096 kfree(calldata);
6097 dprintk("<-- %s\n", __func__);
6098}
6099
6100static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6101 .rpc_call_prepare = nfs4_layoutget_prepare,
6102 .rpc_call_done = nfs4_layoutget_done,
6103 .rpc_release = nfs4_layoutget_release,
6104};
6105
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006106struct pnfs_layout_segment *
6107nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006108{
6109 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006110 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006111 struct rpc_task *task;
6112 struct rpc_message msg = {
6113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6114 .rpc_argp = &lgp->args,
6115 .rpc_resp = &lgp->res,
6116 };
6117 struct rpc_task_setup task_setup_data = {
6118 .rpc_client = server->client,
6119 .rpc_message = &msg,
6120 .callback_ops = &nfs4_layoutget_call_ops,
6121 .callback_data = lgp,
6122 .flags = RPC_TASK_ASYNC,
6123 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006124 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006125 int status = 0;
6126
6127 dprintk("--> %s\n", __func__);
6128
Idan Kedar85541162012-08-02 11:47:10 +03006129 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6130 if (!lgp->args.layout.pages) {
6131 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006132 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006133 }
6134 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6135
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006136 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006137 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006138 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006139 task = rpc_run_task(&task_setup_data);
6140 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006141 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006142 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006143 if (status == 0)
6144 status = task->tk_status;
6145 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006146 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006147 rpc_put_task(task);
6148 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006149 if (status)
6150 return ERR_PTR(status);
6151 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006152}
6153
Benny Halevycbe82602011-05-22 19:52:37 +03006154static void
6155nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6156{
6157 struct nfs4_layoutreturn *lrp = calldata;
6158
6159 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006160 nfs41_setup_sequence(lrp->clp->cl_session,
6161 &lrp->args.seq_args,
6162 &lrp->res.seq_res,
6163 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006164}
6165
6166static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6167{
6168 struct nfs4_layoutreturn *lrp = calldata;
6169 struct nfs_server *server;
6170
6171 dprintk("--> %s\n", __func__);
6172
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006173 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006174 return;
6175
6176 server = NFS_SERVER(lrp->args.inode);
6177 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006178 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006179 return;
6180 }
Benny Halevycbe82602011-05-22 19:52:37 +03006181 dprintk("<-- %s\n", __func__);
6182}
6183
6184static void nfs4_layoutreturn_release(void *calldata)
6185{
6186 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006187 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006188
6189 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006190 spin_lock(&lo->plh_inode->i_lock);
6191 if (lrp->res.lrs_present)
6192 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6193 lo->plh_block_lgets--;
6194 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006195 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006196 kfree(calldata);
6197 dprintk("<-- %s\n", __func__);
6198}
6199
6200static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6201 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6202 .rpc_call_done = nfs4_layoutreturn_done,
6203 .rpc_release = nfs4_layoutreturn_release,
6204};
6205
6206int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6207{
6208 struct rpc_task *task;
6209 struct rpc_message msg = {
6210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6211 .rpc_argp = &lrp->args,
6212 .rpc_resp = &lrp->res,
6213 };
6214 struct rpc_task_setup task_setup_data = {
6215 .rpc_client = lrp->clp->cl_rpcclient,
6216 .rpc_message = &msg,
6217 .callback_ops = &nfs4_layoutreturn_call_ops,
6218 .callback_data = lrp,
6219 };
6220 int status;
6221
6222 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006223 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006224 task = rpc_run_task(&task_setup_data);
6225 if (IS_ERR(task))
6226 return PTR_ERR(task);
6227 status = task->tk_status;
6228 dprintk("<-- %s status=%d\n", __func__, status);
6229 rpc_put_task(task);
6230 return status;
6231}
6232
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006233/*
6234 * Retrieve the list of Data Server devices from the MDS.
6235 */
6236static int _nfs4_getdevicelist(struct nfs_server *server,
6237 const struct nfs_fh *fh,
6238 struct pnfs_devicelist *devlist)
6239{
6240 struct nfs4_getdevicelist_args args = {
6241 .fh = fh,
6242 .layoutclass = server->pnfs_curr_ld->id,
6243 };
6244 struct nfs4_getdevicelist_res res = {
6245 .devlist = devlist,
6246 };
6247 struct rpc_message msg = {
6248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6249 .rpc_argp = &args,
6250 .rpc_resp = &res,
6251 };
6252 int status;
6253
6254 dprintk("--> %s\n", __func__);
6255 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6256 &res.seq_res, 0);
6257 dprintk("<-- %s status=%d\n", __func__, status);
6258 return status;
6259}
6260
6261int nfs4_proc_getdevicelist(struct nfs_server *server,
6262 const struct nfs_fh *fh,
6263 struct pnfs_devicelist *devlist)
6264{
6265 struct nfs4_exception exception = { };
6266 int err;
6267
6268 do {
6269 err = nfs4_handle_exception(server,
6270 _nfs4_getdevicelist(server, fh, devlist),
6271 &exception);
6272 } while (exception.retry);
6273
6274 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6275 err, devlist->num_devs);
6276
6277 return err;
6278}
6279EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6280
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006281static int
6282_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6283{
6284 struct nfs4_getdeviceinfo_args args = {
6285 .pdev = pdev,
6286 };
6287 struct nfs4_getdeviceinfo_res res = {
6288 .pdev = pdev,
6289 };
6290 struct rpc_message msg = {
6291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6292 .rpc_argp = &args,
6293 .rpc_resp = &res,
6294 };
6295 int status;
6296
6297 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006298 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006299 dprintk("<-- %s status=%d\n", __func__, status);
6300
6301 return status;
6302}
6303
6304int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6305{
6306 struct nfs4_exception exception = { };
6307 int err;
6308
6309 do {
6310 err = nfs4_handle_exception(server,
6311 _nfs4_proc_getdeviceinfo(server, pdev),
6312 &exception);
6313 } while (exception.retry);
6314 return err;
6315}
6316EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6317
Andy Adamson863a3c62011-03-23 13:27:54 +00006318static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6319{
6320 struct nfs4_layoutcommit_data *data = calldata;
6321 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006322 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006323
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006324 nfs41_setup_sequence(session,
6325 &data->args.seq_args,
6326 &data->res.seq_res,
6327 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006328}
6329
6330static void
6331nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6332{
6333 struct nfs4_layoutcommit_data *data = calldata;
6334 struct nfs_server *server = NFS_SERVER(data->args.inode);
6335
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006336 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006337 return;
6338
6339 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006340 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6341 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6342 case -NFS4ERR_BADLAYOUT: /* no layout */
6343 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006344 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006345 break;
6346 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006347 nfs_post_op_update_inode_force_wcc(data->args.inode,
6348 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006349 break;
6350 default:
6351 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6352 rpc_restart_call_prepare(task);
6353 return;
6354 }
6355 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006356}
6357
6358static void nfs4_layoutcommit_release(void *calldata)
6359{
6360 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006361 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006362 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006363
Andy Adamsondb29c082011-07-30 20:52:38 -04006364 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006365 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006366 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6367 list_del_init(&lseg->pls_lc_list);
6368 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6369 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006370 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006371 }
Peng Tao92407e72011-10-23 20:21:17 -07006372
6373 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6374 smp_mb__after_clear_bit();
6375 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6376
Andy Adamson863a3c62011-03-23 13:27:54 +00006377 put_rpccred(data->cred);
6378 kfree(data);
6379}
6380
6381static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6382 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6383 .rpc_call_done = nfs4_layoutcommit_done,
6384 .rpc_release = nfs4_layoutcommit_release,
6385};
6386
6387int
Andy Adamsonef311532011-03-12 02:58:10 -05006388nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006389{
6390 struct rpc_message msg = {
6391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6392 .rpc_argp = &data->args,
6393 .rpc_resp = &data->res,
6394 .rpc_cred = data->cred,
6395 };
6396 struct rpc_task_setup task_setup_data = {
6397 .task = &data->task,
6398 .rpc_client = NFS_CLIENT(data->args.inode),
6399 .rpc_message = &msg,
6400 .callback_ops = &nfs4_layoutcommit_ops,
6401 .callback_data = data,
6402 .flags = RPC_TASK_ASYNC,
6403 };
6404 struct rpc_task *task;
6405 int status = 0;
6406
6407 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6408 "lbw: %llu inode %lu\n",
6409 data->task.tk_pid, sync,
6410 data->args.lastbytewritten,
6411 data->args.inode->i_ino);
6412
Trond Myklebust9d12b212012-01-17 22:04:25 -05006413 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006414 task = rpc_run_task(&task_setup_data);
6415 if (IS_ERR(task))
6416 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006417 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006418 goto out;
6419 status = nfs4_wait_for_completion_rpc_task(task);
6420 if (status != 0)
6421 goto out;
6422 status = task->tk_status;
6423out:
6424 dprintk("%s: status %d\n", __func__, status);
6425 rpc_put_task(task);
6426 return status;
6427}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006428
6429static int
6430_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6431 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6432{
6433 struct nfs41_secinfo_no_name_args args = {
6434 .style = SECINFO_STYLE_CURRENT_FH,
6435 };
6436 struct nfs4_secinfo_res res = {
6437 .flavors = flavors,
6438 };
6439 struct rpc_message msg = {
6440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6441 .rpc_argp = &args,
6442 .rpc_resp = &res,
6443 };
6444 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6445}
6446
6447static int
6448nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6449 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6450{
6451 struct nfs4_exception exception = { };
6452 int err;
6453 do {
6454 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6455 switch (err) {
6456 case 0:
6457 case -NFS4ERR_WRONGSEC:
6458 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006459 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006460 default:
6461 err = nfs4_handle_exception(server, err, &exception);
6462 }
6463 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006464out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006465 return err;
6466}
6467
6468static int
6469nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6470 struct nfs_fsinfo *info)
6471{
6472 int err;
6473 struct page *page;
6474 rpc_authflavor_t flavor;
6475 struct nfs4_secinfo_flavors *flavors;
6476
6477 page = alloc_page(GFP_KERNEL);
6478 if (!page) {
6479 err = -ENOMEM;
6480 goto out;
6481 }
6482
6483 flavors = page_address(page);
6484 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6485
6486 /*
6487 * Fall back on "guess and check" method if
6488 * the server doesn't support SECINFO_NO_NAME
6489 */
6490 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6491 err = nfs4_find_root_sec(server, fhandle, info);
6492 goto out_freepage;
6493 }
6494 if (err)
6495 goto out_freepage;
6496
6497 flavor = nfs_find_best_sec(flavors);
6498 if (err == 0)
6499 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6500
6501out_freepage:
6502 put_page(page);
6503 if (err == -EACCES)
6504 return -EPERM;
6505out:
6506 return err;
6507}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006508
6509static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006510{
6511 int status;
6512 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006513 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006514 };
6515 struct nfs41_test_stateid_res res;
6516 struct rpc_message msg = {
6517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6518 .rpc_argp = &args,
6519 .rpc_resp = &res,
6520 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006521
Chuck Lever38527b12012-07-11 16:30:23 -04006522 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006523 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006524 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006525 if (status != NFS_OK) {
6526 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006527 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006528 }
6529 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006530 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006531}
6532
Chuck Lever38527b12012-07-11 16:30:23 -04006533/**
6534 * nfs41_test_stateid - perform a TEST_STATEID operation
6535 *
6536 * @server: server / transport on which to perform the operation
6537 * @stateid: state ID to test
6538 *
6539 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6540 * Otherwise a negative NFS4ERR value is returned if the operation
6541 * failed or the state ID is not currently valid.
6542 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006543static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006544{
6545 struct nfs4_exception exception = { };
6546 int err;
6547 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006548 err = _nfs41_test_stateid(server, stateid);
6549 if (err != -NFS4ERR_DELAY)
6550 break;
6551 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006552 } while (exception.retry);
6553 return err;
6554}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006555
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006556static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006557{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006558 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006559 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006560 };
6561 struct nfs41_free_stateid_res res;
6562 struct rpc_message msg = {
6563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6564 .rpc_argp = &args,
6565 .rpc_resp = &res,
6566 };
Chuck Lever38527b12012-07-11 16:30:23 -04006567 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006568
Chuck Lever38527b12012-07-11 16:30:23 -04006569 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006570 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006571 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006572 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006573 dprintk("NFS reply free_stateid: %d\n", status);
6574 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006575}
6576
Chuck Lever38527b12012-07-11 16:30:23 -04006577/**
6578 * nfs41_free_stateid - perform a FREE_STATEID operation
6579 *
6580 * @server: server / transport on which to perform the operation
6581 * @stateid: state ID to release
6582 *
6583 * Returns NFS_OK if the server freed "stateid". Otherwise a
6584 * negative NFS4ERR value is returned.
6585 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006586static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006587{
6588 struct nfs4_exception exception = { };
6589 int err;
6590 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006591 err = _nfs4_free_stateid(server, stateid);
6592 if (err != -NFS4ERR_DELAY)
6593 break;
6594 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006595 } while (exception.retry);
6596 return err;
6597}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006598
6599static bool nfs41_match_stateid(const nfs4_stateid *s1,
6600 const nfs4_stateid *s2)
6601{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006602 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006603 return false;
6604
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006605 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006606 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006607 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006608 return true;
6609
6610 return false;
6611}
6612
Andy Adamson557134a2009-04-01 09:21:53 -04006613#endif /* CONFIG_NFS_V4_1 */
6614
Trond Myklebust36281ca2012-03-04 18:13:56 -05006615static bool nfs4_match_stateid(const nfs4_stateid *s1,
6616 const nfs4_stateid *s2)
6617{
Trond Myklebustf597c532012-03-04 18:13:56 -05006618 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006619}
6620
6621
Trond Myklebust17280172012-03-11 13:11:00 -04006622static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006623 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006624 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 .recover_open = nfs4_open_reclaim,
6626 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006627 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006628 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006629 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630};
6631
Andy Adamson591d71c2009-04-01 09:22:47 -04006632#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006633static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006634 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6635 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6636 .recover_open = nfs4_open_reclaim,
6637 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006638 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006639 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006640 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006641 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006642};
6643#endif /* CONFIG_NFS_V4_1 */
6644
Trond Myklebust17280172012-03-11 13:11:00 -04006645static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006646 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006647 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 .recover_open = nfs4_open_expired,
6649 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006650 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006651 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652};
6653
Andy Adamson591d71c2009-04-01 09:22:47 -04006654#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006655static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006656 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6657 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006658 .recover_open = nfs41_open_expired,
6659 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006660 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006661 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006662};
6663#endif /* CONFIG_NFS_V4_1 */
6664
Trond Myklebust17280172012-03-11 13:11:00 -04006665static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006666 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006667 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006668 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006669};
6670
6671#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006672static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006673 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006674 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006675 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006676};
6677#endif
6678
Trond Myklebust97dc1352010-06-16 09:52:26 -04006679static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6680 .minor_version = 0,
6681 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006682 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006683 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006684 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6685 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6686 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006687};
6688
6689#if defined(CONFIG_NFS_V4_1)
6690static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6691 .minor_version = 1,
6692 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006693 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006694 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006695 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6696 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6697 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006698};
6699#endif
6700
Trond Myklebust97dc1352010-06-16 09:52:26 -04006701const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6702 [0] = &nfs_v4_0_minor_ops,
6703#if defined(CONFIG_NFS_V4_1)
6704 [1] = &nfs_v4_1_minor_ops,
6705#endif
6706};
6707
Bryan Schumaker73a79702012-07-16 16:39:12 -04006708const struct inode_operations nfs4_dir_inode_operations = {
6709 .create = nfs_create,
6710 .lookup = nfs_lookup,
6711 .atomic_open = nfs_atomic_open,
6712 .link = nfs_link,
6713 .unlink = nfs_unlink,
6714 .symlink = nfs_symlink,
6715 .mkdir = nfs_mkdir,
6716 .rmdir = nfs_rmdir,
6717 .mknod = nfs_mknod,
6718 .rename = nfs_rename,
6719 .permission = nfs_permission,
6720 .getattr = nfs_getattr,
6721 .setattr = nfs_setattr,
6722 .getxattr = generic_getxattr,
6723 .setxattr = generic_setxattr,
6724 .listxattr = generic_listxattr,
6725 .removexattr = generic_removexattr,
6726};
6727
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006728static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006729 .permission = nfs_permission,
6730 .getattr = nfs_getattr,
6731 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006732 .getxattr = generic_getxattr,
6733 .setxattr = generic_setxattr,
6734 .listxattr = generic_listxattr,
6735 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006736};
6737
David Howells509de812006-08-22 20:06:11 -04006738const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 .version = 4, /* protocol version */
6740 .dentry_ops = &nfs4_dentry_operations,
6741 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006742 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006743 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006745 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006746 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 .getattr = nfs4_proc_getattr,
6748 .setattr = nfs4_proc_setattr,
6749 .lookup = nfs4_proc_lookup,
6750 .access = nfs4_proc_access,
6751 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 .create = nfs4_proc_create,
6753 .remove = nfs4_proc_remove,
6754 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006755 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 .unlink_done = nfs4_proc_unlink_done,
6757 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006758 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006759 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006760 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 .link = nfs4_proc_link,
6762 .symlink = nfs4_proc_symlink,
6763 .mkdir = nfs4_proc_mkdir,
6764 .rmdir = nfs4_proc_remove,
6765 .readdir = nfs4_proc_readdir,
6766 .mknod = nfs4_proc_mknod,
6767 .statfs = nfs4_proc_statfs,
6768 .fsinfo = nfs4_proc_fsinfo,
6769 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006770 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 .decode_dirent = nfs4_decode_dirent,
6772 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006773 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006774 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006775 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006777 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006778 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006779 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006781 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006782 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006784 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006785 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006786 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006787 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006788 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006789 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006790 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006791 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006792 .create_server = nfs4_create_server,
6793 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794};
6795
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006796static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6797 .prefix = XATTR_NAME_NFSV4_ACL,
6798 .list = nfs4_xattr_list_nfs4_acl,
6799 .get = nfs4_xattr_get_nfs4_acl,
6800 .set = nfs4_xattr_set_nfs4_acl,
6801};
6802
6803const struct xattr_handler *nfs4_xattr_handlers[] = {
6804 &nfs4_xattr_nfs4_acl_handler,
6805 NULL
6806};
6807
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808/*
6809 * Local variables:
6810 * c-basic-offset: 8
6811 * End:
6812 */