blob: 99d99a5a3f615d827c1c73684ae35d8d5ff829f2 [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;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400493 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500494 if (cache_reply)
495 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500496 res->sr_slot = NULL;
497}
498
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400499static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
500{
501 args->sa_privileged = 1;
502}
503
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000504int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400505 struct nfs4_sequence_args *args,
506 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400507 struct rpc_task *task)
508{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400509 struct nfs4_slot *slot;
510 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400511
512 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400513 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400514 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400515 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400516
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400517 tbl = &session->fc_slot_table;
518
Trond Myklebust69d206b2012-11-22 13:21:02 -0500519 task->tk_timeout = 0;
520
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400521 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400522 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400523 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500524 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500525 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400526 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400527 }
528
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500529 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500530 if (IS_ERR(slot)) {
531 /* If out of memory, try again in 1/4 second */
532 if (slot == ERR_PTR(-ENOMEM))
533 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400534 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400535 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400536 }
537 spin_unlock(&tbl->slot_tbl_lock);
538
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500539 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400540
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500541 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
542 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543
Benny Halevydfb4f3092010-09-24 09:17:01 -0400544 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400545 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400546 /*
547 * sr_status is only set in decode_sequence, and so will remain
548 * set to 1 if an rpc level failure occurs.
549 */
550 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400551out_success:
552 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400553 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400554out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400555 /* Privileged tasks are queued with top priority */
556 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400557 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
558 NULL, RPC_PRIORITY_PRIVILEGED);
559 else
560 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400561 spin_unlock(&tbl->slot_tbl_lock);
562 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000564EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565
Trond Myklebust035168a2010-06-16 09:52:26 -0400566int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 struct nfs4_sequence_args *args,
568 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569 struct rpc_task *task)
570{
Trond Myklebust035168a2010-06-16 09:52:26 -0400571 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400572 int ret = 0;
573
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400574 if (session == NULL) {
575 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400577 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400578
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500579 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400580 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500581 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400582
Trond Myklebust9d12b212012-01-17 22:04:25 -0500583 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400584out:
585 dprintk("<-- %s status=%d\n", __func__, ret);
586 return ret;
587}
588
589struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400590 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 struct nfs4_sequence_args *seq_args;
592 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593};
594
595static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
596{
597 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400598 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599
Trond Myklebust035168a2010-06-16 09:52:26 -0400600 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
601
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400602 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603}
604
Andy Adamson69ab40c2009-04-01 09:22:19 -0400605static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
606{
607 struct nfs41_call_sync_data *data = calldata;
608
Trond Myklebust14516c32010-07-31 14:29:06 -0400609 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400610}
611
Trond Myklebust17280172012-03-11 13:11:00 -0400612static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400614 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615};
616
Bryan Schumaker7c513052011-03-24 17:12:24 +0000617static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
618 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct rpc_message *msg,
620 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400621 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622{
623 int ret;
624 struct rpc_task *task;
625 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400626 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627 .seq_args = args,
628 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 };
630 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000631 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 .rpc_message = msg,
633 .callback_ops = &nfs41_call_sync_ops,
634 .callback_data = &data
635 };
636
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 task = rpc_run_task(&task_setup);
638 if (IS_ERR(task))
639 ret = PTR_ERR(task);
640 else {
641 ret = task->tk_status;
642 rpc_put_task(task);
643 }
644 return ret;
645}
646
Trond Myklebustdf896452010-06-16 09:52:26 -0400647#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400648static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500649void nfs41_init_sequence(struct nfs4_sequence_args *args,
650 struct nfs4_sequence_res *res, int cache_reply)
651{
652}
653
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400654static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
655{
656}
657
658
Trond Myklebust14516c32010-07-31 14:29:06 -0400659static int nfs4_sequence_done(struct rpc_task *task,
660 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400661{
Trond Myklebust14516c32010-07-31 14:29:06 -0400662 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400663}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400664#endif /* CONFIG_NFS_V4_1 */
665
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400666static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000667int _nfs4_call_sync(struct rpc_clnt *clnt,
668 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400671 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400672{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000673 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400674}
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,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000679 struct rpc_message *msg,
680 struct nfs4_sequence_args *args,
681 struct nfs4_sequence_res *res,
682 int cache_reply)
683{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400684 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000685 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400686 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000687}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400688
Trond Myklebust38478b22006-05-25 01:40:57 -0400689static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Trond Myklebust38478b22006-05-25 01:40:57 -0400691 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Trond Myklebust38478b22006-05-25 01:40:57 -0400693 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400694 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700695 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400696 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700697 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400698 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100701struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400702 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100703 struct nfs_openargs o_arg;
704 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100705 struct nfs_open_confirmargs c_arg;
706 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500707 struct nfs4_string owner_name;
708 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100709 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100710 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400711 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100712 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400713 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100714 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100715 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400716 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100717 int rpc_status;
718 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100719};
720
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400721
722static void nfs4_init_opendata_res(struct nfs4_opendata *p)
723{
724 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400725 p->o_res.seqid = p->o_arg.seqid;
726 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400727 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400728 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400729 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500730 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400731}
732
Al Viro82a2c1b2011-06-22 18:30:55 -0400733static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500734 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400735 const struct iattr *attrs,
736 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100737{
Al Viro82a2c1b2011-06-22 18:30:55 -0400738 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739 struct inode *dir = parent->d_inode;
740 struct nfs_server *server = NFS_SERVER(dir);
741 struct nfs4_opendata *p;
742
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400743 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744 if (p == NULL)
745 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400746 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747 if (p->o_arg.seqid == NULL)
748 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400749 nfs_sb_active(dentry->d_sb);
750 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751 p->dir = parent;
752 p->owner = sp;
753 atomic_inc(&sp->so_count);
754 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500755 p->o_arg.open_flags = flags;
756 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700757 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
758 * will return permission denied for all bits until close */
759 if (!(flags & O_EXCL)) {
760 /* ask server to check for all possible rights as results
761 * are cached */
762 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
763 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700764 }
David Howells7539bba2006-08-22 20:06:09 -0400765 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400766 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
767 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400768 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 p->o_arg.server = server;
770 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400771 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500773 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500774 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400775
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 p->o_arg.u.attrs = &p->attrs;
777 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500778
779 verf[0] = jiffies;
780 verf[1] = current->pid;
781 memcpy(p->o_arg.u.verifier.data, verf,
782 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100784 p->c_arg.fh = &p->o_res.fh;
785 p->c_arg.stateid = &p->o_res.stateid;
786 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400787 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400788 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789 return p;
790err_free:
791 kfree(p);
792err:
793 dput(parent);
794 return NULL;
795}
796
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400797static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400799 struct nfs4_opendata *p = container_of(kref,
800 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400801 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400802
803 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400804 if (p->state != NULL)
805 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400806 nfs4_put_state_owner(p->owner);
807 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400808 dput(p->dentry);
809 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500810 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 kfree(p);
812}
813
814static void nfs4_opendata_put(struct nfs4_opendata *p)
815{
816 if (p != NULL)
817 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818}
819
Trond Myklebust06f814a2006-01-03 09:55:07 +0100820static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
821{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100822 int ret;
823
Trond Myklebust06f814a2006-01-03 09:55:07 +0100824 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100825 return ret;
826}
827
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500828static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400829{
830 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500831
Trond Myklebust536e43d2012-01-17 22:04:26 -0500832 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500833 goto out;
834 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400835 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500836 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
837 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400838 break;
839 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500840 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
841 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400842 break;
843 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500844 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
845 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400846 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400848 return ret;
849}
850
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400852{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500853 if (delegation == NULL)
854 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500855 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400856 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500857 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400858 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500859 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400860 return 1;
861}
862
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100864{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100866 case FMODE_WRITE:
867 state->n_wronly++;
868 break;
869 case FMODE_READ:
870 state->n_rdonly++;
871 break;
872 case FMODE_READ|FMODE_WRITE:
873 state->n_rdwr++;
874 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100876}
877
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400879{
880 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500881 nfs4_stateid_copy(&state->stateid, stateid);
882 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500883 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400884 case FMODE_READ:
885 set_bit(NFS_O_RDONLY_STATE, &state->flags);
886 break;
887 case FMODE_WRITE:
888 set_bit(NFS_O_WRONLY_STATE, &state->flags);
889 break;
890 case FMODE_READ|FMODE_WRITE:
891 set_bit(NFS_O_RDWR_STATE, &state->flags);
892 }
893}
894
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400896{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400897 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400899 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400900}
901
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902static 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 -0700903{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400904 /*
905 * Protect the call to nfs4_state_set_mode_locked and
906 * serialise the stateid update
907 */
908 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400909 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500910 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400911 set_bit(NFS_DELEGATED_STATE, &state->flags);
912 }
913 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400915 write_sequnlock(&state->seqlock);
916 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700918 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921static 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 -0500922{
923 struct nfs_inode *nfsi = NFS_I(state->inode);
924 struct nfs_delegation *deleg_cur;
925 int ret = 0;
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500928
929 rcu_read_lock();
930 deleg_cur = rcu_dereference(nfsi->delegation);
931 if (deleg_cur == NULL)
932 goto no_delegation;
933
934 spin_lock(&deleg_cur->lock);
935 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500937 goto no_delegation_unlock;
938
939 if (delegation == NULL)
940 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500941 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500942 goto no_delegation_unlock;
943
Trond Myklebustb7391f42008-12-23 15:21:52 -0500944 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500946 ret = 1;
947no_delegation_unlock:
948 spin_unlock(&deleg_cur->lock);
949no_delegation:
950 rcu_read_unlock();
951
952 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500954 ret = 1;
955 }
956
957 return ret;
958}
959
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400962{
963 struct nfs_delegation *delegation;
964
965 rcu_read_lock();
966 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400968 rcu_read_unlock();
969 return;
970 }
971 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400972 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973}
974
Trond Myklebust6ee41262007-07-08 14:11:36 -0400975static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400976{
977 struct nfs4_state *state = opendata->state;
978 struct nfs_inode *nfsi = NFS_I(state->inode);
979 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500980 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400982 nfs4_stateid stateid;
983 int ret = -EAGAIN;
984
Trond Myklebustaac00a82007-07-05 19:02:21 -0400985 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400987 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 if (can_open_cached(state, fmode, open_mode)) {
989 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400990 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400991 goto out_return_state;
992 }
993 spin_unlock(&state->owner->so_lock);
994 }
Trond Myklebust34310432008-12-23 15:21:38 -0500995 rcu_read_lock();
996 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -0500997 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500998 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400999 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001000 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001002 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001003 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001004 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005 if (ret != 0)
1006 goto out;
1007 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001008
1009 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001011 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001012 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013out:
1014 return ERR_PTR(ret);
1015out_return_state:
1016 atomic_inc(&state->count);
1017 return state;
1018}
1019
Andy Adamsone23008e2012-10-02 21:07:32 -04001020static void
1021nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1022{
1023 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1024 struct nfs_delegation *delegation;
1025 int delegation_flags = 0;
1026
1027 rcu_read_lock();
1028 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1029 if (delegation)
1030 delegation_flags = delegation->flags;
1031 rcu_read_unlock();
1032 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1033 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1034 "returning a delegation for "
1035 "OPEN(CLAIM_DELEGATE_CUR)\n",
1036 clp->cl_hostname);
1037 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1038 nfs_inode_set_delegation(state->inode,
1039 data->owner->so_cred,
1040 &data->o_res);
1041 else
1042 nfs_inode_reclaim_delegation(state->inode,
1043 data->owner->so_cred,
1044 &data->o_res);
1045}
1046
1047/*
1048 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1049 * and update the nfs4_state.
1050 */
1051static struct nfs4_state *
1052_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1053{
1054 struct inode *inode = data->state->inode;
1055 struct nfs4_state *state = data->state;
1056 int ret;
1057
1058 if (!data->rpc_done) {
1059 ret = data->rpc_status;
1060 goto err;
1061 }
1062
1063 ret = -ESTALE;
1064 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1065 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1066 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1067 goto err;
1068
1069 ret = -ENOMEM;
1070 state = nfs4_get_open_state(inode, data->owner);
1071 if (state == NULL)
1072 goto err;
1073
1074 ret = nfs_refresh_inode(inode, &data->f_attr);
1075 if (ret)
1076 goto err;
1077
1078 if (data->o_res.delegation_type != 0)
1079 nfs4_opendata_check_deleg(data, state);
1080 update_open_stateid(state, &data->o_res.stateid, NULL,
1081 data->o_arg.fmode);
1082
1083 return state;
1084err:
1085 return ERR_PTR(ret);
1086
1087}
1088
1089static struct nfs4_state *
1090_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001091{
1092 struct inode *inode;
1093 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001094 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001095
Trond Myklebustaac00a82007-07-05 19:02:21 -04001096 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001098 goto out;
1099 }
1100
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001101 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001102 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001103 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001104 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001105 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001106 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 goto err;
1108 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001109 state = nfs4_get_open_state(inode, data->owner);
1110 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001112 if (data->o_res.delegation_type != 0)
1113 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001114 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001115 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001118 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119err_put_inode:
1120 iput(inode);
1121err:
1122 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001123}
1124
Andy Adamsone23008e2012-10-02 21:07:32 -04001125static struct nfs4_state *
1126nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1127{
1128 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1129 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1130 return _nfs4_opendata_to_nfs4_state(data);
1131}
1132
Trond Myklebust864472e2006-01-03 09:55:15 +01001133static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1134{
1135 struct nfs_inode *nfsi = NFS_I(state->inode);
1136 struct nfs_open_context *ctx;
1137
1138 spin_lock(&state->inode->i_lock);
1139 list_for_each_entry(ctx, &nfsi->open_files, list) {
1140 if (ctx->state != state)
1141 continue;
1142 get_nfs_open_context(ctx);
1143 spin_unlock(&state->inode->i_lock);
1144 return ctx;
1145 }
1146 spin_unlock(&state->inode->i_lock);
1147 return ERR_PTR(-ENOENT);
1148}
1149
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001150static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1151{
1152 struct nfs4_opendata *opendata;
1153
Al Viro3d4ff432011-06-22 18:40:12 -04001154 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001155 if (opendata == NULL)
1156 return ERR_PTR(-ENOMEM);
1157 opendata->state = state;
1158 atomic_inc(&state->count);
1159 return opendata;
1160}
1161
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001163{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001164 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 int ret;
1166
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 opendata->o_arg.open_flags = 0;
1168 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001169 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1170 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1171 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001172 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 if (ret != 0)
1174 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001176 if (IS_ERR(newstate))
1177 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001178 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 return 0;
1181}
1182
1183static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1184{
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 int ret;
1187
1188 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 smp_rmb();
1191 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001192 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 if (ret != 0)
1195 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 if (newstate != state)
1197 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 }
1199 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001200 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 if (ret != 0)
1203 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 if (newstate != state)
1205 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 }
1207 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001208 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 if (ret != 0)
1211 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 if (newstate != state)
1213 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001215 /*
1216 * We may have performed cached opens for all three recoveries.
1217 * Check if we need to update the current stateid.
1218 */
1219 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001220 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001221 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001223 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001224 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 return 0;
1227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
1230 * OPEN_RECLAIM:
1231 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001233static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001237 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int status;
1239
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001240 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1241 if (IS_ERR(opendata))
1242 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001243 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1244 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 rcu_read_lock();
1246 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001247 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001248 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001249 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 opendata->o_arg.u.delegation_type = delegation_type;
1251 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001252 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return status;
1254}
1255
Trond Myklebust539cd032007-06-05 11:46:42 -04001256static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 struct nfs_server *server = NFS_SERVER(state->inode);
1259 struct nfs4_exception exception = { };
1260 int err;
1261 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001262 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001263 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001264 break;
1265 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 } while (exception.retry);
1267 return err;
1268}
1269
Trond Myklebust864472e2006-01-03 09:55:15 +01001270static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1271{
1272 struct nfs_open_context *ctx;
1273 int ret;
1274
1275 ctx = nfs4_state_find_open_context(state);
1276 if (IS_ERR(ctx))
1277 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001278 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 put_nfs_open_context(ctx);
1280 return ret;
1281}
1282
Trond Myklebust13437e12007-07-06 15:10:43 -04001283static 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 -07001284{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001288 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1289 if (IS_ERR(opendata))
1290 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001291 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001292 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001294 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001295 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Trond Myklebust13437e12007-07-06 15:10:43 -04001298int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001301 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int err;
1303 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001304 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 switch (err) {
1306 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001307 case -ENOENT:
1308 case -ESTALE:
1309 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001310 case -NFS4ERR_BADSESSION:
1311 case -NFS4ERR_BADSLOT:
1312 case -NFS4ERR_BAD_HIGH_SLOT:
1313 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1314 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001315 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001316 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 case -NFS4ERR_STALE_CLIENTID:
1318 case -NFS4ERR_STALE_STATEID:
1319 case -NFS4ERR_EXPIRED:
1320 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001321 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001322 goto out;
1323 case -ERESTARTSYS:
1324 /*
1325 * The show must go on: exit, but mark the
1326 * stateid as needing recovery.
1327 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001328 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001329 case -NFS4ERR_ADMIN_REVOKED:
1330 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001331 nfs_inode_find_state_and_recover(state->inode,
1332 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001333 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001334 case -EKEYEXPIRED:
1335 /*
1336 * User RPCSEC_GSS context has expired.
1337 * We cannot recover this stateid now, so
1338 * skip it and allow recovery thread to
1339 * proceed.
1340 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001341 case -ENOMEM:
1342 err = 0;
1343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 err = nfs4_handle_exception(server, err, &exception);
1346 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return err;
1349}
1350
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001351static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1352{
1353 struct nfs4_opendata *data = calldata;
1354
1355 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001356 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001357 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001358 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001359 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001360 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001361 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001362}
1363
1364static void nfs4_open_confirm_release(void *calldata)
1365{
1366 struct nfs4_opendata *data = calldata;
1367 struct nfs4_state *state = NULL;
1368
1369 /* If this request hasn't been cancelled, do nothing */
1370 if (data->cancelled == 0)
1371 goto out_free;
1372 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001373 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001374 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001375 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001376 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001377 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001378out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001379 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001380}
1381
1382static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001383 .rpc_call_done = nfs4_open_confirm_done,
1384 .rpc_release = nfs4_open_confirm_release,
1385};
1386
1387/*
1388 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1389 */
1390static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1391{
1392 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1393 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001394 struct rpc_message msg = {
1395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1396 .rpc_argp = &data->c_arg,
1397 .rpc_resp = &data->c_res,
1398 .rpc_cred = data->owner->so_cred,
1399 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001400 struct rpc_task_setup task_setup_data = {
1401 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001402 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001403 .callback_ops = &nfs4_open_confirm_ops,
1404 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001405 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001406 .flags = RPC_TASK_ASYNC,
1407 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int status;
1409
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001410 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001411 data->rpc_done = 0;
1412 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001413 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001414 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001415 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417 status = nfs4_wait_for_completion_rpc_task(task);
1418 if (status != 0) {
1419 data->cancelled = 1;
1420 smp_wmb();
1421 } else
1422 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001423 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return status;
1425}
1426
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429 struct nfs4_opendata *data = calldata;
1430 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001431
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1433 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001434 /*
1435 * Check if we still need to send an OPEN call, or if we can use
1436 * a delegation instead.
1437 */
1438 if (data->state != NULL) {
1439 struct nfs_delegation *delegation;
1440
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001442 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001443 rcu_read_lock();
1444 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001445 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1446 can_open_delegated(delegation, data->o_arg.fmode))
1447 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001448 rcu_read_unlock();
1449 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001450 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001451 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001452 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001454 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001455 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1456 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001457 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001458 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001459 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001460 &data->o_res.seq_res,
1461 task) != 0)
1462 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001463 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001464unlock_no_action:
1465 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001466out_no_action:
1467 task->tk_action = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001468}
1469
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470static void nfs4_open_done(struct rpc_task *task, void *calldata)
1471{
1472 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001474 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001475
Trond Myklebust14516c32010-07-31 14:29:06 -04001476 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1477 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001478
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001480 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1481 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001482 case S_IFREG:
1483 break;
1484 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001486 break;
1487 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001489 break;
1490 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001492 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001493 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001494 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001495 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1496 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001497 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001498 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001500
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501static void nfs4_open_release(void *calldata)
1502{
1503 struct nfs4_opendata *data = calldata;
1504 struct nfs4_state *state = NULL;
1505
1506 /* If this request hasn't been cancelled, do nothing */
1507 if (data->cancelled == 0)
1508 goto out_free;
1509 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001510 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 goto out_free;
1512 /* In case we need an open_confirm, no cleanup! */
1513 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1514 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001516 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001517 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001519 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520}
1521
1522static const struct rpc_call_ops nfs4_open_ops = {
1523 .rpc_call_prepare = nfs4_open_prepare,
1524 .rpc_call_done = nfs4_open_done,
1525 .rpc_release = nfs4_open_release,
1526};
1527
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001528static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529{
1530 struct inode *dir = data->dir->d_inode;
1531 struct nfs_server *server = NFS_SERVER(dir);
1532 struct nfs_openargs *o_arg = &data->o_arg;
1533 struct nfs_openres *o_res = &data->o_res;
1534 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001535 struct rpc_message msg = {
1536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1537 .rpc_argp = o_arg,
1538 .rpc_resp = o_res,
1539 .rpc_cred = data->owner->so_cred,
1540 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001541 struct rpc_task_setup task_setup_data = {
1542 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001543 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001544 .callback_ops = &nfs4_open_ops,
1545 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001546 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001547 .flags = RPC_TASK_ASYNC,
1548 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 int status;
1550
Trond Myklebust9d12b212012-01-17 22:04:25 -05001551 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001552 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001553 data->rpc_done = 0;
1554 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001555 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001556 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001557 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001558 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001559 if (IS_ERR(task))
1560 return PTR_ERR(task);
1561 status = nfs4_wait_for_completion_rpc_task(task);
1562 if (status != 0) {
1563 data->cancelled = 1;
1564 smp_wmb();
1565 } else
1566 status = data->rpc_status;
1567 rpc_put_task(task);
1568
1569 return status;
1570}
1571
1572static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1573{
1574 struct inode *dir = data->dir->d_inode;
1575 struct nfs_openres *o_res = &data->o_res;
1576 int status;
1577
1578 status = nfs4_run_open_task(data, 1);
1579 if (status != 0 || !data->rpc_done)
1580 return status;
1581
Trond Myklebust6926afd2012-01-07 13:22:46 -05001582 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1583
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001584 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1585 status = _nfs4_proc_open_confirm(data);
1586 if (status != 0)
1587 return status;
1588 }
1589
1590 return status;
1591}
1592
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001593static int nfs4_opendata_access(struct rpc_cred *cred,
1594 struct nfs4_opendata *opendata,
1595 struct nfs4_state *state, fmode_t fmode)
1596{
1597 struct nfs_access_entry cache;
1598 u32 mask;
1599
1600 /* access call failed or for some reason the server doesn't
1601 * support any access modes -- defer access call until later */
1602 if (opendata->o_res.access_supported == 0)
1603 return 0;
1604
1605 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001606 /* don't check MAY_WRITE - a newly created file may not have
1607 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001608 if (fmode & FMODE_READ)
1609 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001610 if (fmode & FMODE_EXEC)
1611 mask |= MAY_EXEC;
1612
1613 cache.cred = cred;
1614 cache.jiffies = jiffies;
1615 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1616 nfs_access_add_cache(state->inode, &cache);
1617
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001618 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001619 return 0;
1620
1621 /* even though OPEN succeeded, access is denied. Close the file */
1622 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001623 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001624}
1625
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001626/*
1627 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1628 */
1629static int _nfs4_proc_open(struct nfs4_opendata *data)
1630{
1631 struct inode *dir = data->dir->d_inode;
1632 struct nfs_server *server = NFS_SERVER(dir);
1633 struct nfs_openargs *o_arg = &data->o_arg;
1634 struct nfs_openres *o_res = &data->o_res;
1635 int status;
1636
1637 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001638 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001640 if (status != 0) {
1641 if (status == -NFS4ERR_BADNAME &&
1642 !(o_arg->open_flags & O_CREAT))
1643 return -ENOENT;
1644 return status;
1645 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646
Trond Myklebust6926afd2012-01-07 13:22:46 -05001647 nfs_fattr_map_and_free_names(server, &data->f_attr);
1648
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001649 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001650 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001651 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1652 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001654 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001659 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Andy Adamsond83217c2011-03-01 01:34:17 +00001663static int nfs4_recover_expired_lease(struct nfs_server *server)
1664{
1665 return nfs4_client_recover_expired_lease(server->nfs_client);
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/*
1669 * OPEN_EXPIRED:
1670 * reclaim state on the server after a network partition.
1671 * Assumes caller holds the appropriate lock
1672 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001673static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001675 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001676 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001678 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1679 if (IS_ERR(opendata))
1680 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001681 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001682 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001683 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001685 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001688static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001689{
Trond Myklebust539cd032007-06-05 11:46:42 -04001690 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001691 struct nfs4_exception exception = { };
1692 int err;
1693
1694 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001695 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001696 switch (err) {
1697 default:
1698 goto out;
1699 case -NFS4ERR_GRACE:
1700 case -NFS4ERR_DELAY:
1701 nfs4_handle_exception(server, err, &exception);
1702 err = 0;
1703 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001704 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001705out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001706 return err;
1707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1710{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001712 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 ctx = nfs4_state_find_open_context(state);
1715 if (IS_ERR(ctx))
1716 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001717 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001718 put_nfs_open_context(ctx);
1719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001722#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001723static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001724{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001725 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001726 nfs4_stateid *stateid = &state->stateid;
1727 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001728
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001729 /* If a state reset has been done, test_stateid is unneeded */
1730 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1731 return;
1732
1733 status = nfs41_test_stateid(server, stateid);
1734 if (status != NFS_OK) {
1735 /* Free the stateid unless the server explicitly
1736 * informs us the stateid is unrecognized. */
1737 if (status != -NFS4ERR_BAD_STATEID)
1738 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001739 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001740
Bryan Schumaker69388672012-09-26 15:25:52 -04001741 write_seqlock(&state->seqlock);
1742 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1743 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001744 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1745 }
1746}
1747
1748/**
1749 * nfs41_check_open_stateid - possibly free an open stateid
1750 *
1751 * @state: NFSv4 state for an inode
1752 *
1753 * Returns NFS_OK if recovery for this stateid is now finished.
1754 * Otherwise a negative NFS4ERR value is returned.
1755 */
1756static int nfs41_check_open_stateid(struct nfs4_state *state)
1757{
1758 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001759 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001760 int status;
1761
1762 /* If a state reset has been done, test_stateid is unneeded */
1763 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1764 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1765 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1766 return -NFS4ERR_BAD_STATEID;
1767
1768 status = nfs41_test_stateid(server, stateid);
1769 if (status != NFS_OK) {
1770 /* Free the stateid unless the server explicitly
1771 * informs us the stateid is unrecognized. */
1772 if (status != -NFS4ERR_BAD_STATEID)
1773 nfs41_free_stateid(server, stateid);
1774
1775 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1776 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1777 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001778 }
1779 return status;
1780}
1781
1782static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1783{
Chuck Levereb64cf92012-07-11 16:30:05 -04001784 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001785
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001786 nfs41_clear_delegation_stateid(state);
1787 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001788 if (status != NFS_OK)
1789 status = nfs4_open_expired(sp, state);
1790 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001791}
1792#endif
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001795 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1796 * fields corresponding to attributes that were used to store the verifier.
1797 * Make sure we clobber those fields in the later setattr call
1798 */
1799static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1800{
1801 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1802 !(sattr->ia_valid & ATTR_ATIME_SET))
1803 sattr->ia_valid |= ATTR_ATIME;
1804
1805 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1806 !(sattr->ia_valid & ATTR_MTIME_SET))
1807 sattr->ia_valid |= ATTR_MTIME;
1808}
1809
1810/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001811 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Andy Adamson82be4172012-05-23 05:02:35 -04001813static int _nfs4_do_open(struct inode *dir,
1814 struct dentry *dentry,
1815 fmode_t fmode,
1816 int flags,
1817 struct iattr *sattr,
1818 struct rpc_cred *cred,
1819 struct nfs4_state **res,
1820 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
1822 struct nfs4_state_owner *sp;
1823 struct nfs4_state *state = NULL;
1824 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001825 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001826 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001830 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1831 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1833 goto out_err;
1834 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001835 status = nfs4_recover_expired_lease(server);
1836 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001837 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001838 if (dentry->d_inode != NULL)
1839 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001840 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001841 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001842 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001843 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Andy Adamson82be4172012-05-23 05:02:35 -04001845 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1846 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1847 if (!opendata->f_attr.mdsthreshold)
1848 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001849 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001850 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001851 if (dentry->d_inode != NULL)
1852 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001853
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001854 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001856 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001858 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001859 status = PTR_ERR(state);
1860 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001861 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001862 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001863 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001864
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001865 status = nfs4_opendata_access(cred, opendata, state, fmode);
1866 if (status != 0)
1867 goto err_opendata_put;
1868
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001869 if (opendata->o_arg.open_flags & O_EXCL) {
1870 nfs4_exclusive_attrset(opendata, sattr);
1871
1872 nfs_fattr_init(opendata->o_res.f_attr);
1873 status = nfs4_do_setattr(state->inode, cred,
1874 opendata->o_res.f_attr, sattr,
1875 state);
1876 if (status == 0)
1877 nfs_setattr_update_inode(state->inode, sattr);
1878 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1879 }
Andy Adamson82be4172012-05-23 05:02:35 -04001880
1881 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1882 *ctx_th = opendata->f_attr.mdsthreshold;
1883 else
1884 kfree(opendata->f_attr.mdsthreshold);
1885 opendata->f_attr.mdsthreshold = NULL;
1886
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001887 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 *res = state;
1890 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001891err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001892 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001893 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001894err_put_state_owner:
1895 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 *res = NULL;
1898 return status;
1899}
1900
1901
Andy Adamson82be4172012-05-23 05:02:35 -04001902static struct nfs4_state *nfs4_do_open(struct inode *dir,
1903 struct dentry *dentry,
1904 fmode_t fmode,
1905 int flags,
1906 struct iattr *sattr,
1907 struct rpc_cred *cred,
1908 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 struct nfs4_exception exception = { };
1911 struct nfs4_state *res;
1912 int status;
1913
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001914 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001916 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1917 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (status == 0)
1919 break;
1920 /* NOTE: BAD_SEQID means the server and client disagree about the
1921 * book-keeping w.r.t. state-changing operations
1922 * (OPEN/CLOSE/LOCK/LOCKU...)
1923 * It is actually a sign of a bug on the client or on the server.
1924 *
1925 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001926 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 * have unhashed the old state_owner for us, and that we can
1928 * therefore safely retry using a new one. We should still warn
1929 * the user though...
1930 */
1931 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001932 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001933 " returned a bad sequence-id error!\n",
1934 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 exception.retry = 1;
1936 continue;
1937 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001938 /*
1939 * BAD_STATEID on OPEN means that the server cancelled our
1940 * state before it received the OPEN_CONFIRM.
1941 * Recover by retrying the request as per the discussion
1942 * on Page 181 of RFC3530.
1943 */
1944 if (status == -NFS4ERR_BAD_STATEID) {
1945 exception.retry = 1;
1946 continue;
1947 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001948 if (status == -EAGAIN) {
1949 /* We must have found a delegation */
1950 exception.retry = 1;
1951 continue;
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1954 status, &exception));
1955 } while (exception.retry);
1956 return res;
1957}
1958
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001959static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1960 struct nfs_fattr *fattr, struct iattr *sattr,
1961 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001963 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001965 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 .iap = sattr,
1967 .server = server,
1968 .bitmask = server->attr_bitmask,
1969 };
1970 struct nfs_setattrres res = {
1971 .fattr = fattr,
1972 .server = server,
1973 };
1974 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1976 .rpc_argp = &arg,
1977 .rpc_resp = &res,
1978 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001980 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001981 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Trond Myklebust0e574af2005-10-27 22:12:38 -04001983 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Trond Myklebust4fc87962012-03-08 17:42:01 -05001985 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04001986 struct nfs_lockowner lockowner = {
1987 .l_owner = current->files,
1988 .l_pid = current->tgid,
1989 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05001990 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04001991 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05001992 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1993 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001994 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001995 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001996 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Bryan Schumaker7c513052011-03-24 17:12:24 +00001998 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001999 if (status == 0 && state != NULL)
2000 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002001 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002004static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2005 struct nfs_fattr *fattr, struct iattr *sattr,
2006 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002008 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002009 struct nfs4_exception exception = {
2010 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002011 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002012 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int err;
2014 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002015 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2016 switch (err) {
2017 case -NFS4ERR_OPENMODE:
2018 if (state && !(state->state & FMODE_WRITE)) {
2019 err = -EBADF;
2020 if (sattr->ia_valid & ATTR_OPEN)
2021 err = -EACCES;
2022 goto out;
2023 }
2024 }
2025 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002027out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return err;
2029}
2030
2031struct nfs4_closedata {
2032 struct inode *inode;
2033 struct nfs4_state *state;
2034 struct nfs_closeargs arg;
2035 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002036 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002037 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002038 bool roc;
2039 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040};
2041
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002042static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002043{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002044 struct nfs4_closedata *calldata = data;
2045 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002046 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002047
Fred Isamanf7e89172011-01-06 11:36:32 +00002048 if (calldata->roc)
2049 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002050 nfs4_put_open_state(calldata->state);
2051 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002052 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002053 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002054 kfree(calldata);
2055}
2056
Trond Myklebust88069f72009-12-08 08:33:16 -05002057static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2058 fmode_t fmode)
2059{
2060 spin_lock(&state->owner->so_lock);
2061 if (!(fmode & FMODE_READ))
2062 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2063 if (!(fmode & FMODE_WRITE))
2064 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2065 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2066 spin_unlock(&state->owner->so_lock);
2067}
2068
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002069static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002071 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct nfs_server *server = NFS_SERVER(calldata->inode);
2074
Chuck Levera3ca5652012-03-01 17:00:40 -05002075 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002076 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2077 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 /* hmm. we are done with the inode, and in the process of freeing
2079 * the state_owner. we keep this around to process errors
2080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 switch (task->tk_status) {
2082 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002083 if (calldata->roc)
2084 pnfs_roc_set_barrier(state->inode,
2085 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002086 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002087 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002088 nfs4_close_clear_stateid_flags(state,
2089 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 break;
2091 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002092 case -NFS4ERR_OLD_STATEID:
2093 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002095 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002098 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2099 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002101 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002102 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002103 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002106static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002108 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002109 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002110 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002111 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002112
Chuck Levera3ca5652012-03-01 17:00:40 -05002113 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002114 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002115 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002116
Trond Myklebust88069f72009-12-08 08:33:16 -05002117 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2118 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002119 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002120 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002121 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002122 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002123 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2124 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2125 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002126 }
2127 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002128 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2129 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2130 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002131 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002132 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002133 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002134
2135 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002136 /* Note: exit _without_ calling nfs4_close_done */
2137 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002138 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002139 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002140
Fred Isamanf7e89172011-01-06 11:36:32 +00002141 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002142 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002143 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002144 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002145 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002146 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002147
Trond Myklebust516a6af2005-10-27 22:12:41 -04002148 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002149 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002150 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002151 &calldata->arg.seq_args,
2152 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002153 task) != 0)
2154 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002155out:
2156 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002159static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002160 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002161 .rpc_call_done = nfs4_close_done,
2162 .rpc_release = nfs4_free_closedata,
2163};
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165/*
2166 * It is possible for data to be read/written from a mem-mapped file
2167 * after the sys_close call (which hits the vfs layer as a flush).
2168 * This means that we can't safely call nfsv4 close on a file until
2169 * the inode is cleared. This in turn means that we are not good
2170 * NFSv4 citizens - we do not indicate to the server to update the file's
2171 * share state even when we are done with one of the three share
2172 * stateid's in the inode.
2173 *
2174 * NOTE: Caller must be holding the sp->so_owner semaphore!
2175 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002176int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002178 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002180 struct nfs4_state_owner *sp = state->owner;
2181 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002182 struct rpc_message msg = {
2183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2184 .rpc_cred = state->owner->so_cred,
2185 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002186 struct rpc_task_setup task_setup_data = {
2187 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002188 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002190 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002191 .flags = RPC_TASK_ASYNC,
2192 };
Trond Myklebust95121352005-10-18 14:20:12 -07002193 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002195 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002197 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002198 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002199 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002201 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002202 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002204 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002205 if (calldata->arg.seqid == NULL)
2206 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002207 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002208 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002209 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002210 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002211 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002212 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002213 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002214
Trond Myklebust1174dd12010-12-21 10:52:24 -05002215 msg.rpc_argp = &calldata->arg;
2216 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002217 task_setup_data.callback_data = calldata;
2218 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002219 if (IS_ERR(task))
2220 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002221 status = 0;
2222 if (wait)
2223 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002224 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002225 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002226out_free_calldata:
2227 kfree(calldata);
2228out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002229 nfs4_put_open_state(state);
2230 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002231 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Trond Myklebust2b484292010-09-17 10:56:51 -04002234static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002235nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct nfs4_state *state;
2238
Trond Myklebust565277f2007-10-15 18:17:53 -04002239 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002240 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2241 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002242 if (IS_ERR(state))
2243 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002244 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002245 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Trond Myklebust1185a552009-12-03 15:54:02 -05002248static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002249{
2250 if (ctx->state == NULL)
2251 return;
2252 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002253 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002254 else
Al Viro643168c2011-06-22 18:20:23 -04002255 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2259{
Benny Halevy43652ad2009-04-01 09:21:54 -04002260 struct nfs4_server_caps_arg args = {
2261 .fhandle = fhandle,
2262 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 struct nfs4_server_caps_res res = {};
2264 struct rpc_message msg = {
2265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002266 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 .rpc_resp = &res,
2268 };
2269 int status;
2270
Bryan Schumaker7c513052011-03-24 17:12:24 +00002271 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (status == 0) {
2273 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002274 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2275 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2276 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2277 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2278 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2280 server->caps |= NFS_CAP_ACLS;
2281 if (res.has_links != 0)
2282 server->caps |= NFS_CAP_HARDLINKS;
2283 if (res.has_symlinks != 0)
2284 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002285 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2286 server->caps |= NFS_CAP_FILEID;
2287 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2288 server->caps |= NFS_CAP_MODE;
2289 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2290 server->caps |= NFS_CAP_NLINK;
2291 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2292 server->caps |= NFS_CAP_OWNER;
2293 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2294 server->caps |= NFS_CAP_OWNER_GROUP;
2295 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2296 server->caps |= NFS_CAP_ATIME;
2297 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2298 server->caps |= NFS_CAP_CTIME;
2299 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2300 server->caps |= NFS_CAP_MTIME;
2301
Trond Myklebusta65318b2009-03-11 14:10:28 -04002302 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2303 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2304 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002306 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return status;
2310}
2311
Trond Myklebust55a97592006-06-09 09:34:19 -04002312int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 struct nfs4_exception exception = { };
2315 int err;
2316 do {
2317 err = nfs4_handle_exception(server,
2318 _nfs4_server_capabilities(server, fhandle),
2319 &exception);
2320 } while (exception.retry);
2321 return err;
2322}
2323
2324static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2325 struct nfs_fsinfo *info)
2326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct nfs4_lookup_root_arg args = {
2328 .bitmask = nfs4_fattr_bitmap,
2329 };
2330 struct nfs4_lookup_res res = {
2331 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002332 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 .fh = fhandle,
2334 };
2335 struct rpc_message msg = {
2336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2337 .rpc_argp = &args,
2338 .rpc_resp = &res,
2339 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002340
Trond Myklebust0e574af2005-10-27 22:12:38 -04002341 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002342 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2346 struct nfs_fsinfo *info)
2347{
2348 struct nfs4_exception exception = { };
2349 int err;
2350 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002351 err = _nfs4_lookup_root(server, fhandle, info);
2352 switch (err) {
2353 case 0:
2354 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002355 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002356 default:
2357 err = nfs4_handle_exception(server, err, &exception);
2358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002360out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return err;
2362}
2363
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002364static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2365 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2366{
2367 struct rpc_auth *auth;
2368 int ret;
2369
2370 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002371 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002372 ret = -EIO;
2373 goto out;
2374 }
2375 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002376out:
2377 return ret;
2378}
2379
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002380static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002381 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002383 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002384 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002386 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002387 if (len < 0)
2388 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002389
2390 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002391 /* AUTH_UNIX is the default flavor if none was specified,
2392 * thus has already been tried. */
2393 if (flav_array[i] == RPC_AUTH_UNIX)
2394 continue;
2395
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002396 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002397 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002398 continue;
2399 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002400 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002401 /*
2402 * -EACCESS could mean that the user doesn't have correct permissions
2403 * to access the mount. It could also mean that we tried to mount
2404 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2405 * existing mount programs don't handle -EACCES very well so it should
2406 * be mapped to -EPERM instead.
2407 */
2408 if (status == -EACCES)
2409 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002410 return status;
2411}
2412
2413/*
2414 * get the file handle for the "/" directory on the server
2415 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002416int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2417 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002418{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002419 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002420 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002421 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2422 /*
2423 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2424 * by nfs4_map_errors() as this function exits.
2425 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002426 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (status == 0)
2428 status = nfs4_server_capabilities(server, fhandle);
2429 if (status == 0)
2430 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002431 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Bryan Schumakerbae36242012-05-10 15:07:31 -04002434static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2435 struct nfs_fsinfo *info)
2436{
2437 int error;
2438 struct nfs_fattr *fattr = info->fattr;
2439
2440 error = nfs4_server_capabilities(server, mntfh);
2441 if (error < 0) {
2442 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2443 return error;
2444 }
2445
2446 error = nfs4_proc_getattr(server, mntfh, fattr);
2447 if (error < 0) {
2448 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2449 return error;
2450 }
2451
2452 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2453 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2454 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2455
2456 return error;
2457}
2458
Manoj Naik6b97fd32006-06-09 09:34:29 -04002459/*
2460 * Get locations and (maybe) other attributes of a referral.
2461 * Note that we'll actually follow the referral later when
2462 * we detect fsid mismatch in inode revalidation
2463 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002464static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2465 const struct qstr *name, struct nfs_fattr *fattr,
2466 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002467{
2468 int status = -ENOMEM;
2469 struct page *page = NULL;
2470 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002471
2472 page = alloc_page(GFP_KERNEL);
2473 if (page == NULL)
2474 goto out;
2475 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2476 if (locations == NULL)
2477 goto out;
2478
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002479 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002480 if (status != 0)
2481 goto out;
2482 /* Make sure server returned a different fsid for the referral */
2483 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002484 dprintk("%s: server did not return a different fsid for"
2485 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002486 status = -EIO;
2487 goto out;
2488 }
Andy Adamson533eb462011-06-13 18:25:56 -04002489 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2490 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002491
Andy Adamson533eb462011-06-13 18:25:56 -04002492 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002493 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002494 memset(fhandle, 0, sizeof(struct nfs_fh));
2495out:
2496 if (page)
2497 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002498 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002499 return status;
2500}
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2503{
2504 struct nfs4_getattr_arg args = {
2505 .fh = fhandle,
2506 .bitmask = server->attr_bitmask,
2507 };
2508 struct nfs4_getattr_res res = {
2509 .fattr = fattr,
2510 .server = server,
2511 };
2512 struct rpc_message msg = {
2513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2514 .rpc_argp = &args,
2515 .rpc_resp = &res,
2516 };
2517
Trond Myklebust0e574af2005-10-27 22:12:38 -04002518 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002519 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2523{
2524 struct nfs4_exception exception = { };
2525 int err;
2526 do {
2527 err = nfs4_handle_exception(server,
2528 _nfs4_proc_getattr(server, fhandle, fattr),
2529 &exception);
2530 } while (exception.retry);
2531 return err;
2532}
2533
2534/*
2535 * The file is not closed if it is opened due to the a request to change
2536 * the size of the file. The open call will not be needed once the
2537 * VFS layer lookup-intents are implemented.
2538 *
2539 * Close is called when the inode is destroyed.
2540 * If we haven't opened the file for O_WRONLY, we
2541 * need to in the size_change case to obtain a stateid.
2542 *
2543 * Got race?
2544 * Because OPEN is always done by name in nfsv4, it is
2545 * possible that we opened a different file by the same
2546 * name. We can recognize this race condition, but we
2547 * can't do anything about it besides returning an error.
2548 *
2549 * This will be fixed with VFS changes (lookup-intent).
2550 */
2551static int
2552nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2553 struct iattr *sattr)
2554{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002555 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002556 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002557 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 int status;
2559
Benny Halevy8a1636c2010-07-14 15:43:57 -04002560 if (pnfs_ld_layoutret_on_setattr(inode))
2561 pnfs_return_layout(inode);
2562
Trond Myklebust0e574af2005-10-27 22:12:38 -04002563 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Andy Adamson26699402012-05-30 16:12:24 -04002565 /* Deal with open(O_TRUNC) */
2566 if (sattr->ia_valid & ATTR_OPEN)
2567 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2568
2569 /* Optimization: if the end result is no change, don't RPC */
2570 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2571 return 0;
2572
Trond Myklebustd5308382005-11-04 15:33:38 -05002573 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002574 if (sattr->ia_valid & ATTR_FILE) {
2575 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002576
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002577 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002578 if (ctx) {
2579 cred = ctx->cred;
2580 state = ctx->state;
2581 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002582 }
2583
2584 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002585 if (status == 0)
2586 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return status;
2588}
2589
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002590static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2591 const struct qstr *name, struct nfs_fh *fhandle,
2592 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002593{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002594 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002595 int status;
2596 struct nfs4_lookup_arg args = {
2597 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002598 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002599 .name = name,
2600 };
2601 struct nfs4_lookup_res res = {
2602 .server = server,
2603 .fattr = fattr,
2604 .fh = fhandle,
2605 };
2606 struct rpc_message msg = {
2607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2608 .rpc_argp = &args,
2609 .rpc_resp = &res,
2610 };
2611
2612 nfs_fattr_init(fattr);
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002615 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 dprintk("NFS reply lookup: %d\n", status);
2617 return status;
2618}
2619
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002620static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002621{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002622 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002623 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002624 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2625 fattr->nlink = 2;
2626}
2627
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002628static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2629 struct qstr *name, struct nfs_fh *fhandle,
2630 struct nfs_fattr *fattr)
2631{
2632 struct nfs4_exception exception = { };
2633 struct rpc_clnt *client = *clnt;
2634 int err;
2635 do {
2636 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2637 switch (err) {
2638 case -NFS4ERR_BADNAME:
2639 err = -ENOENT;
2640 goto out;
2641 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002642 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002643 goto out;
2644 case -NFS4ERR_WRONGSEC:
2645 err = -EPERM;
2646 if (client != *clnt)
2647 goto out;
2648
2649 client = nfs4_create_sec_client(client, dir, name);
2650 if (IS_ERR(client))
2651 return PTR_ERR(client);
2652
2653 exception.retry = 1;
2654 break;
2655 default:
2656 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2657 }
2658 } while (exception.retry);
2659
2660out:
2661 if (err == 0)
2662 *clnt = client;
2663 else if (client != *clnt)
2664 rpc_shutdown_client(client);
2665
2666 return err;
2667}
2668
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002669static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002670 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002672 int status;
2673 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002674
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002675 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2676 if (client != NFS_CLIENT(dir)) {
2677 rpc_shutdown_client(client);
2678 nfs_fixup_secinfo_attributes(fattr);
2679 }
2680 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002683struct rpc_clnt *
2684nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2685 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2686{
2687 int status;
2688 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2689
2690 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2691 if (status < 0) {
2692 rpc_shutdown_client(client);
2693 return ERR_PTR(status);
2694 }
2695 return client;
2696}
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2699{
Trond Myklebust76b32992007-08-10 17:45:11 -04002700 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 struct nfs4_accessargs args = {
2702 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002703 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002705 struct nfs4_accessres res = {
2706 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002707 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 struct rpc_message msg = {
2709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2710 .rpc_argp = &args,
2711 .rpc_resp = &res,
2712 .rpc_cred = entry->cred,
2713 };
2714 int mode = entry->mask;
2715 int status;
2716
2717 /*
2718 * Determine which access bits we want to ask for...
2719 */
2720 if (mode & MAY_READ)
2721 args.access |= NFS4_ACCESS_READ;
2722 if (S_ISDIR(inode->i_mode)) {
2723 if (mode & MAY_WRITE)
2724 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2725 if (mode & MAY_EXEC)
2726 args.access |= NFS4_ACCESS_LOOKUP;
2727 } else {
2728 if (mode & MAY_WRITE)
2729 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2730 if (mode & MAY_EXEC)
2731 args.access |= NFS4_ACCESS_EXECUTE;
2732 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002733
2734 res.fattr = nfs_alloc_fattr();
2735 if (res.fattr == NULL)
2736 return -ENOMEM;
2737
Bryan Schumaker7c513052011-03-24 17:12:24 +00002738 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002740 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002741 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002743 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return status;
2745}
2746
2747static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2748{
2749 struct nfs4_exception exception = { };
2750 int err;
2751 do {
2752 err = nfs4_handle_exception(NFS_SERVER(inode),
2753 _nfs4_proc_access(inode, entry),
2754 &exception);
2755 } while (exception.retry);
2756 return err;
2757}
2758
2759/*
2760 * TODO: For the time being, we don't try to get any attributes
2761 * along with any of the zero-copy operations READ, READDIR,
2762 * READLINK, WRITE.
2763 *
2764 * In the case of the first three, we want to put the GETATTR
2765 * after the read-type operation -- this is because it is hard
2766 * to predict the length of a GETATTR response in v4, and thus
2767 * align the READ data correctly. This means that the GETATTR
2768 * may end up partially falling into the page cache, and we should
2769 * shift it into the 'tail' of the xdr_buf before processing.
2770 * To do this efficiently, we need to know the total length
2771 * of data received, which doesn't seem to be available outside
2772 * of the RPC layer.
2773 *
2774 * In the case of WRITE, we also want to put the GETATTR after
2775 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002776 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 *
2778 * Both of these changes to the XDR layer would in fact be quite
2779 * minor, but I decided to leave them for a subsequent patch.
2780 */
2781static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2782 unsigned int pgbase, unsigned int pglen)
2783{
2784 struct nfs4_readlink args = {
2785 .fh = NFS_FH(inode),
2786 .pgbase = pgbase,
2787 .pglen = pglen,
2788 .pages = &page,
2789 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002790 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 struct rpc_message msg = {
2792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2793 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002794 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 };
2796
Bryan Schumaker7c513052011-03-24 17:12:24 +00002797 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 -07002798}
2799
2800static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2801 unsigned int pgbase, unsigned int pglen)
2802{
2803 struct nfs4_exception exception = { };
2804 int err;
2805 do {
2806 err = nfs4_handle_exception(NFS_SERVER(inode),
2807 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2808 &exception);
2809 } while (exception.retry);
2810 return err;
2811}
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002814 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816static int
2817nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002818 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002820 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 int status = 0;
2823
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002824 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2825 if (IS_ERR(ctx))
2826 return PTR_ERR(ctx);
2827
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002828 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002829 state = nfs4_do_open(dir, dentry, ctx->mode,
2830 flags, sattr, ctx->cred,
2831 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002832 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (IS_ERR(state)) {
2834 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002837 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002838 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002839 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002841 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return status;
2843}
2844
2845static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2846{
Trond Myklebust16e42952005-10-27 22:12:44 -04002847 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002848 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002850 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002852 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002853 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002854 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2857 .rpc_argp = &args,
2858 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002860 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002861
Bryan Schumaker7c513052011-03-24 17:12:24 +00002862 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002863 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002864 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return status;
2866}
2867
2868static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2869{
2870 struct nfs4_exception exception = { };
2871 int err;
2872 do {
2873 err = nfs4_handle_exception(NFS_SERVER(dir),
2874 _nfs4_proc_remove(dir, name),
2875 &exception);
2876 } while (exception.retry);
2877 return err;
2878}
2879
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002880static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002882 struct nfs_server *server = NFS_SERVER(dir);
2883 struct nfs_removeargs *args = msg->rpc_argp;
2884 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002886 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002888 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002891static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2892{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002893 nfs4_setup_sequence(NFS_SERVER(data->dir),
2894 &data->args.seq_args,
2895 &data->res.seq_res,
2896 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002899static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002901 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2902
Trond Myklebust14516c32010-07-31 14:29:06 -04002903 if (!nfs4_sequence_done(task, &res->seq_res))
2904 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002905 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002906 return 0;
2907 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002908 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
Jeff Laytond3d41522010-09-17 17:31:57 -04002911static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2912{
2913 struct nfs_server *server = NFS_SERVER(dir);
2914 struct nfs_renameargs *arg = msg->rpc_argp;
2915 struct nfs_renameres *res = msg->rpc_resp;
2916
2917 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002918 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002919 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002920}
2921
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002922static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2923{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002924 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2925 &data->args.seq_args,
2926 &data->res.seq_res,
2927 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002928}
2929
2930static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2931 struct inode *new_dir)
2932{
2933 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2934
2935 if (!nfs4_sequence_done(task, &res->seq_res))
2936 return 0;
2937 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2938 return 0;
2939
2940 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002941 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002942 return 1;
2943}
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2946 struct inode *new_dir, struct qstr *new_name)
2947{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002948 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002949 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 .old_dir = NFS_FH(old_dir),
2951 .new_dir = NFS_FH(new_dir),
2952 .old_name = old_name,
2953 .new_name = new_name,
2954 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002955 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002956 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002957 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct rpc_message msg = {
2959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2960 .rpc_argp = &arg,
2961 .rpc_resp = &res,
2962 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002963 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Bryan Schumaker7c513052011-03-24 17:12:24 +00002965 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (!status) {
2967 update_changeattr(old_dir, &res.old_cinfo);
2968 update_changeattr(new_dir, &res.new_cinfo);
2969 }
2970 return status;
2971}
2972
2973static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2974 struct inode *new_dir, struct qstr *new_name)
2975{
2976 struct nfs4_exception exception = { };
2977 int err;
2978 do {
2979 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2980 _nfs4_proc_rename(old_dir, old_name,
2981 new_dir, new_name),
2982 &exception);
2983 } while (exception.retry);
2984 return err;
2985}
2986
2987static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2988{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002989 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 struct nfs4_link_arg arg = {
2991 .fh = NFS_FH(inode),
2992 .dir_fh = NFS_FH(dir),
2993 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002994 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002996 struct nfs4_link_res res = {
2997 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 struct rpc_message msg = {
3000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3001 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003002 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003004 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Trond Myklebust136f2622010-04-16 16:22:49 -04003006 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003007 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003008 goto out;
3009
Bryan Schumaker7c513052011-03-24 17:12:24 +00003010 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003011 if (!status) {
3012 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003013 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003014 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003015out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003016 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return status;
3018}
3019
3020static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3021{
3022 struct nfs4_exception exception = { };
3023 int err;
3024 do {
3025 err = nfs4_handle_exception(NFS_SERVER(inode),
3026 _nfs4_proc_link(inode, dir, name),
3027 &exception);
3028 } while (exception.retry);
3029 return err;
3030}
3031
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003032struct nfs4_createdata {
3033 struct rpc_message msg;
3034 struct nfs4_create_arg arg;
3035 struct nfs4_create_res res;
3036 struct nfs_fh fh;
3037 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003038};
3039
3040static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3041 struct qstr *name, struct iattr *sattr, u32 ftype)
3042{
3043 struct nfs4_createdata *data;
3044
3045 data = kzalloc(sizeof(*data), GFP_KERNEL);
3046 if (data != NULL) {
3047 struct nfs_server *server = NFS_SERVER(dir);
3048
3049 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3050 data->msg.rpc_argp = &data->arg;
3051 data->msg.rpc_resp = &data->res;
3052 data->arg.dir_fh = NFS_FH(dir);
3053 data->arg.server = server;
3054 data->arg.name = name;
3055 data->arg.attrs = sattr;
3056 data->arg.ftype = ftype;
3057 data->arg.bitmask = server->attr_bitmask;
3058 data->res.server = server;
3059 data->res.fh = &data->fh;
3060 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003061 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003062 }
3063 return data;
3064}
3065
3066static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3067{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003068 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003069 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003070 if (status == 0) {
3071 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3073 }
3074 return status;
3075}
3076
3077static void nfs4_free_createdata(struct nfs4_createdata *data)
3078{
3079 kfree(data);
3080}
3081
Chuck Lever4f390c12006-08-22 20:06:22 -04003082static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003083 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003085 struct nfs4_createdata *data;
3086 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Chuck Lever94a6d752006-08-22 20:06:23 -04003088 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003089 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003090
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003091 status = -ENOMEM;
3092 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3093 if (data == NULL)
3094 goto out;
3095
3096 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3097 data->arg.u.symlink.pages = &page;
3098 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003100 status = nfs4_do_create(dir, dentry, data);
3101
3102 nfs4_free_createdata(data);
3103out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return status;
3105}
3106
Chuck Lever4f390c12006-08-22 20:06:22 -04003107static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003108 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
3110 struct nfs4_exception exception = { };
3111 int err;
3112 do {
3113 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003114 _nfs4_proc_symlink(dir, dentry, page,
3115 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 &exception);
3117 } while (exception.retry);
3118 return err;
3119}
3120
3121static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3122 struct iattr *sattr)
3123{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003124 struct nfs4_createdata *data;
3125 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3128 if (data == NULL)
3129 goto out;
3130
3131 status = nfs4_do_create(dir, dentry, data);
3132
3133 nfs4_free_createdata(data);
3134out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return status;
3136}
3137
3138static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3139 struct iattr *sattr)
3140{
3141 struct nfs4_exception exception = { };
3142 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003143
3144 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 do {
3146 err = nfs4_handle_exception(NFS_SERVER(dir),
3147 _nfs4_proc_mkdir(dir, dentry, sattr),
3148 &exception);
3149 } while (exception.retry);
3150 return err;
3151}
3152
3153static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003154 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 struct inode *dir = dentry->d_inode;
3157 struct nfs4_readdir_arg args = {
3158 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003159 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 .pgbase = 0,
3161 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003162 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003163 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 };
3165 struct nfs4_readdir_res res;
3166 struct rpc_message msg = {
3167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3168 .rpc_argp = &args,
3169 .rpc_resp = &res,
3170 .rpc_cred = cred,
3171 };
3172 int status;
3173
Harvey Harrison3110ff82008-05-02 13:42:44 -07003174 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003175 dentry->d_parent->d_name.name,
3176 dentry->d_name.name,
3177 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003178 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003180 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 -05003181 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003182 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003183 status += args.pgbase;
3184 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003185
3186 nfs_invalidate_atime(dir);
3187
Harvey Harrison3110ff82008-05-02 13:42:44 -07003188 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return status;
3190}
3191
3192static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003193 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
3195 struct nfs4_exception exception = { };
3196 int err;
3197 do {
3198 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3199 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003200 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 &exception);
3202 } while (exception.retry);
3203 return err;
3204}
3205
3206static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3207 struct iattr *sattr, dev_t rdev)
3208{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003209 struct nfs4_createdata *data;
3210 int mode = sattr->ia_mode;
3211 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003213 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3214 if (data == NULL)
3215 goto out;
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003218 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003220 data->arg.ftype = NF4BLK;
3221 data->arg.u.device.specdata1 = MAJOR(rdev);
3222 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
3224 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003225 data->arg.ftype = NF4CHR;
3226 data->arg.u.device.specdata1 = MAJOR(rdev);
3227 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003228 } else if (!S_ISSOCK(mode)) {
3229 status = -EINVAL;
3230 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003233 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003234out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003235 nfs4_free_createdata(data);
3236out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return status;
3238}
3239
3240static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3241 struct iattr *sattr, dev_t rdev)
3242{
3243 struct nfs4_exception exception = { };
3244 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003245
3246 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 do {
3248 err = nfs4_handle_exception(NFS_SERVER(dir),
3249 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3250 &exception);
3251 } while (exception.retry);
3252 return err;
3253}
3254
3255static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3256 struct nfs_fsstat *fsstat)
3257{
3258 struct nfs4_statfs_arg args = {
3259 .fh = fhandle,
3260 .bitmask = server->attr_bitmask,
3261 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003262 struct nfs4_statfs_res res = {
3263 .fsstat = fsstat,
3264 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct rpc_message msg = {
3266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3267 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003268 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 };
3270
Trond Myklebust0e574af2005-10-27 22:12:38 -04003271 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003272 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
3275static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3276{
3277 struct nfs4_exception exception = { };
3278 int err;
3279 do {
3280 err = nfs4_handle_exception(server,
3281 _nfs4_proc_statfs(server, fhandle, fsstat),
3282 &exception);
3283 } while (exception.retry);
3284 return err;
3285}
3286
3287static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3288 struct nfs_fsinfo *fsinfo)
3289{
3290 struct nfs4_fsinfo_arg args = {
3291 .fh = fhandle,
3292 .bitmask = server->attr_bitmask,
3293 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003294 struct nfs4_fsinfo_res res = {
3295 .fsinfo = fsinfo,
3296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 struct rpc_message msg = {
3298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3299 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003300 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 };
3302
Bryan Schumaker7c513052011-03-24 17:12:24 +00003303 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
3306static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3307{
3308 struct nfs4_exception exception = { };
3309 int err;
3310
3311 do {
3312 err = nfs4_handle_exception(server,
3313 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3314 &exception);
3315 } while (exception.retry);
3316 return err;
3317}
3318
3319static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3320{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003321 int error;
3322
Trond Myklebust0e574af2005-10-27 22:12:38 -04003323 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003324 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003325 if (error == 0) {
3326 /* block layout checks this! */
3327 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003328 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003329 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003330
3331 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
3334static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3335 struct nfs_pathconf *pathconf)
3336{
3337 struct nfs4_pathconf_arg args = {
3338 .fh = fhandle,
3339 .bitmask = server->attr_bitmask,
3340 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003341 struct nfs4_pathconf_res res = {
3342 .pathconf = pathconf,
3343 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct rpc_message msg = {
3345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3346 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003347 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 };
3349
3350 /* None of the pathconf attributes are mandatory to implement */
3351 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3352 memset(pathconf, 0, sizeof(*pathconf));
3353 return 0;
3354 }
3355
Trond Myklebust0e574af2005-10-27 22:12:38 -04003356 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003357 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
3360static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3361 struct nfs_pathconf *pathconf)
3362{
3363 struct nfs4_exception exception = { };
3364 int err;
3365
3366 do {
3367 err = nfs4_handle_exception(server,
3368 _nfs4_proc_pathconf(server, fhandle, pathconf),
3369 &exception);
3370 } while (exception.retry);
3371 return err;
3372}
3373
Benny Halevyd20581a2011-05-22 19:52:03 +03003374void __nfs4_read_done_cb(struct nfs_read_data *data)
3375{
Fred Isamancd841602012-04-20 14:47:44 -04003376 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003377}
3378
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003379static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Fred Isamancd841602012-04-20 14:47:44 -04003381 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003383 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003384 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003385 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003387
Benny Halevyd20581a2011-05-22 19:52:03 +03003388 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003390 renew_lease(server, data->timestamp);
3391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003394static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3395{
3396
3397 dprintk("--> %s\n", __func__);
3398
3399 if (!nfs4_sequence_done(task, &data->res.seq_res))
3400 return -EAGAIN;
3401
Benny Halevyd20581a2011-05-22 19:52:03 +03003402 return data->read_done_cb ? data->read_done_cb(task, data) :
3403 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003404}
3405
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003406static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003409 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003410 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003411 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003414static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3415{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003416 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3417 &data->args.seq_args,
3418 &data->res.seq_res,
3419 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
3421
Fred Isamanb029bc92011-03-03 15:13:42 +00003422static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Fred Isamancd841602012-04-20 14:47:44 -04003424 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003426 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003427 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003428 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003430 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003432 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003433 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Fred Isamanb029bc92011-03-03 15:13:42 +00003437static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3438{
3439 if (!nfs4_sequence_done(task, &data->res.seq_res))
3440 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003441 return data->write_done_cb ? data->write_done_cb(task, data) :
3442 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003443}
3444
Trond Myklebust5a37f852012-04-28 14:55:16 -04003445static
3446bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003447{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003448 const struct nfs_pgio_header *hdr = data->header;
3449
3450 /* Don't request attributes for pNFS or O_DIRECT writes */
3451 if (data->ds_clp != NULL || hdr->dreq != NULL)
3452 return false;
3453 /* Otherwise, request attributes if and only if we don't hold
3454 * a delegation
3455 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003456 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003457}
Fred Isamana69aef12011-03-03 15:13:47 +00003458
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003459static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
Fred Isamancd841602012-04-20 14:47:44 -04003461 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003462
Trond Myklebust5a37f852012-04-28 14:55:16 -04003463 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003464 data->args.bitmask = NULL;
3465 data->res.fattr = NULL;
3466 } else
3467 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003468
Fred Isamanb029bc92011-03-03 15:13:42 +00003469 if (!data->write_done_cb)
3470 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003471 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 data->timestamp = jiffies;
3473
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003474 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003475 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003478static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3479{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003480 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3481 &data->args.seq_args,
3482 &data->res.seq_res,
3483 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
Fred Isaman0b7c0152012-04-20 14:47:39 -04003486static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3487{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003488 nfs4_setup_sequence(NFS_SERVER(data->inode),
3489 &data->args.seq_args,
3490 &data->res.seq_res,
3491 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Fred Isaman0b7c0152012-04-20 14:47:39 -04003494static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003497
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003498 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003499 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003500 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
Fred Isaman0b7c0152012-04-20 14:47:39 -04003505static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003506{
3507 if (!nfs4_sequence_done(task, &data->res.seq_res))
3508 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003509 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003510}
3511
Fred Isaman0b7c0152012-04-20 14:47:39 -04003512static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003514 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003515
Fred Isaman0b7c0152012-04-20 14:47:39 -04003516 if (data->commit_done_cb == NULL)
3517 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003518 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003519 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003520 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003523struct nfs4_renewdata {
3524 struct nfs_client *client;
3525 unsigned long timestamp;
3526};
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528/*
3529 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3530 * standalone procedure for queueing an asynchronous RENEW.
3531 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003532static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003533{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003534 struct nfs4_renewdata *data = calldata;
3535 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003536
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003537 if (atomic_read(&clp->cl_count) > 1)
3538 nfs4_schedule_state_renewal(clp);
3539 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003540 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003541}
3542
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003543static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003545 struct nfs4_renewdata *data = calldata;
3546 struct nfs_client *clp = data->client;
3547 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003550 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003551 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3552 return;
3553 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003554 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003555 return;
3556 }
3557 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003559 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003562static const struct rpc_call_ops nfs4_renew_ops = {
3563 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003564 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003565};
3566
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003567static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
3569 struct rpc_message msg = {
3570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3571 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003572 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003574 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003576 if (renew_flags == 0)
3577 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003578 if (!atomic_inc_not_zero(&clp->cl_count))
3579 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003580 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003581 if (data == NULL)
3582 return -ENOMEM;
3583 data->client = clp;
3584 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003586 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003589static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
3591 struct rpc_message msg = {
3592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3593 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003594 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 };
3596 unsigned long now = jiffies;
3597 int status;
3598
3599 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3600 if (status < 0)
3601 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003602 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 return 0;
3604}
3605
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003606static inline int nfs4_server_supports_acls(struct nfs_server *server)
3607{
3608 return (server->caps & NFS_CAP_ACLS)
3609 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3610 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3611}
3612
Trond Myklebust21f498c2012-08-24 10:59:25 -04003613/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3614 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003615 * the stack.
3616 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003617#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003618
Neil Hormane9e3d722011-03-04 19:26:03 -05003619static int buf_to_pages_noslab(const void *buf, size_t buflen,
3620 struct page **pages, unsigned int *pgbase)
3621{
3622 struct page *newpage, **spages;
3623 int rc = 0;
3624 size_t len;
3625 spages = pages;
3626
3627 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003628 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003629 newpage = alloc_page(GFP_KERNEL);
3630
3631 if (newpage == NULL)
3632 goto unwind;
3633 memcpy(page_address(newpage), buf, len);
3634 buf += len;
3635 buflen -= len;
3636 *pages++ = newpage;
3637 rc++;
3638 } while (buflen != 0);
3639
3640 return rc;
3641
3642unwind:
3643 for(; rc > 0; rc--)
3644 __free_page(spages[rc-1]);
3645 return -ENOMEM;
3646}
3647
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003648struct nfs4_cached_acl {
3649 int cached;
3650 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003651 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003652};
3653
3654static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003655{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003656 struct nfs_inode *nfsi = NFS_I(inode);
3657
3658 spin_lock(&inode->i_lock);
3659 kfree(nfsi->nfs4_acl);
3660 nfsi->nfs4_acl = acl;
3661 spin_unlock(&inode->i_lock);
3662}
3663
3664static void nfs4_zap_acl_attr(struct inode *inode)
3665{
3666 nfs4_set_cached_acl(inode, NULL);
3667}
3668
3669static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3670{
3671 struct nfs_inode *nfsi = NFS_I(inode);
3672 struct nfs4_cached_acl *acl;
3673 int ret = -ENOENT;
3674
3675 spin_lock(&inode->i_lock);
3676 acl = nfsi->nfs4_acl;
3677 if (acl == NULL)
3678 goto out;
3679 if (buf == NULL) /* user is just asking for length */
3680 goto out_len;
3681 if (acl->cached == 0)
3682 goto out;
3683 ret = -ERANGE; /* see getxattr(2) man page */
3684 if (acl->len > buflen)
3685 goto out;
3686 memcpy(buf, acl->data, acl->len);
3687out_len:
3688 ret = acl->len;
3689out:
3690 spin_unlock(&inode->i_lock);
3691 return ret;
3692}
3693
Sachin Prabhu5794d212012-04-17 14:36:40 +01003694static 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 +00003695{
3696 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003697 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003698
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003699 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003700 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003701 if (acl == NULL)
3702 goto out;
3703 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003704 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003705 } else {
3706 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3707 if (acl == NULL)
3708 goto out;
3709 acl->cached = 0;
3710 }
3711 acl->len = acl_len;
3712out:
3713 nfs4_set_cached_acl(inode, acl);
3714}
3715
Andy Adamsonbf118a32011-12-07 11:55:27 -05003716/*
3717 * The getxattr API returns the required buffer length when called with a
3718 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3719 * the required buf. On a NULL buf, we send a page of data to the server
3720 * guessing that the ACL request can be serviced by a page. If so, we cache
3721 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3722 * the cache. If not so, we throw away the page, and cache the required
3723 * length. The next getxattr call will then produce another round trip to
3724 * the server, this time with the input buf of the required size.
3725 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003726static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003727{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003728 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003729 struct nfs_getaclargs args = {
3730 .fh = NFS_FH(inode),
3731 .acl_pages = pages,
3732 .acl_len = buflen,
3733 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003734 struct nfs_getaclres res = {
3735 .acl_len = buflen,
3736 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3739 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003740 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003741 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003742 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3743 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744
Andy Adamsonbf118a32011-12-07 11:55:27 -05003745 /* As long as we're doing a round trip to the server anyway,
3746 * let's be prepared for a page of acl data. */
3747 if (npages == 0)
3748 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003749 if (npages > ARRAY_SIZE(pages))
3750 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003751
Andy Adamsonbf118a32011-12-07 11:55:27 -05003752 for (i = 0; i < npages; i++) {
3753 pages[i] = alloc_page(GFP_KERNEL);
3754 if (!pages[i])
3755 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003756 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003757
3758 /* for decoding across pages */
3759 res.acl_scratch = alloc_page(GFP_KERNEL);
3760 if (!res.acl_scratch)
3761 goto out_free;
3762
Andy Adamsonbf118a32011-12-07 11:55:27 -05003763 args.acl_len = npages * PAGE_SIZE;
3764 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003765
Peng Taode040be2012-01-10 22:42:47 +08003766 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003767 __func__, buf, buflen, npages, args.acl_len);
3768 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3769 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770 if (ret)
3771 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003772
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003773 /* Handle the case where the passed-in buffer is too short */
3774 if (res.acl_flags & NFS4_ACL_TRUNC) {
3775 /* Did the user only issue a request for the acl length? */
3776 if (buf == NULL)
3777 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003778 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003779 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003780 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003781 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3782 if (buf)
3783 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3784out_ok:
3785 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003786out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787 for (i = 0; i < npages; i++)
3788 if (pages[i])
3789 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003790 if (res.acl_scratch)
3791 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003792 return ret;
3793}
3794
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003795static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3796{
3797 struct nfs4_exception exception = { };
3798 ssize_t ret;
3799 do {
3800 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3801 if (ret >= 0)
3802 break;
3803 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3804 } while (exception.retry);
3805 return ret;
3806}
3807
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003808static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3809{
3810 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003811 int ret;
3812
3813 if (!nfs4_server_supports_acls(server))
3814 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 ret = nfs_revalidate_inode(server, inode);
3816 if (ret < 0)
3817 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003818 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3819 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003820 ret = nfs4_read_cached_acl(inode, buf, buflen);
3821 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003822 /* -ENOENT is returned if there is no ACL or if there is an ACL
3823 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003824 return ret;
3825 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003826}
3827
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003828static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003829{
3830 struct nfs_server *server = NFS_SERVER(inode);
3831 struct page *pages[NFS4ACL_MAXPAGES];
3832 struct nfs_setaclargs arg = {
3833 .fh = NFS_FH(inode),
3834 .acl_pages = pages,
3835 .acl_len = buflen,
3836 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003837 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003838 struct rpc_message msg = {
3839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3840 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003841 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003842 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003843 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003844 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003845
3846 if (!nfs4_server_supports_acls(server))
3847 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003848 if (npages > ARRAY_SIZE(pages))
3849 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003850 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3851 if (i < 0)
3852 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003853 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003854 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003855
3856 /*
3857 * Free each page after tx, so the only ref left is
3858 * held by the network stack
3859 */
3860 for (; i > 0; i--)
3861 put_page(pages[i-1]);
3862
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003863 /*
3864 * Acl update can result in inode attribute update.
3865 * so mark the attribute cache invalid.
3866 */
3867 spin_lock(&inode->i_lock);
3868 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3869 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003870 nfs_access_zap_cache(inode);
3871 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003872 return ret;
3873}
3874
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003875static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3876{
3877 struct nfs4_exception exception = { };
3878 int err;
3879 do {
3880 err = nfs4_handle_exception(NFS_SERVER(inode),
3881 __nfs4_proc_set_acl(inode, buf, buflen),
3882 &exception);
3883 } while (exception.retry);
3884 return err;
3885}
3886
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003888nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003890 struct nfs_client *clp = server->nfs_client;
3891
3892 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 return 0;
3894 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003895 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003896 case -NFS4ERR_ADMIN_REVOKED:
3897 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003898 if (state == NULL)
3899 break;
3900 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003901 case -NFS4ERR_OPENMODE:
3902 if (state == NULL)
3903 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003904 nfs4_schedule_stateid_recovery(server, state);
3905 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003906 case -NFS4ERR_EXPIRED:
3907 if (state != NULL)
3908 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003910 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003911 nfs4_schedule_lease_recovery(clp);
3912 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003913#if defined(CONFIG_NFS_V4_1)
3914 case -NFS4ERR_BADSESSION:
3915 case -NFS4ERR_BADSLOT:
3916 case -NFS4ERR_BAD_HIGH_SLOT:
3917 case -NFS4ERR_DEADSESSION:
3918 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3919 case -NFS4ERR_SEQ_FALSE_RETRY:
3920 case -NFS4ERR_SEQ_MISORDERED:
3921 dprintk("%s ERROR %d, Reset session\n", __func__,
3922 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003923 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003924 task->tk_status = 0;
3925 return -EAGAIN;
3926#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003928 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003929 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003930 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3932 task->tk_status = 0;
3933 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003934 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 case -NFS4ERR_OLD_STATEID:
3936 task->tk_status = 0;
3937 return -EAGAIN;
3938 }
3939 task->tk_status = nfs4_map_errors(task->tk_status);
3940 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003941wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003942 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003943 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3944 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3945 task->tk_status = 0;
3946 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947}
3948
Chuck Leverf0920752012-05-21 22:45:41 -04003949static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3950 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003951{
3952 __be32 verf[2];
3953
Chuck Lever2c820d92012-05-21 22:45:33 -04003954 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3955 /* An impossible timestamp guarantees this value
3956 * will never match a generated boot time. */
3957 verf[0] = 0;
3958 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3959 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003960 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3961 verf[0] = (__be32)nn->boot_time.tv_sec;
3962 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003963 }
Chuck Levercd937102012-03-02 17:14:31 -05003964 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3965}
3966
Chuck Levere984a552012-09-14 17:24:21 -04003967static unsigned int
3968nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
3969 char *buf, size_t len)
3970{
3971 unsigned int result;
3972
3973 rcu_read_lock();
3974 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
3975 clp->cl_ipaddr,
3976 rpc_peeraddr2str(clp->cl_rpcclient,
3977 RPC_DISPLAY_ADDR),
3978 rpc_peeraddr2str(clp->cl_rpcclient,
3979 RPC_DISPLAY_PROTO));
3980 rcu_read_unlock();
3981 return result;
3982}
3983
3984static unsigned int
3985nfs4_init_uniform_client_string(const struct nfs_client *clp,
3986 char *buf, size_t len)
3987{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04003988 char *nodename = clp->cl_rpcclient->cl_nodename;
3989
3990 if (nfs4_client_id_uniquifier[0] != '\0')
3991 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04003992 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
3993 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04003994 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04003995}
3996
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04003997/**
3998 * nfs4_proc_setclientid - Negotiate client ID
3999 * @clp: state data structure
4000 * @program: RPC program for NFSv4 callback service
4001 * @port: IP port number for NFS4 callback service
4002 * @cred: RPC credential to use for this call
4003 * @res: where to place the result
4004 *
4005 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4006 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004007int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4008 unsigned short port, struct rpc_cred *cred,
4009 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
4011 nfs4_verifier sc_verifier;
4012 struct nfs4_setclientid setclientid = {
4013 .sc_verifier = &sc_verifier,
4014 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004015 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 };
4017 struct rpc_message msg = {
4018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4019 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004020 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004021 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004023 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Chuck Leverde734832012-07-11 16:30:50 -04004025 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004026 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004027 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4028 setclientid.sc_name_len =
4029 nfs4_init_uniform_client_string(clp,
4030 setclientid.sc_name,
4031 sizeof(setclientid.sc_name));
4032 else
4033 setclientid.sc_name_len =
4034 nfs4_init_nonuniform_client_string(clp,
4035 setclientid.sc_name,
4036 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004037 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004038 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004039 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004040 sizeof(setclientid.sc_netid),
4041 rpc_peeraddr2str(clp->cl_rpcclient,
4042 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004043 rcu_read_unlock();
4044 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004045 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 clp->cl_ipaddr, port >> 8, port & 255);
4047
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004048 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4049 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4050 setclientid.sc_name_len, setclientid.sc_name);
4051 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4052 dprintk("NFS reply setclientid: %d\n", status);
4053 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004056/**
4057 * nfs4_proc_setclientid_confirm - Confirm client ID
4058 * @clp: state data structure
4059 * @res: result of a previous SETCLIENTID
4060 * @cred: RPC credential to use for this call
4061 *
4062 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4063 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004064int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004065 struct nfs4_setclientid_res *arg,
4066 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
4068 struct nfs_fsinfo fsinfo;
4069 struct rpc_message msg = {
4070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004071 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004073 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 };
4075 unsigned long now;
4076 int status;
4077
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004078 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4079 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4080 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004082 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 if (status == 0) {
4084 spin_lock(&clp->cl_lock);
4085 clp->cl_lease_time = fsinfo.lease_time * HZ;
4086 clp->cl_last_renewal = now;
4087 spin_unlock(&clp->cl_lock);
4088 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004089 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 return status;
4091}
4092
Trond Myklebustfe650402006-01-03 09:55:18 +01004093struct nfs4_delegreturndata {
4094 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004095 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004096 struct nfs_fh fh;
4097 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004098 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004099 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004100 int rpc_status;
4101};
4102
Trond Myklebustfe650402006-01-03 09:55:18 +01004103static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4104{
4105 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004106
Trond Myklebust14516c32010-07-31 14:29:06 -04004107 if (!nfs4_sequence_done(task, &data->res.seq_res))
4108 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004109
Ricardo Labiaga79708862009-12-07 09:23:21 -05004110 switch (task->tk_status) {
4111 case -NFS4ERR_STALE_STATEID:
4112 case -NFS4ERR_EXPIRED:
4113 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004114 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004115 break;
4116 default:
4117 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4118 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004119 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004120 return;
4121 }
4122 }
4123 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004124}
4125
4126static void nfs4_delegreturn_release(void *calldata)
4127{
Trond Myklebustfe650402006-01-03 09:55:18 +01004128 kfree(calldata);
4129}
4130
Andy Adamson938e1012009-04-01 09:22:28 -04004131#if defined(CONFIG_NFS_V4_1)
4132static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4133{
4134 struct nfs4_delegreturndata *d_data;
4135
4136 d_data = (struct nfs4_delegreturndata *)data;
4137
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004138 nfs4_setup_sequence(d_data->res.server,
4139 &d_data->args.seq_args,
4140 &d_data->res.seq_res,
4141 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004142}
4143#endif /* CONFIG_NFS_V4_1 */
4144
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004145static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004146#if defined(CONFIG_NFS_V4_1)
4147 .rpc_call_prepare = nfs4_delegreturn_prepare,
4148#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004149 .rpc_call_done = nfs4_delegreturn_done,
4150 .rpc_release = nfs4_delegreturn_release,
4151};
4152
Trond Myklebuste6f81072008-01-24 18:14:34 -05004153static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004154{
4155 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004156 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004157 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004158 struct rpc_message msg = {
4159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4160 .rpc_cred = cred,
4161 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004162 struct rpc_task_setup task_setup_data = {
4163 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004164 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004165 .callback_ops = &nfs4_delegreturn_ops,
4166 .flags = RPC_TASK_ASYNC,
4167 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004168 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004169
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004170 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004171 if (data == NULL)
4172 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004173 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004174 data->args.fhandle = &data->fh;
4175 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004176 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004177 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004178 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004179 data->res.fattr = &data->fattr;
4180 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004181 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004182 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004183 data->rpc_status = 0;
4184
Trond Myklebustc970aa82007-07-14 15:39:59 -04004185 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004186 msg.rpc_argp = &data->args;
4187 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004188 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004189 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004190 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004191 if (!issync)
4192 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004193 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004194 if (status != 0)
4195 goto out;
4196 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004197 if (status == 0)
4198 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4199 else
4200 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004201out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004202 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004203 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
Trond Myklebuste6f81072008-01-24 18:14:34 -05004206int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
4208 struct nfs_server *server = NFS_SERVER(inode);
4209 struct nfs4_exception exception = { };
4210 int err;
4211 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004212 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 switch (err) {
4214 case -NFS4ERR_STALE_STATEID:
4215 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 case 0:
4217 return 0;
4218 }
4219 err = nfs4_handle_exception(server, err, &exception);
4220 } while (exception.retry);
4221 return err;
4222}
4223
4224#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4225#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4226
4227/*
4228 * sleep, with exponential backoff, and retry the LOCK operation.
4229 */
4230static unsigned long
4231nfs4_set_lock_task_retry(unsigned long timeout)
4232{
Jeff Laytond3103102011-12-01 22:44:39 +01004233 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 timeout <<= 1;
4235 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4236 return NFS4_LOCK_MAXTIMEOUT;
4237 return timeout;
4238}
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4241{
4242 struct inode *inode = state->inode;
4243 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004244 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004245 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004247 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004249 struct nfs_lockt_res res = {
4250 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 };
4252 struct rpc_message msg = {
4253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4254 .rpc_argp = &arg,
4255 .rpc_resp = &res,
4256 .rpc_cred = state->owner->so_cred,
4257 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 struct nfs4_lock_state *lsp;
4259 int status;
4260
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004261 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004262 status = nfs4_set_lock_state(state, request);
4263 if (status != 0)
4264 goto out;
4265 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004266 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004267 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004268 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004269 switch (status) {
4270 case 0:
4271 request->fl_type = F_UNLCK;
4272 break;
4273 case -NFS4ERR_DENIED:
4274 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004276 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004277out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return status;
4279}
4280
4281static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4282{
4283 struct nfs4_exception exception = { };
4284 int err;
4285
4286 do {
4287 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4288 _nfs4_proc_getlk(state, cmd, request),
4289 &exception);
4290 } while (exception.retry);
4291 return err;
4292}
4293
4294static int do_vfs_lock(struct file *file, struct file_lock *fl)
4295{
4296 int res = 0;
4297 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4298 case FL_POSIX:
4299 res = posix_lock_file_wait(file, fl);
4300 break;
4301 case FL_FLOCK:
4302 res = flock_lock_file_wait(file, fl);
4303 break;
4304 default:
4305 BUG();
4306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return res;
4308}
4309
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004310struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004311 struct nfs_locku_args arg;
4312 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004313 struct nfs4_lock_state *lsp;
4314 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004315 struct file_lock fl;
4316 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004317 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004318};
4319
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004320static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4321 struct nfs_open_context *ctx,
4322 struct nfs4_lock_state *lsp,
4323 struct nfs_seqid *seqid)
4324{
4325 struct nfs4_unlockdata *p;
4326 struct inode *inode = lsp->ls_state->inode;
4327
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004328 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004329 if (p == NULL)
4330 return NULL;
4331 p->arg.fh = NFS_FH(inode);
4332 p->arg.fl = &p->fl;
4333 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004334 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004335 p->arg.stateid = &lsp->ls_stateid;
4336 p->lsp = lsp;
4337 atomic_inc(&lsp->ls_count);
4338 /* Ensure we don't close file until we're done freeing locks! */
4339 p->ctx = get_nfs_open_context(ctx);
4340 memcpy(&p->fl, fl, sizeof(p->fl));
4341 p->server = NFS_SERVER(inode);
4342 return p;
4343}
4344
Trond Myklebust06f814a2006-01-03 09:55:07 +01004345static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004346{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004347 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004348 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004349 nfs4_put_lock_state(calldata->lsp);
4350 put_nfs_open_context(calldata->ctx);
4351 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004352}
4353
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004354static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004355{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004356 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004357
Trond Myklebust14516c32010-07-31 14:29:06 -04004358 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4359 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004360 switch (task->tk_status) {
4361 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004362 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4363 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004364 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004365 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004366 case -NFS4ERR_BAD_STATEID:
4367 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004368 case -NFS4ERR_STALE_STATEID:
4369 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004370 break;
4371 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004372 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004373 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004374 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004375 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004376}
4377
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004378static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004379{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004380 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004382 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004383 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004384 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004385 /* Note: exit _without_ running nfs4_locku_done */
4386 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004387 return;
4388 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004389 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004390 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004391 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004392 &calldata->res.seq_res,
4393 task) != 0)
4394 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395}
4396
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004397static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004398 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004399 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004400 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004401};
4402
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004403static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4404 struct nfs_open_context *ctx,
4405 struct nfs4_lock_state *lsp,
4406 struct nfs_seqid *seqid)
4407{
4408 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004409 struct rpc_message msg = {
4410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4411 .rpc_cred = ctx->cred,
4412 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004413 struct rpc_task_setup task_setup_data = {
4414 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004415 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004416 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004417 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004418 .flags = RPC_TASK_ASYNC,
4419 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004420
Frank Filz137d6ac2007-07-09 15:32:29 -07004421 /* Ensure this is an unlock - when canceling a lock, the
4422 * canceled lock is passed in, and it won't be an unlock.
4423 */
4424 fl->fl_type = F_UNLCK;
4425
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004426 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4427 if (data == NULL) {
4428 nfs_free_seqid(seqid);
4429 return ERR_PTR(-ENOMEM);
4430 }
4431
Trond Myklebust9d12b212012-01-17 22:04:25 -05004432 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004433 msg.rpc_argp = &data->arg;
4434 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004435 task_setup_data.callback_data = data;
4436 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004437}
4438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4440{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004441 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004442 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004443 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004444 struct rpc_task *task;
4445 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004446 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Trond Myklebust9b073572006-06-29 16:38:34 -04004448 status = nfs4_set_lock_state(state, request);
4449 /* Unlock _before_ we do the RPC call */
4450 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004451 down_read(&nfsi->rwsem);
4452 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4453 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004454 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004455 }
4456 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004457 if (status != 0)
4458 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004459 /* Is this a delegated lock? */
4460 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004461 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004462 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004463 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004464 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004465 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004466 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004467 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004468 status = PTR_ERR(task);
4469 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004470 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004471 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004472 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004473out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004474 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004475 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476}
4477
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004478struct nfs4_lockdata {
4479 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004480 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 struct nfs4_lock_state *lsp;
4482 struct nfs_open_context *ctx;
4483 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004484 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485 int rpc_status;
4486 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004487 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004488};
4489
4490static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004491 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4492 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004493{
4494 struct nfs4_lockdata *p;
4495 struct inode *inode = lsp->ls_state->inode;
4496 struct nfs_server *server = NFS_SERVER(inode);
4497
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004498 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004499 if (p == NULL)
4500 return NULL;
4501
4502 p->arg.fh = NFS_FH(inode);
4503 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004504 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004505 if (p->arg.open_seqid == NULL)
4506 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004507 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004509 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004511 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004512 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004513 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004514 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004516 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517 atomic_inc(&lsp->ls_count);
4518 p->ctx = get_nfs_open_context(ctx);
4519 memcpy(&p->fl, fl, sizeof(p->fl));
4520 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004521out_free_seqid:
4522 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523out_free:
4524 kfree(p);
4525 return NULL;
4526}
4527
4528static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4529{
4530 struct nfs4_lockdata *data = calldata;
4531 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Harvey Harrison3110ff82008-05-02 13:42:44 -07004533 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004534 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4535 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004536 /* Do we need to do an open_to_lock_owner? */
4537 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004538 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004539 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004540 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004541 data->arg.open_stateid = &state->stateid;
4542 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004543 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004544 } else
4545 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004546 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004547 if (nfs4_setup_sequence(data->server,
4548 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004549 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004550 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004551 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004552 nfs_release_seqid(data->arg.open_seqid);
4553out_release_lock_seqid:
4554 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004555 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004556}
4557
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004558static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4559{
4560 struct nfs4_lockdata *data = calldata;
4561
Harvey Harrison3110ff82008-05-02 13:42:44 -07004562 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004563
Trond Myklebust14516c32010-07-31 14:29:06 -04004564 if (!nfs4_sequence_done(task, &data->res.seq_res))
4565 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004566
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004569 if (data->rpc_status == 0)
4570 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4571 else
4572 goto out;
4573 }
4574 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004575 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004576 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004577 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004580 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004581}
4582
4583static void nfs4_lock_release(void *calldata)
4584{
4585 struct nfs4_lockdata *data = calldata;
4586
Harvey Harrison3110ff82008-05-02 13:42:44 -07004587 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004588 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004589 if (data->cancelled != 0) {
4590 struct rpc_task *task;
4591 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4592 data->arg.lock_seqid);
4593 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004594 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004595 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004596 } else
4597 nfs_free_seqid(data->arg.lock_seqid);
4598 nfs4_put_lock_state(data->lsp);
4599 put_nfs_open_context(data->ctx);
4600 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004601 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602}
4603
4604static const struct rpc_call_ops nfs4_lock_ops = {
4605 .rpc_call_prepare = nfs4_lock_prepare,
4606 .rpc_call_done = nfs4_lock_done,
4607 .rpc_release = nfs4_lock_release,
4608};
4609
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004610static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4611{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004612 switch (error) {
4613 case -NFS4ERR_ADMIN_REVOKED:
4614 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004615 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004616 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004617 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004618 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004619 break;
4620 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004621 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004622 case -NFS4ERR_EXPIRED:
4623 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004624 };
4625}
4626
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004627static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004628{
4629 struct nfs4_lockdata *data;
4630 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004631 struct rpc_message msg = {
4632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4633 .rpc_cred = state->owner->so_cred,
4634 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004635 struct rpc_task_setup task_setup_data = {
4636 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004637 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004638 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004639 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004640 .flags = RPC_TASK_ASYNC,
4641 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004642 int ret;
4643
Harvey Harrison3110ff82008-05-02 13:42:44 -07004644 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004645 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004646 fl->fl_u.nfs4_fl.owner,
4647 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648 if (data == NULL)
4649 return -ENOMEM;
4650 if (IS_SETLKW(cmd))
4651 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004652 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004653 msg.rpc_argp = &data->arg;
4654 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004655 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004656 if (recovery_type > NFS_LOCK_NEW) {
4657 if (recovery_type == NFS_LOCK_RECLAIM)
4658 data->arg.reclaim = NFS_LOCK_RECLAIM;
4659 nfs4_set_sequence_privileged(&data->arg.seq_args);
4660 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004661 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004662 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004663 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004664 ret = nfs4_wait_for_completion_rpc_task(task);
4665 if (ret == 0) {
4666 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004667 if (ret)
4668 nfs4_handle_setlk_error(data->server, data->lsp,
4669 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004670 } else
4671 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004672 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004673 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
4677static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4678{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004679 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004680 struct nfs4_exception exception = {
4681 .inode = state->inode,
4682 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004683 int err;
4684
4685 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004686 /* Cache the lock if possible... */
4687 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4688 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004689 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004690 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004691 break;
4692 nfs4_handle_exception(server, err, &exception);
4693 } while (exception.retry);
4694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
4697static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4698{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004699 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004700 struct nfs4_exception exception = {
4701 .inode = state->inode,
4702 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004703 int err;
4704
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004705 err = nfs4_set_lock_state(state, request);
4706 if (err != 0)
4707 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004708 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004709 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4710 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004711 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004712 switch (err) {
4713 default:
4714 goto out;
4715 case -NFS4ERR_GRACE:
4716 case -NFS4ERR_DELAY:
4717 nfs4_handle_exception(server, err, &exception);
4718 err = 0;
4719 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004720 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004721out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004722 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723}
4724
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004725#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004726/**
4727 * nfs41_check_expired_locks - possibly free a lock stateid
4728 *
4729 * @state: NFSv4 state for an inode
4730 *
4731 * Returns NFS_OK if recovery for this stateid is now finished.
4732 * Otherwise a negative NFS4ERR value is returned.
4733 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004734static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004735{
Chuck Levereb64cf92012-07-11 16:30:05 -04004736 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004737 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004738 struct nfs_server *server = NFS_SERVER(state->inode);
4739
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004740 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004741 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004742 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4743 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004744 /* Free the stateid unless the server
4745 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004746 if (status != -NFS4ERR_BAD_STATEID)
4747 nfs41_free_stateid(server,
4748 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004749 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004750 ret = status;
4751 }
4752 }
4753 };
4754
4755 return ret;
4756}
4757
4758static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4759{
4760 int status = NFS_OK;
4761
4762 if (test_bit(LK_STATE_IN_USE, &state->flags))
4763 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004764 if (status != NFS_OK)
4765 status = nfs4_lock_expired(state, request);
4766 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004767}
4768#endif
4769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4771{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004772 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004773 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004774 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004776 if ((fl_flags & FL_POSIX) &&
4777 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4778 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004779 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004780 status = nfs4_set_lock_state(state, request);
4781 if (status != 0)
4782 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004783 request->fl_flags |= FL_ACCESS;
4784 status = do_vfs_lock(request->fl_file, request);
4785 if (status < 0)
4786 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004787 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004788 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004789 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004790 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004791 request->fl_flags = fl_flags & ~FL_SLEEP;
4792 status = do_vfs_lock(request->fl_file, request);
4793 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004794 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004795 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004796 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004797 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004798 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004799 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004800 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004801 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4802 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004803out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004804 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004805out:
4806 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 return status;
4808}
4809
4810static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4811{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004812 struct nfs4_exception exception = {
4813 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004814 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004815 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 int err;
4817
4818 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004819 err = _nfs4_proc_setlk(state, cmd, request);
4820 if (err == -NFS4ERR_DENIED)
4821 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004823 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 } while (exception.retry);
4825 return err;
4826}
4827
4828static int
4829nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4830{
4831 struct nfs_open_context *ctx;
4832 struct nfs4_state *state;
4833 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4834 int status;
4835
4836 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004837 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 state = ctx->state;
4839
4840 if (request->fl_start < 0 || request->fl_end < 0)
4841 return -EINVAL;
4842
Trond Myklebustd9531262009-07-21 19:22:38 -04004843 if (IS_GETLK(cmd)) {
4844 if (state != NULL)
4845 return nfs4_proc_getlk(state, F_GETLK, request);
4846 return 0;
4847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
4849 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4850 return -EINVAL;
4851
Trond Myklebustd9531262009-07-21 19:22:38 -04004852 if (request->fl_type == F_UNLCK) {
4853 if (state != NULL)
4854 return nfs4_proc_unlck(state, cmd, request);
4855 return 0;
4856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Trond Myklebustd9531262009-07-21 19:22:38 -04004858 if (state == NULL)
4859 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004860 /*
4861 * Don't rely on the VFS having checked the file open mode,
4862 * since it won't do this for flock() locks.
4863 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004864 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004865 case F_RDLCK:
4866 if (!(filp->f_mode & FMODE_READ))
4867 return -EBADF;
4868 break;
4869 case F_WRLCK:
4870 if (!(filp->f_mode & FMODE_WRITE))
4871 return -EBADF;
4872 }
4873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 do {
4875 status = nfs4_proc_setlk(state, cmd, request);
4876 if ((status != -EAGAIN) || IS_SETLK(cmd))
4877 break;
4878 timeout = nfs4_set_lock_task_retry(timeout);
4879 status = -ERESTARTSYS;
4880 if (signalled())
4881 break;
4882 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 return status;
4884}
4885
Trond Myklebust888e6942005-11-04 15:38:11 -05004886int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4887{
4888 struct nfs_server *server = NFS_SERVER(state->inode);
4889 struct nfs4_exception exception = { };
4890 int err;
4891
4892 err = nfs4_set_lock_state(state, fl);
4893 if (err != 0)
4894 goto out;
4895 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004896 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004897 switch (err) {
4898 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004899 printk(KERN_ERR "NFS: %s: unhandled error "
4900 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004901 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004902 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004903 goto out;
4904 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004905 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004906 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004907 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004908 nfs4_schedule_lease_recovery(server->nfs_client);
4909 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004910 case -NFS4ERR_BADSESSION:
4911 case -NFS4ERR_BADSLOT:
4912 case -NFS4ERR_BAD_HIGH_SLOT:
4913 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4914 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004915 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004916 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004917 case -ERESTARTSYS:
4918 /*
4919 * The show must go on: exit, but mark the
4920 * stateid as needing recovery.
4921 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004922 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004923 case -NFS4ERR_ADMIN_REVOKED:
4924 case -NFS4ERR_BAD_STATEID:
4925 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004926 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004927 err = 0;
4928 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004929 case -EKEYEXPIRED:
4930 /*
4931 * User RPCSEC_GSS context has expired.
4932 * We cannot recover this stateid now, so
4933 * skip it and allow recovery thread to
4934 * proceed.
4935 */
4936 err = 0;
4937 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004938 case -ENOMEM:
4939 case -NFS4ERR_DENIED:
4940 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4941 err = 0;
4942 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004943 case -NFS4ERR_DELAY:
4944 break;
4945 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004946 err = nfs4_handle_exception(server, err, &exception);
4947 } while (exception.retry);
4948out:
4949 return err;
4950}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004951
Trond Myklebustcf470c32012-03-07 13:49:12 -05004952struct nfs_release_lockowner_data {
4953 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004954 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004955 struct nfs_release_lockowner_args args;
4956};
4957
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004958static void nfs4_release_lockowner_release(void *calldata)
4959{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004960 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004961 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004962 kfree(calldata);
4963}
4964
Trond Myklebust17280172012-03-11 13:11:00 -04004965static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004966 .rpc_release = nfs4_release_lockowner_release,
4967};
4968
Trond Myklebustcf470c32012-03-07 13:49:12 -05004969int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004970{
4971 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004972 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004973 struct rpc_message msg = {
4974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4975 };
4976
4977 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004978 return -EINVAL;
4979 data = kmalloc(sizeof(*data), GFP_NOFS);
4980 if (!data)
4981 return -ENOMEM;
4982 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004983 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004984 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4985 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4986 data->args.lock_owner.s_dev = server->s_dev;
4987 msg.rpc_argp = &data->args;
4988 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4989 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004990}
4991
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004992#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4993
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004994static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4995 const void *buf, size_t buflen,
4996 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004997{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004998 if (strcmp(key, "") != 0)
4999 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005000
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005001 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005002}
5003
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005004static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5005 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005006{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005007 if (strcmp(key, "") != 0)
5008 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005009
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005010 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005011}
5012
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005013static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5014 size_t list_len, const char *name,
5015 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005016{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005017 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005018
J. Bruce Fields096455a2006-03-20 23:23:42 -05005019 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5020 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005021
5022 if (list && len <= list_len)
5023 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005024 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005025}
5026
Andy Adamson533eb462011-06-13 18:25:56 -04005027/*
5028 * nfs_fhget will use either the mounted_on_fileid or the fileid
5029 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005030static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5031{
Andy Adamson533eb462011-06-13 18:25:56 -04005032 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5033 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5034 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005035 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005036 return;
5037
5038 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005039 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005040 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5041 fattr->nlink = 2;
5042}
5043
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005044static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5045 const struct qstr *name,
5046 struct nfs4_fs_locations *fs_locations,
5047 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005048{
5049 struct nfs_server *server = NFS_SERVER(dir);
5050 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005051 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005052 };
5053 struct nfs4_fs_locations_arg args = {
5054 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005055 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005056 .page = page,
5057 .bitmask = bitmask,
5058 };
Benny Halevy22958462009-04-01 09:22:02 -04005059 struct nfs4_fs_locations_res res = {
5060 .fs_locations = fs_locations,
5061 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005062 struct rpc_message msg = {
5063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5064 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005065 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005066 };
5067 int status;
5068
Harvey Harrison3110ff82008-05-02 13:42:44 -07005069 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005070
5071 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5072 * is not supported */
5073 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5074 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5075 else
5076 bitmask[0] |= FATTR4_WORD0_FILEID;
5077
Trond Myklebustc228fd32007-01-13 02:28:11 -05005078 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005079 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005080 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005081 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005082 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005083 return status;
5084}
5085
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005086int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5087 const struct qstr *name,
5088 struct nfs4_fs_locations *fs_locations,
5089 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005090{
5091 struct nfs4_exception exception = { };
5092 int err;
5093 do {
5094 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005095 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005096 &exception);
5097 } while (exception.retry);
5098 return err;
5099}
5100
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005101static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5102{
5103 int status;
5104 struct nfs4_secinfo_arg args = {
5105 .dir_fh = NFS_FH(dir),
5106 .name = name,
5107 };
5108 struct nfs4_secinfo_res res = {
5109 .flavors = flavors,
5110 };
5111 struct rpc_message msg = {
5112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5113 .rpc_argp = &args,
5114 .rpc_resp = &res,
5115 };
5116
5117 dprintk("NFS call secinfo %s\n", name->name);
5118 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5119 dprintk("NFS reply secinfo: %d\n", status);
5120 return status;
5121}
5122
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005123int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5124 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005125{
5126 struct nfs4_exception exception = { };
5127 int err;
5128 do {
5129 err = nfs4_handle_exception(NFS_SERVER(dir),
5130 _nfs4_proc_secinfo(dir, name, flavors),
5131 &exception);
5132 } while (exception.retry);
5133 return err;
5134}
5135
Andy Adamson557134a2009-04-01 09:21:53 -04005136#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005137/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005138 * Check the exchange flags returned by the server for invalid flags, having
5139 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5140 * DS flags set.
5141 */
5142static int nfs4_check_cl_exchange_flags(u32 flags)
5143{
5144 if (flags & ~EXCHGID4_FLAG_MASK_R)
5145 goto out_inval;
5146 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5147 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5148 goto out_inval;
5149 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5150 goto out_inval;
5151 return NFS_OK;
5152out_inval:
5153 return -NFS4ERR_INVAL;
5154}
5155
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005156static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005157nfs41_same_server_scope(struct nfs41_server_scope *a,
5158 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005159{
5160 if (a->server_scope_sz == b->server_scope_sz &&
5161 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5162 return true;
5163
5164 return false;
5165}
5166
Andy Adamson357f54d2010-12-14 10:11:57 -05005167/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005168 * nfs4_proc_bind_conn_to_session()
5169 *
5170 * The 4.1 client currently uses the same TCP connection for the
5171 * fore and backchannel.
5172 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005173int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005174{
5175 int status;
5176 struct nfs41_bind_conn_to_session_res res;
5177 struct rpc_message msg = {
5178 .rpc_proc =
5179 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5180 .rpc_argp = clp,
5181 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005182 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005183 };
5184
5185 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005186
5187 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5188 if (unlikely(res.session == NULL)) {
5189 status = -ENOMEM;
5190 goto out;
5191 }
5192
5193 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5194 if (status == 0) {
5195 if (memcmp(res.session->sess_id.data,
5196 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5197 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5198 status = -EIO;
5199 goto out_session;
5200 }
5201 if (res.dir != NFS4_CDFS4_BOTH) {
5202 dprintk("NFS: %s: Unexpected direction from server\n",
5203 __func__);
5204 status = -EIO;
5205 goto out_session;
5206 }
5207 if (res.use_conn_in_rdma_mode) {
5208 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5209 __func__);
5210 status = -EIO;
5211 goto out_session;
5212 }
5213 }
5214out_session:
5215 kfree(res.session);
5216out:
5217 dprintk("<-- %s status= %d\n", __func__, status);
5218 return status;
5219}
5220
5221/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005222 * nfs4_proc_exchange_id()
5223 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005224 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5225 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005226 * Since the clientid has expired, all compounds using sessions
5227 * associated with the stale clientid will be returning
5228 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5229 * be in some phase of session reset.
5230 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005231int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005232{
5233 nfs4_verifier verifier;
5234 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005235 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005236 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005237 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005238 };
5239 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005240 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005241 };
5242 int status;
5243 struct rpc_message msg = {
5244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5245 .rpc_argp = &args,
5246 .rpc_resp = &res,
5247 .rpc_cred = cred,
5248 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005249
Chuck Leverf0920752012-05-21 22:45:41 -04005250 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005251 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5252 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005253 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5254 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5255 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005256
Chuck Leveracdeb692012-05-21 22:46:16 -04005257 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005258 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005259 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005260 status = -ENOMEM;
5261 goto out;
5262 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005263
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005264 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005265 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005266 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005267 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005268 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005269 }
5270
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005271 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005272 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005273 status = -ENOMEM;
5274 goto out_server_scope;
5275 }
5276
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005277 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005278 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005279 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005280
Chuck Lever177313f2012-05-21 22:44:58 -04005281 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005282 clp->cl_clientid = res.clientid;
5283 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5284 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5285 clp->cl_seqid = res.seqid;
5286
Chuck Leveracdeb692012-05-21 22:46:16 -04005287 kfree(clp->cl_serverowner);
5288 clp->cl_serverowner = res.server_owner;
5289 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005290
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005291 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005292 kfree(clp->cl_implid);
5293 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005294
Chuck Lever177313f2012-05-21 22:44:58 -04005295 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005296 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005297 res.server_scope)) {
5298 dprintk("%s: server_scope mismatch detected\n",
5299 __func__);
5300 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005301 kfree(clp->cl_serverscope);
5302 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005303 }
5304
Chuck Lever177313f2012-05-21 22:44:58 -04005305 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005306 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005307 goto out;
5308 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005309 } else
5310 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005311
Chuck Leveracdeb692012-05-21 22:46:16 -04005312out_server_owner:
5313 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005314out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005315 kfree(res.server_scope);
5316out:
Chuck Lever177313f2012-05-21 22:44:58 -04005317 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005318 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005319 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005320 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005321 clp->cl_implid->date.seconds,
5322 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005323 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005324 return status;
5325}
5326
Trond Myklebust66245532012-05-25 17:18:09 -04005327static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5328 struct rpc_cred *cred)
5329{
5330 struct rpc_message msg = {
5331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5332 .rpc_argp = clp,
5333 .rpc_cred = cred,
5334 };
5335 int status;
5336
5337 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5338 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005339 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005340 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5341 return status;
5342}
5343
5344static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5345 struct rpc_cred *cred)
5346{
5347 unsigned int loop;
5348 int ret;
5349
5350 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5351 ret = _nfs4_proc_destroy_clientid(clp, cred);
5352 switch (ret) {
5353 case -NFS4ERR_DELAY:
5354 case -NFS4ERR_CLIENTID_BUSY:
5355 ssleep(1);
5356 break;
5357 default:
5358 return ret;
5359 }
5360 }
5361 return 0;
5362}
5363
5364int nfs4_destroy_clientid(struct nfs_client *clp)
5365{
5366 struct rpc_cred *cred;
5367 int ret = 0;
5368
5369 if (clp->cl_mvops->minor_version < 1)
5370 goto out;
5371 if (clp->cl_exchange_flags == 0)
5372 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005373 if (clp->cl_preserve_clid)
5374 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005375 cred = nfs4_get_exchange_id_cred(clp);
5376 ret = nfs4_proc_destroy_clientid(clp, cred);
5377 if (cred)
5378 put_rpccred(cred);
5379 switch (ret) {
5380 case 0:
5381 case -NFS4ERR_STALE_CLIENTID:
5382 clp->cl_exchange_flags = 0;
5383 }
5384out:
5385 return ret;
5386}
5387
Andy Adamson2050f0c2009-04-01 09:22:30 -04005388struct nfs4_get_lease_time_data {
5389 struct nfs4_get_lease_time_args *args;
5390 struct nfs4_get_lease_time_res *res;
5391 struct nfs_client *clp;
5392};
5393
5394static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5395 void *calldata)
5396{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005397 struct nfs4_get_lease_time_data *data =
5398 (struct nfs4_get_lease_time_data *)calldata;
5399
5400 dprintk("--> %s\n", __func__);
5401 /* just setup sequence, do not trigger session recovery
5402 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005403 nfs41_setup_sequence(data->clp->cl_session,
5404 &data->args->la_seq_args,
5405 &data->res->lr_seq_res,
5406 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005407 dprintk("<-- %s\n", __func__);
5408}
5409
5410/*
5411 * Called from nfs4_state_manager thread for session setup, so don't recover
5412 * from sequence operation or clientid errors.
5413 */
5414static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5415{
5416 struct nfs4_get_lease_time_data *data =
5417 (struct nfs4_get_lease_time_data *)calldata;
5418
5419 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005420 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5421 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005422 switch (task->tk_status) {
5423 case -NFS4ERR_DELAY:
5424 case -NFS4ERR_GRACE:
5425 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5426 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5427 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005428 /* fall through */
5429 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005430 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005431 return;
5432 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005433 dprintk("<-- %s\n", __func__);
5434}
5435
Trond Myklebust17280172012-03-11 13:11:00 -04005436static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005437 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5438 .rpc_call_done = nfs4_get_lease_time_done,
5439};
5440
5441int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5442{
5443 struct rpc_task *task;
5444 struct nfs4_get_lease_time_args args;
5445 struct nfs4_get_lease_time_res res = {
5446 .lr_fsinfo = fsinfo,
5447 };
5448 struct nfs4_get_lease_time_data data = {
5449 .args = &args,
5450 .res = &res,
5451 .clp = clp,
5452 };
5453 struct rpc_message msg = {
5454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5455 .rpc_argp = &args,
5456 .rpc_resp = &res,
5457 };
5458 struct rpc_task_setup task_setup = {
5459 .rpc_client = clp->cl_rpcclient,
5460 .rpc_message = &msg,
5461 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005462 .callback_data = &data,
5463 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005464 };
5465 int status;
5466
Trond Myklebust9d12b212012-01-17 22:04:25 -05005467 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005468 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005469 dprintk("--> %s\n", __func__);
5470 task = rpc_run_task(&task_setup);
5471
5472 if (IS_ERR(task))
5473 status = PTR_ERR(task);
5474 else {
5475 status = task->tk_status;
5476 rpc_put_task(task);
5477 }
5478 dprintk("<-- %s return %d\n", __func__, status);
5479
5480 return status;
5481}
5482
Andy Adamsonfc931582009-04-01 09:22:31 -04005483/*
5484 * Initialize the values to be used by the client in CREATE_SESSION
5485 * If nfs4_init_session set the fore channel request and response sizes,
5486 * use them.
5487 *
5488 * Set the back channel max_resp_sz_cached to zero to force the client to
5489 * always set csa_cachethis to FALSE because the current implementation
5490 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5491 */
5492static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5493{
5494 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005495 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5496 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005497
5498 if (mxrqst_sz == 0)
5499 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5500 if (mxresp_sz == 0)
5501 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5502 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005503 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5504 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005505 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005506 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005507
5508 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005509 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005510 __func__,
5511 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005512 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005513
5514 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005515 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5516 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5517 args->bc_attrs.max_resp_sz_cached = 0;
5518 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5519 args->bc_attrs.max_reqs = 1;
5520
5521 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5522 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5523 __func__,
5524 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5525 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5526 args->bc_attrs.max_reqs);
5527}
5528
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005529static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005530{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005531 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5532 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5533
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005534 if (rcvd->max_resp_sz > sent->max_resp_sz)
5535 return -EINVAL;
5536 /*
5537 * Our requested max_ops is the minimum we need; we're not
5538 * prepared to break up compounds into smaller pieces than that.
5539 * So, no point even trying to continue if the server won't
5540 * cooperate:
5541 */
5542 if (rcvd->max_ops < sent->max_ops)
5543 return -EINVAL;
5544 if (rcvd->max_reqs == 0)
5545 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005546 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5547 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005548 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005549}
5550
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005551static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5552{
5553 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5554 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005555
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005556 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5557 return -EINVAL;
5558 if (rcvd->max_resp_sz < sent->max_resp_sz)
5559 return -EINVAL;
5560 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5561 return -EINVAL;
5562 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005563 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005564 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005565 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005566 return -EINVAL;
5567 return 0;
5568}
Andy Adamson8d353012009-04-01 09:22:32 -04005569
Andy Adamson8d353012009-04-01 09:22:32 -04005570static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5571 struct nfs4_session *session)
5572{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005573 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005574
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005575 ret = nfs4_verify_fore_channel_attrs(args, session);
5576 if (ret)
5577 return ret;
5578 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005579}
5580
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005581static int _nfs4_proc_create_session(struct nfs_client *clp,
5582 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005583{
5584 struct nfs4_session *session = clp->cl_session;
5585 struct nfs41_create_session_args args = {
5586 .client = clp,
5587 .cb_program = NFS4_CALLBACK,
5588 };
5589 struct nfs41_create_session_res res = {
5590 .client = clp,
5591 };
5592 struct rpc_message msg = {
5593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5594 .rpc_argp = &args,
5595 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005596 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005597 };
5598 int status;
5599
5600 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005601 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005602
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005603 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005604
Trond Myklebust43095d32012-11-20 11:13:12 -05005605 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005606 /* Verify the session's negotiated channel_attrs values */
5607 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005608 /* Increment the clientid slot sequence id */
5609 clp->cl_seqid++;
5610 }
5611
5612 return status;
5613}
5614
5615/*
5616 * Issues a CREATE_SESSION operation to the server.
5617 * It is the responsibility of the caller to verify the session is
5618 * expired before calling this routine.
5619 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005620int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005621{
5622 int status;
5623 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005624 struct nfs4_session *session = clp->cl_session;
5625
5626 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5627
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005628 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005629 if (status)
5630 goto out;
5631
Andy Adamsonaacd5532011-11-09 13:58:21 -05005632 /* Init or reset the session slot tables */
5633 status = nfs4_setup_session_slot_tables(session);
5634 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005635 if (status)
5636 goto out;
5637
5638 ptr = (unsigned *)&session->sess_id.data[0];
5639 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5640 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005641out:
5642 dprintk("<-- %s\n", __func__);
5643 return status;
5644}
5645
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005646/*
5647 * Issue the over-the-wire RPC DESTROY_SESSION.
5648 * The caller must serialize access to this routine.
5649 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005650int nfs4_proc_destroy_session(struct nfs4_session *session,
5651 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005652{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005653 struct rpc_message msg = {
5654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5655 .rpc_argp = session,
5656 .rpc_cred = cred,
5657 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005658 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005659
5660 dprintk("--> nfs4_proc_destroy_session\n");
5661
5662 /* session is still being setup */
5663 if (session->clp->cl_cons_state != NFS_CS_READY)
5664 return status;
5665
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005666 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005667
5668 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005669 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005670 "Session has been destroyed regardless...\n", status);
5671
5672 dprintk("<-- nfs4_proc_destroy_session\n");
5673 return status;
5674}
5675
Trond Myklebust7b38c362012-05-23 13:23:31 -04005676/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005677 * Renew the cl_session lease.
5678 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005679struct nfs4_sequence_data {
5680 struct nfs_client *clp;
5681 struct nfs4_sequence_args args;
5682 struct nfs4_sequence_res res;
5683};
5684
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005685static void nfs41_sequence_release(void *data)
5686{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005687 struct nfs4_sequence_data *calldata = data;
5688 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005689
Alexandros Batsakis71358402010-02-05 03:45:05 -08005690 if (atomic_read(&clp->cl_count) > 1)
5691 nfs4_schedule_state_renewal(clp);
5692 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005693 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005694}
5695
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005696static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5697{
5698 switch(task->tk_status) {
5699 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005700 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5701 return -EAGAIN;
5702 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005703 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005704 }
5705 return 0;
5706}
5707
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005708static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005709{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005710 struct nfs4_sequence_data *calldata = data;
5711 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005712
Trond Myklebust14516c32010-07-31 14:29:06 -04005713 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5714 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005715
5716 if (task->tk_status < 0) {
5717 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005718 if (atomic_read(&clp->cl_count) == 1)
5719 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005720
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005721 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5722 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005723 return;
5724 }
5725 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005726 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005727out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005728 dprintk("<-- %s\n", __func__);
5729}
5730
5731static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5732{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005733 struct nfs4_sequence_data *calldata = data;
5734 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005735 struct nfs4_sequence_args *args;
5736 struct nfs4_sequence_res *res;
5737
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005738 args = task->tk_msg.rpc_argp;
5739 res = task->tk_msg.rpc_resp;
5740
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005741 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005742}
5743
5744static const struct rpc_call_ops nfs41_sequence_ops = {
5745 .rpc_call_done = nfs41_sequence_call_done,
5746 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005747 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005748};
5749
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005750static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5751 struct rpc_cred *cred,
5752 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005753{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005754 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005755 struct rpc_message msg = {
5756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5757 .rpc_cred = cred,
5758 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005759 struct rpc_task_setup task_setup_data = {
5760 .rpc_client = clp->cl_rpcclient,
5761 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005762 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005763 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5764 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005765
Alexandros Batsakis71358402010-02-05 03:45:05 -08005766 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005767 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005768 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005769 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005770 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005771 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005772 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005773 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005774 if (is_privileged)
5775 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005776 msg.rpc_argp = &calldata->args;
5777 msg.rpc_resp = &calldata->res;
5778 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005779 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005780
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005781 return rpc_run_task(&task_setup_data);
5782}
5783
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005784static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005785{
5786 struct rpc_task *task;
5787 int ret = 0;
5788
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005789 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5790 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005791 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005792 if (IS_ERR(task))
5793 ret = PTR_ERR(task);
5794 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005795 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005796 dprintk("<-- %s status=%d\n", __func__, ret);
5797 return ret;
5798}
5799
5800static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5801{
5802 struct rpc_task *task;
5803 int ret;
5804
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005805 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005806 if (IS_ERR(task)) {
5807 ret = PTR_ERR(task);
5808 goto out;
5809 }
5810 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005811 if (!ret) {
5812 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5813
5814 if (task->tk_status == 0)
5815 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005816 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005817 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005818 rpc_put_task(task);
5819out:
5820 dprintk("<-- %s status=%d\n", __func__, ret);
5821 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005822}
5823
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005824struct nfs4_reclaim_complete_data {
5825 struct nfs_client *clp;
5826 struct nfs41_reclaim_complete_args arg;
5827 struct nfs41_reclaim_complete_res res;
5828};
5829
5830static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5831{
5832 struct nfs4_reclaim_complete_data *calldata = data;
5833
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005834 nfs41_setup_sequence(calldata->clp->cl_session,
5835 &calldata->arg.seq_args,
5836 &calldata->res.seq_res,
5837 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005838}
5839
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005840static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5841{
5842 switch(task->tk_status) {
5843 case 0:
5844 case -NFS4ERR_COMPLETE_ALREADY:
5845 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5846 break;
5847 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005848 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005849 /* fall through */
5850 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005851 return -EAGAIN;
5852 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005853 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005854 }
5855 return 0;
5856}
5857
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005858static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5859{
5860 struct nfs4_reclaim_complete_data *calldata = data;
5861 struct nfs_client *clp = calldata->clp;
5862 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5863
5864 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005865 if (!nfs41_sequence_done(task, res))
5866 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005867
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005868 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5869 rpc_restart_call_prepare(task);
5870 return;
5871 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005872 dprintk("<-- %s\n", __func__);
5873}
5874
5875static void nfs4_free_reclaim_complete_data(void *data)
5876{
5877 struct nfs4_reclaim_complete_data *calldata = data;
5878
5879 kfree(calldata);
5880}
5881
5882static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5883 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5884 .rpc_call_done = nfs4_reclaim_complete_done,
5885 .rpc_release = nfs4_free_reclaim_complete_data,
5886};
5887
5888/*
5889 * Issue a global reclaim complete.
5890 */
5891static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5892{
5893 struct nfs4_reclaim_complete_data *calldata;
5894 struct rpc_task *task;
5895 struct rpc_message msg = {
5896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5897 };
5898 struct rpc_task_setup task_setup_data = {
5899 .rpc_client = clp->cl_rpcclient,
5900 .rpc_message = &msg,
5901 .callback_ops = &nfs4_reclaim_complete_call_ops,
5902 .flags = RPC_TASK_ASYNC,
5903 };
5904 int status = -ENOMEM;
5905
5906 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005907 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005908 if (calldata == NULL)
5909 goto out;
5910 calldata->clp = clp;
5911 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005912
Trond Myklebust9d12b212012-01-17 22:04:25 -05005913 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005914 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005915 msg.rpc_argp = &calldata->arg;
5916 msg.rpc_resp = &calldata->res;
5917 task_setup_data.callback_data = calldata;
5918 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005919 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005920 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005921 goto out;
5922 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005923 status = nfs4_wait_for_completion_rpc_task(task);
5924 if (status == 0)
5925 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005926 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005927 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005928out:
5929 dprintk("<-- %s status=%d\n", __func__, status);
5930 return status;
5931}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005932
5933static void
5934nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5935{
5936 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005937 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005938 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005939
5940 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005941 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5942 * right now covering the LAYOUTGET we are about to send.
5943 * However, that is not so catastrophic, and there seems
5944 * to be no way to prevent it completely.
5945 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005946 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005947 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005948 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005949 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5950 NFS_I(lgp->args.inode)->layout,
5951 lgp->args.ctx->state)) {
5952 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005953 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005954}
5955
5956static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5957{
5958 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07005959 struct inode *inode = lgp->args.inode;
5960 struct nfs_server *server = NFS_SERVER(inode);
5961 struct pnfs_layout_hdr *lo;
5962 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005963
5964 dprintk("--> %s\n", __func__);
5965
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005966 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07005967 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005968
5969 switch (task->tk_status) {
5970 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07005971 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005972 case -NFS4ERR_LAYOUTTRYLATER:
5973 case -NFS4ERR_RECALLCONFLICT:
5974 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07005975 break;
5976 case -NFS4ERR_EXPIRED:
5977 case -NFS4ERR_BAD_STATEID:
5978 spin_lock(&inode->i_lock);
5979 lo = NFS_I(inode)->layout;
5980 if (!lo || list_empty(&lo->plh_segs)) {
5981 spin_unlock(&inode->i_lock);
5982 /* If the open stateid was bad, then recover it. */
5983 state = lgp->args.ctx->state;
5984 } else {
5985 LIST_HEAD(head);
5986
5987 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
5988 spin_unlock(&inode->i_lock);
5989 /* Mark the bad layout state as invalid, then
5990 * retry using the open stateid. */
5991 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005992 }
5993 }
Trond Myklebustee314c22012-10-01 17:25:48 -07005994 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
5995 rpc_restart_call_prepare(task);
5996out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005997 dprintk("<-- %s\n", __func__);
5998}
5999
Idan Kedar85541162012-08-02 11:47:10 +03006000static size_t max_response_pages(struct nfs_server *server)
6001{
6002 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6003 return nfs_page_array_len(0, max_resp_sz);
6004}
6005
6006static void nfs4_free_pages(struct page **pages, size_t size)
6007{
6008 int i;
6009
6010 if (!pages)
6011 return;
6012
6013 for (i = 0; i < size; i++) {
6014 if (!pages[i])
6015 break;
6016 __free_page(pages[i]);
6017 }
6018 kfree(pages);
6019}
6020
6021static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6022{
6023 struct page **pages;
6024 int i;
6025
6026 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6027 if (!pages) {
6028 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6029 return NULL;
6030 }
6031
6032 for (i = 0; i < size; i++) {
6033 pages[i] = alloc_page(gfp_flags);
6034 if (!pages[i]) {
6035 dprintk("%s: failed to allocate page\n", __func__);
6036 nfs4_free_pages(pages, size);
6037 return NULL;
6038 }
6039 }
6040
6041 return pages;
6042}
6043
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006044static void nfs4_layoutget_release(void *calldata)
6045{
6046 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006047 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6048 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006049
6050 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006051 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006052 put_nfs_open_context(lgp->args.ctx);
6053 kfree(calldata);
6054 dprintk("<-- %s\n", __func__);
6055}
6056
6057static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6058 .rpc_call_prepare = nfs4_layoutget_prepare,
6059 .rpc_call_done = nfs4_layoutget_done,
6060 .rpc_release = nfs4_layoutget_release,
6061};
6062
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006063struct pnfs_layout_segment *
6064nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006065{
6066 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006067 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006068 struct rpc_task *task;
6069 struct rpc_message msg = {
6070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6071 .rpc_argp = &lgp->args,
6072 .rpc_resp = &lgp->res,
6073 };
6074 struct rpc_task_setup task_setup_data = {
6075 .rpc_client = server->client,
6076 .rpc_message = &msg,
6077 .callback_ops = &nfs4_layoutget_call_ops,
6078 .callback_data = lgp,
6079 .flags = RPC_TASK_ASYNC,
6080 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006081 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006082 int status = 0;
6083
6084 dprintk("--> %s\n", __func__);
6085
Idan Kedar85541162012-08-02 11:47:10 +03006086 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6087 if (!lgp->args.layout.pages) {
6088 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006089 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006090 }
6091 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6092
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006093 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006094 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006095 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006096 task = rpc_run_task(&task_setup_data);
6097 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006098 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006099 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006100 if (status == 0)
6101 status = task->tk_status;
6102 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006103 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006104 rpc_put_task(task);
6105 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006106 if (status)
6107 return ERR_PTR(status);
6108 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006109}
6110
Benny Halevycbe82602011-05-22 19:52:37 +03006111static void
6112nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6113{
6114 struct nfs4_layoutreturn *lrp = calldata;
6115
6116 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006117 nfs41_setup_sequence(lrp->clp->cl_session,
6118 &lrp->args.seq_args,
6119 &lrp->res.seq_res,
6120 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006121}
6122
6123static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6124{
6125 struct nfs4_layoutreturn *lrp = calldata;
6126 struct nfs_server *server;
6127
6128 dprintk("--> %s\n", __func__);
6129
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006130 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006131 return;
6132
6133 server = NFS_SERVER(lrp->args.inode);
6134 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006135 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006136 return;
6137 }
Benny Halevycbe82602011-05-22 19:52:37 +03006138 dprintk("<-- %s\n", __func__);
6139}
6140
6141static void nfs4_layoutreturn_release(void *calldata)
6142{
6143 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006144 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006145
6146 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006147 spin_lock(&lo->plh_inode->i_lock);
6148 if (lrp->res.lrs_present)
6149 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6150 lo->plh_block_lgets--;
6151 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006152 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006153 kfree(calldata);
6154 dprintk("<-- %s\n", __func__);
6155}
6156
6157static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6158 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6159 .rpc_call_done = nfs4_layoutreturn_done,
6160 .rpc_release = nfs4_layoutreturn_release,
6161};
6162
6163int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6164{
6165 struct rpc_task *task;
6166 struct rpc_message msg = {
6167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6168 .rpc_argp = &lrp->args,
6169 .rpc_resp = &lrp->res,
6170 };
6171 struct rpc_task_setup task_setup_data = {
6172 .rpc_client = lrp->clp->cl_rpcclient,
6173 .rpc_message = &msg,
6174 .callback_ops = &nfs4_layoutreturn_call_ops,
6175 .callback_data = lrp,
6176 };
6177 int status;
6178
6179 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006180 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006181 task = rpc_run_task(&task_setup_data);
6182 if (IS_ERR(task))
6183 return PTR_ERR(task);
6184 status = task->tk_status;
6185 dprintk("<-- %s status=%d\n", __func__, status);
6186 rpc_put_task(task);
6187 return status;
6188}
6189
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006190/*
6191 * Retrieve the list of Data Server devices from the MDS.
6192 */
6193static int _nfs4_getdevicelist(struct nfs_server *server,
6194 const struct nfs_fh *fh,
6195 struct pnfs_devicelist *devlist)
6196{
6197 struct nfs4_getdevicelist_args args = {
6198 .fh = fh,
6199 .layoutclass = server->pnfs_curr_ld->id,
6200 };
6201 struct nfs4_getdevicelist_res res = {
6202 .devlist = devlist,
6203 };
6204 struct rpc_message msg = {
6205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6206 .rpc_argp = &args,
6207 .rpc_resp = &res,
6208 };
6209 int status;
6210
6211 dprintk("--> %s\n", __func__);
6212 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6213 &res.seq_res, 0);
6214 dprintk("<-- %s status=%d\n", __func__, status);
6215 return status;
6216}
6217
6218int nfs4_proc_getdevicelist(struct nfs_server *server,
6219 const struct nfs_fh *fh,
6220 struct pnfs_devicelist *devlist)
6221{
6222 struct nfs4_exception exception = { };
6223 int err;
6224
6225 do {
6226 err = nfs4_handle_exception(server,
6227 _nfs4_getdevicelist(server, fh, devlist),
6228 &exception);
6229 } while (exception.retry);
6230
6231 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6232 err, devlist->num_devs);
6233
6234 return err;
6235}
6236EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6237
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006238static int
6239_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6240{
6241 struct nfs4_getdeviceinfo_args args = {
6242 .pdev = pdev,
6243 };
6244 struct nfs4_getdeviceinfo_res res = {
6245 .pdev = pdev,
6246 };
6247 struct rpc_message msg = {
6248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6249 .rpc_argp = &args,
6250 .rpc_resp = &res,
6251 };
6252 int status;
6253
6254 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006255 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006256 dprintk("<-- %s status=%d\n", __func__, status);
6257
6258 return status;
6259}
6260
6261int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6262{
6263 struct nfs4_exception exception = { };
6264 int err;
6265
6266 do {
6267 err = nfs4_handle_exception(server,
6268 _nfs4_proc_getdeviceinfo(server, pdev),
6269 &exception);
6270 } while (exception.retry);
6271 return err;
6272}
6273EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6274
Andy Adamson863a3c62011-03-23 13:27:54 +00006275static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6276{
6277 struct nfs4_layoutcommit_data *data = calldata;
6278 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006279 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006280
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006281 nfs41_setup_sequence(session,
6282 &data->args.seq_args,
6283 &data->res.seq_res,
6284 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006285}
6286
6287static void
6288nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6289{
6290 struct nfs4_layoutcommit_data *data = calldata;
6291 struct nfs_server *server = NFS_SERVER(data->args.inode);
6292
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006293 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006294 return;
6295
6296 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006297 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6298 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6299 case -NFS4ERR_BADLAYOUT: /* no layout */
6300 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006301 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006302 break;
6303 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006304 nfs_post_op_update_inode_force_wcc(data->args.inode,
6305 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006306 break;
6307 default:
6308 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6309 rpc_restart_call_prepare(task);
6310 return;
6311 }
6312 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006313}
6314
6315static void nfs4_layoutcommit_release(void *calldata)
6316{
6317 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006318 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006319 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006320
Andy Adamsondb29c082011-07-30 20:52:38 -04006321 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006322 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006323 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6324 list_del_init(&lseg->pls_lc_list);
6325 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6326 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006327 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006328 }
Peng Tao92407e72011-10-23 20:21:17 -07006329
6330 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6331 smp_mb__after_clear_bit();
6332 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6333
Andy Adamson863a3c62011-03-23 13:27:54 +00006334 put_rpccred(data->cred);
6335 kfree(data);
6336}
6337
6338static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6339 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6340 .rpc_call_done = nfs4_layoutcommit_done,
6341 .rpc_release = nfs4_layoutcommit_release,
6342};
6343
6344int
Andy Adamsonef311532011-03-12 02:58:10 -05006345nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006346{
6347 struct rpc_message msg = {
6348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6349 .rpc_argp = &data->args,
6350 .rpc_resp = &data->res,
6351 .rpc_cred = data->cred,
6352 };
6353 struct rpc_task_setup task_setup_data = {
6354 .task = &data->task,
6355 .rpc_client = NFS_CLIENT(data->args.inode),
6356 .rpc_message = &msg,
6357 .callback_ops = &nfs4_layoutcommit_ops,
6358 .callback_data = data,
6359 .flags = RPC_TASK_ASYNC,
6360 };
6361 struct rpc_task *task;
6362 int status = 0;
6363
6364 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6365 "lbw: %llu inode %lu\n",
6366 data->task.tk_pid, sync,
6367 data->args.lastbytewritten,
6368 data->args.inode->i_ino);
6369
Trond Myklebust9d12b212012-01-17 22:04:25 -05006370 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006371 task = rpc_run_task(&task_setup_data);
6372 if (IS_ERR(task))
6373 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006374 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006375 goto out;
6376 status = nfs4_wait_for_completion_rpc_task(task);
6377 if (status != 0)
6378 goto out;
6379 status = task->tk_status;
6380out:
6381 dprintk("%s: status %d\n", __func__, status);
6382 rpc_put_task(task);
6383 return status;
6384}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006385
6386static int
6387_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6388 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6389{
6390 struct nfs41_secinfo_no_name_args args = {
6391 .style = SECINFO_STYLE_CURRENT_FH,
6392 };
6393 struct nfs4_secinfo_res res = {
6394 .flavors = flavors,
6395 };
6396 struct rpc_message msg = {
6397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6398 .rpc_argp = &args,
6399 .rpc_resp = &res,
6400 };
6401 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6402}
6403
6404static int
6405nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6406 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6407{
6408 struct nfs4_exception exception = { };
6409 int err;
6410 do {
6411 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6412 switch (err) {
6413 case 0:
6414 case -NFS4ERR_WRONGSEC:
6415 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006416 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006417 default:
6418 err = nfs4_handle_exception(server, err, &exception);
6419 }
6420 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006421out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006422 return err;
6423}
6424
6425static int
6426nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6427 struct nfs_fsinfo *info)
6428{
6429 int err;
6430 struct page *page;
6431 rpc_authflavor_t flavor;
6432 struct nfs4_secinfo_flavors *flavors;
6433
6434 page = alloc_page(GFP_KERNEL);
6435 if (!page) {
6436 err = -ENOMEM;
6437 goto out;
6438 }
6439
6440 flavors = page_address(page);
6441 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6442
6443 /*
6444 * Fall back on "guess and check" method if
6445 * the server doesn't support SECINFO_NO_NAME
6446 */
6447 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6448 err = nfs4_find_root_sec(server, fhandle, info);
6449 goto out_freepage;
6450 }
6451 if (err)
6452 goto out_freepage;
6453
6454 flavor = nfs_find_best_sec(flavors);
6455 if (err == 0)
6456 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6457
6458out_freepage:
6459 put_page(page);
6460 if (err == -EACCES)
6461 return -EPERM;
6462out:
6463 return err;
6464}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006465
6466static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006467{
6468 int status;
6469 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006470 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006471 };
6472 struct nfs41_test_stateid_res res;
6473 struct rpc_message msg = {
6474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6475 .rpc_argp = &args,
6476 .rpc_resp = &res,
6477 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006478
Chuck Lever38527b12012-07-11 16:30:23 -04006479 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006480 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006481 nfs4_set_sequence_privileged(&args.seq_args);
6482 status = nfs4_call_sync_sequence(server->client, server, &msg,
6483 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006484 if (status != NFS_OK) {
6485 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006486 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006487 }
6488 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006489 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006490}
6491
Chuck Lever38527b12012-07-11 16:30:23 -04006492/**
6493 * nfs41_test_stateid - perform a TEST_STATEID operation
6494 *
6495 * @server: server / transport on which to perform the operation
6496 * @stateid: state ID to test
6497 *
6498 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6499 * Otherwise a negative NFS4ERR value is returned if the operation
6500 * failed or the state ID is not currently valid.
6501 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006502static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006503{
6504 struct nfs4_exception exception = { };
6505 int err;
6506 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006507 err = _nfs41_test_stateid(server, stateid);
6508 if (err != -NFS4ERR_DELAY)
6509 break;
6510 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006511 } while (exception.retry);
6512 return err;
6513}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006514
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006515static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006516{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006517 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006518 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006519 };
6520 struct nfs41_free_stateid_res res;
6521 struct rpc_message msg = {
6522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6523 .rpc_argp = &args,
6524 .rpc_resp = &res,
6525 };
Chuck Lever38527b12012-07-11 16:30:23 -04006526 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006527
Chuck Lever38527b12012-07-11 16:30:23 -04006528 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006529 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006530 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006531 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006532 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006533 dprintk("NFS reply free_stateid: %d\n", status);
6534 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006535}
6536
Chuck Lever38527b12012-07-11 16:30:23 -04006537/**
6538 * nfs41_free_stateid - perform a FREE_STATEID operation
6539 *
6540 * @server: server / transport on which to perform the operation
6541 * @stateid: state ID to release
6542 *
6543 * Returns NFS_OK if the server freed "stateid". Otherwise a
6544 * negative NFS4ERR value is returned.
6545 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006546static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006547{
6548 struct nfs4_exception exception = { };
6549 int err;
6550 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006551 err = _nfs4_free_stateid(server, stateid);
6552 if (err != -NFS4ERR_DELAY)
6553 break;
6554 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006555 } while (exception.retry);
6556 return err;
6557}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006558
6559static bool nfs41_match_stateid(const nfs4_stateid *s1,
6560 const nfs4_stateid *s2)
6561{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006562 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006563 return false;
6564
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006565 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006566 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006567 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006568 return true;
6569
6570 return false;
6571}
6572
Andy Adamson557134a2009-04-01 09:21:53 -04006573#endif /* CONFIG_NFS_V4_1 */
6574
Trond Myklebust36281ca2012-03-04 18:13:56 -05006575static bool nfs4_match_stateid(const nfs4_stateid *s1,
6576 const nfs4_stateid *s2)
6577{
Trond Myklebustf597c532012-03-04 18:13:56 -05006578 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006579}
6580
6581
Trond Myklebust17280172012-03-11 13:11:00 -04006582static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006583 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006584 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 .recover_open = nfs4_open_reclaim,
6586 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006587 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006588 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006589 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590};
6591
Andy Adamson591d71c2009-04-01 09:22:47 -04006592#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006593static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006594 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6595 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6596 .recover_open = nfs4_open_reclaim,
6597 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006598 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006599 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006600 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006601 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006602};
6603#endif /* CONFIG_NFS_V4_1 */
6604
Trond Myklebust17280172012-03-11 13:11:00 -04006605static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006606 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006607 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 .recover_open = nfs4_open_expired,
6609 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006610 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006611 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612};
6613
Andy Adamson591d71c2009-04-01 09:22:47 -04006614#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006615static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006616 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6617 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006618 .recover_open = nfs41_open_expired,
6619 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006620 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006621 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006622};
6623#endif /* CONFIG_NFS_V4_1 */
6624
Trond Myklebust17280172012-03-11 13:11:00 -04006625static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006626 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006627 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006628 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006629};
6630
6631#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006632static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006633 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006634 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006635 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006636};
6637#endif
6638
Trond Myklebust97dc1352010-06-16 09:52:26 -04006639static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6640 .minor_version = 0,
6641 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006642 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006643 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006644 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6645 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6646 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006647};
6648
6649#if defined(CONFIG_NFS_V4_1)
6650static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6651 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006652 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006653 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006654 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006655 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6656 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6657 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006658};
6659#endif
6660
Trond Myklebust97dc1352010-06-16 09:52:26 -04006661const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6662 [0] = &nfs_v4_0_minor_ops,
6663#if defined(CONFIG_NFS_V4_1)
6664 [1] = &nfs_v4_1_minor_ops,
6665#endif
6666};
6667
Bryan Schumaker73a79702012-07-16 16:39:12 -04006668const struct inode_operations nfs4_dir_inode_operations = {
6669 .create = nfs_create,
6670 .lookup = nfs_lookup,
6671 .atomic_open = nfs_atomic_open,
6672 .link = nfs_link,
6673 .unlink = nfs_unlink,
6674 .symlink = nfs_symlink,
6675 .mkdir = nfs_mkdir,
6676 .rmdir = nfs_rmdir,
6677 .mknod = nfs_mknod,
6678 .rename = nfs_rename,
6679 .permission = nfs_permission,
6680 .getattr = nfs_getattr,
6681 .setattr = nfs_setattr,
6682 .getxattr = generic_getxattr,
6683 .setxattr = generic_setxattr,
6684 .listxattr = generic_listxattr,
6685 .removexattr = generic_removexattr,
6686};
6687
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006688static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006689 .permission = nfs_permission,
6690 .getattr = nfs_getattr,
6691 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006692 .getxattr = generic_getxattr,
6693 .setxattr = generic_setxattr,
6694 .listxattr = generic_listxattr,
6695 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006696};
6697
David Howells509de812006-08-22 20:06:11 -04006698const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 .version = 4, /* protocol version */
6700 .dentry_ops = &nfs4_dentry_operations,
6701 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006702 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006703 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006705 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006706 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 .getattr = nfs4_proc_getattr,
6708 .setattr = nfs4_proc_setattr,
6709 .lookup = nfs4_proc_lookup,
6710 .access = nfs4_proc_access,
6711 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 .create = nfs4_proc_create,
6713 .remove = nfs4_proc_remove,
6714 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006715 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 .unlink_done = nfs4_proc_unlink_done,
6717 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006718 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006719 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006720 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 .link = nfs4_proc_link,
6722 .symlink = nfs4_proc_symlink,
6723 .mkdir = nfs4_proc_mkdir,
6724 .rmdir = nfs4_proc_remove,
6725 .readdir = nfs4_proc_readdir,
6726 .mknod = nfs4_proc_mknod,
6727 .statfs = nfs4_proc_statfs,
6728 .fsinfo = nfs4_proc_fsinfo,
6729 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006730 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 .decode_dirent = nfs4_decode_dirent,
6732 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006733 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006734 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006735 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006737 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006738 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006739 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006741 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006742 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006744 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006745 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006746 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006747 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006748 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006749 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006750 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006751 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006752 .create_server = nfs4_create_server,
6753 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754};
6755
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006756static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6757 .prefix = XATTR_NAME_NFSV4_ACL,
6758 .list = nfs4_xattr_list_nfs4_acl,
6759 .get = nfs4_xattr_get_nfs4_acl,
6760 .set = nfs4_xattr_set_nfs4_acl,
6761};
6762
6763const struct xattr_handler *nfs4_xattr_handlers[] = {
6764 &nfs4_xattr_nfs4_acl_handler,
6765 NULL
6766};
6767
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768/*
6769 * Local variables:
6770 * c-basic-offset: 8
6771 * End:
6772 */