blob: 92bd799eee012bf3eab59105a28816e631c6f58e [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 Myklebustb75ad4c2012-11-29 17:27:47 -0500404 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
405 send_new_highest_used_slotid = false;
406 goto out_unlock;
407 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500408 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500409
410 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
411 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500412out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500413 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400414 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500415 if (send_new_highest_used_slotid)
416 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400417}
418
Trond Myklebust14516c32010-07-31 14:29:06 -0400419static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400420{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500421 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500422 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400423 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400424 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400425
426 /*
427 * sr_status remains 1 if an RPC level error occurred. The server
428 * may or may not have processed the sequence operation..
429 * Proceed as if the server received and processed the sequence
430 * operation.
431 */
432 if (res->sr_status == 1)
433 res->sr_status = NFS_OK;
434
Bryan Schumaker468f8612011-04-18 15:57:32 -0400435 /* don't increment the sequence number if the task wasn't sent */
436 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400437 goto out;
438
Trond Myklebust933602e2012-11-16 12:12:38 -0500439 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500440 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500441
Andy Adamson691daf32009-12-04 15:55:39 -0500442 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 switch (res->sr_status) {
444 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400445 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500446 ++slot->seq_nr;
447 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500448 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400449 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500450 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500451 if (res->sr_status_flags != 0)
452 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500453 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400454 break;
455 case -NFS4ERR_DELAY:
456 /* The server detected a resend of the RPC call and
457 * returned NFS4ERR_DELAY as per Section 2.10.6.2
458 * of RFC5661.
459 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500460 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400461 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500462 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500463 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 goto out_retry;
465 default:
466 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500467 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400468 }
469out:
470 /* The session may be reset by one of the error handlers. */
471 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400472 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400473 return 1;
474out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400475 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 goto out;
477 rpc_delay(task, NFS4_POLL_RETRY_MAX);
478 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400479}
480
Trond Myklebust14516c32010-07-31 14:29:06 -0400481static int nfs4_sequence_done(struct rpc_task *task,
482 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400483{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500484 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 return 1;
486 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400487}
488
Trond Myklebust9d12b212012-01-17 22:04:25 -0500489static void nfs41_init_sequence(struct nfs4_sequence_args *args,
490 struct nfs4_sequence_res *res, int cache_reply)
491{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500492 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500493 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400494 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500495 if (cache_reply)
496 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500497 res->sr_slot = NULL;
498}
499
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400500static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
501{
502 args->sa_privileged = 1;
503}
504
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000505int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 struct rpc_task *task)
509{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 struct nfs4_slot *slot;
511 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512
513 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400514 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400515 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400516 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400517
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 tbl = &session->fc_slot_table;
519
Trond Myklebust69d206b2012-11-22 13:21:02 -0500520 task->tk_timeout = 0;
521
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400523 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400524 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500525 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500526 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400527 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400528 }
529
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500530 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500531 if (IS_ERR(slot)) {
532 /* If out of memory, try again in 1/4 second */
533 if (slot == ERR_PTR(-ENOMEM))
534 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400535 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400536 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400537 }
538 spin_unlock(&tbl->slot_tbl_lock);
539
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500540 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400541
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500542 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
543 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400544
Benny Halevydfb4f3092010-09-24 09:17:01 -0400545 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400546 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 /*
548 * sr_status is only set in decode_sequence, and so will remain
549 * set to 1 if an rpc level failure occurs.
550 */
551 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400552out_success:
553 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400555out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400556 /* Privileged tasks are queued with top priority */
557 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400558 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
559 NULL, RPC_PRIORITY_PRIVILEGED);
560 else
561 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400562 spin_unlock(&tbl->slot_tbl_lock);
563 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400564}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000565EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566
Trond Myklebust035168a2010-06-16 09:52:26 -0400567int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 struct nfs4_sequence_args *args,
569 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400570 struct rpc_task *task)
571{
Trond Myklebust035168a2010-06-16 09:52:26 -0400572 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 int ret = 0;
574
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400575 if (session == NULL) {
576 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400578 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400579
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500580 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400581 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500582 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400583
Trond Myklebust9d12b212012-01-17 22:04:25 -0500584 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400585out:
586 dprintk("<-- %s status=%d\n", __func__, ret);
587 return ret;
588}
589
590struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400591 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592 struct nfs4_sequence_args *seq_args;
593 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400594};
595
596static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
597{
598 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400599 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600
Trond Myklebust035168a2010-06-16 09:52:26 -0400601 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
602
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400603 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604}
605
Andy Adamson69ab40c2009-04-01 09:22:19 -0400606static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
607{
608 struct nfs41_call_sync_data *data = calldata;
609
Trond Myklebust14516c32010-07-31 14:29:06 -0400610 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400611}
612
Trond Myklebust17280172012-03-11 13:11:00 -0400613static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400615 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616};
617
Bryan Schumaker7c513052011-03-24 17:12:24 +0000618static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
619 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 struct rpc_message *msg,
621 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400622 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623{
624 int ret;
625 struct rpc_task *task;
626 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400627 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628 .seq_args = args,
629 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 };
631 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000632 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 .rpc_message = msg,
634 .callback_ops = &nfs41_call_sync_ops,
635 .callback_data = &data
636 };
637
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 task = rpc_run_task(&task_setup);
639 if (IS_ERR(task))
640 ret = PTR_ERR(task);
641 else {
642 ret = task->tk_status;
643 rpc_put_task(task);
644 }
645 return ret;
646}
647
Trond Myklebustdf896452010-06-16 09:52:26 -0400648#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400649static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500650void nfs41_init_sequence(struct nfs4_sequence_args *args,
651 struct nfs4_sequence_res *res, int cache_reply)
652{
653}
654
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400655static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
656{
657}
658
659
Trond Myklebust14516c32010-07-31 14:29:06 -0400660static int nfs4_sequence_done(struct rpc_task *task,
661 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400662{
Trond Myklebust14516c32010-07-31 14:29:06 -0400663 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400664}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400665#endif /* CONFIG_NFS_V4_1 */
666
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400667static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000668int _nfs4_call_sync(struct rpc_clnt *clnt,
669 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400670 struct rpc_message *msg,
671 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400672 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400673{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000674 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400675}
676
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400677static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000678int nfs4_call_sync(struct rpc_clnt *clnt,
679 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000680 struct rpc_message *msg,
681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
683 int cache_reply)
684{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400685 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000686 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400687 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000688}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689
Trond Myklebust38478b22006-05-25 01:40:57 -0400690static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Trond Myklebust38478b22006-05-25 01:40:57 -0400692 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Trond Myklebust38478b22006-05-25 01:40:57 -0400694 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400695 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700696 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400697 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700698 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400699 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100702struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400703 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100704 struct nfs_openargs o_arg;
705 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100706 struct nfs_open_confirmargs c_arg;
707 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500708 struct nfs4_string owner_name;
709 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100710 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400712 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400714 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100716 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400717 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100718 int rpc_status;
719 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720};
721
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400722
723static void nfs4_init_opendata_res(struct nfs4_opendata *p)
724{
725 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400726 p->o_res.seqid = p->o_arg.seqid;
727 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400728 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400729 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400730 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500731 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400732}
733
Al Viro82a2c1b2011-06-22 18:30:55 -0400734static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500735 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400736 const struct iattr *attrs,
737 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738{
Al Viro82a2c1b2011-06-22 18:30:55 -0400739 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100740 struct inode *dir = parent->d_inode;
741 struct nfs_server *server = NFS_SERVER(dir);
742 struct nfs4_opendata *p;
743
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400744 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745 if (p == NULL)
746 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400747 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100748 if (p->o_arg.seqid == NULL)
749 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400750 nfs_sb_active(dentry->d_sb);
751 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 p->dir = parent;
753 p->owner = sp;
754 atomic_inc(&sp->so_count);
755 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500756 p->o_arg.open_flags = flags;
757 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700758 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
759 * will return permission denied for all bits until close */
760 if (!(flags & O_EXCL)) {
761 /* ask server to check for all possible rights as results
762 * are cached */
763 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
764 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700765 }
David Howells7539bba2006-08-22 20:06:09 -0400766 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400767 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
768 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400769 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 p->o_arg.server = server;
771 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400772 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500774 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500775 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400776
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 p->o_arg.u.attrs = &p->attrs;
778 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500779
780 verf[0] = jiffies;
781 verf[1] = current->pid;
782 memcpy(p->o_arg.u.verifier.data, verf,
783 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100785 p->c_arg.fh = &p->o_res.fh;
786 p->c_arg.stateid = &p->o_res.stateid;
787 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400788 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400789 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 return p;
791err_free:
792 kfree(p);
793err:
794 dput(parent);
795 return NULL;
796}
797
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400798static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400800 struct nfs4_opendata *p = container_of(kref,
801 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400802 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400803
804 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400805 if (p->state != NULL)
806 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400807 nfs4_put_state_owner(p->owner);
808 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400809 dput(p->dentry);
810 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500811 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400812 kfree(p);
813}
814
815static void nfs4_opendata_put(struct nfs4_opendata *p)
816{
817 if (p != NULL)
818 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819}
820
Trond Myklebust06f814a2006-01-03 09:55:07 +0100821static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
822{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100823 int ret;
824
Trond Myklebust06f814a2006-01-03 09:55:07 +0100825 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100826 return ret;
827}
828
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500829static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400830{
831 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832
Trond Myklebust536e43d2012-01-17 22:04:26 -0500833 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500834 goto out;
835 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400836 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500837 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
838 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839 break;
840 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500841 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
842 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400843 break;
844 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500845 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
846 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400847 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500848out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400849 return ret;
850}
851
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400853{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500854 if (delegation == NULL)
855 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400857 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500858 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400859 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500860 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400861 return 1;
862}
863
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100865{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100867 case FMODE_WRITE:
868 state->n_wronly++;
869 break;
870 case FMODE_READ:
871 state->n_rdonly++;
872 break;
873 case FMODE_READ|FMODE_WRITE:
874 state->n_rdwr++;
875 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100877}
878
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400880{
881 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500882 nfs4_stateid_copy(&state->stateid, stateid);
883 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400885 case FMODE_READ:
886 set_bit(NFS_O_RDONLY_STATE, &state->flags);
887 break;
888 case FMODE_WRITE:
889 set_bit(NFS_O_WRONLY_STATE, &state->flags);
890 break;
891 case FMODE_READ|FMODE_WRITE:
892 set_bit(NFS_O_RDWR_STATE, &state->flags);
893 }
894}
895
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400897{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400898 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400900 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static 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 -0700904{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400905 /*
906 * Protect the call to nfs4_state_set_mode_locked and
907 * serialise the stateid update
908 */
909 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400910 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500911 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400912 set_bit(NFS_DELEGATED_STATE, &state->flags);
913 }
914 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400916 write_sequnlock(&state->seqlock);
917 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700919 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static 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 -0500923{
924 struct nfs_inode *nfsi = NFS_I(state->inode);
925 struct nfs_delegation *deleg_cur;
926 int ret = 0;
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500929
930 rcu_read_lock();
931 deleg_cur = rcu_dereference(nfsi->delegation);
932 if (deleg_cur == NULL)
933 goto no_delegation;
934
935 spin_lock(&deleg_cur->lock);
936 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500938 goto no_delegation_unlock;
939
940 if (delegation == NULL)
941 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500942 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500943 goto no_delegation_unlock;
944
Trond Myklebustb7391f42008-12-23 15:21:52 -0500945 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500947 ret = 1;
948no_delegation_unlock:
949 spin_unlock(&deleg_cur->lock);
950no_delegation:
951 rcu_read_unlock();
952
953 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500955 ret = 1;
956 }
957
958 return ret;
959}
960
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963{
964 struct nfs_delegation *delegation;
965
966 rcu_read_lock();
967 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 rcu_read_unlock();
970 return;
971 }
972 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400973 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974}
975
Trond Myklebust6ee41262007-07-08 14:11:36 -0400976static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400977{
978 struct nfs4_state *state = opendata->state;
979 struct nfs_inode *nfsi = NFS_I(state->inode);
980 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500981 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400983 nfs4_stateid stateid;
984 int ret = -EAGAIN;
985
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400988 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 if (can_open_cached(state, fmode, open_mode)) {
990 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400991 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400992 goto out_return_state;
993 }
994 spin_unlock(&state->owner->so_lock);
995 }
Trond Myklebust34310432008-12-23 15:21:38 -0500996 rcu_read_lock();
997 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -0500998 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500999 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001000 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001001 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001003 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001005 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001006 if (ret != 0)
1007 goto out;
1008 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001009
1010 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001012 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001014out:
1015 return ERR_PTR(ret);
1016out_return_state:
1017 atomic_inc(&state->count);
1018 return state;
1019}
1020
Andy Adamsone23008e2012-10-02 21:07:32 -04001021static void
1022nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1023{
1024 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1025 struct nfs_delegation *delegation;
1026 int delegation_flags = 0;
1027
1028 rcu_read_lock();
1029 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1030 if (delegation)
1031 delegation_flags = delegation->flags;
1032 rcu_read_unlock();
1033 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1034 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1035 "returning a delegation for "
1036 "OPEN(CLAIM_DELEGATE_CUR)\n",
1037 clp->cl_hostname);
1038 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1039 nfs_inode_set_delegation(state->inode,
1040 data->owner->so_cred,
1041 &data->o_res);
1042 else
1043 nfs_inode_reclaim_delegation(state->inode,
1044 data->owner->so_cred,
1045 &data->o_res);
1046}
1047
1048/*
1049 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1050 * and update the nfs4_state.
1051 */
1052static struct nfs4_state *
1053_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1054{
1055 struct inode *inode = data->state->inode;
1056 struct nfs4_state *state = data->state;
1057 int ret;
1058
1059 if (!data->rpc_done) {
1060 ret = data->rpc_status;
1061 goto err;
1062 }
1063
1064 ret = -ESTALE;
1065 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1066 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1067 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1068 goto err;
1069
1070 ret = -ENOMEM;
1071 state = nfs4_get_open_state(inode, data->owner);
1072 if (state == NULL)
1073 goto err;
1074
1075 ret = nfs_refresh_inode(inode, &data->f_attr);
1076 if (ret)
1077 goto err;
1078
1079 if (data->o_res.delegation_type != 0)
1080 nfs4_opendata_check_deleg(data, state);
1081 update_open_stateid(state, &data->o_res.stateid, NULL,
1082 data->o_arg.fmode);
1083
1084 return state;
1085err:
1086 return ERR_PTR(ret);
1087
1088}
1089
1090static struct nfs4_state *
1091_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001092{
1093 struct inode *inode;
1094 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001095 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001096
Trond Myklebustaac00a82007-07-05 19:02:21 -04001097 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001098 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 goto out;
1100 }
1101
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001102 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001103 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001104 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001106 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001107 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001108 goto err;
1109 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 state = nfs4_get_open_state(inode, data->owner);
1111 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001112 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001113 if (data->o_res.delegation_type != 0)
1114 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001115 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001116 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001117 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001119 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001120err_put_inode:
1121 iput(inode);
1122err:
1123 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124}
1125
Andy Adamsone23008e2012-10-02 21:07:32 -04001126static struct nfs4_state *
1127nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1128{
1129 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1130 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1131 return _nfs4_opendata_to_nfs4_state(data);
1132}
1133
Trond Myklebust864472e2006-01-03 09:55:15 +01001134static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1135{
1136 struct nfs_inode *nfsi = NFS_I(state->inode);
1137 struct nfs_open_context *ctx;
1138
1139 spin_lock(&state->inode->i_lock);
1140 list_for_each_entry(ctx, &nfsi->open_files, list) {
1141 if (ctx->state != state)
1142 continue;
1143 get_nfs_open_context(ctx);
1144 spin_unlock(&state->inode->i_lock);
1145 return ctx;
1146 }
1147 spin_unlock(&state->inode->i_lock);
1148 return ERR_PTR(-ENOENT);
1149}
1150
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001151static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1152{
1153 struct nfs4_opendata *opendata;
1154
Al Viro3d4ff432011-06-22 18:40:12 -04001155 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001156 if (opendata == NULL)
1157 return ERR_PTR(-ENOMEM);
1158 opendata->state = state;
1159 atomic_inc(&state->count);
1160 return opendata;
1161}
1162
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001163static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001164{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001165 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 int ret;
1167
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001168 opendata->o_arg.open_flags = 0;
1169 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001170 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1171 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1172 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001173 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001174 if (ret != 0)
1175 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001176 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001177 if (IS_ERR(newstate))
1178 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001179 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 return 0;
1182}
1183
1184static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1185{
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 int ret;
1188
1189 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 smp_rmb();
1192 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001193 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 if (ret != 0)
1196 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 if (newstate != state)
1198 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 }
1200 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001201 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001202 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 if (ret != 0)
1204 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 if (newstate != state)
1206 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 }
1208 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001209 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 if (ret != 0)
1212 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 if (newstate != state)
1214 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001216 /*
1217 * We may have performed cached opens for all three recoveries.
1218 * Check if we need to update the current stateid.
1219 */
1220 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001221 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001222 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001223 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001224 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001225 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001226 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 return 0;
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
1231 * OPEN_RECLAIM:
1232 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001234static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001236 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int status;
1240
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001241 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1242 if (IS_ERR(opendata))
1243 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1245 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001246 rcu_read_lock();
1247 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001248 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001249 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001250 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 opendata->o_arg.u.delegation_type = delegation_type;
1252 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001253 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return status;
1255}
1256
Trond Myklebust539cd032007-06-05 11:46:42 -04001257static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 struct nfs_server *server = NFS_SERVER(state->inode);
1260 struct nfs4_exception exception = { };
1261 int err;
1262 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001263 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001264 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001265 break;
1266 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 } while (exception.retry);
1268 return err;
1269}
1270
Trond Myklebust864472e2006-01-03 09:55:15 +01001271static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1272{
1273 struct nfs_open_context *ctx;
1274 int ret;
1275
1276 ctx = nfs4_state_find_open_context(state);
1277 if (IS_ERR(ctx))
1278 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001279 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 put_nfs_open_context(ctx);
1281 return ret;
1282}
1283
Trond Myklebust13437e12007-07-06 15:10:43 -04001284static 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 -07001285{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001286 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001289 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1290 if (IS_ERR(opendata))
1291 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001293 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Trond Myklebust13437e12007-07-06 15:10:43 -04001299int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 int err;
1304 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001305 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 switch (err) {
1307 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001308 case -ENOENT:
1309 case -ESTALE:
1310 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001311 case -NFS4ERR_BADSESSION:
1312 case -NFS4ERR_BADSLOT:
1313 case -NFS4ERR_BAD_HIGH_SLOT:
1314 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1315 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001316 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case -NFS4ERR_STALE_CLIENTID:
1319 case -NFS4ERR_STALE_STATEID:
1320 case -NFS4ERR_EXPIRED:
1321 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001322 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001323 goto out;
1324 case -ERESTARTSYS:
1325 /*
1326 * The show must go on: exit, but mark the
1327 * stateid as needing recovery.
1328 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001329 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001330 case -NFS4ERR_ADMIN_REVOKED:
1331 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001332 nfs_inode_find_state_and_recover(state->inode,
1333 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001334 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001335 case -EKEYEXPIRED:
1336 /*
1337 * User RPCSEC_GSS context has expired.
1338 * We cannot recover this stateid now, so
1339 * skip it and allow recovery thread to
1340 * proceed.
1341 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001342 case -ENOMEM:
1343 err = 0;
1344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 err = nfs4_handle_exception(server, err, &exception);
1347 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001348out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return err;
1350}
1351
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001352static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1353{
1354 struct nfs4_opendata *data = calldata;
1355
1356 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001357 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001358 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001359 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001360 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001361 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001362 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363}
1364
1365static void nfs4_open_confirm_release(void *calldata)
1366{
1367 struct nfs4_opendata *data = calldata;
1368 struct nfs4_state *state = NULL;
1369
1370 /* If this request hasn't been cancelled, do nothing */
1371 if (data->cancelled == 0)
1372 goto out_free;
1373 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001374 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001375 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001376 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001377 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001378 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001379out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001380 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001381}
1382
1383static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384 .rpc_call_done = nfs4_open_confirm_done,
1385 .rpc_release = nfs4_open_confirm_release,
1386};
1387
1388/*
1389 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1390 */
1391static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1392{
1393 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1394 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001395 struct rpc_message msg = {
1396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1397 .rpc_argp = &data->c_arg,
1398 .rpc_resp = &data->c_res,
1399 .rpc_cred = data->owner->so_cred,
1400 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001401 struct rpc_task_setup task_setup_data = {
1402 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001403 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001404 .callback_ops = &nfs4_open_confirm_ops,
1405 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001406 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001407 .flags = RPC_TASK_ASYNC,
1408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int status;
1410
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001411 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001412 data->rpc_done = 0;
1413 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001415 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001416 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418 status = nfs4_wait_for_completion_rpc_task(task);
1419 if (status != 0) {
1420 data->cancelled = 1;
1421 smp_wmb();
1422 } else
1423 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001424 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return status;
1426}
1427
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430 struct nfs4_opendata *data = calldata;
1431 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001432
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1434 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001435 /*
1436 * Check if we still need to send an OPEN call, or if we can use
1437 * a delegation instead.
1438 */
1439 if (data->state != NULL) {
1440 struct nfs_delegation *delegation;
1441
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001442 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001443 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001444 rcu_read_lock();
1445 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001446 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1447 can_open_delegated(delegation, data->o_arg.fmode))
1448 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001449 rcu_read_unlock();
1450 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001451 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001452 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001453 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001454 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001455 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001456 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1457 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001458 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001459 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001460 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001461 &data->o_res.seq_res,
1462 task) != 0)
1463 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001464 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001465unlock_no_action:
1466 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001467out_no_action:
1468 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001469 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001470}
1471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472static void nfs4_open_done(struct rpc_task *task, void *calldata)
1473{
1474 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001477
Trond Myklebust14516c32010-07-31 14:29:06 -04001478 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1479 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001482 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1483 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001484 case S_IFREG:
1485 break;
1486 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001488 break;
1489 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001491 break;
1492 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001494 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001495 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001496 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001497 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1498 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001499 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001500 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503static void nfs4_open_release(void *calldata)
1504{
1505 struct nfs4_opendata *data = calldata;
1506 struct nfs4_state *state = NULL;
1507
1508 /* If this request hasn't been cancelled, do nothing */
1509 if (data->cancelled == 0)
1510 goto out_free;
1511 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001512 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 goto out_free;
1514 /* In case we need an open_confirm, no cleanup! */
1515 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1516 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001518 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001519 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001521 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522}
1523
1524static const struct rpc_call_ops nfs4_open_ops = {
1525 .rpc_call_prepare = nfs4_open_prepare,
1526 .rpc_call_done = nfs4_open_done,
1527 .rpc_release = nfs4_open_release,
1528};
1529
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001530static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531{
1532 struct inode *dir = data->dir->d_inode;
1533 struct nfs_server *server = NFS_SERVER(dir);
1534 struct nfs_openargs *o_arg = &data->o_arg;
1535 struct nfs_openres *o_res = &data->o_res;
1536 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001537 struct rpc_message msg = {
1538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1539 .rpc_argp = o_arg,
1540 .rpc_resp = o_res,
1541 .rpc_cred = data->owner->so_cred,
1542 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 struct rpc_task_setup task_setup_data = {
1544 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001546 .callback_ops = &nfs4_open_ops,
1547 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001548 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001549 .flags = RPC_TASK_ASYNC,
1550 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551 int status;
1552
Trond Myklebust9d12b212012-01-17 22:04:25 -05001553 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001554 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001555 data->rpc_done = 0;
1556 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001557 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001558 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001559 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001560 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001561 if (IS_ERR(task))
1562 return PTR_ERR(task);
1563 status = nfs4_wait_for_completion_rpc_task(task);
1564 if (status != 0) {
1565 data->cancelled = 1;
1566 smp_wmb();
1567 } else
1568 status = data->rpc_status;
1569 rpc_put_task(task);
1570
1571 return status;
1572}
1573
1574static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1575{
1576 struct inode *dir = data->dir->d_inode;
1577 struct nfs_openres *o_res = &data->o_res;
1578 int status;
1579
1580 status = nfs4_run_open_task(data, 1);
1581 if (status != 0 || !data->rpc_done)
1582 return status;
1583
Trond Myklebust6926afd2012-01-07 13:22:46 -05001584 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1585
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001586 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1587 status = _nfs4_proc_open_confirm(data);
1588 if (status != 0)
1589 return status;
1590 }
1591
1592 return status;
1593}
1594
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001595static int nfs4_opendata_access(struct rpc_cred *cred,
1596 struct nfs4_opendata *opendata,
1597 struct nfs4_state *state, fmode_t fmode)
1598{
1599 struct nfs_access_entry cache;
1600 u32 mask;
1601
1602 /* access call failed or for some reason the server doesn't
1603 * support any access modes -- defer access call until later */
1604 if (opendata->o_res.access_supported == 0)
1605 return 0;
1606
1607 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001608 /* don't check MAY_WRITE - a newly created file may not have
1609 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001610 if (fmode & FMODE_READ)
1611 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001612 if (fmode & FMODE_EXEC)
1613 mask |= MAY_EXEC;
1614
1615 cache.cred = cred;
1616 cache.jiffies = jiffies;
1617 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1618 nfs_access_add_cache(state->inode, &cache);
1619
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001620 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001621 return 0;
1622
1623 /* even though OPEN succeeded, access is denied. Close the file */
1624 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001625 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001626}
1627
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001628/*
1629 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1630 */
1631static int _nfs4_proc_open(struct nfs4_opendata *data)
1632{
1633 struct inode *dir = data->dir->d_inode;
1634 struct nfs_server *server = NFS_SERVER(dir);
1635 struct nfs_openargs *o_arg = &data->o_arg;
1636 struct nfs_openres *o_res = &data->o_res;
1637 int status;
1638
1639 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001640 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001642 if (status != 0) {
1643 if (status == -NFS4ERR_BADNAME &&
1644 !(o_arg->open_flags & O_CREAT))
1645 return -ENOENT;
1646 return status;
1647 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001648
Trond Myklebust6926afd2012-01-07 13:22:46 -05001649 nfs_fattr_map_and_free_names(server, &data->f_attr);
1650
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001651 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001652 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001653 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1654 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001656 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001658 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001661 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Andy Adamsond83217c2011-03-01 01:34:17 +00001665static int nfs4_recover_expired_lease(struct nfs_server *server)
1666{
1667 return nfs4_client_recover_expired_lease(server->nfs_client);
1668}
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670/*
1671 * OPEN_EXPIRED:
1672 * reclaim state on the server after a network partition.
1673 * Assumes caller holds the appropriate lock
1674 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001675static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001677 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001678 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001680 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1681 if (IS_ERR(opendata))
1682 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001683 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001684 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001685 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001686 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001687 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001690static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001691{
Trond Myklebust539cd032007-06-05 11:46:42 -04001692 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001693 struct nfs4_exception exception = { };
1694 int err;
1695
1696 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001697 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001698 switch (err) {
1699 default:
1700 goto out;
1701 case -NFS4ERR_GRACE:
1702 case -NFS4ERR_DELAY:
1703 nfs4_handle_exception(server, err, &exception);
1704 err = 0;
1705 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001706 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001707out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001708 return err;
1709}
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Trond Myklebust864472e2006-01-03 09:55:15 +01001716 ctx = nfs4_state_find_open_context(state);
1717 if (IS_ERR(ctx))
1718 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001719 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001720 put_nfs_open_context(ctx);
1721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001724#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001725static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001726{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001727 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001728 nfs4_stateid *stateid = &state->stateid;
1729 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001730
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001731 /* If a state reset has been done, test_stateid is unneeded */
1732 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1733 return;
1734
1735 status = nfs41_test_stateid(server, stateid);
1736 if (status != NFS_OK) {
1737 /* Free the stateid unless the server explicitly
1738 * informs us the stateid is unrecognized. */
1739 if (status != -NFS4ERR_BAD_STATEID)
1740 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001741 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001742
Bryan Schumaker69388672012-09-26 15:25:52 -04001743 write_seqlock(&state->seqlock);
1744 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1745 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001746 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1747 }
1748}
1749
1750/**
1751 * nfs41_check_open_stateid - possibly free an open stateid
1752 *
1753 * @state: NFSv4 state for an inode
1754 *
1755 * Returns NFS_OK if recovery for this stateid is now finished.
1756 * Otherwise a negative NFS4ERR value is returned.
1757 */
1758static int nfs41_check_open_stateid(struct nfs4_state *state)
1759{
1760 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001761 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001762 int status;
1763
1764 /* If a state reset has been done, test_stateid is unneeded */
1765 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1766 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1767 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1768 return -NFS4ERR_BAD_STATEID;
1769
1770 status = nfs41_test_stateid(server, stateid);
1771 if (status != NFS_OK) {
1772 /* Free the stateid unless the server explicitly
1773 * informs us the stateid is unrecognized. */
1774 if (status != -NFS4ERR_BAD_STATEID)
1775 nfs41_free_stateid(server, stateid);
1776
1777 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1778 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1779 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001780 }
1781 return status;
1782}
1783
1784static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1785{
Chuck Levereb64cf92012-07-11 16:30:05 -04001786 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001787
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001788 nfs41_clear_delegation_stateid(state);
1789 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001790 if (status != NFS_OK)
1791 status = nfs4_open_expired(sp, state);
1792 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001793}
1794#endif
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001797 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1798 * fields corresponding to attributes that were used to store the verifier.
1799 * Make sure we clobber those fields in the later setattr call
1800 */
1801static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1802{
1803 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1804 !(sattr->ia_valid & ATTR_ATIME_SET))
1805 sattr->ia_valid |= ATTR_ATIME;
1806
1807 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1808 !(sattr->ia_valid & ATTR_MTIME_SET))
1809 sattr->ia_valid |= ATTR_MTIME;
1810}
1811
1812/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Andy Adamson82be4172012-05-23 05:02:35 -04001815static int _nfs4_do_open(struct inode *dir,
1816 struct dentry *dentry,
1817 fmode_t fmode,
1818 int flags,
1819 struct iattr *sattr,
1820 struct rpc_cred *cred,
1821 struct nfs4_state **res,
1822 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 struct nfs4_state_owner *sp;
1825 struct nfs4_state *state = NULL;
1826 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001827 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001828 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001832 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1833 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1835 goto out_err;
1836 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001837 status = nfs4_recover_expired_lease(server);
1838 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001839 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001840 if (dentry->d_inode != NULL)
1841 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001842 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001843 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001844 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001845 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Andy Adamson82be4172012-05-23 05:02:35 -04001847 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1848 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1849 if (!opendata->f_attr.mdsthreshold)
1850 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001851 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001852 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001853 if (dentry->d_inode != NULL)
1854 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001855
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001858 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001860 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001861 status = PTR_ERR(state);
1862 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001863 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001864 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001865 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001866
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001867 status = nfs4_opendata_access(cred, opendata, state, fmode);
1868 if (status != 0)
1869 goto err_opendata_put;
1870
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001871 if (opendata->o_arg.open_flags & O_EXCL) {
1872 nfs4_exclusive_attrset(opendata, sattr);
1873
1874 nfs_fattr_init(opendata->o_res.f_attr);
1875 status = nfs4_do_setattr(state->inode, cred,
1876 opendata->o_res.f_attr, sattr,
1877 state);
1878 if (status == 0)
1879 nfs_setattr_update_inode(state->inode, sattr);
1880 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1881 }
Andy Adamson82be4172012-05-23 05:02:35 -04001882
1883 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1884 *ctx_th = opendata->f_attr.mdsthreshold;
1885 else
1886 kfree(opendata->f_attr.mdsthreshold);
1887 opendata->f_attr.mdsthreshold = NULL;
1888
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001889 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 *res = state;
1892 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001893err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001894 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001895 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001896err_put_state_owner:
1897 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 *res = NULL;
1900 return status;
1901}
1902
1903
Andy Adamson82be4172012-05-23 05:02:35 -04001904static struct nfs4_state *nfs4_do_open(struct inode *dir,
1905 struct dentry *dentry,
1906 fmode_t fmode,
1907 int flags,
1908 struct iattr *sattr,
1909 struct rpc_cred *cred,
1910 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 struct nfs4_exception exception = { };
1913 struct nfs4_state *res;
1914 int status;
1915
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001916 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001918 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1919 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (status == 0)
1921 break;
1922 /* NOTE: BAD_SEQID means the server and client disagree about the
1923 * book-keeping w.r.t. state-changing operations
1924 * (OPEN/CLOSE/LOCK/LOCKU...)
1925 * It is actually a sign of a bug on the client or on the server.
1926 *
1927 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001928 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * have unhashed the old state_owner for us, and that we can
1930 * therefore safely retry using a new one. We should still warn
1931 * the user though...
1932 */
1933 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001934 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001935 " returned a bad sequence-id error!\n",
1936 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 exception.retry = 1;
1938 continue;
1939 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001940 /*
1941 * BAD_STATEID on OPEN means that the server cancelled our
1942 * state before it received the OPEN_CONFIRM.
1943 * Recover by retrying the request as per the discussion
1944 * on Page 181 of RFC3530.
1945 */
1946 if (status == -NFS4ERR_BAD_STATEID) {
1947 exception.retry = 1;
1948 continue;
1949 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001950 if (status == -EAGAIN) {
1951 /* We must have found a delegation */
1952 exception.retry = 1;
1953 continue;
1954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1956 status, &exception));
1957 } while (exception.retry);
1958 return res;
1959}
1960
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001961static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1962 struct nfs_fattr *fattr, struct iattr *sattr,
1963 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001965 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001967 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 .iap = sattr,
1969 .server = server,
1970 .bitmask = server->attr_bitmask,
1971 };
1972 struct nfs_setattrres res = {
1973 .fattr = fattr,
1974 .server = server,
1975 };
1976 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1978 .rpc_argp = &arg,
1979 .rpc_resp = &res,
1980 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001982 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001983 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Trond Myklebust0e574af2005-10-27 22:12:38 -04001985 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Trond Myklebust4fc87962012-03-08 17:42:01 -05001987 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04001988 struct nfs_lockowner lockowner = {
1989 .l_owner = current->files,
1990 .l_pid = current->tgid,
1991 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05001992 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04001993 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05001994 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1995 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001996 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001997 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001998 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Bryan Schumaker7c513052011-03-24 17:12:24 +00002000 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002001 if (status == 0 && state != NULL)
2002 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002003 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002006static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2007 struct nfs_fattr *fattr, struct iattr *sattr,
2008 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002010 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002011 struct nfs4_exception exception = {
2012 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002013 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002014 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 int err;
2016 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002017 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2018 switch (err) {
2019 case -NFS4ERR_OPENMODE:
2020 if (state && !(state->state & FMODE_WRITE)) {
2021 err = -EBADF;
2022 if (sattr->ia_valid & ATTR_OPEN)
2023 err = -EACCES;
2024 goto out;
2025 }
2026 }
2027 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002029out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return err;
2031}
2032
2033struct nfs4_closedata {
2034 struct inode *inode;
2035 struct nfs4_state *state;
2036 struct nfs_closeargs arg;
2037 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002038 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002039 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002040 bool roc;
2041 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042};
2043
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002044static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002045{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002046 struct nfs4_closedata *calldata = data;
2047 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002048 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002049
Fred Isamanf7e89172011-01-06 11:36:32 +00002050 if (calldata->roc)
2051 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002052 nfs4_put_open_state(calldata->state);
2053 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002054 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002055 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002056 kfree(calldata);
2057}
2058
Trond Myklebust88069f72009-12-08 08:33:16 -05002059static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2060 fmode_t fmode)
2061{
2062 spin_lock(&state->owner->so_lock);
2063 if (!(fmode & FMODE_READ))
2064 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2065 if (!(fmode & FMODE_WRITE))
2066 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2067 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2068 spin_unlock(&state->owner->so_lock);
2069}
2070
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002071static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002073 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct nfs_server *server = NFS_SERVER(calldata->inode);
2076
Chuck Levera3ca5652012-03-01 17:00:40 -05002077 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002078 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2079 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* hmm. we are done with the inode, and in the process of freeing
2081 * the state_owner. we keep this around to process errors
2082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 switch (task->tk_status) {
2084 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002085 if (calldata->roc)
2086 pnfs_roc_set_barrier(state->inode,
2087 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002088 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002089 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002090 nfs4_close_clear_stateid_flags(state,
2091 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 break;
2093 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002094 case -NFS4ERR_OLD_STATEID:
2095 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002097 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002100 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2101 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002103 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002104 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002105 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002108static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002110 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002111 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002112 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002113 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002114
Chuck Levera3ca5652012-03-01 17:00:40 -05002115 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002116 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002117 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002118
Trond Myklebust88069f72009-12-08 08:33:16 -05002119 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2120 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002121 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002122 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002123 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002124 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002125 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2126 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2127 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002128 }
2129 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002130 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2131 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2132 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002133 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002134 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002135 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002136
2137 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002138 /* Note: exit _without_ calling nfs4_close_done */
2139 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002140 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002141 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002142 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002143
Fred Isamanf7e89172011-01-06 11:36:32 +00002144 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002145 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002146 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002147 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002148 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002149 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002150
Trond Myklebust516a6af2005-10-27 22:12:41 -04002151 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002152 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002153 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002154 &calldata->arg.seq_args,
2155 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002156 task) != 0)
2157 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002158out:
2159 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002162static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002163 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002164 .rpc_call_done = nfs4_close_done,
2165 .rpc_release = nfs4_free_closedata,
2166};
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168/*
2169 * It is possible for data to be read/written from a mem-mapped file
2170 * after the sys_close call (which hits the vfs layer as a flush).
2171 * This means that we can't safely call nfsv4 close on a file until
2172 * the inode is cleared. This in turn means that we are not good
2173 * NFSv4 citizens - we do not indicate to the server to update the file's
2174 * share state even when we are done with one of the three share
2175 * stateid's in the inode.
2176 *
2177 * NOTE: Caller must be holding the sp->so_owner semaphore!
2178 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002179int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002181 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002183 struct nfs4_state_owner *sp = state->owner;
2184 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 struct rpc_message msg = {
2186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2187 .rpc_cred = state->owner->so_cred,
2188 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 struct rpc_task_setup task_setup_data = {
2190 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002191 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002192 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002193 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002194 .flags = RPC_TASK_ASYNC,
2195 };
Trond Myklebust95121352005-10-18 14:20:12 -07002196 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002198 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002200 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002201 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002202 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002204 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002205 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002207 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002208 if (calldata->arg.seqid == NULL)
2209 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002210 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002211 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002212 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002213 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002214 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002215 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002216 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002217
Trond Myklebust1174dd12010-12-21 10:52:24 -05002218 msg.rpc_argp = &calldata->arg;
2219 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 task_setup_data.callback_data = calldata;
2221 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002222 if (IS_ERR(task))
2223 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002224 status = 0;
2225 if (wait)
2226 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002227 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002228 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002229out_free_calldata:
2230 kfree(calldata);
2231out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002232 nfs4_put_open_state(state);
2233 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002234 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Trond Myklebust2b484292010-09-17 10:56:51 -04002237static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002238nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 struct nfs4_state *state;
2241
Trond Myklebust565277f2007-10-15 18:17:53 -04002242 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002243 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2244 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002245 if (IS_ERR(state))
2246 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002247 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002248 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Trond Myklebust1185a552009-12-03 15:54:02 -05002251static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002252{
2253 if (ctx->state == NULL)
2254 return;
2255 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002256 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002257 else
Al Viro643168c2011-06-22 18:20:23 -04002258 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2262{
Benny Halevy43652ad2009-04-01 09:21:54 -04002263 struct nfs4_server_caps_arg args = {
2264 .fhandle = fhandle,
2265 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 struct nfs4_server_caps_res res = {};
2267 struct rpc_message msg = {
2268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002269 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 .rpc_resp = &res,
2271 };
2272 int status;
2273
Bryan Schumaker7c513052011-03-24 17:12:24 +00002274 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (status == 0) {
2276 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002277 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2278 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2279 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2280 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2281 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2283 server->caps |= NFS_CAP_ACLS;
2284 if (res.has_links != 0)
2285 server->caps |= NFS_CAP_HARDLINKS;
2286 if (res.has_symlinks != 0)
2287 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002288 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2289 server->caps |= NFS_CAP_FILEID;
2290 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2291 server->caps |= NFS_CAP_MODE;
2292 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2293 server->caps |= NFS_CAP_NLINK;
2294 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2295 server->caps |= NFS_CAP_OWNER;
2296 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2297 server->caps |= NFS_CAP_OWNER_GROUP;
2298 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2299 server->caps |= NFS_CAP_ATIME;
2300 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2301 server->caps |= NFS_CAP_CTIME;
2302 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2303 server->caps |= NFS_CAP_MTIME;
2304
Trond Myklebusta65318b2009-03-11 14:10:28 -04002305 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2306 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2307 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002309 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return status;
2313}
2314
Trond Myklebust55a97592006-06-09 09:34:19 -04002315int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317 struct nfs4_exception exception = { };
2318 int err;
2319 do {
2320 err = nfs4_handle_exception(server,
2321 _nfs4_server_capabilities(server, fhandle),
2322 &exception);
2323 } while (exception.retry);
2324 return err;
2325}
2326
2327static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2328 struct nfs_fsinfo *info)
2329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 struct nfs4_lookup_root_arg args = {
2331 .bitmask = nfs4_fattr_bitmap,
2332 };
2333 struct nfs4_lookup_res res = {
2334 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002335 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 .fh = fhandle,
2337 };
2338 struct rpc_message msg = {
2339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2340 .rpc_argp = &args,
2341 .rpc_resp = &res,
2342 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002343
Trond Myklebust0e574af2005-10-27 22:12:38 -04002344 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2349 struct nfs_fsinfo *info)
2350{
2351 struct nfs4_exception exception = { };
2352 int err;
2353 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002354 err = _nfs4_lookup_root(server, fhandle, info);
2355 switch (err) {
2356 case 0:
2357 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002358 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002359 default:
2360 err = nfs4_handle_exception(server, err, &exception);
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002363out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return err;
2365}
2366
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002367static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2368 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2369{
2370 struct rpc_auth *auth;
2371 int ret;
2372
2373 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002374 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002375 ret = -EIO;
2376 goto out;
2377 }
2378 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002379out:
2380 return ret;
2381}
2382
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002383static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002384 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002386 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002387 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002389 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002390 if (len < 0)
2391 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002392
2393 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002394 /* AUTH_UNIX is the default flavor if none was specified,
2395 * thus has already been tried. */
2396 if (flav_array[i] == RPC_AUTH_UNIX)
2397 continue;
2398
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002399 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002400 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002401 continue;
2402 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002403 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002404 /*
2405 * -EACCESS could mean that the user doesn't have correct permissions
2406 * to access the mount. It could also mean that we tried to mount
2407 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2408 * existing mount programs don't handle -EACCES very well so it should
2409 * be mapped to -EPERM instead.
2410 */
2411 if (status == -EACCES)
2412 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002413 return status;
2414}
2415
2416/*
2417 * get the file handle for the "/" directory on the server
2418 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002419int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2420 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002421{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002422 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002423 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002424 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2425 /*
2426 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2427 * by nfs4_map_errors() as this function exits.
2428 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002429 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (status == 0)
2431 status = nfs4_server_capabilities(server, fhandle);
2432 if (status == 0)
2433 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002434 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Bryan Schumakerbae36242012-05-10 15:07:31 -04002437static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2438 struct nfs_fsinfo *info)
2439{
2440 int error;
2441 struct nfs_fattr *fattr = info->fattr;
2442
2443 error = nfs4_server_capabilities(server, mntfh);
2444 if (error < 0) {
2445 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2446 return error;
2447 }
2448
2449 error = nfs4_proc_getattr(server, mntfh, fattr);
2450 if (error < 0) {
2451 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2452 return error;
2453 }
2454
2455 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2456 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2457 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2458
2459 return error;
2460}
2461
Manoj Naik6b97fd32006-06-09 09:34:29 -04002462/*
2463 * Get locations and (maybe) other attributes of a referral.
2464 * Note that we'll actually follow the referral later when
2465 * we detect fsid mismatch in inode revalidation
2466 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002467static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2468 const struct qstr *name, struct nfs_fattr *fattr,
2469 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002470{
2471 int status = -ENOMEM;
2472 struct page *page = NULL;
2473 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002474
2475 page = alloc_page(GFP_KERNEL);
2476 if (page == NULL)
2477 goto out;
2478 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2479 if (locations == NULL)
2480 goto out;
2481
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002482 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002483 if (status != 0)
2484 goto out;
2485 /* Make sure server returned a different fsid for the referral */
2486 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002487 dprintk("%s: server did not return a different fsid for"
2488 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002489 status = -EIO;
2490 goto out;
2491 }
Andy Adamson533eb462011-06-13 18:25:56 -04002492 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2493 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002494
Andy Adamson533eb462011-06-13 18:25:56 -04002495 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002496 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002497 memset(fhandle, 0, sizeof(struct nfs_fh));
2498out:
2499 if (page)
2500 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002501 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002502 return status;
2503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2506{
2507 struct nfs4_getattr_arg args = {
2508 .fh = fhandle,
2509 .bitmask = server->attr_bitmask,
2510 };
2511 struct nfs4_getattr_res res = {
2512 .fattr = fattr,
2513 .server = server,
2514 };
2515 struct rpc_message msg = {
2516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2517 .rpc_argp = &args,
2518 .rpc_resp = &res,
2519 };
2520
Trond Myklebust0e574af2005-10-27 22:12:38 -04002521 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002522 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
2525static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2526{
2527 struct nfs4_exception exception = { };
2528 int err;
2529 do {
2530 err = nfs4_handle_exception(server,
2531 _nfs4_proc_getattr(server, fhandle, fattr),
2532 &exception);
2533 } while (exception.retry);
2534 return err;
2535}
2536
2537/*
2538 * The file is not closed if it is opened due to the a request to change
2539 * the size of the file. The open call will not be needed once the
2540 * VFS layer lookup-intents are implemented.
2541 *
2542 * Close is called when the inode is destroyed.
2543 * If we haven't opened the file for O_WRONLY, we
2544 * need to in the size_change case to obtain a stateid.
2545 *
2546 * Got race?
2547 * Because OPEN is always done by name in nfsv4, it is
2548 * possible that we opened a different file by the same
2549 * name. We can recognize this race condition, but we
2550 * can't do anything about it besides returning an error.
2551 *
2552 * This will be fixed with VFS changes (lookup-intent).
2553 */
2554static int
2555nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2556 struct iattr *sattr)
2557{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002558 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002559 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002560 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 int status;
2562
Benny Halevy8a1636c2010-07-14 15:43:57 -04002563 if (pnfs_ld_layoutret_on_setattr(inode))
2564 pnfs_return_layout(inode);
2565
Trond Myklebust0e574af2005-10-27 22:12:38 -04002566 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Andy Adamson26699402012-05-30 16:12:24 -04002568 /* Deal with open(O_TRUNC) */
2569 if (sattr->ia_valid & ATTR_OPEN)
2570 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2571
2572 /* Optimization: if the end result is no change, don't RPC */
2573 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2574 return 0;
2575
Trond Myklebustd5308382005-11-04 15:33:38 -05002576 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002577 if (sattr->ia_valid & ATTR_FILE) {
2578 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002579
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002580 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002581 if (ctx) {
2582 cred = ctx->cred;
2583 state = ctx->state;
2584 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002585 }
2586
2587 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002588 if (status == 0)
2589 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return status;
2591}
2592
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002593static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2594 const struct qstr *name, struct nfs_fh *fhandle,
2595 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002596{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002597 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002598 int status;
2599 struct nfs4_lookup_arg args = {
2600 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002601 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002602 .name = name,
2603 };
2604 struct nfs4_lookup_res res = {
2605 .server = server,
2606 .fattr = fattr,
2607 .fh = fhandle,
2608 };
2609 struct rpc_message msg = {
2610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2611 .rpc_argp = &args,
2612 .rpc_resp = &res,
2613 };
2614
2615 nfs_fattr_init(fattr);
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002618 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 dprintk("NFS reply lookup: %d\n", status);
2620 return status;
2621}
2622
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002623static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002624{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002625 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002626 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002627 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2628 fattr->nlink = 2;
2629}
2630
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002631static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2632 struct qstr *name, struct nfs_fh *fhandle,
2633 struct nfs_fattr *fattr)
2634{
2635 struct nfs4_exception exception = { };
2636 struct rpc_clnt *client = *clnt;
2637 int err;
2638 do {
2639 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2640 switch (err) {
2641 case -NFS4ERR_BADNAME:
2642 err = -ENOENT;
2643 goto out;
2644 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002645 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002646 goto out;
2647 case -NFS4ERR_WRONGSEC:
2648 err = -EPERM;
2649 if (client != *clnt)
2650 goto out;
2651
2652 client = nfs4_create_sec_client(client, dir, name);
2653 if (IS_ERR(client))
2654 return PTR_ERR(client);
2655
2656 exception.retry = 1;
2657 break;
2658 default:
2659 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2660 }
2661 } while (exception.retry);
2662
2663out:
2664 if (err == 0)
2665 *clnt = client;
2666 else if (client != *clnt)
2667 rpc_shutdown_client(client);
2668
2669 return err;
2670}
2671
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002672static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002673 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002675 int status;
2676 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002677
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002678 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2679 if (client != NFS_CLIENT(dir)) {
2680 rpc_shutdown_client(client);
2681 nfs_fixup_secinfo_attributes(fattr);
2682 }
2683 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002686struct rpc_clnt *
2687nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2688 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2689{
2690 int status;
2691 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2692
2693 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2694 if (status < 0) {
2695 rpc_shutdown_client(client);
2696 return ERR_PTR(status);
2697 }
2698 return client;
2699}
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2702{
Trond Myklebust76b32992007-08-10 17:45:11 -04002703 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 struct nfs4_accessargs args = {
2705 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002706 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002708 struct nfs4_accessres res = {
2709 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002710 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 struct rpc_message msg = {
2712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2713 .rpc_argp = &args,
2714 .rpc_resp = &res,
2715 .rpc_cred = entry->cred,
2716 };
2717 int mode = entry->mask;
2718 int status;
2719
2720 /*
2721 * Determine which access bits we want to ask for...
2722 */
2723 if (mode & MAY_READ)
2724 args.access |= NFS4_ACCESS_READ;
2725 if (S_ISDIR(inode->i_mode)) {
2726 if (mode & MAY_WRITE)
2727 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2728 if (mode & MAY_EXEC)
2729 args.access |= NFS4_ACCESS_LOOKUP;
2730 } else {
2731 if (mode & MAY_WRITE)
2732 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2733 if (mode & MAY_EXEC)
2734 args.access |= NFS4_ACCESS_EXECUTE;
2735 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002736
2737 res.fattr = nfs_alloc_fattr();
2738 if (res.fattr == NULL)
2739 return -ENOMEM;
2740
Bryan Schumaker7c513052011-03-24 17:12:24 +00002741 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002743 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002744 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002746 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return status;
2748}
2749
2750static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2751{
2752 struct nfs4_exception exception = { };
2753 int err;
2754 do {
2755 err = nfs4_handle_exception(NFS_SERVER(inode),
2756 _nfs4_proc_access(inode, entry),
2757 &exception);
2758 } while (exception.retry);
2759 return err;
2760}
2761
2762/*
2763 * TODO: For the time being, we don't try to get any attributes
2764 * along with any of the zero-copy operations READ, READDIR,
2765 * READLINK, WRITE.
2766 *
2767 * In the case of the first three, we want to put the GETATTR
2768 * after the read-type operation -- this is because it is hard
2769 * to predict the length of a GETATTR response in v4, and thus
2770 * align the READ data correctly. This means that the GETATTR
2771 * may end up partially falling into the page cache, and we should
2772 * shift it into the 'tail' of the xdr_buf before processing.
2773 * To do this efficiently, we need to know the total length
2774 * of data received, which doesn't seem to be available outside
2775 * of the RPC layer.
2776 *
2777 * In the case of WRITE, we also want to put the GETATTR after
2778 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002779 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 *
2781 * Both of these changes to the XDR layer would in fact be quite
2782 * minor, but I decided to leave them for a subsequent patch.
2783 */
2784static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2785 unsigned int pgbase, unsigned int pglen)
2786{
2787 struct nfs4_readlink args = {
2788 .fh = NFS_FH(inode),
2789 .pgbase = pgbase,
2790 .pglen = pglen,
2791 .pages = &page,
2792 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002793 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct rpc_message msg = {
2795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2796 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002797 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 };
2799
Bryan Schumaker7c513052011-03-24 17:12:24 +00002800 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 -07002801}
2802
2803static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2804 unsigned int pgbase, unsigned int pglen)
2805{
2806 struct nfs4_exception exception = { };
2807 int err;
2808 do {
2809 err = nfs4_handle_exception(NFS_SERVER(inode),
2810 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2811 &exception);
2812 } while (exception.retry);
2813 return err;
2814}
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002817 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819static int
2820nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002821 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002823 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 int status = 0;
2826
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002827 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2828 if (IS_ERR(ctx))
2829 return PTR_ERR(ctx);
2830
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002831 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002832 state = nfs4_do_open(dir, dentry, ctx->mode,
2833 flags, sattr, ctx->cred,
2834 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002835 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (IS_ERR(state)) {
2837 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002840 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002841 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002842 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002844 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return status;
2846}
2847
2848static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2849{
Trond Myklebust16e42952005-10-27 22:12:44 -04002850 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002851 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002853 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002855 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002856 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002857 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2860 .rpc_argp = &args,
2861 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002863 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002864
Bryan Schumaker7c513052011-03-24 17:12:24 +00002865 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002866 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002867 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return status;
2869}
2870
2871static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2872{
2873 struct nfs4_exception exception = { };
2874 int err;
2875 do {
2876 err = nfs4_handle_exception(NFS_SERVER(dir),
2877 _nfs4_proc_remove(dir, name),
2878 &exception);
2879 } while (exception.retry);
2880 return err;
2881}
2882
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002883static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002885 struct nfs_server *server = NFS_SERVER(dir);
2886 struct nfs_removeargs *args = msg->rpc_argp;
2887 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002889 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002891 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002894static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2895{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002896 nfs4_setup_sequence(NFS_SERVER(data->dir),
2897 &data->args.seq_args,
2898 &data->res.seq_res,
2899 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002902static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002904 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2905
Trond Myklebust14516c32010-07-31 14:29:06 -04002906 if (!nfs4_sequence_done(task, &res->seq_res))
2907 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002908 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002909 return 0;
2910 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002911 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
Jeff Laytond3d41522010-09-17 17:31:57 -04002914static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2915{
2916 struct nfs_server *server = NFS_SERVER(dir);
2917 struct nfs_renameargs *arg = msg->rpc_argp;
2918 struct nfs_renameres *res = msg->rpc_resp;
2919
2920 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002921 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002922 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002923}
2924
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002925static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2926{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002927 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2928 &data->args.seq_args,
2929 &data->res.seq_res,
2930 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002931}
2932
2933static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2934 struct inode *new_dir)
2935{
2936 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2937
2938 if (!nfs4_sequence_done(task, &res->seq_res))
2939 return 0;
2940 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2941 return 0;
2942
2943 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002944 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002945 return 1;
2946}
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2949 struct inode *new_dir, struct qstr *new_name)
2950{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002951 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002952 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 .old_dir = NFS_FH(old_dir),
2954 .new_dir = NFS_FH(new_dir),
2955 .old_name = old_name,
2956 .new_name = new_name,
2957 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002958 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002959 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002960 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 struct rpc_message msg = {
2962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2963 .rpc_argp = &arg,
2964 .rpc_resp = &res,
2965 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002966 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Bryan Schumaker7c513052011-03-24 17:12:24 +00002968 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (!status) {
2970 update_changeattr(old_dir, &res.old_cinfo);
2971 update_changeattr(new_dir, &res.new_cinfo);
2972 }
2973 return status;
2974}
2975
2976static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2977 struct inode *new_dir, struct qstr *new_name)
2978{
2979 struct nfs4_exception exception = { };
2980 int err;
2981 do {
2982 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2983 _nfs4_proc_rename(old_dir, old_name,
2984 new_dir, new_name),
2985 &exception);
2986 } while (exception.retry);
2987 return err;
2988}
2989
2990static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2991{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002992 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct nfs4_link_arg arg = {
2994 .fh = NFS_FH(inode),
2995 .dir_fh = NFS_FH(dir),
2996 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002997 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002999 struct nfs4_link_res res = {
3000 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003001 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 struct rpc_message msg = {
3003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3004 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003005 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003007 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Trond Myklebust136f2622010-04-16 16:22:49 -04003009 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003010 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003011 goto out;
3012
Bryan Schumaker7c513052011-03-24 17:12:24 +00003013 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003014 if (!status) {
3015 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003016 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003017 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003018out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003019 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return status;
3021}
3022
3023static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3024{
3025 struct nfs4_exception exception = { };
3026 int err;
3027 do {
3028 err = nfs4_handle_exception(NFS_SERVER(inode),
3029 _nfs4_proc_link(inode, dir, name),
3030 &exception);
3031 } while (exception.retry);
3032 return err;
3033}
3034
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003035struct nfs4_createdata {
3036 struct rpc_message msg;
3037 struct nfs4_create_arg arg;
3038 struct nfs4_create_res res;
3039 struct nfs_fh fh;
3040 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003041};
3042
3043static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3044 struct qstr *name, struct iattr *sattr, u32 ftype)
3045{
3046 struct nfs4_createdata *data;
3047
3048 data = kzalloc(sizeof(*data), GFP_KERNEL);
3049 if (data != NULL) {
3050 struct nfs_server *server = NFS_SERVER(dir);
3051
3052 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3053 data->msg.rpc_argp = &data->arg;
3054 data->msg.rpc_resp = &data->res;
3055 data->arg.dir_fh = NFS_FH(dir);
3056 data->arg.server = server;
3057 data->arg.name = name;
3058 data->arg.attrs = sattr;
3059 data->arg.ftype = ftype;
3060 data->arg.bitmask = server->attr_bitmask;
3061 data->res.server = server;
3062 data->res.fh = &data->fh;
3063 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003064 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003065 }
3066 return data;
3067}
3068
3069static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3070{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003071 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003072 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003073 if (status == 0) {
3074 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003075 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3076 }
3077 return status;
3078}
3079
3080static void nfs4_free_createdata(struct nfs4_createdata *data)
3081{
3082 kfree(data);
3083}
3084
Chuck Lever4f390c12006-08-22 20:06:22 -04003085static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003086 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003088 struct nfs4_createdata *data;
3089 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Chuck Lever94a6d752006-08-22 20:06:23 -04003091 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003092 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003093
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003094 status = -ENOMEM;
3095 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3096 if (data == NULL)
3097 goto out;
3098
3099 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3100 data->arg.u.symlink.pages = &page;
3101 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 status = nfs4_do_create(dir, dentry, data);
3104
3105 nfs4_free_createdata(data);
3106out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return status;
3108}
3109
Chuck Lever4f390c12006-08-22 20:06:22 -04003110static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003111 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 struct nfs4_exception exception = { };
3114 int err;
3115 do {
3116 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003117 _nfs4_proc_symlink(dir, dentry, page,
3118 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 &exception);
3120 } while (exception.retry);
3121 return err;
3122}
3123
3124static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3125 struct iattr *sattr)
3126{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 struct nfs4_createdata *data;
3128 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003130 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3131 if (data == NULL)
3132 goto out;
3133
3134 status = nfs4_do_create(dir, dentry, data);
3135
3136 nfs4_free_createdata(data);
3137out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return status;
3139}
3140
3141static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3142 struct iattr *sattr)
3143{
3144 struct nfs4_exception exception = { };
3145 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003146
3147 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 do {
3149 err = nfs4_handle_exception(NFS_SERVER(dir),
3150 _nfs4_proc_mkdir(dir, dentry, sattr),
3151 &exception);
3152 } while (exception.retry);
3153 return err;
3154}
3155
3156static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003157 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
3159 struct inode *dir = dentry->d_inode;
3160 struct nfs4_readdir_arg args = {
3161 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003162 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 .pgbase = 0,
3164 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003165 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003166 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 };
3168 struct nfs4_readdir_res res;
3169 struct rpc_message msg = {
3170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3171 .rpc_argp = &args,
3172 .rpc_resp = &res,
3173 .rpc_cred = cred,
3174 };
3175 int status;
3176
Harvey Harrison3110ff82008-05-02 13:42:44 -07003177 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003178 dentry->d_parent->d_name.name,
3179 dentry->d_name.name,
3180 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003181 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003183 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 -05003184 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003185 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003186 status += args.pgbase;
3187 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003188
3189 nfs_invalidate_atime(dir);
3190
Harvey Harrison3110ff82008-05-02 13:42:44 -07003191 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return status;
3193}
3194
3195static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003196 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct nfs4_exception exception = { };
3199 int err;
3200 do {
3201 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3202 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003203 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 &exception);
3205 } while (exception.retry);
3206 return err;
3207}
3208
3209static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3210 struct iattr *sattr, dev_t rdev)
3211{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003212 struct nfs4_createdata *data;
3213 int mode = sattr->ia_mode;
3214 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3217 if (data == NULL)
3218 goto out;
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003221 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003223 data->arg.ftype = NF4BLK;
3224 data->arg.u.device.specdata1 = MAJOR(rdev);
3225 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003228 data->arg.ftype = NF4CHR;
3229 data->arg.u.device.specdata1 = MAJOR(rdev);
3230 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003231 } else if (!S_ISSOCK(mode)) {
3232 status = -EINVAL;
3233 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003236 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003237out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003238 nfs4_free_createdata(data);
3239out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return status;
3241}
3242
3243static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3244 struct iattr *sattr, dev_t rdev)
3245{
3246 struct nfs4_exception exception = { };
3247 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003248
3249 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 do {
3251 err = nfs4_handle_exception(NFS_SERVER(dir),
3252 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3253 &exception);
3254 } while (exception.retry);
3255 return err;
3256}
3257
3258static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3259 struct nfs_fsstat *fsstat)
3260{
3261 struct nfs4_statfs_arg args = {
3262 .fh = fhandle,
3263 .bitmask = server->attr_bitmask,
3264 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003265 struct nfs4_statfs_res res = {
3266 .fsstat = fsstat,
3267 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct rpc_message msg = {
3269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3270 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003271 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 };
3273
Trond Myklebust0e574af2005-10-27 22:12:38 -04003274 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003275 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
3278static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3279{
3280 struct nfs4_exception exception = { };
3281 int err;
3282 do {
3283 err = nfs4_handle_exception(server,
3284 _nfs4_proc_statfs(server, fhandle, fsstat),
3285 &exception);
3286 } while (exception.retry);
3287 return err;
3288}
3289
3290static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3291 struct nfs_fsinfo *fsinfo)
3292{
3293 struct nfs4_fsinfo_arg args = {
3294 .fh = fhandle,
3295 .bitmask = server->attr_bitmask,
3296 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003297 struct nfs4_fsinfo_res res = {
3298 .fsinfo = fsinfo,
3299 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct rpc_message msg = {
3301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3302 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003303 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 };
3305
Bryan Schumaker7c513052011-03-24 17:12:24 +00003306 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
3309static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3310{
3311 struct nfs4_exception exception = { };
3312 int err;
3313
3314 do {
3315 err = nfs4_handle_exception(server,
3316 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3317 &exception);
3318 } while (exception.retry);
3319 return err;
3320}
3321
3322static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3323{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003324 int error;
3325
Trond Myklebust0e574af2005-10-27 22:12:38 -04003326 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003327 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003328 if (error == 0) {
3329 /* block layout checks this! */
3330 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003331 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003332 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003333
3334 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
3337static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3338 struct nfs_pathconf *pathconf)
3339{
3340 struct nfs4_pathconf_arg args = {
3341 .fh = fhandle,
3342 .bitmask = server->attr_bitmask,
3343 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003344 struct nfs4_pathconf_res res = {
3345 .pathconf = pathconf,
3346 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 struct rpc_message msg = {
3348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3349 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003350 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 };
3352
3353 /* None of the pathconf attributes are mandatory to implement */
3354 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3355 memset(pathconf, 0, sizeof(*pathconf));
3356 return 0;
3357 }
3358
Trond Myklebust0e574af2005-10-27 22:12:38 -04003359 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003360 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
3363static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3364 struct nfs_pathconf *pathconf)
3365{
3366 struct nfs4_exception exception = { };
3367 int err;
3368
3369 do {
3370 err = nfs4_handle_exception(server,
3371 _nfs4_proc_pathconf(server, fhandle, pathconf),
3372 &exception);
3373 } while (exception.retry);
3374 return err;
3375}
3376
Benny Halevyd20581a2011-05-22 19:52:03 +03003377void __nfs4_read_done_cb(struct nfs_read_data *data)
3378{
Fred Isamancd841602012-04-20 14:47:44 -04003379 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003380}
3381
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003382static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Fred Isamancd841602012-04-20 14:47:44 -04003384 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003386 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003387 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003388 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003390
Benny Halevyd20581a2011-05-22 19:52:03 +03003391 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003393 renew_lease(server, data->timestamp);
3394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003397static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3398{
3399
3400 dprintk("--> %s\n", __func__);
3401
3402 if (!nfs4_sequence_done(task, &data->res.seq_res))
3403 return -EAGAIN;
3404
Benny Halevyd20581a2011-05-22 19:52:03 +03003405 return data->read_done_cb ? data->read_done_cb(task, data) :
3406 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003407}
3408
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003409static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003412 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003413 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003414 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003417static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3418{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003419 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3420 &data->args.seq_args,
3421 &data->res.seq_res,
3422 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Fred Isamanb029bc92011-03-03 15:13:42 +00003425static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Fred Isamancd841602012-04-20 14:47:44 -04003427 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003429 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003430 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003431 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003433 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003435 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003436 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
Fred Isamanb029bc92011-03-03 15:13:42 +00003440static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3441{
3442 if (!nfs4_sequence_done(task, &data->res.seq_res))
3443 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003444 return data->write_done_cb ? data->write_done_cb(task, data) :
3445 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003446}
3447
Trond Myklebust5a37f852012-04-28 14:55:16 -04003448static
3449bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003450{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003451 const struct nfs_pgio_header *hdr = data->header;
3452
3453 /* Don't request attributes for pNFS or O_DIRECT writes */
3454 if (data->ds_clp != NULL || hdr->dreq != NULL)
3455 return false;
3456 /* Otherwise, request attributes if and only if we don't hold
3457 * a delegation
3458 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003459 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003460}
Fred Isamana69aef12011-03-03 15:13:47 +00003461
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003462static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
Fred Isamancd841602012-04-20 14:47:44 -04003464 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003465
Trond Myklebust5a37f852012-04-28 14:55:16 -04003466 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003467 data->args.bitmask = NULL;
3468 data->res.fattr = NULL;
3469 } else
3470 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003471
Fred Isamanb029bc92011-03-03 15:13:42 +00003472 if (!data->write_done_cb)
3473 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003474 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 data->timestamp = jiffies;
3476
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003477 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003478 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003481static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3482{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003483 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3484 &data->args.seq_args,
3485 &data->res.seq_res,
3486 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
Fred Isaman0b7c0152012-04-20 14:47:39 -04003489static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3490{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003491 nfs4_setup_sequence(NFS_SERVER(data->inode),
3492 &data->args.seq_args,
3493 &data->res.seq_res,
3494 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
Fred Isaman0b7c0152012-04-20 14:47:39 -04003497static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003500
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003501 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003502 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003503 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
Fred Isaman0b7c0152012-04-20 14:47:39 -04003508static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003509{
3510 if (!nfs4_sequence_done(task, &data->res.seq_res))
3511 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003512 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003513}
3514
Fred Isaman0b7c0152012-04-20 14:47:39 -04003515static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003517 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003518
Fred Isaman0b7c0152012-04-20 14:47:39 -04003519 if (data->commit_done_cb == NULL)
3520 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003521 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003522 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003523 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003526struct nfs4_renewdata {
3527 struct nfs_client *client;
3528 unsigned long timestamp;
3529};
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531/*
3532 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3533 * standalone procedure for queueing an asynchronous RENEW.
3534 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003535static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003536{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003537 struct nfs4_renewdata *data = calldata;
3538 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003539
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003540 if (atomic_read(&clp->cl_count) > 1)
3541 nfs4_schedule_state_renewal(clp);
3542 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003543 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003544}
3545
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003546static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003548 struct nfs4_renewdata *data = calldata;
3549 struct nfs_client *clp = data->client;
3550 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003553 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003554 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3555 return;
3556 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003557 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003558 return;
3559 }
3560 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003562 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003565static const struct rpc_call_ops nfs4_renew_ops = {
3566 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003567 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003568};
3569
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003570static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 struct rpc_message msg = {
3573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3574 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003575 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003577 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003579 if (renew_flags == 0)
3580 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003581 if (!atomic_inc_not_zero(&clp->cl_count))
3582 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003583 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003584 if (data == NULL)
3585 return -ENOMEM;
3586 data->client = clp;
3587 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003589 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003592static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
3594 struct rpc_message msg = {
3595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3596 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003597 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 };
3599 unsigned long now = jiffies;
3600 int status;
3601
3602 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3603 if (status < 0)
3604 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003605 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 return 0;
3607}
3608
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003609static inline int nfs4_server_supports_acls(struct nfs_server *server)
3610{
3611 return (server->caps & NFS_CAP_ACLS)
3612 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3613 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3614}
3615
Trond Myklebust21f498c2012-08-24 10:59:25 -04003616/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3617 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003618 * the stack.
3619 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003620#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003621
Neil Hormane9e3d722011-03-04 19:26:03 -05003622static int buf_to_pages_noslab(const void *buf, size_t buflen,
3623 struct page **pages, unsigned int *pgbase)
3624{
3625 struct page *newpage, **spages;
3626 int rc = 0;
3627 size_t len;
3628 spages = pages;
3629
3630 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003631 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003632 newpage = alloc_page(GFP_KERNEL);
3633
3634 if (newpage == NULL)
3635 goto unwind;
3636 memcpy(page_address(newpage), buf, len);
3637 buf += len;
3638 buflen -= len;
3639 *pages++ = newpage;
3640 rc++;
3641 } while (buflen != 0);
3642
3643 return rc;
3644
3645unwind:
3646 for(; rc > 0; rc--)
3647 __free_page(spages[rc-1]);
3648 return -ENOMEM;
3649}
3650
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003651struct nfs4_cached_acl {
3652 int cached;
3653 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003654 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003655};
3656
3657static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003658{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003659 struct nfs_inode *nfsi = NFS_I(inode);
3660
3661 spin_lock(&inode->i_lock);
3662 kfree(nfsi->nfs4_acl);
3663 nfsi->nfs4_acl = acl;
3664 spin_unlock(&inode->i_lock);
3665}
3666
3667static void nfs4_zap_acl_attr(struct inode *inode)
3668{
3669 nfs4_set_cached_acl(inode, NULL);
3670}
3671
3672static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3673{
3674 struct nfs_inode *nfsi = NFS_I(inode);
3675 struct nfs4_cached_acl *acl;
3676 int ret = -ENOENT;
3677
3678 spin_lock(&inode->i_lock);
3679 acl = nfsi->nfs4_acl;
3680 if (acl == NULL)
3681 goto out;
3682 if (buf == NULL) /* user is just asking for length */
3683 goto out_len;
3684 if (acl->cached == 0)
3685 goto out;
3686 ret = -ERANGE; /* see getxattr(2) man page */
3687 if (acl->len > buflen)
3688 goto out;
3689 memcpy(buf, acl->data, acl->len);
3690out_len:
3691 ret = acl->len;
3692out:
3693 spin_unlock(&inode->i_lock);
3694 return ret;
3695}
3696
Sachin Prabhu5794d212012-04-17 14:36:40 +01003697static 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 +00003698{
3699 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003700 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003701
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003702 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003703 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003704 if (acl == NULL)
3705 goto out;
3706 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003707 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003708 } else {
3709 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3710 if (acl == NULL)
3711 goto out;
3712 acl->cached = 0;
3713 }
3714 acl->len = acl_len;
3715out:
3716 nfs4_set_cached_acl(inode, acl);
3717}
3718
Andy Adamsonbf118a32011-12-07 11:55:27 -05003719/*
3720 * The getxattr API returns the required buffer length when called with a
3721 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3722 * the required buf. On a NULL buf, we send a page of data to the server
3723 * guessing that the ACL request can be serviced by a page. If so, we cache
3724 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3725 * the cache. If not so, we throw away the page, and cache the required
3726 * length. The next getxattr call will then produce another round trip to
3727 * the server, this time with the input buf of the required size.
3728 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003729static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003730{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003731 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003732 struct nfs_getaclargs args = {
3733 .fh = NFS_FH(inode),
3734 .acl_pages = pages,
3735 .acl_len = buflen,
3736 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003737 struct nfs_getaclres res = {
3738 .acl_len = buflen,
3739 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003740 struct rpc_message msg = {
3741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3742 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003743 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003744 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003745 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3746 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003747
Andy Adamsonbf118a32011-12-07 11:55:27 -05003748 /* As long as we're doing a round trip to the server anyway,
3749 * let's be prepared for a page of acl data. */
3750 if (npages == 0)
3751 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003752 if (npages > ARRAY_SIZE(pages))
3753 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003754
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755 for (i = 0; i < npages; i++) {
3756 pages[i] = alloc_page(GFP_KERNEL);
3757 if (!pages[i])
3758 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003759 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003760
3761 /* for decoding across pages */
3762 res.acl_scratch = alloc_page(GFP_KERNEL);
3763 if (!res.acl_scratch)
3764 goto out_free;
3765
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766 args.acl_len = npages * PAGE_SIZE;
3767 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003768
Peng Taode040be2012-01-10 22:42:47 +08003769 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003770 __func__, buf, buflen, npages, args.acl_len);
3771 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3772 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 if (ret)
3774 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003775
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003776 /* Handle the case where the passed-in buffer is too short */
3777 if (res.acl_flags & NFS4_ACL_TRUNC) {
3778 /* Did the user only issue a request for the acl length? */
3779 if (buf == NULL)
3780 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003782 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003783 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003784 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003785 if (buf) {
3786 if (res.acl_len > buflen) {
3787 ret = -ERANGE;
3788 goto out_free;
3789 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003790 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003791 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003792out_ok:
3793 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003794out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003795 for (i = 0; i < npages; i++)
3796 if (pages[i])
3797 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003798 if (res.acl_scratch)
3799 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003800 return ret;
3801}
3802
Trond Myklebust16b42892006-08-24 12:27:15 -04003803static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3804{
3805 struct nfs4_exception exception = { };
3806 ssize_t ret;
3807 do {
3808 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3809 if (ret >= 0)
3810 break;
3811 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3812 } while (exception.retry);
3813 return ret;
3814}
3815
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3817{
3818 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003819 int ret;
3820
3821 if (!nfs4_server_supports_acls(server))
3822 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003823 ret = nfs_revalidate_inode(server, inode);
3824 if (ret < 0)
3825 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003826 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3827 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828 ret = nfs4_read_cached_acl(inode, buf, buflen);
3829 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003830 /* -ENOENT is returned if there is no ACL or if there is an ACL
3831 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003832 return ret;
3833 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003834}
3835
Trond Myklebust16b42892006-08-24 12:27:15 -04003836static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003837{
3838 struct nfs_server *server = NFS_SERVER(inode);
3839 struct page *pages[NFS4ACL_MAXPAGES];
3840 struct nfs_setaclargs arg = {
3841 .fh = NFS_FH(inode),
3842 .acl_pages = pages,
3843 .acl_len = buflen,
3844 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003845 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3848 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003849 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003850 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003851 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003852 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003853
3854 if (!nfs4_server_supports_acls(server))
3855 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003856 if (npages > ARRAY_SIZE(pages))
3857 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003858 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3859 if (i < 0)
3860 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003861 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003862 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003863
3864 /*
3865 * Free each page after tx, so the only ref left is
3866 * held by the network stack
3867 */
3868 for (; i > 0; i--)
3869 put_page(pages[i-1]);
3870
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003871 /*
3872 * Acl update can result in inode attribute update.
3873 * so mark the attribute cache invalid.
3874 */
3875 spin_lock(&inode->i_lock);
3876 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3877 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003878 nfs_access_zap_cache(inode);
3879 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003880 return ret;
3881}
3882
Trond Myklebust16b42892006-08-24 12:27:15 -04003883static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3884{
3885 struct nfs4_exception exception = { };
3886 int err;
3887 do {
3888 err = nfs4_handle_exception(NFS_SERVER(inode),
3889 __nfs4_proc_set_acl(inode, buf, buflen),
3890 &exception);
3891 } while (exception.retry);
3892 return err;
3893}
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003896nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003898 struct nfs_client *clp = server->nfs_client;
3899
3900 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return 0;
3902 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003903 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003904 case -NFS4ERR_ADMIN_REVOKED:
3905 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003906 if (state == NULL)
3907 break;
3908 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003909 case -NFS4ERR_OPENMODE:
3910 if (state == NULL)
3911 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003912 nfs4_schedule_stateid_recovery(server, state);
3913 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003914 case -NFS4ERR_EXPIRED:
3915 if (state != NULL)
3916 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003918 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003919 nfs4_schedule_lease_recovery(clp);
3920 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003921#if defined(CONFIG_NFS_V4_1)
3922 case -NFS4ERR_BADSESSION:
3923 case -NFS4ERR_BADSLOT:
3924 case -NFS4ERR_BAD_HIGH_SLOT:
3925 case -NFS4ERR_DEADSESSION:
3926 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3927 case -NFS4ERR_SEQ_FALSE_RETRY:
3928 case -NFS4ERR_SEQ_MISORDERED:
3929 dprintk("%s ERROR %d, Reset session\n", __func__,
3930 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003931 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003932 task->tk_status = 0;
3933 return -EAGAIN;
3934#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003936 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003937 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003938 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3940 task->tk_status = 0;
3941 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003942 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 case -NFS4ERR_OLD_STATEID:
3944 task->tk_status = 0;
3945 return -EAGAIN;
3946 }
3947 task->tk_status = nfs4_map_errors(task->tk_status);
3948 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003949wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003950 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003951 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3952 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3953 task->tk_status = 0;
3954 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955}
3956
Chuck Leverf0920752012-05-21 22:45:41 -04003957static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3958 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003959{
3960 __be32 verf[2];
3961
Chuck Lever2c820d92012-05-21 22:45:33 -04003962 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3963 /* An impossible timestamp guarantees this value
3964 * will never match a generated boot time. */
3965 verf[0] = 0;
3966 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3967 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003968 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3969 verf[0] = (__be32)nn->boot_time.tv_sec;
3970 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003971 }
Chuck Levercd937102012-03-02 17:14:31 -05003972 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3973}
3974
Chuck Levere984a552012-09-14 17:24:21 -04003975static unsigned int
3976nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
3977 char *buf, size_t len)
3978{
3979 unsigned int result;
3980
3981 rcu_read_lock();
3982 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
3983 clp->cl_ipaddr,
3984 rpc_peeraddr2str(clp->cl_rpcclient,
3985 RPC_DISPLAY_ADDR),
3986 rpc_peeraddr2str(clp->cl_rpcclient,
3987 RPC_DISPLAY_PROTO));
3988 rcu_read_unlock();
3989 return result;
3990}
3991
3992static unsigned int
3993nfs4_init_uniform_client_string(const struct nfs_client *clp,
3994 char *buf, size_t len)
3995{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04003996 char *nodename = clp->cl_rpcclient->cl_nodename;
3997
3998 if (nfs4_client_id_uniquifier[0] != '\0')
3999 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004000 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4001 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004002 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004003}
4004
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004005/**
4006 * nfs4_proc_setclientid - Negotiate client ID
4007 * @clp: state data structure
4008 * @program: RPC program for NFSv4 callback service
4009 * @port: IP port number for NFS4 callback service
4010 * @cred: RPC credential to use for this call
4011 * @res: where to place the result
4012 *
4013 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4014 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004015int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4016 unsigned short port, struct rpc_cred *cred,
4017 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
4019 nfs4_verifier sc_verifier;
4020 struct nfs4_setclientid setclientid = {
4021 .sc_verifier = &sc_verifier,
4022 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004023 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 };
4025 struct rpc_message msg = {
4026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4027 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004028 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004029 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004031 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Chuck Leverde734832012-07-11 16:30:50 -04004033 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004034 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004035 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4036 setclientid.sc_name_len =
4037 nfs4_init_uniform_client_string(clp,
4038 setclientid.sc_name,
4039 sizeof(setclientid.sc_name));
4040 else
4041 setclientid.sc_name_len =
4042 nfs4_init_nonuniform_client_string(clp,
4043 setclientid.sc_name,
4044 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004045 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004046 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004047 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004048 sizeof(setclientid.sc_netid),
4049 rpc_peeraddr2str(clp->cl_rpcclient,
4050 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004051 rcu_read_unlock();
4052 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004053 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 clp->cl_ipaddr, port >> 8, port & 255);
4055
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004056 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4057 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4058 setclientid.sc_name_len, setclientid.sc_name);
4059 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4060 dprintk("NFS reply setclientid: %d\n", status);
4061 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004064/**
4065 * nfs4_proc_setclientid_confirm - Confirm client ID
4066 * @clp: state data structure
4067 * @res: result of a previous SETCLIENTID
4068 * @cred: RPC credential to use for this call
4069 *
4070 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4071 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004072int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004073 struct nfs4_setclientid_res *arg,
4074 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075{
4076 struct nfs_fsinfo fsinfo;
4077 struct rpc_message msg = {
4078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004079 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004081 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 };
4083 unsigned long now;
4084 int status;
4085
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004086 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4087 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4088 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004090 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 if (status == 0) {
4092 spin_lock(&clp->cl_lock);
4093 clp->cl_lease_time = fsinfo.lease_time * HZ;
4094 clp->cl_last_renewal = now;
4095 spin_unlock(&clp->cl_lock);
4096 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004097 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 return status;
4099}
4100
Trond Myklebustfe650402006-01-03 09:55:18 +01004101struct nfs4_delegreturndata {
4102 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004103 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004104 struct nfs_fh fh;
4105 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004106 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004107 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004108 int rpc_status;
4109};
4110
Trond Myklebustfe650402006-01-03 09:55:18 +01004111static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4112{
4113 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004114
Trond Myklebust14516c32010-07-31 14:29:06 -04004115 if (!nfs4_sequence_done(task, &data->res.seq_res))
4116 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004117
Ricardo Labiaga79708862009-12-07 09:23:21 -05004118 switch (task->tk_status) {
4119 case -NFS4ERR_STALE_STATEID:
4120 case -NFS4ERR_EXPIRED:
4121 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004122 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004123 break;
4124 default:
4125 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4126 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004127 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004128 return;
4129 }
4130 }
4131 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004132}
4133
4134static void nfs4_delegreturn_release(void *calldata)
4135{
Trond Myklebustfe650402006-01-03 09:55:18 +01004136 kfree(calldata);
4137}
4138
Andy Adamson938e1012009-04-01 09:22:28 -04004139#if defined(CONFIG_NFS_V4_1)
4140static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4141{
4142 struct nfs4_delegreturndata *d_data;
4143
4144 d_data = (struct nfs4_delegreturndata *)data;
4145
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004146 nfs4_setup_sequence(d_data->res.server,
4147 &d_data->args.seq_args,
4148 &d_data->res.seq_res,
4149 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004150}
4151#endif /* CONFIG_NFS_V4_1 */
4152
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004153static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004154#if defined(CONFIG_NFS_V4_1)
4155 .rpc_call_prepare = nfs4_delegreturn_prepare,
4156#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004157 .rpc_call_done = nfs4_delegreturn_done,
4158 .rpc_release = nfs4_delegreturn_release,
4159};
4160
Trond Myklebuste6f81072008-01-24 18:14:34 -05004161static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004162{
4163 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004164 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004165 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004166 struct rpc_message msg = {
4167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4168 .rpc_cred = cred,
4169 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004170 struct rpc_task_setup task_setup_data = {
4171 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004172 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004173 .callback_ops = &nfs4_delegreturn_ops,
4174 .flags = RPC_TASK_ASYNC,
4175 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004176 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004177
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004178 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004179 if (data == NULL)
4180 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004181 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004182 data->args.fhandle = &data->fh;
4183 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004184 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004185 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004186 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004187 data->res.fattr = &data->fattr;
4188 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004189 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004190 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004191 data->rpc_status = 0;
4192
Trond Myklebustc970aa82007-07-14 15:39:59 -04004193 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004194 msg.rpc_argp = &data->args;
4195 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004196 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004197 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004198 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004199 if (!issync)
4200 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004201 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004202 if (status != 0)
4203 goto out;
4204 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004205 if (status == 0)
4206 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4207 else
4208 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004209out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004210 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004211 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Trond Myklebuste6f81072008-01-24 18:14:34 -05004214int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
4216 struct nfs_server *server = NFS_SERVER(inode);
4217 struct nfs4_exception exception = { };
4218 int err;
4219 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004220 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 switch (err) {
4222 case -NFS4ERR_STALE_STATEID:
4223 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 case 0:
4225 return 0;
4226 }
4227 err = nfs4_handle_exception(server, err, &exception);
4228 } while (exception.retry);
4229 return err;
4230}
4231
4232#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4233#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4234
4235/*
4236 * sleep, with exponential backoff, and retry the LOCK operation.
4237 */
4238static unsigned long
4239nfs4_set_lock_task_retry(unsigned long timeout)
4240{
Jeff Laytond3103102011-12-01 22:44:39 +01004241 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 timeout <<= 1;
4243 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4244 return NFS4_LOCK_MAXTIMEOUT;
4245 return timeout;
4246}
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4249{
4250 struct inode *inode = state->inode;
4251 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004252 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004253 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004255 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004257 struct nfs_lockt_res res = {
4258 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 };
4260 struct rpc_message msg = {
4261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4262 .rpc_argp = &arg,
4263 .rpc_resp = &res,
4264 .rpc_cred = state->owner->so_cred,
4265 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 struct nfs4_lock_state *lsp;
4267 int status;
4268
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004269 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004270 status = nfs4_set_lock_state(state, request);
4271 if (status != 0)
4272 goto out;
4273 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004274 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004275 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004276 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004277 switch (status) {
4278 case 0:
4279 request->fl_type = F_UNLCK;
4280 break;
4281 case -NFS4ERR_DENIED:
4282 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004284 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004285out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 return status;
4287}
4288
4289static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4290{
4291 struct nfs4_exception exception = { };
4292 int err;
4293
4294 do {
4295 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4296 _nfs4_proc_getlk(state, cmd, request),
4297 &exception);
4298 } while (exception.retry);
4299 return err;
4300}
4301
4302static int do_vfs_lock(struct file *file, struct file_lock *fl)
4303{
4304 int res = 0;
4305 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4306 case FL_POSIX:
4307 res = posix_lock_file_wait(file, fl);
4308 break;
4309 case FL_FLOCK:
4310 res = flock_lock_file_wait(file, fl);
4311 break;
4312 default:
4313 BUG();
4314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return res;
4316}
4317
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004318struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004319 struct nfs_locku_args arg;
4320 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321 struct nfs4_lock_state *lsp;
4322 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004323 struct file_lock fl;
4324 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004325 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004326};
4327
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004328static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4329 struct nfs_open_context *ctx,
4330 struct nfs4_lock_state *lsp,
4331 struct nfs_seqid *seqid)
4332{
4333 struct nfs4_unlockdata *p;
4334 struct inode *inode = lsp->ls_state->inode;
4335
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004336 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004337 if (p == NULL)
4338 return NULL;
4339 p->arg.fh = NFS_FH(inode);
4340 p->arg.fl = &p->fl;
4341 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004342 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004343 p->arg.stateid = &lsp->ls_stateid;
4344 p->lsp = lsp;
4345 atomic_inc(&lsp->ls_count);
4346 /* Ensure we don't close file until we're done freeing locks! */
4347 p->ctx = get_nfs_open_context(ctx);
4348 memcpy(&p->fl, fl, sizeof(p->fl));
4349 p->server = NFS_SERVER(inode);
4350 return p;
4351}
4352
Trond Myklebust06f814a2006-01-03 09:55:07 +01004353static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004354{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004355 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004356 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004357 nfs4_put_lock_state(calldata->lsp);
4358 put_nfs_open_context(calldata->ctx);
4359 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004360}
4361
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004362static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004363{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004364 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004365
Trond Myklebust14516c32010-07-31 14:29:06 -04004366 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4367 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004368 switch (task->tk_status) {
4369 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004370 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4371 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004372 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004373 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004374 case -NFS4ERR_BAD_STATEID:
4375 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004376 case -NFS4ERR_STALE_STATEID:
4377 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004378 break;
4379 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004380 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004381 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004382 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004383 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384}
4385
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004386static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004387{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004388 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004390 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004392 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004393 /* Note: exit _without_ running nfs4_locku_done */
4394 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004395 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004396 return;
4397 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004398 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004399 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004400 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004401 &calldata->res.seq_res,
4402 task) != 0)
4403 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004406static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004407 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004408 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004409 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004410};
4411
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004412static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4413 struct nfs_open_context *ctx,
4414 struct nfs4_lock_state *lsp,
4415 struct nfs_seqid *seqid)
4416{
4417 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004418 struct rpc_message msg = {
4419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4420 .rpc_cred = ctx->cred,
4421 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004422 struct rpc_task_setup task_setup_data = {
4423 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004424 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004425 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004426 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004427 .flags = RPC_TASK_ASYNC,
4428 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004429
Frank Filz137d6ac2007-07-09 15:32:29 -07004430 /* Ensure this is an unlock - when canceling a lock, the
4431 * canceled lock is passed in, and it won't be an unlock.
4432 */
4433 fl->fl_type = F_UNLCK;
4434
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004435 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4436 if (data == NULL) {
4437 nfs_free_seqid(seqid);
4438 return ERR_PTR(-ENOMEM);
4439 }
4440
Trond Myklebust9d12b212012-01-17 22:04:25 -05004441 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004442 msg.rpc_argp = &data->arg;
4443 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004444 task_setup_data.callback_data = data;
4445 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004446}
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4449{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004450 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004451 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004452 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004453 struct rpc_task *task;
4454 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004455 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Trond Myklebust9b073572006-06-29 16:38:34 -04004457 status = nfs4_set_lock_state(state, request);
4458 /* Unlock _before_ we do the RPC call */
4459 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004460 down_read(&nfsi->rwsem);
4461 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4462 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004463 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004464 }
4465 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004466 if (status != 0)
4467 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004468 /* Is this a delegated lock? */
4469 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004470 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004471 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004472 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004473 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004474 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004475 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004476 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477 status = PTR_ERR(task);
4478 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004479 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004480 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004481 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004482out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004483 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004484 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485}
4486
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004487struct nfs4_lockdata {
4488 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004489 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004490 struct nfs4_lock_state *lsp;
4491 struct nfs_open_context *ctx;
4492 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004493 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004494 int rpc_status;
4495 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004496 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004497};
4498
4499static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004500 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4501 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004502{
4503 struct nfs4_lockdata *p;
4504 struct inode *inode = lsp->ls_state->inode;
4505 struct nfs_server *server = NFS_SERVER(inode);
4506
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004507 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508 if (p == NULL)
4509 return NULL;
4510
4511 p->arg.fh = NFS_FH(inode);
4512 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004513 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004514 if (p->arg.open_seqid == NULL)
4515 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004516 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004518 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004520 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004521 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004522 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004523 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004525 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004526 atomic_inc(&lsp->ls_count);
4527 p->ctx = get_nfs_open_context(ctx);
4528 memcpy(&p->fl, fl, sizeof(p->fl));
4529 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004530out_free_seqid:
4531 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532out_free:
4533 kfree(p);
4534 return NULL;
4535}
4536
4537static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4538{
4539 struct nfs4_lockdata *data = calldata;
4540 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Harvey Harrison3110ff82008-05-02 13:42:44 -07004542 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004543 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4544 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004545 /* Do we need to do an open_to_lock_owner? */
4546 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004547 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004548 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004549 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004550 data->arg.open_stateid = &state->stateid;
4551 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004552 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004553 } else
4554 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004555 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004556 if (nfs4_setup_sequence(data->server,
4557 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004558 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004559 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004560 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004561 nfs_release_seqid(data->arg.open_seqid);
4562out_release_lock_seqid:
4563 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004564 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004565}
4566
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4568{
4569 struct nfs4_lockdata *data = calldata;
4570
Harvey Harrison3110ff82008-05-02 13:42:44 -07004571 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004572
Trond Myklebust14516c32010-07-31 14:29:06 -04004573 if (!nfs4_sequence_done(task, &data->res.seq_res))
4574 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004575
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004576 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578 if (data->rpc_status == 0)
4579 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4580 else
4581 goto out;
4582 }
4583 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004584 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004585 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004586 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004587 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004589 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004590}
4591
4592static void nfs4_lock_release(void *calldata)
4593{
4594 struct nfs4_lockdata *data = calldata;
4595
Harvey Harrison3110ff82008-05-02 13:42:44 -07004596 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004597 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598 if (data->cancelled != 0) {
4599 struct rpc_task *task;
4600 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4601 data->arg.lock_seqid);
4602 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004603 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004604 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004605 } else
4606 nfs_free_seqid(data->arg.lock_seqid);
4607 nfs4_put_lock_state(data->lsp);
4608 put_nfs_open_context(data->ctx);
4609 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004610 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004611}
4612
4613static const struct rpc_call_ops nfs4_lock_ops = {
4614 .rpc_call_prepare = nfs4_lock_prepare,
4615 .rpc_call_done = nfs4_lock_done,
4616 .rpc_release = nfs4_lock_release,
4617};
4618
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004619static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4620{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004621 switch (error) {
4622 case -NFS4ERR_ADMIN_REVOKED:
4623 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004624 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004625 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004626 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004627 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004628 break;
4629 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004630 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004631 case -NFS4ERR_EXPIRED:
4632 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004633 };
4634}
4635
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004636static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004637{
4638 struct nfs4_lockdata *data;
4639 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004640 struct rpc_message msg = {
4641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4642 .rpc_cred = state->owner->so_cred,
4643 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004644 struct rpc_task_setup task_setup_data = {
4645 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004646 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004647 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004648 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004649 .flags = RPC_TASK_ASYNC,
4650 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651 int ret;
4652
Harvey Harrison3110ff82008-05-02 13:42:44 -07004653 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004654 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004655 fl->fl_u.nfs4_fl.owner,
4656 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657 if (data == NULL)
4658 return -ENOMEM;
4659 if (IS_SETLKW(cmd))
4660 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004661 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004662 msg.rpc_argp = &data->arg;
4663 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004664 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004665 if (recovery_type > NFS_LOCK_NEW) {
4666 if (recovery_type == NFS_LOCK_RECLAIM)
4667 data->arg.reclaim = NFS_LOCK_RECLAIM;
4668 nfs4_set_sequence_privileged(&data->arg.seq_args);
4669 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004670 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004671 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673 ret = nfs4_wait_for_completion_rpc_task(task);
4674 if (ret == 0) {
4675 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004676 if (ret)
4677 nfs4_handle_setlk_error(data->server, data->lsp,
4678 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004679 } else
4680 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004681 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004682 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
4686static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4687{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004688 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004689 struct nfs4_exception exception = {
4690 .inode = state->inode,
4691 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004692 int err;
4693
4694 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004695 /* Cache the lock if possible... */
4696 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4697 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004698 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004699 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004700 break;
4701 nfs4_handle_exception(server, err, &exception);
4702 } while (exception.retry);
4703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
4706static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4707{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004708 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004709 struct nfs4_exception exception = {
4710 .inode = state->inode,
4711 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004712 int err;
4713
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004714 err = nfs4_set_lock_state(state, request);
4715 if (err != 0)
4716 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004717 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004718 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4719 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004720 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004721 switch (err) {
4722 default:
4723 goto out;
4724 case -NFS4ERR_GRACE:
4725 case -NFS4ERR_DELAY:
4726 nfs4_handle_exception(server, err, &exception);
4727 err = 0;
4728 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004729 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004730out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004731 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732}
4733
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004734#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004735/**
4736 * nfs41_check_expired_locks - possibly free a lock stateid
4737 *
4738 * @state: NFSv4 state for an inode
4739 *
4740 * Returns NFS_OK if recovery for this stateid is now finished.
4741 * Otherwise a negative NFS4ERR value is returned.
4742 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004743static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004744{
Chuck Levereb64cf92012-07-11 16:30:05 -04004745 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004746 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004747 struct nfs_server *server = NFS_SERVER(state->inode);
4748
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004749 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004750 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004751 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4752 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004753 /* Free the stateid unless the server
4754 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004755 if (status != -NFS4ERR_BAD_STATEID)
4756 nfs41_free_stateid(server,
4757 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004758 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004759 ret = status;
4760 }
4761 }
4762 };
4763
4764 return ret;
4765}
4766
4767static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4768{
4769 int status = NFS_OK;
4770
4771 if (test_bit(LK_STATE_IN_USE, &state->flags))
4772 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004773 if (status != NFS_OK)
4774 status = nfs4_lock_expired(state, request);
4775 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004776}
4777#endif
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4780{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004781 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004782 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004783 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004785 if ((fl_flags & FL_POSIX) &&
4786 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4787 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004788 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004789 status = nfs4_set_lock_state(state, request);
4790 if (status != 0)
4791 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004792 request->fl_flags |= FL_ACCESS;
4793 status = do_vfs_lock(request->fl_file, request);
4794 if (status < 0)
4795 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004796 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004797 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004798 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004799 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004800 request->fl_flags = fl_flags & ~FL_SLEEP;
4801 status = do_vfs_lock(request->fl_file, request);
4802 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004803 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004804 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004805 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004806 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004807 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004808 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004809 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004810 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4811 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004812out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004813 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004814out:
4815 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 return status;
4817}
4818
4819static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4820{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004821 struct nfs4_exception exception = {
4822 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004823 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004824 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 int err;
4826
4827 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004828 err = _nfs4_proc_setlk(state, cmd, request);
4829 if (err == -NFS4ERR_DENIED)
4830 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004832 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 } while (exception.retry);
4834 return err;
4835}
4836
4837static int
4838nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4839{
4840 struct nfs_open_context *ctx;
4841 struct nfs4_state *state;
4842 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4843 int status;
4844
4845 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004846 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 state = ctx->state;
4848
4849 if (request->fl_start < 0 || request->fl_end < 0)
4850 return -EINVAL;
4851
Trond Myklebustd9531262009-07-21 19:22:38 -04004852 if (IS_GETLK(cmd)) {
4853 if (state != NULL)
4854 return nfs4_proc_getlk(state, F_GETLK, request);
4855 return 0;
4856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
4858 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4859 return -EINVAL;
4860
Trond Myklebustd9531262009-07-21 19:22:38 -04004861 if (request->fl_type == F_UNLCK) {
4862 if (state != NULL)
4863 return nfs4_proc_unlck(state, cmd, request);
4864 return 0;
4865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Trond Myklebustd9531262009-07-21 19:22:38 -04004867 if (state == NULL)
4868 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004869 /*
4870 * Don't rely on the VFS having checked the file open mode,
4871 * since it won't do this for flock() locks.
4872 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004873 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004874 case F_RDLCK:
4875 if (!(filp->f_mode & FMODE_READ))
4876 return -EBADF;
4877 break;
4878 case F_WRLCK:
4879 if (!(filp->f_mode & FMODE_WRITE))
4880 return -EBADF;
4881 }
4882
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 do {
4884 status = nfs4_proc_setlk(state, cmd, request);
4885 if ((status != -EAGAIN) || IS_SETLK(cmd))
4886 break;
4887 timeout = nfs4_set_lock_task_retry(timeout);
4888 status = -ERESTARTSYS;
4889 if (signalled())
4890 break;
4891 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 return status;
4893}
4894
Trond Myklebust888e6942005-11-04 15:38:11 -05004895int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4896{
4897 struct nfs_server *server = NFS_SERVER(state->inode);
4898 struct nfs4_exception exception = { };
4899 int err;
4900
4901 err = nfs4_set_lock_state(state, fl);
4902 if (err != 0)
4903 goto out;
4904 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004905 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004906 switch (err) {
4907 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004908 printk(KERN_ERR "NFS: %s: unhandled error "
4909 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004910 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004911 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004912 goto out;
4913 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004914 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004915 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004916 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004917 nfs4_schedule_lease_recovery(server->nfs_client);
4918 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004919 case -NFS4ERR_BADSESSION:
4920 case -NFS4ERR_BADSLOT:
4921 case -NFS4ERR_BAD_HIGH_SLOT:
4922 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4923 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004924 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004925 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004926 case -ERESTARTSYS:
4927 /*
4928 * The show must go on: exit, but mark the
4929 * stateid as needing recovery.
4930 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004931 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004932 case -NFS4ERR_ADMIN_REVOKED:
4933 case -NFS4ERR_BAD_STATEID:
4934 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004935 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004936 err = 0;
4937 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004938 case -EKEYEXPIRED:
4939 /*
4940 * User RPCSEC_GSS context has expired.
4941 * We cannot recover this stateid now, so
4942 * skip it and allow recovery thread to
4943 * proceed.
4944 */
4945 err = 0;
4946 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004947 case -ENOMEM:
4948 case -NFS4ERR_DENIED:
4949 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4950 err = 0;
4951 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004952 case -NFS4ERR_DELAY:
4953 break;
4954 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004955 err = nfs4_handle_exception(server, err, &exception);
4956 } while (exception.retry);
4957out:
4958 return err;
4959}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004960
Trond Myklebustcf470c32012-03-07 13:49:12 -05004961struct nfs_release_lockowner_data {
4962 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004963 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004964 struct nfs_release_lockowner_args args;
4965};
4966
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004967static void nfs4_release_lockowner_release(void *calldata)
4968{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004969 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004970 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004971 kfree(calldata);
4972}
4973
Trond Myklebust17280172012-03-11 13:11:00 -04004974static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004975 .rpc_release = nfs4_release_lockowner_release,
4976};
4977
Trond Myklebustcf470c32012-03-07 13:49:12 -05004978int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004979{
4980 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004981 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004982 struct rpc_message msg = {
4983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4984 };
4985
4986 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004987 return -EINVAL;
4988 data = kmalloc(sizeof(*data), GFP_NOFS);
4989 if (!data)
4990 return -ENOMEM;
4991 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004992 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004993 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4994 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4995 data->args.lock_owner.s_dev = server->s_dev;
4996 msg.rpc_argp = &data->args;
4997 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4998 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004999}
5000
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005001#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5002
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005003static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5004 const void *buf, size_t buflen,
5005 int flags, 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 Fields4b580ee2005-06-22 17:16:23 +00005009
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005010 return nfs4_proc_set_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 int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5014 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005015{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005016 if (strcmp(key, "") != 0)
5017 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005018
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005019 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005020}
5021
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005022static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5023 size_t list_len, const char *name,
5024 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005025{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005026 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005027
J. Bruce Fields096455a2006-03-20 23:23:42 -05005028 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5029 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005030
5031 if (list && len <= list_len)
5032 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005033 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005034}
5035
Andy Adamson533eb462011-06-13 18:25:56 -04005036/*
5037 * nfs_fhget will use either the mounted_on_fileid or the fileid
5038 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005039static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5040{
Andy Adamson533eb462011-06-13 18:25:56 -04005041 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5042 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5043 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005044 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005045 return;
5046
5047 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005048 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005049 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5050 fattr->nlink = 2;
5051}
5052
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005053static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5054 const struct qstr *name,
5055 struct nfs4_fs_locations *fs_locations,
5056 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005057{
5058 struct nfs_server *server = NFS_SERVER(dir);
5059 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005060 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005061 };
5062 struct nfs4_fs_locations_arg args = {
5063 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005064 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005065 .page = page,
5066 .bitmask = bitmask,
5067 };
Benny Halevy22958462009-04-01 09:22:02 -04005068 struct nfs4_fs_locations_res res = {
5069 .fs_locations = fs_locations,
5070 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005071 struct rpc_message msg = {
5072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5073 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005074 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005075 };
5076 int status;
5077
Harvey Harrison3110ff82008-05-02 13:42:44 -07005078 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005079
5080 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5081 * is not supported */
5082 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5083 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5084 else
5085 bitmask[0] |= FATTR4_WORD0_FILEID;
5086
Trond Myklebustc228fd32007-01-13 02:28:11 -05005087 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005088 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005089 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005090 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005091 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005092 return status;
5093}
5094
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005095int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5096 const struct qstr *name,
5097 struct nfs4_fs_locations *fs_locations,
5098 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005099{
5100 struct nfs4_exception exception = { };
5101 int err;
5102 do {
5103 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005104 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005105 &exception);
5106 } while (exception.retry);
5107 return err;
5108}
5109
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005110static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5111{
5112 int status;
5113 struct nfs4_secinfo_arg args = {
5114 .dir_fh = NFS_FH(dir),
5115 .name = name,
5116 };
5117 struct nfs4_secinfo_res res = {
5118 .flavors = flavors,
5119 };
5120 struct rpc_message msg = {
5121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5122 .rpc_argp = &args,
5123 .rpc_resp = &res,
5124 };
5125
5126 dprintk("NFS call secinfo %s\n", name->name);
5127 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5128 dprintk("NFS reply secinfo: %d\n", status);
5129 return status;
5130}
5131
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005132int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5133 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005134{
5135 struct nfs4_exception exception = { };
5136 int err;
5137 do {
5138 err = nfs4_handle_exception(NFS_SERVER(dir),
5139 _nfs4_proc_secinfo(dir, name, flavors),
5140 &exception);
5141 } while (exception.retry);
5142 return err;
5143}
5144
Andy Adamson557134a2009-04-01 09:21:53 -04005145#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005146/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005147 * Check the exchange flags returned by the server for invalid flags, having
5148 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5149 * DS flags set.
5150 */
5151static int nfs4_check_cl_exchange_flags(u32 flags)
5152{
5153 if (flags & ~EXCHGID4_FLAG_MASK_R)
5154 goto out_inval;
5155 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5156 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5157 goto out_inval;
5158 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5159 goto out_inval;
5160 return NFS_OK;
5161out_inval:
5162 return -NFS4ERR_INVAL;
5163}
5164
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005165static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005166nfs41_same_server_scope(struct nfs41_server_scope *a,
5167 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005168{
5169 if (a->server_scope_sz == b->server_scope_sz &&
5170 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5171 return true;
5172
5173 return false;
5174}
5175
Andy Adamson357f54d2010-12-14 10:11:57 -05005176/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005177 * nfs4_proc_bind_conn_to_session()
5178 *
5179 * The 4.1 client currently uses the same TCP connection for the
5180 * fore and backchannel.
5181 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005182int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005183{
5184 int status;
5185 struct nfs41_bind_conn_to_session_res res;
5186 struct rpc_message msg = {
5187 .rpc_proc =
5188 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5189 .rpc_argp = clp,
5190 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005191 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005192 };
5193
5194 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005195
5196 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5197 if (unlikely(res.session == NULL)) {
5198 status = -ENOMEM;
5199 goto out;
5200 }
5201
5202 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5203 if (status == 0) {
5204 if (memcmp(res.session->sess_id.data,
5205 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5206 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5207 status = -EIO;
5208 goto out_session;
5209 }
5210 if (res.dir != NFS4_CDFS4_BOTH) {
5211 dprintk("NFS: %s: Unexpected direction from server\n",
5212 __func__);
5213 status = -EIO;
5214 goto out_session;
5215 }
5216 if (res.use_conn_in_rdma_mode) {
5217 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5218 __func__);
5219 status = -EIO;
5220 goto out_session;
5221 }
5222 }
5223out_session:
5224 kfree(res.session);
5225out:
5226 dprintk("<-- %s status= %d\n", __func__, status);
5227 return status;
5228}
5229
5230/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005231 * nfs4_proc_exchange_id()
5232 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005233 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5234 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005235 * Since the clientid has expired, all compounds using sessions
5236 * associated with the stale clientid will be returning
5237 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5238 * be in some phase of session reset.
5239 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005240int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005241{
5242 nfs4_verifier verifier;
5243 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005244 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005245 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005246 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005247 };
5248 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005249 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005250 };
5251 int status;
5252 struct rpc_message msg = {
5253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5254 .rpc_argp = &args,
5255 .rpc_resp = &res,
5256 .rpc_cred = cred,
5257 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005258
Chuck Leverf0920752012-05-21 22:45:41 -04005259 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005260 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5261 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005262 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5263 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5264 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005265
Chuck Leveracdeb692012-05-21 22:46:16 -04005266 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005267 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005268 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005269 status = -ENOMEM;
5270 goto out;
5271 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005272
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005273 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005274 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005275 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005276 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005277 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005278 }
5279
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005280 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005281 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005282 status = -ENOMEM;
5283 goto out_server_scope;
5284 }
5285
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005286 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005287 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005288 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005289
Chuck Lever177313f2012-05-21 22:44:58 -04005290 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005291 clp->cl_clientid = res.clientid;
5292 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5293 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5294 clp->cl_seqid = res.seqid;
5295
Chuck Leveracdeb692012-05-21 22:46:16 -04005296 kfree(clp->cl_serverowner);
5297 clp->cl_serverowner = res.server_owner;
5298 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005299
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005300 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005301 kfree(clp->cl_implid);
5302 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005303
Chuck Lever177313f2012-05-21 22:44:58 -04005304 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005305 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005306 res.server_scope)) {
5307 dprintk("%s: server_scope mismatch detected\n",
5308 __func__);
5309 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005310 kfree(clp->cl_serverscope);
5311 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005312 }
5313
Chuck Lever177313f2012-05-21 22:44:58 -04005314 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005315 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005316 goto out;
5317 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005318 } else
5319 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005320
Chuck Leveracdeb692012-05-21 22:46:16 -04005321out_server_owner:
5322 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005323out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005324 kfree(res.server_scope);
5325out:
Chuck Lever177313f2012-05-21 22:44:58 -04005326 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005327 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005328 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005329 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005330 clp->cl_implid->date.seconds,
5331 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005332 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005333 return status;
5334}
5335
Trond Myklebust66245532012-05-25 17:18:09 -04005336static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5337 struct rpc_cred *cred)
5338{
5339 struct rpc_message msg = {
5340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5341 .rpc_argp = clp,
5342 .rpc_cred = cred,
5343 };
5344 int status;
5345
5346 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5347 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005348 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005349 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5350 return status;
5351}
5352
5353static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5354 struct rpc_cred *cred)
5355{
5356 unsigned int loop;
5357 int ret;
5358
5359 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5360 ret = _nfs4_proc_destroy_clientid(clp, cred);
5361 switch (ret) {
5362 case -NFS4ERR_DELAY:
5363 case -NFS4ERR_CLIENTID_BUSY:
5364 ssleep(1);
5365 break;
5366 default:
5367 return ret;
5368 }
5369 }
5370 return 0;
5371}
5372
5373int nfs4_destroy_clientid(struct nfs_client *clp)
5374{
5375 struct rpc_cred *cred;
5376 int ret = 0;
5377
5378 if (clp->cl_mvops->minor_version < 1)
5379 goto out;
5380 if (clp->cl_exchange_flags == 0)
5381 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005382 if (clp->cl_preserve_clid)
5383 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005384 cred = nfs4_get_exchange_id_cred(clp);
5385 ret = nfs4_proc_destroy_clientid(clp, cred);
5386 if (cred)
5387 put_rpccred(cred);
5388 switch (ret) {
5389 case 0:
5390 case -NFS4ERR_STALE_CLIENTID:
5391 clp->cl_exchange_flags = 0;
5392 }
5393out:
5394 return ret;
5395}
5396
Andy Adamson2050f0c2009-04-01 09:22:30 -04005397struct nfs4_get_lease_time_data {
5398 struct nfs4_get_lease_time_args *args;
5399 struct nfs4_get_lease_time_res *res;
5400 struct nfs_client *clp;
5401};
5402
5403static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5404 void *calldata)
5405{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005406 struct nfs4_get_lease_time_data *data =
5407 (struct nfs4_get_lease_time_data *)calldata;
5408
5409 dprintk("--> %s\n", __func__);
5410 /* just setup sequence, do not trigger session recovery
5411 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005412 nfs41_setup_sequence(data->clp->cl_session,
5413 &data->args->la_seq_args,
5414 &data->res->lr_seq_res,
5415 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005416 dprintk("<-- %s\n", __func__);
5417}
5418
5419/*
5420 * Called from nfs4_state_manager thread for session setup, so don't recover
5421 * from sequence operation or clientid errors.
5422 */
5423static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5424{
5425 struct nfs4_get_lease_time_data *data =
5426 (struct nfs4_get_lease_time_data *)calldata;
5427
5428 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005429 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5430 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005431 switch (task->tk_status) {
5432 case -NFS4ERR_DELAY:
5433 case -NFS4ERR_GRACE:
5434 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5435 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5436 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005437 /* fall through */
5438 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005439 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005440 return;
5441 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005442 dprintk("<-- %s\n", __func__);
5443}
5444
Trond Myklebust17280172012-03-11 13:11:00 -04005445static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005446 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5447 .rpc_call_done = nfs4_get_lease_time_done,
5448};
5449
5450int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5451{
5452 struct rpc_task *task;
5453 struct nfs4_get_lease_time_args args;
5454 struct nfs4_get_lease_time_res res = {
5455 .lr_fsinfo = fsinfo,
5456 };
5457 struct nfs4_get_lease_time_data data = {
5458 .args = &args,
5459 .res = &res,
5460 .clp = clp,
5461 };
5462 struct rpc_message msg = {
5463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5464 .rpc_argp = &args,
5465 .rpc_resp = &res,
5466 };
5467 struct rpc_task_setup task_setup = {
5468 .rpc_client = clp->cl_rpcclient,
5469 .rpc_message = &msg,
5470 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005471 .callback_data = &data,
5472 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005473 };
5474 int status;
5475
Trond Myklebust9d12b212012-01-17 22:04:25 -05005476 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005477 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005478 dprintk("--> %s\n", __func__);
5479 task = rpc_run_task(&task_setup);
5480
5481 if (IS_ERR(task))
5482 status = PTR_ERR(task);
5483 else {
5484 status = task->tk_status;
5485 rpc_put_task(task);
5486 }
5487 dprintk("<-- %s return %d\n", __func__, status);
5488
5489 return status;
5490}
5491
Andy Adamsonfc931582009-04-01 09:22:31 -04005492/*
5493 * Initialize the values to be used by the client in CREATE_SESSION
5494 * If nfs4_init_session set the fore channel request and response sizes,
5495 * use them.
5496 *
5497 * Set the back channel max_resp_sz_cached to zero to force the client to
5498 * always set csa_cachethis to FALSE because the current implementation
5499 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5500 */
5501static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5502{
5503 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005504 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5505 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005506
5507 if (mxrqst_sz == 0)
5508 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5509 if (mxresp_sz == 0)
5510 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5511 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005512 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5513 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005514 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005515 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005516
5517 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005518 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005519 __func__,
5520 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005521 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005522
5523 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005524 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5525 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5526 args->bc_attrs.max_resp_sz_cached = 0;
5527 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5528 args->bc_attrs.max_reqs = 1;
5529
5530 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5531 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5532 __func__,
5533 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5534 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5535 args->bc_attrs.max_reqs);
5536}
5537
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005538static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005539{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005540 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5541 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5542
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005543 if (rcvd->max_resp_sz > sent->max_resp_sz)
5544 return -EINVAL;
5545 /*
5546 * Our requested max_ops is the minimum we need; we're not
5547 * prepared to break up compounds into smaller pieces than that.
5548 * So, no point even trying to continue if the server won't
5549 * cooperate:
5550 */
5551 if (rcvd->max_ops < sent->max_ops)
5552 return -EINVAL;
5553 if (rcvd->max_reqs == 0)
5554 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005555 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5556 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005557 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005558}
5559
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005560static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5561{
5562 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5563 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005564
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005565 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5566 return -EINVAL;
5567 if (rcvd->max_resp_sz < sent->max_resp_sz)
5568 return -EINVAL;
5569 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5570 return -EINVAL;
5571 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005572 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005573 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005574 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005575 return -EINVAL;
5576 return 0;
5577}
Andy Adamson8d353012009-04-01 09:22:32 -04005578
Andy Adamson8d353012009-04-01 09:22:32 -04005579static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5580 struct nfs4_session *session)
5581{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005582 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005583
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005584 ret = nfs4_verify_fore_channel_attrs(args, session);
5585 if (ret)
5586 return ret;
5587 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005588}
5589
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005590static int _nfs4_proc_create_session(struct nfs_client *clp,
5591 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005592{
5593 struct nfs4_session *session = clp->cl_session;
5594 struct nfs41_create_session_args args = {
5595 .client = clp,
5596 .cb_program = NFS4_CALLBACK,
5597 };
5598 struct nfs41_create_session_res res = {
5599 .client = clp,
5600 };
5601 struct rpc_message msg = {
5602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5603 .rpc_argp = &args,
5604 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005605 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005606 };
5607 int status;
5608
5609 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005610 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005611
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005612 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005613
Trond Myklebust43095d32012-11-20 11:13:12 -05005614 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005615 /* Verify the session's negotiated channel_attrs values */
5616 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005617 /* Increment the clientid slot sequence id */
5618 clp->cl_seqid++;
5619 }
5620
5621 return status;
5622}
5623
5624/*
5625 * Issues a CREATE_SESSION operation to the server.
5626 * It is the responsibility of the caller to verify the session is
5627 * expired before calling this routine.
5628 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005629int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005630{
5631 int status;
5632 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005633 struct nfs4_session *session = clp->cl_session;
5634
5635 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5636
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005637 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005638 if (status)
5639 goto out;
5640
Andy Adamsonaacd5532011-11-09 13:58:21 -05005641 /* Init or reset the session slot tables */
5642 status = nfs4_setup_session_slot_tables(session);
5643 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005644 if (status)
5645 goto out;
5646
5647 ptr = (unsigned *)&session->sess_id.data[0];
5648 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5649 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005650out:
5651 dprintk("<-- %s\n", __func__);
5652 return status;
5653}
5654
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005655/*
5656 * Issue the over-the-wire RPC DESTROY_SESSION.
5657 * The caller must serialize access to this routine.
5658 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005659int nfs4_proc_destroy_session(struct nfs4_session *session,
5660 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005661{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005662 struct rpc_message msg = {
5663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5664 .rpc_argp = session,
5665 .rpc_cred = cred,
5666 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005667 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005668
5669 dprintk("--> nfs4_proc_destroy_session\n");
5670
5671 /* session is still being setup */
5672 if (session->clp->cl_cons_state != NFS_CS_READY)
5673 return status;
5674
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005675 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005676
5677 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005678 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005679 "Session has been destroyed regardless...\n", status);
5680
5681 dprintk("<-- nfs4_proc_destroy_session\n");
5682 return status;
5683}
5684
Trond Myklebust7b38c362012-05-23 13:23:31 -04005685/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005686 * Renew the cl_session lease.
5687 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005688struct nfs4_sequence_data {
5689 struct nfs_client *clp;
5690 struct nfs4_sequence_args args;
5691 struct nfs4_sequence_res res;
5692};
5693
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005694static void nfs41_sequence_release(void *data)
5695{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005696 struct nfs4_sequence_data *calldata = data;
5697 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005698
Alexandros Batsakis71358402010-02-05 03:45:05 -08005699 if (atomic_read(&clp->cl_count) > 1)
5700 nfs4_schedule_state_renewal(clp);
5701 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005702 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005703}
5704
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005705static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5706{
5707 switch(task->tk_status) {
5708 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005709 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5710 return -EAGAIN;
5711 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005712 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005713 }
5714 return 0;
5715}
5716
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005717static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005718{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005719 struct nfs4_sequence_data *calldata = data;
5720 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005721
Trond Myklebust14516c32010-07-31 14:29:06 -04005722 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5723 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005724
5725 if (task->tk_status < 0) {
5726 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005727 if (atomic_read(&clp->cl_count) == 1)
5728 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005729
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005730 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5731 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005732 return;
5733 }
5734 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005735 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005736out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005737 dprintk("<-- %s\n", __func__);
5738}
5739
5740static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5741{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005742 struct nfs4_sequence_data *calldata = data;
5743 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005744 struct nfs4_sequence_args *args;
5745 struct nfs4_sequence_res *res;
5746
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005747 args = task->tk_msg.rpc_argp;
5748 res = task->tk_msg.rpc_resp;
5749
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005750 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005751}
5752
5753static const struct rpc_call_ops nfs41_sequence_ops = {
5754 .rpc_call_done = nfs41_sequence_call_done,
5755 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005756 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005757};
5758
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005759static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5760 struct rpc_cred *cred,
5761 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005763 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005764 struct rpc_message msg = {
5765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5766 .rpc_cred = cred,
5767 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005768 struct rpc_task_setup task_setup_data = {
5769 .rpc_client = clp->cl_rpcclient,
5770 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005771 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005772 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5773 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005774
Alexandros Batsakis71358402010-02-05 03:45:05 -08005775 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005776 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005777 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005778 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005779 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005780 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005781 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005782 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005783 if (is_privileged)
5784 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005785 msg.rpc_argp = &calldata->args;
5786 msg.rpc_resp = &calldata->res;
5787 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005788 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005789
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005790 return rpc_run_task(&task_setup_data);
5791}
5792
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005793static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005794{
5795 struct rpc_task *task;
5796 int ret = 0;
5797
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005798 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5799 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005800 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005801 if (IS_ERR(task))
5802 ret = PTR_ERR(task);
5803 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005804 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005805 dprintk("<-- %s status=%d\n", __func__, ret);
5806 return ret;
5807}
5808
5809static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5810{
5811 struct rpc_task *task;
5812 int ret;
5813
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005814 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005815 if (IS_ERR(task)) {
5816 ret = PTR_ERR(task);
5817 goto out;
5818 }
5819 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005820 if (!ret) {
5821 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5822
5823 if (task->tk_status == 0)
5824 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005825 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005826 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005827 rpc_put_task(task);
5828out:
5829 dprintk("<-- %s status=%d\n", __func__, ret);
5830 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005831}
5832
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005833struct nfs4_reclaim_complete_data {
5834 struct nfs_client *clp;
5835 struct nfs41_reclaim_complete_args arg;
5836 struct nfs41_reclaim_complete_res res;
5837};
5838
5839static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5840{
5841 struct nfs4_reclaim_complete_data *calldata = data;
5842
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005843 nfs41_setup_sequence(calldata->clp->cl_session,
5844 &calldata->arg.seq_args,
5845 &calldata->res.seq_res,
5846 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005847}
5848
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005849static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5850{
5851 switch(task->tk_status) {
5852 case 0:
5853 case -NFS4ERR_COMPLETE_ALREADY:
5854 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5855 break;
5856 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005857 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005858 /* fall through */
5859 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005860 return -EAGAIN;
5861 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005862 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005863 }
5864 return 0;
5865}
5866
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005867static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5868{
5869 struct nfs4_reclaim_complete_data *calldata = data;
5870 struct nfs_client *clp = calldata->clp;
5871 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5872
5873 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005874 if (!nfs41_sequence_done(task, res))
5875 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005876
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005877 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5878 rpc_restart_call_prepare(task);
5879 return;
5880 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005881 dprintk("<-- %s\n", __func__);
5882}
5883
5884static void nfs4_free_reclaim_complete_data(void *data)
5885{
5886 struct nfs4_reclaim_complete_data *calldata = data;
5887
5888 kfree(calldata);
5889}
5890
5891static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5892 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5893 .rpc_call_done = nfs4_reclaim_complete_done,
5894 .rpc_release = nfs4_free_reclaim_complete_data,
5895};
5896
5897/*
5898 * Issue a global reclaim complete.
5899 */
5900static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5901{
5902 struct nfs4_reclaim_complete_data *calldata;
5903 struct rpc_task *task;
5904 struct rpc_message msg = {
5905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5906 };
5907 struct rpc_task_setup task_setup_data = {
5908 .rpc_client = clp->cl_rpcclient,
5909 .rpc_message = &msg,
5910 .callback_ops = &nfs4_reclaim_complete_call_ops,
5911 .flags = RPC_TASK_ASYNC,
5912 };
5913 int status = -ENOMEM;
5914
5915 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005916 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005917 if (calldata == NULL)
5918 goto out;
5919 calldata->clp = clp;
5920 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005921
Trond Myklebust9d12b212012-01-17 22:04:25 -05005922 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005923 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924 msg.rpc_argp = &calldata->arg;
5925 msg.rpc_resp = &calldata->res;
5926 task_setup_data.callback_data = calldata;
5927 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005928 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005929 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005930 goto out;
5931 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005932 status = nfs4_wait_for_completion_rpc_task(task);
5933 if (status == 0)
5934 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005935 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005936 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005937out:
5938 dprintk("<-- %s status=%d\n", __func__, status);
5939 return status;
5940}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005941
5942static void
5943nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5944{
5945 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005946 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005947 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005948
5949 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005950 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5951 * right now covering the LAYOUTGET we are about to send.
5952 * However, that is not so catastrophic, and there seems
5953 * to be no way to prevent it completely.
5954 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005955 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005956 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005957 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005958 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5959 NFS_I(lgp->args.inode)->layout,
5960 lgp->args.ctx->state)) {
5961 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005962 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005963}
5964
5965static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5966{
5967 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07005968 struct inode *inode = lgp->args.inode;
5969 struct nfs_server *server = NFS_SERVER(inode);
5970 struct pnfs_layout_hdr *lo;
5971 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005972
5973 dprintk("--> %s\n", __func__);
5974
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005975 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07005976 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005977
5978 switch (task->tk_status) {
5979 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07005980 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005981 case -NFS4ERR_LAYOUTTRYLATER:
5982 case -NFS4ERR_RECALLCONFLICT:
5983 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07005984 break;
5985 case -NFS4ERR_EXPIRED:
5986 case -NFS4ERR_BAD_STATEID:
5987 spin_lock(&inode->i_lock);
5988 lo = NFS_I(inode)->layout;
5989 if (!lo || list_empty(&lo->plh_segs)) {
5990 spin_unlock(&inode->i_lock);
5991 /* If the open stateid was bad, then recover it. */
5992 state = lgp->args.ctx->state;
5993 } else {
5994 LIST_HEAD(head);
5995
5996 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
5997 spin_unlock(&inode->i_lock);
5998 /* Mark the bad layout state as invalid, then
5999 * retry using the open stateid. */
6000 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006001 }
6002 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006003 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6004 rpc_restart_call_prepare(task);
6005out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006006 dprintk("<-- %s\n", __func__);
6007}
6008
Idan Kedar85541162012-08-02 11:47:10 +03006009static size_t max_response_pages(struct nfs_server *server)
6010{
6011 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6012 return nfs_page_array_len(0, max_resp_sz);
6013}
6014
6015static void nfs4_free_pages(struct page **pages, size_t size)
6016{
6017 int i;
6018
6019 if (!pages)
6020 return;
6021
6022 for (i = 0; i < size; i++) {
6023 if (!pages[i])
6024 break;
6025 __free_page(pages[i]);
6026 }
6027 kfree(pages);
6028}
6029
6030static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6031{
6032 struct page **pages;
6033 int i;
6034
6035 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6036 if (!pages) {
6037 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6038 return NULL;
6039 }
6040
6041 for (i = 0; i < size; i++) {
6042 pages[i] = alloc_page(gfp_flags);
6043 if (!pages[i]) {
6044 dprintk("%s: failed to allocate page\n", __func__);
6045 nfs4_free_pages(pages, size);
6046 return NULL;
6047 }
6048 }
6049
6050 return pages;
6051}
6052
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006053static void nfs4_layoutget_release(void *calldata)
6054{
6055 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006056 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6057 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006058
6059 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006060 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006061 put_nfs_open_context(lgp->args.ctx);
6062 kfree(calldata);
6063 dprintk("<-- %s\n", __func__);
6064}
6065
6066static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6067 .rpc_call_prepare = nfs4_layoutget_prepare,
6068 .rpc_call_done = nfs4_layoutget_done,
6069 .rpc_release = nfs4_layoutget_release,
6070};
6071
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006072struct pnfs_layout_segment *
6073nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006074{
6075 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006076 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006077 struct rpc_task *task;
6078 struct rpc_message msg = {
6079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6080 .rpc_argp = &lgp->args,
6081 .rpc_resp = &lgp->res,
6082 };
6083 struct rpc_task_setup task_setup_data = {
6084 .rpc_client = server->client,
6085 .rpc_message = &msg,
6086 .callback_ops = &nfs4_layoutget_call_ops,
6087 .callback_data = lgp,
6088 .flags = RPC_TASK_ASYNC,
6089 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006090 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006091 int status = 0;
6092
6093 dprintk("--> %s\n", __func__);
6094
Idan Kedar85541162012-08-02 11:47:10 +03006095 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6096 if (!lgp->args.layout.pages) {
6097 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006098 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006099 }
6100 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6101
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006102 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006103 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006104 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006105 task = rpc_run_task(&task_setup_data);
6106 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006107 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006108 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006109 if (status == 0)
6110 status = task->tk_status;
6111 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006112 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006113 rpc_put_task(task);
6114 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006115 if (status)
6116 return ERR_PTR(status);
6117 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006118}
6119
Benny Halevycbe82602011-05-22 19:52:37 +03006120static void
6121nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6122{
6123 struct nfs4_layoutreturn *lrp = calldata;
6124
6125 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006126 nfs41_setup_sequence(lrp->clp->cl_session,
6127 &lrp->args.seq_args,
6128 &lrp->res.seq_res,
6129 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006130}
6131
6132static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6133{
6134 struct nfs4_layoutreturn *lrp = calldata;
6135 struct nfs_server *server;
6136
6137 dprintk("--> %s\n", __func__);
6138
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006139 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006140 return;
6141
6142 server = NFS_SERVER(lrp->args.inode);
6143 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006144 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006145 return;
6146 }
Benny Halevycbe82602011-05-22 19:52:37 +03006147 dprintk("<-- %s\n", __func__);
6148}
6149
6150static void nfs4_layoutreturn_release(void *calldata)
6151{
6152 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006153 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006154
6155 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006156 spin_lock(&lo->plh_inode->i_lock);
6157 if (lrp->res.lrs_present)
6158 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6159 lo->plh_block_lgets--;
6160 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006161 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006162 kfree(calldata);
6163 dprintk("<-- %s\n", __func__);
6164}
6165
6166static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6167 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6168 .rpc_call_done = nfs4_layoutreturn_done,
6169 .rpc_release = nfs4_layoutreturn_release,
6170};
6171
6172int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6173{
6174 struct rpc_task *task;
6175 struct rpc_message msg = {
6176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6177 .rpc_argp = &lrp->args,
6178 .rpc_resp = &lrp->res,
6179 };
6180 struct rpc_task_setup task_setup_data = {
6181 .rpc_client = lrp->clp->cl_rpcclient,
6182 .rpc_message = &msg,
6183 .callback_ops = &nfs4_layoutreturn_call_ops,
6184 .callback_data = lrp,
6185 };
6186 int status;
6187
6188 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006189 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006190 task = rpc_run_task(&task_setup_data);
6191 if (IS_ERR(task))
6192 return PTR_ERR(task);
6193 status = task->tk_status;
6194 dprintk("<-- %s status=%d\n", __func__, status);
6195 rpc_put_task(task);
6196 return status;
6197}
6198
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006199/*
6200 * Retrieve the list of Data Server devices from the MDS.
6201 */
6202static int _nfs4_getdevicelist(struct nfs_server *server,
6203 const struct nfs_fh *fh,
6204 struct pnfs_devicelist *devlist)
6205{
6206 struct nfs4_getdevicelist_args args = {
6207 .fh = fh,
6208 .layoutclass = server->pnfs_curr_ld->id,
6209 };
6210 struct nfs4_getdevicelist_res res = {
6211 .devlist = devlist,
6212 };
6213 struct rpc_message msg = {
6214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6215 .rpc_argp = &args,
6216 .rpc_resp = &res,
6217 };
6218 int status;
6219
6220 dprintk("--> %s\n", __func__);
6221 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6222 &res.seq_res, 0);
6223 dprintk("<-- %s status=%d\n", __func__, status);
6224 return status;
6225}
6226
6227int nfs4_proc_getdevicelist(struct nfs_server *server,
6228 const struct nfs_fh *fh,
6229 struct pnfs_devicelist *devlist)
6230{
6231 struct nfs4_exception exception = { };
6232 int err;
6233
6234 do {
6235 err = nfs4_handle_exception(server,
6236 _nfs4_getdevicelist(server, fh, devlist),
6237 &exception);
6238 } while (exception.retry);
6239
6240 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6241 err, devlist->num_devs);
6242
6243 return err;
6244}
6245EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6246
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006247static int
6248_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6249{
6250 struct nfs4_getdeviceinfo_args args = {
6251 .pdev = pdev,
6252 };
6253 struct nfs4_getdeviceinfo_res res = {
6254 .pdev = pdev,
6255 };
6256 struct rpc_message msg = {
6257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6258 .rpc_argp = &args,
6259 .rpc_resp = &res,
6260 };
6261 int status;
6262
6263 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006264 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006265 dprintk("<-- %s status=%d\n", __func__, status);
6266
6267 return status;
6268}
6269
6270int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6271{
6272 struct nfs4_exception exception = { };
6273 int err;
6274
6275 do {
6276 err = nfs4_handle_exception(server,
6277 _nfs4_proc_getdeviceinfo(server, pdev),
6278 &exception);
6279 } while (exception.retry);
6280 return err;
6281}
6282EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6283
Andy Adamson863a3c62011-03-23 13:27:54 +00006284static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6285{
6286 struct nfs4_layoutcommit_data *data = calldata;
6287 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006288 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006289
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006290 nfs41_setup_sequence(session,
6291 &data->args.seq_args,
6292 &data->res.seq_res,
6293 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006294}
6295
6296static void
6297nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6298{
6299 struct nfs4_layoutcommit_data *data = calldata;
6300 struct nfs_server *server = NFS_SERVER(data->args.inode);
6301
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006302 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006303 return;
6304
6305 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006306 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6307 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6308 case -NFS4ERR_BADLAYOUT: /* no layout */
6309 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006310 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006311 break;
6312 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006313 nfs_post_op_update_inode_force_wcc(data->args.inode,
6314 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006315 break;
6316 default:
6317 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6318 rpc_restart_call_prepare(task);
6319 return;
6320 }
6321 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006322}
6323
6324static void nfs4_layoutcommit_release(void *calldata)
6325{
6326 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006327 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006328 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006329
Andy Adamsondb29c082011-07-30 20:52:38 -04006330 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006331 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006332 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6333 list_del_init(&lseg->pls_lc_list);
6334 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6335 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006336 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006337 }
Peng Tao92407e72011-10-23 20:21:17 -07006338
6339 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6340 smp_mb__after_clear_bit();
6341 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6342
Andy Adamson863a3c62011-03-23 13:27:54 +00006343 put_rpccred(data->cred);
6344 kfree(data);
6345}
6346
6347static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6348 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6349 .rpc_call_done = nfs4_layoutcommit_done,
6350 .rpc_release = nfs4_layoutcommit_release,
6351};
6352
6353int
Andy Adamsonef311532011-03-12 02:58:10 -05006354nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006355{
6356 struct rpc_message msg = {
6357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6358 .rpc_argp = &data->args,
6359 .rpc_resp = &data->res,
6360 .rpc_cred = data->cred,
6361 };
6362 struct rpc_task_setup task_setup_data = {
6363 .task = &data->task,
6364 .rpc_client = NFS_CLIENT(data->args.inode),
6365 .rpc_message = &msg,
6366 .callback_ops = &nfs4_layoutcommit_ops,
6367 .callback_data = data,
6368 .flags = RPC_TASK_ASYNC,
6369 };
6370 struct rpc_task *task;
6371 int status = 0;
6372
6373 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6374 "lbw: %llu inode %lu\n",
6375 data->task.tk_pid, sync,
6376 data->args.lastbytewritten,
6377 data->args.inode->i_ino);
6378
Trond Myklebust9d12b212012-01-17 22:04:25 -05006379 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006380 task = rpc_run_task(&task_setup_data);
6381 if (IS_ERR(task))
6382 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006383 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006384 goto out;
6385 status = nfs4_wait_for_completion_rpc_task(task);
6386 if (status != 0)
6387 goto out;
6388 status = task->tk_status;
6389out:
6390 dprintk("%s: status %d\n", __func__, status);
6391 rpc_put_task(task);
6392 return status;
6393}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006394
6395static int
6396_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6397 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6398{
6399 struct nfs41_secinfo_no_name_args args = {
6400 .style = SECINFO_STYLE_CURRENT_FH,
6401 };
6402 struct nfs4_secinfo_res res = {
6403 .flavors = flavors,
6404 };
6405 struct rpc_message msg = {
6406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6407 .rpc_argp = &args,
6408 .rpc_resp = &res,
6409 };
6410 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6411}
6412
6413static int
6414nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6415 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6416{
6417 struct nfs4_exception exception = { };
6418 int err;
6419 do {
6420 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6421 switch (err) {
6422 case 0:
6423 case -NFS4ERR_WRONGSEC:
6424 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006425 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006426 default:
6427 err = nfs4_handle_exception(server, err, &exception);
6428 }
6429 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006430out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006431 return err;
6432}
6433
6434static int
6435nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6436 struct nfs_fsinfo *info)
6437{
6438 int err;
6439 struct page *page;
6440 rpc_authflavor_t flavor;
6441 struct nfs4_secinfo_flavors *flavors;
6442
6443 page = alloc_page(GFP_KERNEL);
6444 if (!page) {
6445 err = -ENOMEM;
6446 goto out;
6447 }
6448
6449 flavors = page_address(page);
6450 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6451
6452 /*
6453 * Fall back on "guess and check" method if
6454 * the server doesn't support SECINFO_NO_NAME
6455 */
6456 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6457 err = nfs4_find_root_sec(server, fhandle, info);
6458 goto out_freepage;
6459 }
6460 if (err)
6461 goto out_freepage;
6462
6463 flavor = nfs_find_best_sec(flavors);
6464 if (err == 0)
6465 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6466
6467out_freepage:
6468 put_page(page);
6469 if (err == -EACCES)
6470 return -EPERM;
6471out:
6472 return err;
6473}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006474
6475static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006476{
6477 int status;
6478 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006479 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006480 };
6481 struct nfs41_test_stateid_res res;
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6484 .rpc_argp = &args,
6485 .rpc_resp = &res,
6486 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006487
Chuck Lever38527b12012-07-11 16:30:23 -04006488 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006489 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006490 nfs4_set_sequence_privileged(&args.seq_args);
6491 status = nfs4_call_sync_sequence(server->client, server, &msg,
6492 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006493 if (status != NFS_OK) {
6494 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006495 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006496 }
6497 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006498 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006499}
6500
Chuck Lever38527b12012-07-11 16:30:23 -04006501/**
6502 * nfs41_test_stateid - perform a TEST_STATEID operation
6503 *
6504 * @server: server / transport on which to perform the operation
6505 * @stateid: state ID to test
6506 *
6507 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6508 * Otherwise a negative NFS4ERR value is returned if the operation
6509 * failed or the state ID is not currently valid.
6510 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006511static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006512{
6513 struct nfs4_exception exception = { };
6514 int err;
6515 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006516 err = _nfs41_test_stateid(server, stateid);
6517 if (err != -NFS4ERR_DELAY)
6518 break;
6519 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006520 } while (exception.retry);
6521 return err;
6522}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006523
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006524static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006525{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006526 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006527 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006528 };
6529 struct nfs41_free_stateid_res res;
6530 struct rpc_message msg = {
6531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6532 .rpc_argp = &args,
6533 .rpc_resp = &res,
6534 };
Chuck Lever38527b12012-07-11 16:30:23 -04006535 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006536
Chuck Lever38527b12012-07-11 16:30:23 -04006537 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006538 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006539 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006540 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006541 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006542 dprintk("NFS reply free_stateid: %d\n", status);
6543 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006544}
6545
Chuck Lever38527b12012-07-11 16:30:23 -04006546/**
6547 * nfs41_free_stateid - perform a FREE_STATEID operation
6548 *
6549 * @server: server / transport on which to perform the operation
6550 * @stateid: state ID to release
6551 *
6552 * Returns NFS_OK if the server freed "stateid". Otherwise a
6553 * negative NFS4ERR value is returned.
6554 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006555static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006556{
6557 struct nfs4_exception exception = { };
6558 int err;
6559 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006560 err = _nfs4_free_stateid(server, stateid);
6561 if (err != -NFS4ERR_DELAY)
6562 break;
6563 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006564 } while (exception.retry);
6565 return err;
6566}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006567
6568static bool nfs41_match_stateid(const nfs4_stateid *s1,
6569 const nfs4_stateid *s2)
6570{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006571 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006572 return false;
6573
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006574 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006575 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006576 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006577 return true;
6578
6579 return false;
6580}
6581
Andy Adamson557134a2009-04-01 09:21:53 -04006582#endif /* CONFIG_NFS_V4_1 */
6583
Trond Myklebust36281ca2012-03-04 18:13:56 -05006584static bool nfs4_match_stateid(const nfs4_stateid *s1,
6585 const nfs4_stateid *s2)
6586{
Trond Myklebustf597c532012-03-04 18:13:56 -05006587 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006588}
6589
6590
Trond Myklebust17280172012-03-11 13:11:00 -04006591static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006592 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006593 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 .recover_open = nfs4_open_reclaim,
6595 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006596 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006597 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006598 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599};
6600
Andy Adamson591d71c2009-04-01 09:22:47 -04006601#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006602static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006603 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6604 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6605 .recover_open = nfs4_open_reclaim,
6606 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006607 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006608 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006609 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006610 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006611};
6612#endif /* CONFIG_NFS_V4_1 */
6613
Trond Myklebust17280172012-03-11 13:11:00 -04006614static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006615 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006616 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 .recover_open = nfs4_open_expired,
6618 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006619 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006620 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621};
6622
Andy Adamson591d71c2009-04-01 09:22:47 -04006623#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006624static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006625 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6626 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006627 .recover_open = nfs41_open_expired,
6628 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006629 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006630 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006631};
6632#endif /* CONFIG_NFS_V4_1 */
6633
Trond Myklebust17280172012-03-11 13:11:00 -04006634static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006635 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006636 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006637 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006638};
6639
6640#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006641static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006642 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006643 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006644 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006645};
6646#endif
6647
Trond Myklebust97dc1352010-06-16 09:52:26 -04006648static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6649 .minor_version = 0,
6650 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006651 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006652 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006653 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6654 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6655 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006656};
6657
6658#if defined(CONFIG_NFS_V4_1)
6659static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6660 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006661 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006662 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006663 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006664 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6665 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6666 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006667};
6668#endif
6669
Trond Myklebust97dc1352010-06-16 09:52:26 -04006670const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6671 [0] = &nfs_v4_0_minor_ops,
6672#if defined(CONFIG_NFS_V4_1)
6673 [1] = &nfs_v4_1_minor_ops,
6674#endif
6675};
6676
Bryan Schumaker73a79702012-07-16 16:39:12 -04006677const struct inode_operations nfs4_dir_inode_operations = {
6678 .create = nfs_create,
6679 .lookup = nfs_lookup,
6680 .atomic_open = nfs_atomic_open,
6681 .link = nfs_link,
6682 .unlink = nfs_unlink,
6683 .symlink = nfs_symlink,
6684 .mkdir = nfs_mkdir,
6685 .rmdir = nfs_rmdir,
6686 .mknod = nfs_mknod,
6687 .rename = nfs_rename,
6688 .permission = nfs_permission,
6689 .getattr = nfs_getattr,
6690 .setattr = nfs_setattr,
6691 .getxattr = generic_getxattr,
6692 .setxattr = generic_setxattr,
6693 .listxattr = generic_listxattr,
6694 .removexattr = generic_removexattr,
6695};
6696
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006697static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006698 .permission = nfs_permission,
6699 .getattr = nfs_getattr,
6700 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006701 .getxattr = generic_getxattr,
6702 .setxattr = generic_setxattr,
6703 .listxattr = generic_listxattr,
6704 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006705};
6706
David Howells509de812006-08-22 20:06:11 -04006707const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 .version = 4, /* protocol version */
6709 .dentry_ops = &nfs4_dentry_operations,
6710 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006711 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006712 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006714 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006715 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 .getattr = nfs4_proc_getattr,
6717 .setattr = nfs4_proc_setattr,
6718 .lookup = nfs4_proc_lookup,
6719 .access = nfs4_proc_access,
6720 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 .create = nfs4_proc_create,
6722 .remove = nfs4_proc_remove,
6723 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006724 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 .unlink_done = nfs4_proc_unlink_done,
6726 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006727 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006728 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006729 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 .link = nfs4_proc_link,
6731 .symlink = nfs4_proc_symlink,
6732 .mkdir = nfs4_proc_mkdir,
6733 .rmdir = nfs4_proc_remove,
6734 .readdir = nfs4_proc_readdir,
6735 .mknod = nfs4_proc_mknod,
6736 .statfs = nfs4_proc_statfs,
6737 .fsinfo = nfs4_proc_fsinfo,
6738 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006739 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 .decode_dirent = nfs4_decode_dirent,
6741 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006742 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006743 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006744 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006746 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006747 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006748 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006750 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006751 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006753 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006754 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006755 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006756 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006757 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006758 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006759 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006760 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006761 .create_server = nfs4_create_server,
6762 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763};
6764
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006765static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6766 .prefix = XATTR_NAME_NFSV4_ACL,
6767 .list = nfs4_xattr_list_nfs4_acl,
6768 .get = nfs4_xattr_get_nfs4_acl,
6769 .set = nfs4_xattr_set_nfs4_acl,
6770};
6771
6772const struct xattr_handler *nfs4_xattr_handlers[] = {
6773 &nfs4_xattr_nfs4_acl_handler,
6774 NULL
6775};
6776
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777/*
6778 * Local variables:
6779 * c-basic-offset: 8
6780 * End:
6781 */