blob: 4aaaa3ba30888de9b04852cbaa857c785ce061e6 [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 Myklebust6926afd12012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond Myklebust961a8282012-01-17 22:57:37 -0500382bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
383{
384 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
385 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386}
387
Trond Myklebustd185a332010-06-16 09:52:25 -0400388static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400389{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500390 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400391 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500392 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400393
Benny Halevydfb4f3092010-09-24 09:17:01 -0400394 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400395 /* just wake up the next guy waiting since
396 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500397 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500398 return;
Andy Adamson13615872009-04-01 09:22:17 -0400399 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500400 tbl = res->sr_slot->table;
401 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500402
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500403 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500404 /* Be nice to the server: try to ensure that the last transmitted
405 * value for highest_user_slotid <= target_highest_slotid
406 */
407 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
408 send_new_highest_used_slotid = true;
409
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500410 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500411
412 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
413 send_new_highest_used_slotid = false;
414 if (!nfs4_session_draining(session)) {
415 if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
416 nfs4_set_task_privileged, NULL) != NULL)
417 send_new_highest_used_slotid = false;
418 }
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500419 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400420 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500421 if (send_new_highest_used_slotid)
422 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400423}
424
Trond Myklebust14516c32010-07-31 14:29:06 -0400425static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500427 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500428 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400429 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400430 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431
432 /*
433 * sr_status remains 1 if an RPC level error occurred. The server
434 * may or may not have processed the sequence operation..
435 * Proceed as if the server received and processed the sequence
436 * operation.
437 */
438 if (res->sr_status == 1)
439 res->sr_status = NFS_OK;
440
Bryan Schumaker468f8612011-04-18 15:57:32 -0400441 /* don't increment the sequence number if the task wasn't sent */
442 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 goto out;
444
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500446 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500447
Andy Adamson691daf32009-12-04 15:55:39 -0500448 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 switch (res->sr_status) {
450 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400451 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500452 ++slot->seq_nr;
453 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500454 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400455 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500456 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500459 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 break;
461 case -NFS4ERR_DELAY:
462 /* The server detected a resend of the RPC call and
463 * returned NFS4ERR_DELAY as per Section 2.10.6.2
464 * of RFC5661.
465 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500466 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400467 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500468 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500469 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 goto out_retry;
471 default:
472 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500473 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400474 }
475out:
476 /* The session may be reset by one of the error handlers. */
477 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400478 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 return 1;
480out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400481 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 goto out;
483 rpc_delay(task, NFS4_POLL_RETRY_MAX);
484 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485}
486
Trond Myklebust14516c32010-07-31 14:29:06 -0400487static int nfs4_sequence_done(struct rpc_task *task,
488 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400489{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500490 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 return 1;
492 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400493}
494
Trond Myklebust9d12b212012-01-17 22:04:25 -0500495static void nfs41_init_sequence(struct nfs4_sequence_args *args,
496 struct nfs4_sequence_res *res, int cache_reply)
497{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500498 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500499 args->sa_cache_this = 0;
500 if (cache_reply)
501 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500502 res->sr_slot = NULL;
503}
504
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000505int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 struct rpc_task *task)
509{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 struct nfs4_slot *slot;
511 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512
513 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400514 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400515 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400516 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400517
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 tbl = &session->fc_slot_table;
519
Trond Myklebust69d206b2012-11-22 13:21:02 -0500520 task->tk_timeout = 0;
521
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400523 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800524 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500525 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500526 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400527 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500528 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500529 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400530 }
531
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800532 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
533 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
534 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
535 spin_unlock(&tbl->slot_tbl_lock);
536 dprintk("%s enforce FIFO order\n", __func__);
537 return -EAGAIN;
538 }
539
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500540 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500541 if (IS_ERR(slot)) {
542 /* If out of memory, try again in 1/4 second */
543 if (slot == ERR_PTR(-ENOMEM))
544 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400545 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
546 spin_unlock(&tbl->slot_tbl_lock);
547 dprintk("<-- %s: no free slots\n", __func__);
548 return -EAGAIN;
549 }
550 spin_unlock(&tbl->slot_tbl_lock);
551
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800552 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500553
554 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500556 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
557 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558
Benny Halevydfb4f3092010-09-24 09:17:01 -0400559 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400560 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400561 /*
562 * sr_status is only set in decode_sequence, and so will remain
563 * set to 1 if an rpc level failure occurs.
564 */
565 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400566out_success:
567 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 return 0;
569}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000570EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571
Trond Myklebust035168ab2010-06-16 09:52:26 -0400572int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 struct nfs4_sequence_args *args,
574 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575 struct rpc_task *task)
576{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400577 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578 int ret = 0;
579
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400580 if (session == NULL) {
581 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400582 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400583 }
Trond Myklebust035168ab2010-06-16 09:52:26 -0400584
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500585 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400586 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500587 res->sr_slot->slot_nr : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400588
Trond Myklebust9d12b212012-01-17 22:04:25 -0500589 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590out:
591 dprintk("<-- %s status=%d\n", __func__, ret);
592 return ret;
593}
594
595struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400596 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597 struct nfs4_sequence_args *seq_args;
598 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599};
600
601static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
602{
603 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400604 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605
Trond Myklebust035168ab2010-06-16 09:52:26 -0400606 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
607
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400608 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609}
610
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800611static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
612{
613 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
614 nfs41_call_sync_prepare(task, calldata);
615}
616
Andy Adamson69ab40c2009-04-01 09:22:19 -0400617static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
618{
619 struct nfs41_call_sync_data *data = calldata;
620
Trond Myklebust14516c32010-07-31 14:29:06 -0400621 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400622}
623
Trond Myklebust17280172012-03-11 13:11:00 -0400624static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400626 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627};
628
Trond Myklebust17280172012-03-11 13:11:00 -0400629static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800630 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
631 .rpc_call_done = nfs41_call_sync_done,
632};
633
Bryan Schumaker7c513052011-03-24 17:12:24 +0000634static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
635 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636 struct rpc_message *msg,
637 struct nfs4_sequence_args *args,
638 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800639 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640{
641 int ret;
642 struct rpc_task *task;
643 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400644 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 .seq_args = args,
646 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647 };
648 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000649 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650 .rpc_message = msg,
651 .callback_ops = &nfs41_call_sync_ops,
652 .callback_data = &data
653 };
654
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800655 if (privileged)
656 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657 task = rpc_run_task(&task_setup);
658 if (IS_ERR(task))
659 ret = PTR_ERR(task);
660 else {
661 ret = task->tk_status;
662 rpc_put_task(task);
663 }
664 return ret;
665}
666
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400667static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000668int _nfs4_call_sync_session(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{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500674 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400675}
676
Trond Myklebustdf896452010-06-16 09:52:26 -0400677#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500678static inline
679void nfs41_init_sequence(struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res, int cache_reply)
681{
682}
683
Trond Myklebust14516c32010-07-31 14:29:06 -0400684static int nfs4_sequence_done(struct rpc_task *task,
685 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400686{
Trond Myklebust14516c32010-07-31 14:29:06 -0400687 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400688}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689#endif /* CONFIG_NFS_V4_1 */
690
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400691static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000692int _nfs4_call_sync(struct rpc_clnt *clnt,
693 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694 struct rpc_message *msg,
695 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400696 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400697{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000698 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400699}
700
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400701static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000702int nfs4_call_sync(struct rpc_clnt *clnt,
703 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000704 struct rpc_message *msg,
705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
707 int cache_reply)
708{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400709 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000710 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400711 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000712}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713
Trond Myklebust38478b22006-05-25 01:40:57 -0400714static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Trond Myklebust38478b22006-05-25 01:40:57 -0400716 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Trond Myklebust38478b22006-05-25 01:40:57 -0400718 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400719 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700720 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400721 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700722 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400723 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100726struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400727 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 struct nfs_openargs o_arg;
729 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100730 struct nfs_open_confirmargs c_arg;
731 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -0500732 struct nfs4_string owner_name;
733 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100734 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100735 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400736 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100737 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400738 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100740 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400741 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100742 int rpc_status;
743 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744};
745
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400746
747static void nfs4_init_opendata_res(struct nfs4_opendata *p)
748{
749 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400750 p->o_res.seqid = p->o_arg.seqid;
751 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400752 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400753 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400754 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -0500755 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400756}
757
Al Viro82a2c1b2011-06-22 18:30:55 -0400758static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500759 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400760 const struct iattr *attrs,
761 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762{
Al Viro82a2c1b2011-06-22 18:30:55 -0400763 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct inode *dir = parent->d_inode;
765 struct nfs_server *server = NFS_SERVER(dir);
766 struct nfs4_opendata *p;
767
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400768 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 if (p == NULL)
770 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400771 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 if (p->o_arg.seqid == NULL)
773 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400774 nfs_sb_active(dentry->d_sb);
775 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 p->dir = parent;
777 p->owner = sp;
778 atomic_inc(&sp->so_count);
779 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500780 p->o_arg.open_flags = flags;
781 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700782 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
783 * will return permission denied for all bits until close */
784 if (!(flags & O_EXCL)) {
785 /* ask server to check for all possible rights as results
786 * are cached */
787 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
788 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700789 }
David Howells7539bba2006-08-22 20:06:09 -0400790 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400791 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
792 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400793 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 p->o_arg.server = server;
795 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400796 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500798 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500799 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400800
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801 p->o_arg.u.attrs = &p->attrs;
802 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500803
804 verf[0] = jiffies;
805 verf[1] = current->pid;
806 memcpy(p->o_arg.u.verifier.data, verf,
807 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100809 p->c_arg.fh = &p->o_res.fh;
810 p->c_arg.stateid = &p->o_res.stateid;
811 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400813 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 return p;
815err_free:
816 kfree(p);
817err:
818 dput(parent);
819 return NULL;
820}
821
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400822static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400824 struct nfs4_opendata *p = container_of(kref,
825 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400826 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400827
828 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 if (p->state != NULL)
830 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400831 nfs4_put_state_owner(p->owner);
832 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400833 dput(p->dentry);
834 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -0500835 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836 kfree(p);
837}
838
839static void nfs4_opendata_put(struct nfs4_opendata *p)
840{
841 if (p != NULL)
842 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843}
844
Trond Myklebust06f814a2006-01-03 09:55:07 +0100845static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
846{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100847 int ret;
848
Trond Myklebust06f814a2006-01-03 09:55:07 +0100849 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100850 return ret;
851}
852
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400854{
855 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856
Trond Myklebust536e43d2012-01-17 22:04:26 -0500857 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 goto out;
859 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400860 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500861 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
862 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400863 break;
864 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500865 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
866 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400867 break;
868 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500869 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
870 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400873 return ret;
874}
875
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400877{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500878 if (delegation == NULL)
879 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400881 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500882 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400883 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500884 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400885 return 1;
886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100889{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100891 case FMODE_WRITE:
892 state->n_wronly++;
893 break;
894 case FMODE_READ:
895 state->n_rdonly++;
896 break;
897 case FMODE_READ|FMODE_WRITE:
898 state->n_rdwr++;
899 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400904{
905 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500906 nfs4_stateid_copy(&state->stateid, stateid);
907 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400909 case FMODE_READ:
910 set_bit(NFS_O_RDONLY_STATE, &state->flags);
911 break;
912 case FMODE_WRITE:
913 set_bit(NFS_O_WRONLY_STATE, &state->flags);
914 break;
915 case FMODE_READ|FMODE_WRITE:
916 set_bit(NFS_O_RDWR_STATE, &state->flags);
917 }
918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400922 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400924 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static 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 -0700928{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400929 /*
930 * Protect the call to nfs4_state_set_mode_locked and
931 * serialise the stateid update
932 */
933 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400934 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500935 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400936 set_bit(NFS_DELEGATED_STATE, &state->flags);
937 }
938 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400940 write_sequnlock(&state->seqlock);
941 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700943 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946static 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 -0500947{
948 struct nfs_inode *nfsi = NFS_I(state->inode);
949 struct nfs_delegation *deleg_cur;
950 int ret = 0;
951
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500953
954 rcu_read_lock();
955 deleg_cur = rcu_dereference(nfsi->delegation);
956 if (deleg_cur == NULL)
957 goto no_delegation;
958
959 spin_lock(&deleg_cur->lock);
960 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500962 goto no_delegation_unlock;
963
964 if (delegation == NULL)
965 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500966 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500967 goto no_delegation_unlock;
968
Trond Myklebustb7391f42008-12-23 15:21:52 -0500969 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500971 ret = 1;
972no_delegation_unlock:
973 spin_unlock(&deleg_cur->lock);
974no_delegation:
975 rcu_read_unlock();
976
977 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500979 ret = 1;
980 }
981
982 return ret;
983}
984
985
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400987{
988 struct nfs_delegation *delegation;
989
990 rcu_read_lock();
991 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400993 rcu_read_unlock();
994 return;
995 }
996 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400997 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400998}
999
Trond Myklebust6ee41262007-07-08 14:11:36 -04001000static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001{
1002 struct nfs4_state *state = opendata->state;
1003 struct nfs_inode *nfsi = NFS_I(state->inode);
1004 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001005 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007 nfs4_stateid stateid;
1008 int ret = -EAGAIN;
1009
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001012 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 if (can_open_cached(state, fmode, open_mode)) {
1014 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 goto out_return_state;
1017 }
1018 spin_unlock(&state->owner->so_lock);
1019 }
Trond Myklebust34310432008-12-23 15:21:38 -05001020 rcu_read_lock();
1021 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001022 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001023 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001024 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001025 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001027 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001029 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 if (ret != 0)
1031 goto out;
1032 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001033
1034 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001036 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038out:
1039 return ERR_PTR(ret);
1040out_return_state:
1041 atomic_inc(&state->count);
1042 return state;
1043}
1044
Andy Adamsone23008e2012-10-02 21:07:32 -04001045static void
1046nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1047{
1048 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1049 struct nfs_delegation *delegation;
1050 int delegation_flags = 0;
1051
1052 rcu_read_lock();
1053 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1054 if (delegation)
1055 delegation_flags = delegation->flags;
1056 rcu_read_unlock();
1057 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1058 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1059 "returning a delegation for "
1060 "OPEN(CLAIM_DELEGATE_CUR)\n",
1061 clp->cl_hostname);
1062 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1063 nfs_inode_set_delegation(state->inode,
1064 data->owner->so_cred,
1065 &data->o_res);
1066 else
1067 nfs_inode_reclaim_delegation(state->inode,
1068 data->owner->so_cred,
1069 &data->o_res);
1070}
1071
1072/*
1073 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1074 * and update the nfs4_state.
1075 */
1076static struct nfs4_state *
1077_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1078{
1079 struct inode *inode = data->state->inode;
1080 struct nfs4_state *state = data->state;
1081 int ret;
1082
1083 if (!data->rpc_done) {
1084 ret = data->rpc_status;
1085 goto err;
1086 }
1087
1088 ret = -ESTALE;
1089 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1090 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1091 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1092 goto err;
1093
1094 ret = -ENOMEM;
1095 state = nfs4_get_open_state(inode, data->owner);
1096 if (state == NULL)
1097 goto err;
1098
1099 ret = nfs_refresh_inode(inode, &data->f_attr);
1100 if (ret)
1101 goto err;
1102
1103 if (data->o_res.delegation_type != 0)
1104 nfs4_opendata_check_deleg(data, state);
1105 update_open_stateid(state, &data->o_res.stateid, NULL,
1106 data->o_arg.fmode);
1107
1108 return state;
1109err:
1110 return ERR_PTR(ret);
1111
1112}
1113
1114static struct nfs4_state *
1115_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116{
1117 struct inode *inode;
1118 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001120
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001122 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001123 goto out;
1124 }
1125
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001126 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001131 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 goto err;
1133 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134 state = nfs4_get_open_state(inode, data->owner);
1135 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001136 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001137 if (data->o_res.delegation_type != 0)
1138 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001139 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001142out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001144err_put_inode:
1145 iput(inode);
1146err:
1147 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001148}
1149
Andy Adamsone23008e2012-10-02 21:07:32 -04001150static struct nfs4_state *
1151nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1152{
1153 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1154 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1155 return _nfs4_opendata_to_nfs4_state(data);
1156}
1157
Trond Myklebust864472e2006-01-03 09:55:15 +01001158static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1159{
1160 struct nfs_inode *nfsi = NFS_I(state->inode);
1161 struct nfs_open_context *ctx;
1162
1163 spin_lock(&state->inode->i_lock);
1164 list_for_each_entry(ctx, &nfsi->open_files, list) {
1165 if (ctx->state != state)
1166 continue;
1167 get_nfs_open_context(ctx);
1168 spin_unlock(&state->inode->i_lock);
1169 return ctx;
1170 }
1171 spin_unlock(&state->inode->i_lock);
1172 return ERR_PTR(-ENOENT);
1173}
1174
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001175static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1176{
1177 struct nfs4_opendata *opendata;
1178
Al Viro3d4ff432011-06-22 18:40:12 -04001179 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001180 if (opendata == NULL)
1181 return ERR_PTR(-ENOMEM);
1182 opendata->state = state;
1183 atomic_inc(&state->count);
1184 return opendata;
1185}
1186
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001188{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 int ret;
1191
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001192 opendata->o_arg.open_flags = 0;
1193 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1195 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1196 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001197 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 if (ret != 0)
1199 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001201 if (IS_ERR(newstate))
1202 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001203 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 return 0;
1206}
1207
1208static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1209{
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 int ret;
1212
1213 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 smp_rmb();
1216 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001217 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 if (ret != 0)
1220 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 if (newstate != state)
1222 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 }
1224 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001225 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001226 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 if (ret != 0)
1228 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 if (newstate != state)
1230 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 }
1232 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001233 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001234 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 if (ret != 0)
1236 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001237 if (newstate != state)
1238 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001240 /*
1241 * We may have performed cached opens for all three recoveries.
1242 * Check if we need to update the current stateid.
1243 */
1244 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001245 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001246 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001247 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001248 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001249 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001250 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 return 0;
1252}
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/*
1255 * OPEN_RECLAIM:
1256 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001258static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001260 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001262 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int status;
1264
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001265 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1266 if (IS_ERR(opendata))
1267 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1269 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001270 rcu_read_lock();
1271 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001272 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001273 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001274 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 opendata->o_arg.u.delegation_type = delegation_type;
1276 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return status;
1279}
1280
Trond Myklebust539cd032007-06-05 11:46:42 -04001281static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct nfs_server *server = NFS_SERVER(state->inode);
1284 struct nfs4_exception exception = { };
1285 int err;
1286 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001287 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001288 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001289 break;
1290 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 } while (exception.retry);
1292 return err;
1293}
1294
Trond Myklebust864472e2006-01-03 09:55:15 +01001295static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1296{
1297 struct nfs_open_context *ctx;
1298 int ret;
1299
1300 ctx = nfs4_state_find_open_context(state);
1301 if (IS_ERR(ctx))
1302 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001303 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 put_nfs_open_context(ctx);
1305 return ret;
1306}
1307
Trond Myklebust13437e12007-07-06 15:10:43 -04001308static 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 -07001309{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001311 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001313 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1314 if (IS_ERR(opendata))
1315 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001316 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001317 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001319 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Trond Myklebust13437e12007-07-06 15:10:43 -04001323int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001326 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int err;
1328 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001329 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 switch (err) {
1331 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001332 case -ENOENT:
1333 case -ESTALE:
1334 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001335 case -NFS4ERR_BADSESSION:
1336 case -NFS4ERR_BADSLOT:
1337 case -NFS4ERR_BAD_HIGH_SLOT:
1338 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1339 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001340 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 case -NFS4ERR_STALE_CLIENTID:
1343 case -NFS4ERR_STALE_STATEID:
1344 case -NFS4ERR_EXPIRED:
1345 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001346 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347 goto out;
1348 case -ERESTARTSYS:
1349 /*
1350 * The show must go on: exit, but mark the
1351 * stateid as needing recovery.
1352 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001353 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001354 case -NFS4ERR_ADMIN_REVOKED:
1355 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001356 nfs_inode_find_state_and_recover(state->inode,
1357 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001358 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001359 case -EKEYEXPIRED:
1360 /*
1361 * User RPCSEC_GSS context has expired.
1362 * We cannot recover this stateid now, so
1363 * skip it and allow recovery thread to
1364 * proceed.
1365 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001366 case -ENOMEM:
1367 err = 0;
1368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370 err = nfs4_handle_exception(server, err, &exception);
1371 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001372out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return err;
1374}
1375
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001376static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1377{
1378 struct nfs4_opendata *data = calldata;
1379
1380 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001381 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001382 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001383 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001384 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001385 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001386 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001387}
1388
1389static void nfs4_open_confirm_release(void *calldata)
1390{
1391 struct nfs4_opendata *data = calldata;
1392 struct nfs4_state *state = NULL;
1393
1394 /* If this request hasn't been cancelled, do nothing */
1395 if (data->cancelled == 0)
1396 goto out_free;
1397 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001398 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001401 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001402 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001404 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405}
1406
1407static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001408 .rpc_call_done = nfs4_open_confirm_done,
1409 .rpc_release = nfs4_open_confirm_release,
1410};
1411
1412/*
1413 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1414 */
1415static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1416{
1417 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1418 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001419 struct rpc_message msg = {
1420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1421 .rpc_argp = &data->c_arg,
1422 .rpc_resp = &data->c_res,
1423 .rpc_cred = data->owner->so_cred,
1424 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001425 struct rpc_task_setup task_setup_data = {
1426 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001427 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001428 .callback_ops = &nfs4_open_confirm_ops,
1429 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001430 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001431 .flags = RPC_TASK_ASYNC,
1432 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int status;
1434
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001435 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001436 data->rpc_done = 0;
1437 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001438 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001440 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001441 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001442 status = nfs4_wait_for_completion_rpc_task(task);
1443 if (status != 0) {
1444 data->cancelled = 1;
1445 smp_wmb();
1446 } else
1447 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001448 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return status;
1450}
1451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454 struct nfs4_opendata *data = calldata;
1455 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1458 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001459 /*
1460 * Check if we still need to send an OPEN call, or if we can use
1461 * a delegation instead.
1462 */
1463 if (data->state != NULL) {
1464 struct nfs_delegation *delegation;
1465
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001466 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001467 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001468 rcu_read_lock();
1469 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001470 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1471 can_open_delegated(delegation, data->o_arg.fmode))
1472 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001473 rcu_read_unlock();
1474 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001475 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001476 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001477 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001478 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001479 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001480 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1481 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001482 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001483 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001484 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001485 &data->o_res.seq_res,
1486 task) != 0)
1487 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001488 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001489unlock_no_action:
1490 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001491out_no_action:
1492 task->tk_action = NULL;
1493
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001496static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1497{
1498 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1499 nfs4_open_prepare(task, calldata);
1500}
1501
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001502static void nfs4_open_done(struct rpc_task *task, void *calldata)
1503{
1504 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001507
Trond Myklebust14516c32010-07-31 14:29:06 -04001508 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1509 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001510
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001512 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1513 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001514 case S_IFREG:
1515 break;
1516 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001518 break;
1519 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001521 break;
1522 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001524 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001525 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001526 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001527 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1528 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001529 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001530 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001532
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533static void nfs4_open_release(void *calldata)
1534{
1535 struct nfs4_opendata *data = calldata;
1536 struct nfs4_state *state = NULL;
1537
1538 /* If this request hasn't been cancelled, do nothing */
1539 if (data->cancelled == 0)
1540 goto out_free;
1541 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001542 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543 goto out_free;
1544 /* In case we need an open_confirm, no cleanup! */
1545 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1546 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001548 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001549 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001551 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552}
1553
1554static const struct rpc_call_ops nfs4_open_ops = {
1555 .rpc_call_prepare = nfs4_open_prepare,
1556 .rpc_call_done = nfs4_open_done,
1557 .rpc_release = nfs4_open_release,
1558};
1559
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001560static const struct rpc_call_ops nfs4_recover_open_ops = {
1561 .rpc_call_prepare = nfs4_recover_open_prepare,
1562 .rpc_call_done = nfs4_open_done,
1563 .rpc_release = nfs4_open_release,
1564};
1565
1566static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567{
1568 struct inode *dir = data->dir->d_inode;
1569 struct nfs_server *server = NFS_SERVER(dir);
1570 struct nfs_openargs *o_arg = &data->o_arg;
1571 struct nfs_openres *o_res = &data->o_res;
1572 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001573 struct rpc_message msg = {
1574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1575 .rpc_argp = o_arg,
1576 .rpc_resp = o_res,
1577 .rpc_cred = data->owner->so_cred,
1578 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001579 struct rpc_task_setup task_setup_data = {
1580 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001581 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001582 .callback_ops = &nfs4_open_ops,
1583 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001584 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001585 .flags = RPC_TASK_ASYNC,
1586 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001587 int status;
1588
Trond Myklebust9d12b212012-01-17 22:04:25 -05001589 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001590 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001591 data->rpc_done = 0;
1592 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001593 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001594 if (isrecover)
1595 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001596 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001597 if (IS_ERR(task))
1598 return PTR_ERR(task);
1599 status = nfs4_wait_for_completion_rpc_task(task);
1600 if (status != 0) {
1601 data->cancelled = 1;
1602 smp_wmb();
1603 } else
1604 status = data->rpc_status;
1605 rpc_put_task(task);
1606
1607 return status;
1608}
1609
1610static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1611{
1612 struct inode *dir = data->dir->d_inode;
1613 struct nfs_openres *o_res = &data->o_res;
1614 int status;
1615
1616 status = nfs4_run_open_task(data, 1);
1617 if (status != 0 || !data->rpc_done)
1618 return status;
1619
Trond Myklebust6926afd12012-01-07 13:22:46 -05001620 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1621
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001622 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1623 status = _nfs4_proc_open_confirm(data);
1624 if (status != 0)
1625 return status;
1626 }
1627
1628 return status;
1629}
1630
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001631static int nfs4_opendata_access(struct rpc_cred *cred,
1632 struct nfs4_opendata *opendata,
1633 struct nfs4_state *state, fmode_t fmode)
1634{
1635 struct nfs_access_entry cache;
1636 u32 mask;
1637
1638 /* access call failed or for some reason the server doesn't
1639 * support any access modes -- defer access call until later */
1640 if (opendata->o_res.access_supported == 0)
1641 return 0;
1642
1643 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001644 /* don't check MAY_WRITE - a newly created file may not have
1645 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001646 if (fmode & FMODE_READ)
1647 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001648 if (fmode & FMODE_EXEC)
1649 mask |= MAY_EXEC;
1650
1651 cache.cred = cred;
1652 cache.jiffies = jiffies;
1653 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1654 nfs_access_add_cache(state->inode, &cache);
1655
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001656 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001657 return 0;
1658
1659 /* even though OPEN succeeded, access is denied. Close the file */
1660 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001661 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001662}
1663
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001664/*
1665 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1666 */
1667static int _nfs4_proc_open(struct nfs4_opendata *data)
1668{
1669 struct inode *dir = data->dir->d_inode;
1670 struct nfs_server *server = NFS_SERVER(dir);
1671 struct nfs_openargs *o_arg = &data->o_arg;
1672 struct nfs_openres *o_res = &data->o_res;
1673 int status;
1674
1675 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001676 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001677 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001678 if (status != 0) {
1679 if (status == -NFS4ERR_BADNAME &&
1680 !(o_arg->open_flags & O_CREAT))
1681 return -ENOENT;
1682 return status;
1683 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684
Trond Myklebust6926afd12012-01-07 13:22:46 -05001685 nfs_fattr_map_and_free_names(server, &data->f_attr);
1686
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001687 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001688 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001689 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1690 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001692 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001697 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Andy Adamsond83217c2011-03-01 01:34:17 +00001701static int nfs4_recover_expired_lease(struct nfs_server *server)
1702{
1703 return nfs4_client_recover_expired_lease(server->nfs_client);
1704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * OPEN_EXPIRED:
1708 * reclaim state on the server after a network partition.
1709 * Assumes caller holds the appropriate lock
1710 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001711static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001713 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001716 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1717 if (IS_ERR(opendata))
1718 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001720 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001721 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001722 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001726static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727{
Trond Myklebust539cd032007-06-05 11:46:42 -04001728 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729 struct nfs4_exception exception = { };
1730 int err;
1731
1732 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001733 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001734 switch (err) {
1735 default:
1736 goto out;
1737 case -NFS4ERR_GRACE:
1738 case -NFS4ERR_DELAY:
1739 nfs4_handle_exception(server, err, &exception);
1740 err = 0;
1741 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001742 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001743out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001744 return err;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001750 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 ctx = nfs4_state_find_open_context(state);
1753 if (IS_ERR(ctx))
1754 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001755 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001756 put_nfs_open_context(ctx);
1757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001761static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001764 nfs4_stateid *stateid = &state->stateid;
1765 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001766
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001767 /* If a state reset has been done, test_stateid is unneeded */
1768 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1769 return;
1770
1771 status = nfs41_test_stateid(server, stateid);
1772 if (status != NFS_OK) {
1773 /* Free the stateid unless the server explicitly
1774 * informs us the stateid is unrecognized. */
1775 if (status != -NFS4ERR_BAD_STATEID)
1776 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001777 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001778
Bryan Schumaker69388672012-09-26 15:25:52 -04001779 write_seqlock(&state->seqlock);
1780 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1781 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001782 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1783 }
1784}
1785
1786/**
1787 * nfs41_check_open_stateid - possibly free an open stateid
1788 *
1789 * @state: NFSv4 state for an inode
1790 *
1791 * Returns NFS_OK if recovery for this stateid is now finished.
1792 * Otherwise a negative NFS4ERR value is returned.
1793 */
1794static int nfs41_check_open_stateid(struct nfs4_state *state)
1795{
1796 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001797 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001798 int status;
1799
1800 /* If a state reset has been done, test_stateid is unneeded */
1801 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1802 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1803 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1804 return -NFS4ERR_BAD_STATEID;
1805
1806 status = nfs41_test_stateid(server, stateid);
1807 if (status != NFS_OK) {
1808 /* Free the stateid unless the server explicitly
1809 * informs us the stateid is unrecognized. */
1810 if (status != -NFS4ERR_BAD_STATEID)
1811 nfs41_free_stateid(server, stateid);
1812
1813 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1814 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1815 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001816 }
1817 return status;
1818}
1819
1820static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1821{
Chuck Levereb64cf92012-07-11 16:30:05 -04001822 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001823
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001824 nfs41_clear_delegation_stateid(state);
1825 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001826 if (status != NFS_OK)
1827 status = nfs4_open_expired(sp, state);
1828 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001829}
1830#endif
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001833 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1834 * fields corresponding to attributes that were used to store the verifier.
1835 * Make sure we clobber those fields in the later setattr call
1836 */
1837static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1838{
1839 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1840 !(sattr->ia_valid & ATTR_ATIME_SET))
1841 sattr->ia_valid |= ATTR_ATIME;
1842
1843 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1844 !(sattr->ia_valid & ATTR_MTIME_SET))
1845 sattr->ia_valid |= ATTR_MTIME;
1846}
1847
1848/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001849 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 */
Andy Adamson82be4172012-05-23 05:02:35 -04001851static int _nfs4_do_open(struct inode *dir,
1852 struct dentry *dentry,
1853 fmode_t fmode,
1854 int flags,
1855 struct iattr *sattr,
1856 struct rpc_cred *cred,
1857 struct nfs4_state **res,
1858 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 struct nfs4_state_owner *sp;
1861 struct nfs4_state *state = NULL;
1862 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001863 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001864 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001868 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1869 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1871 goto out_err;
1872 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001873 status = nfs4_recover_expired_lease(server);
1874 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001875 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001876 if (dentry->d_inode != NULL)
1877 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001878 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001879 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001880 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001881 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Andy Adamson82be4172012-05-23 05:02:35 -04001883 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1884 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1885 if (!opendata->f_attr.mdsthreshold)
1886 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001887 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001888 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001889 if (dentry->d_inode != NULL)
1890 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001891
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001892 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001894 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001896 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001897 status = PTR_ERR(state);
1898 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001899 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001900 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001901 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001902
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001903 status = nfs4_opendata_access(cred, opendata, state, fmode);
1904 if (status != 0)
1905 goto err_opendata_put;
1906
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001907 if (opendata->o_arg.open_flags & O_EXCL) {
1908 nfs4_exclusive_attrset(opendata, sattr);
1909
1910 nfs_fattr_init(opendata->o_res.f_attr);
1911 status = nfs4_do_setattr(state->inode, cred,
1912 opendata->o_res.f_attr, sattr,
1913 state);
1914 if (status == 0)
1915 nfs_setattr_update_inode(state->inode, sattr);
1916 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1917 }
Andy Adamson82be4172012-05-23 05:02:35 -04001918
1919 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1920 *ctx_th = opendata->f_attr.mdsthreshold;
1921 else
1922 kfree(opendata->f_attr.mdsthreshold);
1923 opendata->f_attr.mdsthreshold = NULL;
1924
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001925 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *res = state;
1928 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001929err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001930 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001931 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001932err_put_state_owner:
1933 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 *res = NULL;
1936 return status;
1937}
1938
1939
Andy Adamson82be4172012-05-23 05:02:35 -04001940static struct nfs4_state *nfs4_do_open(struct inode *dir,
1941 struct dentry *dentry,
1942 fmode_t fmode,
1943 int flags,
1944 struct iattr *sattr,
1945 struct rpc_cred *cred,
1946 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct nfs4_exception exception = { };
1949 struct nfs4_state *res;
1950 int status;
1951
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001952 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001954 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1955 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (status == 0)
1957 break;
1958 /* NOTE: BAD_SEQID means the server and client disagree about the
1959 * book-keeping w.r.t. state-changing operations
1960 * (OPEN/CLOSE/LOCK/LOCKU...)
1961 * It is actually a sign of a bug on the client or on the server.
1962 *
1963 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001964 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 * have unhashed the old state_owner for us, and that we can
1966 * therefore safely retry using a new one. We should still warn
1967 * the user though...
1968 */
1969 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001970 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001971 " returned a bad sequence-id error!\n",
1972 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 exception.retry = 1;
1974 continue;
1975 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001976 /*
1977 * BAD_STATEID on OPEN means that the server cancelled our
1978 * state before it received the OPEN_CONFIRM.
1979 * Recover by retrying the request as per the discussion
1980 * on Page 181 of RFC3530.
1981 */
1982 if (status == -NFS4ERR_BAD_STATEID) {
1983 exception.retry = 1;
1984 continue;
1985 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001986 if (status == -EAGAIN) {
1987 /* We must have found a delegation */
1988 exception.retry = 1;
1989 continue;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1992 status, &exception));
1993 } while (exception.retry);
1994 return res;
1995}
1996
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001997static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1998 struct nfs_fattr *fattr, struct iattr *sattr,
1999 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002001 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002003 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 .iap = sattr,
2005 .server = server,
2006 .bitmask = server->attr_bitmask,
2007 };
2008 struct nfs_setattrres res = {
2009 .fattr = fattr,
2010 .server = server,
2011 };
2012 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2014 .rpc_argp = &arg,
2015 .rpc_resp = &res,
2016 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002018 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002019 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Trond Myklebust0e574af2005-10-27 22:12:38 -04002021 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Trond Myklebust4fc87962012-03-08 17:42:01 -05002023 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002024 struct nfs_lockowner lockowner = {
2025 .l_owner = current->files,
2026 .l_pid = current->tgid,
2027 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002028 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002029 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002030 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2031 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002032 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002033 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002034 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Bryan Schumaker7c513052011-03-24 17:12:24 +00002036 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002037 if (status == 0 && state != NULL)
2038 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002039 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002042static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2043 struct nfs_fattr *fattr, struct iattr *sattr,
2044 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002046 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002047 struct nfs4_exception exception = {
2048 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002049 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002050 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int err;
2052 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002053 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2054 switch (err) {
2055 case -NFS4ERR_OPENMODE:
2056 if (state && !(state->state & FMODE_WRITE)) {
2057 err = -EBADF;
2058 if (sattr->ia_valid & ATTR_OPEN)
2059 err = -EACCES;
2060 goto out;
2061 }
2062 }
2063 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002065out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return err;
2067}
2068
2069struct nfs4_closedata {
2070 struct inode *inode;
2071 struct nfs4_state *state;
2072 struct nfs_closeargs arg;
2073 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002074 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002075 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002076 bool roc;
2077 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078};
2079
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002080static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002081{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002082 struct nfs4_closedata *calldata = data;
2083 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002084 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002085
Fred Isamanf7e89172011-01-06 11:36:32 +00002086 if (calldata->roc)
2087 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002088 nfs4_put_open_state(calldata->state);
2089 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002090 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002091 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002092 kfree(calldata);
2093}
2094
Trond Myklebust88069f72009-12-08 08:33:16 -05002095static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2096 fmode_t fmode)
2097{
2098 spin_lock(&state->owner->so_lock);
2099 if (!(fmode & FMODE_READ))
2100 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2101 if (!(fmode & FMODE_WRITE))
2102 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2103 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2104 spin_unlock(&state->owner->so_lock);
2105}
2106
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002107static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002109 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct nfs_server *server = NFS_SERVER(calldata->inode);
2112
Chuck Levera3ca5652012-03-01 17:00:40 -05002113 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002114 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* hmm. we are done with the inode, and in the process of freeing
2117 * the state_owner. we keep this around to process errors
2118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 switch (task->tk_status) {
2120 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002121 if (calldata->roc)
2122 pnfs_roc_set_barrier(state->inode,
2123 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002124 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002125 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002126 nfs4_close_clear_stateid_flags(state,
2127 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 break;
2129 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002130 case -NFS4ERR_OLD_STATEID:
2131 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002133 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002136 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2137 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002139 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002140 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002141 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002144static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002146 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002147 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002148 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002149 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002150
Chuck Levera3ca5652012-03-01 17:00:40 -05002151 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002152 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002153 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002154
Trond Myklebust88069f72009-12-08 08:33:16 -05002155 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2156 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002157 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002158 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002159 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002160 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002161 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2162 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2163 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002164 }
2165 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002166 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2167 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2168 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002169 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002170 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002171 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002172
2173 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002174 /* Note: exit _without_ calling nfs4_close_done */
2175 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002176 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002177 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002178
Fred Isamanf7e89172011-01-06 11:36:32 +00002179 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002180 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002181 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002182 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002183 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002184 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002185
Trond Myklebust516a6af2005-10-27 22:12:41 -04002186 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002187 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002188 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002189 &calldata->arg.seq_args,
2190 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002191 task) != 0)
2192 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002193out:
2194 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002197static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002198 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002199 .rpc_call_done = nfs4_close_done,
2200 .rpc_release = nfs4_free_closedata,
2201};
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203/*
2204 * It is possible for data to be read/written from a mem-mapped file
2205 * after the sys_close call (which hits the vfs layer as a flush).
2206 * This means that we can't safely call nfsv4 close on a file until
2207 * the inode is cleared. This in turn means that we are not good
2208 * NFSv4 citizens - we do not indicate to the server to update the file's
2209 * share state even when we are done with one of the three share
2210 * stateid's in the inode.
2211 *
2212 * NOTE: Caller must be holding the sp->so_owner semaphore!
2213 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002214int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002216 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002218 struct nfs4_state_owner *sp = state->owner;
2219 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002220 struct rpc_message msg = {
2221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2222 .rpc_cred = state->owner->so_cred,
2223 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002224 struct rpc_task_setup task_setup_data = {
2225 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002226 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002227 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002228 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002229 .flags = RPC_TASK_ASYNC,
2230 };
Trond Myklebust95121352005-10-18 14:20:12 -07002231 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002233 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002235 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002236 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002237 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002239 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002240 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002242 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002243 if (calldata->arg.seqid == NULL)
2244 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002245 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002246 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002247 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002248 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002249 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002250 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002251 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002252
Trond Myklebust1174dd12010-12-21 10:52:24 -05002253 msg.rpc_argp = &calldata->arg;
2254 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002255 task_setup_data.callback_data = calldata;
2256 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002257 if (IS_ERR(task))
2258 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002259 status = 0;
2260 if (wait)
2261 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002262 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002263 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002264out_free_calldata:
2265 kfree(calldata);
2266out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002267 nfs4_put_open_state(state);
2268 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002269 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Trond Myklebust2b484292010-09-17 10:56:51 -04002272static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002273nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 struct nfs4_state *state;
2276
Trond Myklebust565277f2007-10-15 18:17:53 -04002277 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002278 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2279 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002280 if (IS_ERR(state))
2281 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002282 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002283 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Trond Myklebust1185a552009-12-03 15:54:02 -05002286static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002287{
2288 if (ctx->state == NULL)
2289 return;
2290 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002291 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002292 else
Al Viro643168c2011-06-22 18:20:23 -04002293 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2297{
Benny Halevy43652ad2009-04-01 09:21:54 -04002298 struct nfs4_server_caps_arg args = {
2299 .fhandle = fhandle,
2300 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 struct nfs4_server_caps_res res = {};
2302 struct rpc_message msg = {
2303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002304 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 .rpc_resp = &res,
2306 };
2307 int status;
2308
Bryan Schumaker7c513052011-03-24 17:12:24 +00002309 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (status == 0) {
2311 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002312 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2313 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2314 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2315 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2316 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2318 server->caps |= NFS_CAP_ACLS;
2319 if (res.has_links != 0)
2320 server->caps |= NFS_CAP_HARDLINKS;
2321 if (res.has_symlinks != 0)
2322 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002323 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2324 server->caps |= NFS_CAP_FILEID;
2325 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2326 server->caps |= NFS_CAP_MODE;
2327 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2328 server->caps |= NFS_CAP_NLINK;
2329 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2330 server->caps |= NFS_CAP_OWNER;
2331 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2332 server->caps |= NFS_CAP_OWNER_GROUP;
2333 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2334 server->caps |= NFS_CAP_ATIME;
2335 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2336 server->caps |= NFS_CAP_CTIME;
2337 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2338 server->caps |= NFS_CAP_MTIME;
2339
Trond Myklebusta65318b2009-03-11 14:10:28 -04002340 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2341 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2342 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002344 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return status;
2348}
2349
Trond Myklebust55a97592006-06-09 09:34:19 -04002350int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 struct nfs4_exception exception = { };
2353 int err;
2354 do {
2355 err = nfs4_handle_exception(server,
2356 _nfs4_server_capabilities(server, fhandle),
2357 &exception);
2358 } while (exception.retry);
2359 return err;
2360}
2361
2362static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2363 struct nfs_fsinfo *info)
2364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct nfs4_lookup_root_arg args = {
2366 .bitmask = nfs4_fattr_bitmap,
2367 };
2368 struct nfs4_lookup_res res = {
2369 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002370 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 .fh = fhandle,
2372 };
2373 struct rpc_message msg = {
2374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2375 .rpc_argp = &args,
2376 .rpc_resp = &res,
2377 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002378
Trond Myklebust0e574af2005-10-27 22:12:38 -04002379 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002380 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
2383static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2384 struct nfs_fsinfo *info)
2385{
2386 struct nfs4_exception exception = { };
2387 int err;
2388 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002389 err = _nfs4_lookup_root(server, fhandle, info);
2390 switch (err) {
2391 case 0:
2392 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002393 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002394 default:
2395 err = nfs4_handle_exception(server, err, &exception);
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002398out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return err;
2400}
2401
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002402static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2403 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2404{
2405 struct rpc_auth *auth;
2406 int ret;
2407
2408 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002409 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002410 ret = -EIO;
2411 goto out;
2412 }
2413 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002414out:
2415 return ret;
2416}
2417
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002418static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002419 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002421 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002422 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002424 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002425 if (len < 0)
2426 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002427
2428 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002429 /* AUTH_UNIX is the default flavor if none was specified,
2430 * thus has already been tried. */
2431 if (flav_array[i] == RPC_AUTH_UNIX)
2432 continue;
2433
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002434 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002435 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002436 continue;
2437 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002438 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002439 /*
2440 * -EACCESS could mean that the user doesn't have correct permissions
2441 * to access the mount. It could also mean that we tried to mount
2442 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2443 * existing mount programs don't handle -EACCES very well so it should
2444 * be mapped to -EPERM instead.
2445 */
2446 if (status == -EACCES)
2447 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002448 return status;
2449}
2450
2451/*
2452 * get the file handle for the "/" directory on the server
2453 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002454int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2455 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002456{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002457 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002458 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002459 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2460 /*
2461 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2462 * by nfs4_map_errors() as this function exits.
2463 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002464 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (status == 0)
2466 status = nfs4_server_capabilities(server, fhandle);
2467 if (status == 0)
2468 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002469 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Bryan Schumakerbae36242012-05-10 15:07:31 -04002472static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2473 struct nfs_fsinfo *info)
2474{
2475 int error;
2476 struct nfs_fattr *fattr = info->fattr;
2477
2478 error = nfs4_server_capabilities(server, mntfh);
2479 if (error < 0) {
2480 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2481 return error;
2482 }
2483
2484 error = nfs4_proc_getattr(server, mntfh, fattr);
2485 if (error < 0) {
2486 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2487 return error;
2488 }
2489
2490 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2491 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2492 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2493
2494 return error;
2495}
2496
Manoj Naik6b97fd32006-06-09 09:34:29 -04002497/*
2498 * Get locations and (maybe) other attributes of a referral.
2499 * Note that we'll actually follow the referral later when
2500 * we detect fsid mismatch in inode revalidation
2501 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002502static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2503 const struct qstr *name, struct nfs_fattr *fattr,
2504 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002505{
2506 int status = -ENOMEM;
2507 struct page *page = NULL;
2508 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002509
2510 page = alloc_page(GFP_KERNEL);
2511 if (page == NULL)
2512 goto out;
2513 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2514 if (locations == NULL)
2515 goto out;
2516
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002517 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002518 if (status != 0)
2519 goto out;
2520 /* Make sure server returned a different fsid for the referral */
2521 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002522 dprintk("%s: server did not return a different fsid for"
2523 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002524 status = -EIO;
2525 goto out;
2526 }
Andy Adamson533eb462011-06-13 18:25:56 -04002527 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2528 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002529
Andy Adamson533eb462011-06-13 18:25:56 -04002530 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002531 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002532 memset(fhandle, 0, sizeof(struct nfs_fh));
2533out:
2534 if (page)
2535 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002536 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002537 return status;
2538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2541{
2542 struct nfs4_getattr_arg args = {
2543 .fh = fhandle,
2544 .bitmask = server->attr_bitmask,
2545 };
2546 struct nfs4_getattr_res res = {
2547 .fattr = fattr,
2548 .server = server,
2549 };
2550 struct rpc_message msg = {
2551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2552 .rpc_argp = &args,
2553 .rpc_resp = &res,
2554 };
2555
Trond Myklebust0e574af2005-10-27 22:12:38 -04002556 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002557 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
2560static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2561{
2562 struct nfs4_exception exception = { };
2563 int err;
2564 do {
2565 err = nfs4_handle_exception(server,
2566 _nfs4_proc_getattr(server, fhandle, fattr),
2567 &exception);
2568 } while (exception.retry);
2569 return err;
2570}
2571
2572/*
2573 * The file is not closed if it is opened due to the a request to change
2574 * the size of the file. The open call will not be needed once the
2575 * VFS layer lookup-intents are implemented.
2576 *
2577 * Close is called when the inode is destroyed.
2578 * If we haven't opened the file for O_WRONLY, we
2579 * need to in the size_change case to obtain a stateid.
2580 *
2581 * Got race?
2582 * Because OPEN is always done by name in nfsv4, it is
2583 * possible that we opened a different file by the same
2584 * name. We can recognize this race condition, but we
2585 * can't do anything about it besides returning an error.
2586 *
2587 * This will be fixed with VFS changes (lookup-intent).
2588 */
2589static int
2590nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2591 struct iattr *sattr)
2592{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002593 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002594 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002595 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 int status;
2597
Benny Halevy8a1636c2010-07-14 15:43:57 -04002598 if (pnfs_ld_layoutret_on_setattr(inode))
2599 pnfs_return_layout(inode);
2600
Trond Myklebust0e574af2005-10-27 22:12:38 -04002601 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Andy Adamson26699402012-05-30 16:12:24 -04002603 /* Deal with open(O_TRUNC) */
2604 if (sattr->ia_valid & ATTR_OPEN)
2605 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2606
2607 /* Optimization: if the end result is no change, don't RPC */
2608 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2609 return 0;
2610
Trond Myklebustd5308382005-11-04 15:33:38 -05002611 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002612 if (sattr->ia_valid & ATTR_FILE) {
2613 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002614
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002615 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002616 if (ctx) {
2617 cred = ctx->cred;
2618 state = ctx->state;
2619 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002620 }
2621
2622 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002623 if (status == 0)
2624 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return status;
2626}
2627
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002628static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2629 const struct qstr *name, struct nfs_fh *fhandle,
2630 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002631{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002632 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002633 int status;
2634 struct nfs4_lookup_arg args = {
2635 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002636 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002637 .name = name,
2638 };
2639 struct nfs4_lookup_res res = {
2640 .server = server,
2641 .fattr = fattr,
2642 .fh = fhandle,
2643 };
2644 struct rpc_message msg = {
2645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2646 .rpc_argp = &args,
2647 .rpc_resp = &res,
2648 };
2649
2650 nfs_fattr_init(fattr);
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002653 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 dprintk("NFS reply lookup: %d\n", status);
2655 return status;
2656}
2657
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002658static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002659{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002660 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002661 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002662 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2663 fattr->nlink = 2;
2664}
2665
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002666static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2667 struct qstr *name, struct nfs_fh *fhandle,
2668 struct nfs_fattr *fattr)
2669{
2670 struct nfs4_exception exception = { };
2671 struct rpc_clnt *client = *clnt;
2672 int err;
2673 do {
2674 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2675 switch (err) {
2676 case -NFS4ERR_BADNAME:
2677 err = -ENOENT;
2678 goto out;
2679 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002680 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002681 goto out;
2682 case -NFS4ERR_WRONGSEC:
2683 err = -EPERM;
2684 if (client != *clnt)
2685 goto out;
2686
2687 client = nfs4_create_sec_client(client, dir, name);
2688 if (IS_ERR(client))
2689 return PTR_ERR(client);
2690
2691 exception.retry = 1;
2692 break;
2693 default:
2694 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2695 }
2696 } while (exception.retry);
2697
2698out:
2699 if (err == 0)
2700 *clnt = client;
2701 else if (client != *clnt)
2702 rpc_shutdown_client(client);
2703
2704 return err;
2705}
2706
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002707static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002708 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002710 int status;
2711 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002712
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002713 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2714 if (client != NFS_CLIENT(dir)) {
2715 rpc_shutdown_client(client);
2716 nfs_fixup_secinfo_attributes(fattr);
2717 }
2718 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002721struct rpc_clnt *
2722nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2723 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2724{
2725 int status;
2726 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2727
2728 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2729 if (status < 0) {
2730 rpc_shutdown_client(client);
2731 return ERR_PTR(status);
2732 }
2733 return client;
2734}
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2737{
Trond Myklebust76b32992007-08-10 17:45:11 -04002738 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 struct nfs4_accessargs args = {
2740 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002741 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002743 struct nfs4_accessres res = {
2744 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002745 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 struct rpc_message msg = {
2747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2748 .rpc_argp = &args,
2749 .rpc_resp = &res,
2750 .rpc_cred = entry->cred,
2751 };
2752 int mode = entry->mask;
2753 int status;
2754
2755 /*
2756 * Determine which access bits we want to ask for...
2757 */
2758 if (mode & MAY_READ)
2759 args.access |= NFS4_ACCESS_READ;
2760 if (S_ISDIR(inode->i_mode)) {
2761 if (mode & MAY_WRITE)
2762 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2763 if (mode & MAY_EXEC)
2764 args.access |= NFS4_ACCESS_LOOKUP;
2765 } else {
2766 if (mode & MAY_WRITE)
2767 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2768 if (mode & MAY_EXEC)
2769 args.access |= NFS4_ACCESS_EXECUTE;
2770 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002771
2772 res.fattr = nfs_alloc_fattr();
2773 if (res.fattr == NULL)
2774 return -ENOMEM;
2775
Bryan Schumaker7c513052011-03-24 17:12:24 +00002776 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002778 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002779 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002781 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return status;
2783}
2784
2785static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2786{
2787 struct nfs4_exception exception = { };
2788 int err;
2789 do {
2790 err = nfs4_handle_exception(NFS_SERVER(inode),
2791 _nfs4_proc_access(inode, entry),
2792 &exception);
2793 } while (exception.retry);
2794 return err;
2795}
2796
2797/*
2798 * TODO: For the time being, we don't try to get any attributes
2799 * along with any of the zero-copy operations READ, READDIR,
2800 * READLINK, WRITE.
2801 *
2802 * In the case of the first three, we want to put the GETATTR
2803 * after the read-type operation -- this is because it is hard
2804 * to predict the length of a GETATTR response in v4, and thus
2805 * align the READ data correctly. This means that the GETATTR
2806 * may end up partially falling into the page cache, and we should
2807 * shift it into the 'tail' of the xdr_buf before processing.
2808 * To do this efficiently, we need to know the total length
2809 * of data received, which doesn't seem to be available outside
2810 * of the RPC layer.
2811 *
2812 * In the case of WRITE, we also want to put the GETATTR after
2813 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002814 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 *
2816 * Both of these changes to the XDR layer would in fact be quite
2817 * minor, but I decided to leave them for a subsequent patch.
2818 */
2819static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2820 unsigned int pgbase, unsigned int pglen)
2821{
2822 struct nfs4_readlink args = {
2823 .fh = NFS_FH(inode),
2824 .pgbase = pgbase,
2825 .pglen = pglen,
2826 .pages = &page,
2827 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002828 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2831 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002832 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 };
2834
Bryan Schumaker7c513052011-03-24 17:12:24 +00002835 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 -07002836}
2837
2838static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2839 unsigned int pgbase, unsigned int pglen)
2840{
2841 struct nfs4_exception exception = { };
2842 int err;
2843 do {
2844 err = nfs4_handle_exception(NFS_SERVER(inode),
2845 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2846 &exception);
2847 } while (exception.retry);
2848 return err;
2849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002852 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854static int
2855nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002858 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 int status = 0;
2861
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002862 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2863 if (IS_ERR(ctx))
2864 return PTR_ERR(ctx);
2865
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002866 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002867 state = nfs4_do_open(dir, dentry, ctx->mode,
2868 flags, sattr, ctx->cred,
2869 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002870 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (IS_ERR(state)) {
2872 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002875 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002876 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002877 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002879 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return status;
2881}
2882
2883static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2884{
Trond Myklebust16e42952005-10-27 22:12:44 -04002885 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002886 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002888 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002890 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002891 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2895 .rpc_argp = &args,
2896 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002898 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002899
Bryan Schumaker7c513052011-03-24 17:12:24 +00002900 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002901 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002902 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return status;
2904}
2905
2906static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2907{
2908 struct nfs4_exception exception = { };
2909 int err;
2910 do {
2911 err = nfs4_handle_exception(NFS_SERVER(dir),
2912 _nfs4_proc_remove(dir, name),
2913 &exception);
2914 } while (exception.retry);
2915 return err;
2916}
2917
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002918static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002920 struct nfs_server *server = NFS_SERVER(dir);
2921 struct nfs_removeargs *args = msg->rpc_argp;
2922 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002924 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002926 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002929static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2930{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002931 nfs4_setup_sequence(NFS_SERVER(data->dir),
2932 &data->args.seq_args,
2933 &data->res.seq_res,
2934 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002937static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002939 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2940
Trond Myklebust14516c32010-07-31 14:29:06 -04002941 if (!nfs4_sequence_done(task, &res->seq_res))
2942 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002943 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002944 return 0;
2945 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002946 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
Jeff Laytond3d41522010-09-17 17:31:57 -04002949static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2950{
2951 struct nfs_server *server = NFS_SERVER(dir);
2952 struct nfs_renameargs *arg = msg->rpc_argp;
2953 struct nfs_renameres *res = msg->rpc_resp;
2954
2955 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002956 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002957 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002958}
2959
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002960static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2961{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002962 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2963 &data->args.seq_args,
2964 &data->res.seq_res,
2965 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002966}
2967
2968static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2969 struct inode *new_dir)
2970{
2971 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2972
2973 if (!nfs4_sequence_done(task, &res->seq_res))
2974 return 0;
2975 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2976 return 0;
2977
2978 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002979 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002980 return 1;
2981}
2982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2984 struct inode *new_dir, struct qstr *new_name)
2985{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002986 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002987 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 .old_dir = NFS_FH(old_dir),
2989 .new_dir = NFS_FH(new_dir),
2990 .old_name = old_name,
2991 .new_name = new_name,
2992 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002993 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002994 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002995 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 struct rpc_message msg = {
2997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2998 .rpc_argp = &arg,
2999 .rpc_resp = &res,
3000 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003001 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Bryan Schumaker7c513052011-03-24 17:12:24 +00003003 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (!status) {
3005 update_changeattr(old_dir, &res.old_cinfo);
3006 update_changeattr(new_dir, &res.new_cinfo);
3007 }
3008 return status;
3009}
3010
3011static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3012 struct inode *new_dir, struct qstr *new_name)
3013{
3014 struct nfs4_exception exception = { };
3015 int err;
3016 do {
3017 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3018 _nfs4_proc_rename(old_dir, old_name,
3019 new_dir, new_name),
3020 &exception);
3021 } while (exception.retry);
3022 return err;
3023}
3024
3025static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3026{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003027 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 struct nfs4_link_arg arg = {
3029 .fh = NFS_FH(inode),
3030 .dir_fh = NFS_FH(dir),
3031 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003032 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003034 struct nfs4_link_res res = {
3035 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003036 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct rpc_message msg = {
3038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3039 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003040 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003042 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Trond Myklebust136f2622010-04-16 16:22:49 -04003044 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003045 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003046 goto out;
3047
Bryan Schumaker7c513052011-03-24 17:12:24 +00003048 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003049 if (!status) {
3050 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003051 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003052 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003053out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003054 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return status;
3056}
3057
3058static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3059{
3060 struct nfs4_exception exception = { };
3061 int err;
3062 do {
3063 err = nfs4_handle_exception(NFS_SERVER(inode),
3064 _nfs4_proc_link(inode, dir, name),
3065 &exception);
3066 } while (exception.retry);
3067 return err;
3068}
3069
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003070struct nfs4_createdata {
3071 struct rpc_message msg;
3072 struct nfs4_create_arg arg;
3073 struct nfs4_create_res res;
3074 struct nfs_fh fh;
3075 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003076};
3077
3078static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3079 struct qstr *name, struct iattr *sattr, u32 ftype)
3080{
3081 struct nfs4_createdata *data;
3082
3083 data = kzalloc(sizeof(*data), GFP_KERNEL);
3084 if (data != NULL) {
3085 struct nfs_server *server = NFS_SERVER(dir);
3086
3087 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3088 data->msg.rpc_argp = &data->arg;
3089 data->msg.rpc_resp = &data->res;
3090 data->arg.dir_fh = NFS_FH(dir);
3091 data->arg.server = server;
3092 data->arg.name = name;
3093 data->arg.attrs = sattr;
3094 data->arg.ftype = ftype;
3095 data->arg.bitmask = server->attr_bitmask;
3096 data->res.server = server;
3097 data->res.fh = &data->fh;
3098 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003100 }
3101 return data;
3102}
3103
3104static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3105{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003106 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003107 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003108 if (status == 0) {
3109 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3111 }
3112 return status;
3113}
3114
3115static void nfs4_free_createdata(struct nfs4_createdata *data)
3116{
3117 kfree(data);
3118}
3119
Chuck Lever4f390c12006-08-22 20:06:22 -04003120static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003121 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003123 struct nfs4_createdata *data;
3124 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Chuck Lever94a6d752006-08-22 20:06:23 -04003126 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003128
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003129 status = -ENOMEM;
3130 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3131 if (data == NULL)
3132 goto out;
3133
3134 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3135 data->arg.u.symlink.pages = &page;
3136 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003138 status = nfs4_do_create(dir, dentry, data);
3139
3140 nfs4_free_createdata(data);
3141out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return status;
3143}
3144
Chuck Lever4f390c12006-08-22 20:06:22 -04003145static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003146 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
3148 struct nfs4_exception exception = { };
3149 int err;
3150 do {
3151 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003152 _nfs4_proc_symlink(dir, dentry, page,
3153 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 &exception);
3155 } while (exception.retry);
3156 return err;
3157}
3158
3159static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3160 struct iattr *sattr)
3161{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003162 struct nfs4_createdata *data;
3163 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003165 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3166 if (data == NULL)
3167 goto out;
3168
3169 status = nfs4_do_create(dir, dentry, data);
3170
3171 nfs4_free_createdata(data);
3172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return status;
3174}
3175
3176static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3177 struct iattr *sattr)
3178{
3179 struct nfs4_exception exception = { };
3180 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003181
3182 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 do {
3184 err = nfs4_handle_exception(NFS_SERVER(dir),
3185 _nfs4_proc_mkdir(dir, dentry, sattr),
3186 &exception);
3187 } while (exception.retry);
3188 return err;
3189}
3190
3191static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003192 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 struct inode *dir = dentry->d_inode;
3195 struct nfs4_readdir_arg args = {
3196 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003197 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 .pgbase = 0,
3199 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003200 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003201 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 };
3203 struct nfs4_readdir_res res;
3204 struct rpc_message msg = {
3205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3206 .rpc_argp = &args,
3207 .rpc_resp = &res,
3208 .rpc_cred = cred,
3209 };
3210 int status;
3211
Harvey Harrison3110ff82008-05-02 13:42:44 -07003212 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003213 dentry->d_parent->d_name.name,
3214 dentry->d_name.name,
3215 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003216 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003218 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 -05003219 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003220 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003221 status += args.pgbase;
3222 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003223
3224 nfs_invalidate_atime(dir);
3225
Harvey Harrison3110ff82008-05-02 13:42:44 -07003226 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return status;
3228}
3229
3230static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003231 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
3233 struct nfs4_exception exception = { };
3234 int err;
3235 do {
3236 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3237 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003238 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 &exception);
3240 } while (exception.retry);
3241 return err;
3242}
3243
3244static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3245 struct iattr *sattr, dev_t rdev)
3246{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003247 struct nfs4_createdata *data;
3248 int mode = sattr->ia_mode;
3249 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3252 if (data == NULL)
3253 goto out;
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003256 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003258 data->arg.ftype = NF4BLK;
3259 data->arg.u.device.specdata1 = MAJOR(rdev);
3260 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
3262 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003263 data->arg.ftype = NF4CHR;
3264 data->arg.u.device.specdata1 = MAJOR(rdev);
3265 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003266 } else if (!S_ISSOCK(mode)) {
3267 status = -EINVAL;
3268 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003271 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003272out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003273 nfs4_free_createdata(data);
3274out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return status;
3276}
3277
3278static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3279 struct iattr *sattr, dev_t rdev)
3280{
3281 struct nfs4_exception exception = { };
3282 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003283
3284 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 do {
3286 err = nfs4_handle_exception(NFS_SERVER(dir),
3287 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3288 &exception);
3289 } while (exception.retry);
3290 return err;
3291}
3292
3293static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3294 struct nfs_fsstat *fsstat)
3295{
3296 struct nfs4_statfs_arg args = {
3297 .fh = fhandle,
3298 .bitmask = server->attr_bitmask,
3299 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003300 struct nfs4_statfs_res res = {
3301 .fsstat = fsstat,
3302 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 struct rpc_message msg = {
3304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3305 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003306 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 };
3308
Trond Myklebust0e574af2005-10-27 22:12:38 -04003309 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003310 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311}
3312
3313static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3314{
3315 struct nfs4_exception exception = { };
3316 int err;
3317 do {
3318 err = nfs4_handle_exception(server,
3319 _nfs4_proc_statfs(server, fhandle, fsstat),
3320 &exception);
3321 } while (exception.retry);
3322 return err;
3323}
3324
3325static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3326 struct nfs_fsinfo *fsinfo)
3327{
3328 struct nfs4_fsinfo_arg args = {
3329 .fh = fhandle,
3330 .bitmask = server->attr_bitmask,
3331 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003332 struct nfs4_fsinfo_res res = {
3333 .fsinfo = fsinfo,
3334 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 struct rpc_message msg = {
3336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3337 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003338 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 };
3340
Bryan Schumaker7c513052011-03-24 17:12:24 +00003341 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
3344static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3345{
3346 struct nfs4_exception exception = { };
3347 int err;
3348
3349 do {
3350 err = nfs4_handle_exception(server,
3351 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3352 &exception);
3353 } while (exception.retry);
3354 return err;
3355}
3356
3357static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3358{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003359 int error;
3360
Trond Myklebust0e574af2005-10-27 22:12:38 -04003361 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003362 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003363 if (error == 0) {
3364 /* block layout checks this! */
3365 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003366 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003367 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003368
3369 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
3372static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3373 struct nfs_pathconf *pathconf)
3374{
3375 struct nfs4_pathconf_arg args = {
3376 .fh = fhandle,
3377 .bitmask = server->attr_bitmask,
3378 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003379 struct nfs4_pathconf_res res = {
3380 .pathconf = pathconf,
3381 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 struct rpc_message msg = {
3383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3384 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003385 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 };
3387
3388 /* None of the pathconf attributes are mandatory to implement */
3389 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3390 memset(pathconf, 0, sizeof(*pathconf));
3391 return 0;
3392 }
3393
Trond Myklebust0e574af2005-10-27 22:12:38 -04003394 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003395 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396}
3397
3398static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3399 struct nfs_pathconf *pathconf)
3400{
3401 struct nfs4_exception exception = { };
3402 int err;
3403
3404 do {
3405 err = nfs4_handle_exception(server,
3406 _nfs4_proc_pathconf(server, fhandle, pathconf),
3407 &exception);
3408 } while (exception.retry);
3409 return err;
3410}
3411
Benny Halevyd20581a2011-05-22 19:52:03 +03003412void __nfs4_read_done_cb(struct nfs_read_data *data)
3413{
Fred Isamancd841602012-04-20 14:47:44 -04003414 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003415}
3416
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003417static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
Fred Isamancd841602012-04-20 14:47:44 -04003419 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003421 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003422 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003423 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003425
Benny Halevyd20581a2011-05-22 19:52:03 +03003426 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003428 renew_lease(server, data->timestamp);
3429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003432static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3433{
3434
3435 dprintk("--> %s\n", __func__);
3436
3437 if (!nfs4_sequence_done(task, &data->res.seq_res))
3438 return -EAGAIN;
3439
Benny Halevyd20581a2011-05-22 19:52:03 +03003440 return data->read_done_cb ? data->read_done_cb(task, data) :
3441 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003442}
3443
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003444static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003447 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003448 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003449 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450}
3451
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003452static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3453{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003454 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3455 &data->args.seq_args,
3456 &data->res.seq_res,
3457 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Fred Isamanb029bc92011-03-03 15:13:42 +00003460static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
Fred Isamancd841602012-04-20 14:47:44 -04003462 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003464 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003465 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003466 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003468 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003470 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003471 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
Fred Isamanb029bc92011-03-03 15:13:42 +00003475static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3476{
3477 if (!nfs4_sequence_done(task, &data->res.seq_res))
3478 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003479 return data->write_done_cb ? data->write_done_cb(task, data) :
3480 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003481}
3482
Trond Myklebust5a37f852012-04-28 14:55:16 -04003483static
3484bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003485{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003486 const struct nfs_pgio_header *hdr = data->header;
3487
3488 /* Don't request attributes for pNFS or O_DIRECT writes */
3489 if (data->ds_clp != NULL || hdr->dreq != NULL)
3490 return false;
3491 /* Otherwise, request attributes if and only if we don't hold
3492 * a delegation
3493 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003494 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003495}
Fred Isamana69aef12011-03-03 15:13:47 +00003496
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003497static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Fred Isamancd841602012-04-20 14:47:44 -04003499 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003500
Trond Myklebust5a37f852012-04-28 14:55:16 -04003501 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003502 data->args.bitmask = NULL;
3503 data->res.fattr = NULL;
3504 } else
3505 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003506
Fred Isamanb029bc92011-03-03 15:13:42 +00003507 if (!data->write_done_cb)
3508 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003509 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 data->timestamp = jiffies;
3511
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003512 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003513 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003516static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3517{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003518 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3519 &data->args.seq_args,
3520 &data->res.seq_res,
3521 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522}
3523
Fred Isaman0b7c0152012-04-20 14:47:39 -04003524static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3525{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003526 nfs4_setup_sequence(NFS_SERVER(data->inode),
3527 &data->args.seq_args,
3528 &data->res.seq_res,
3529 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530}
3531
Fred Isaman0b7c0152012-04-20 14:47:39 -04003532static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003535
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003536 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003537 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003538 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
Fred Isaman0b7c0152012-04-20 14:47:39 -04003543static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003544{
3545 if (!nfs4_sequence_done(task, &data->res.seq_res))
3546 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003547 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003548}
3549
Fred Isaman0b7c0152012-04-20 14:47:39 -04003550static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003552 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003553
Fred Isaman0b7c0152012-04-20 14:47:39 -04003554 if (data->commit_done_cb == NULL)
3555 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003556 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003557 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003558 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003561struct nfs4_renewdata {
3562 struct nfs_client *client;
3563 unsigned long timestamp;
3564};
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566/*
3567 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3568 * standalone procedure for queueing an asynchronous RENEW.
3569 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003570static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003571{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003572 struct nfs4_renewdata *data = calldata;
3573 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003574
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003575 if (atomic_read(&clp->cl_count) > 1)
3576 nfs4_schedule_state_renewal(clp);
3577 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003578 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003579}
3580
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003581static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003583 struct nfs4_renewdata *data = calldata;
3584 struct nfs_client *clp = data->client;
3585 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003588 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003589 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3590 return;
3591 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003592 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003593 return;
3594 }
3595 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003597 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003600static const struct rpc_call_ops nfs4_renew_ops = {
3601 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003602 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003603};
3604
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003605static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
3607 struct rpc_message msg = {
3608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3609 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003610 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003612 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003614 if (renew_flags == 0)
3615 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003616 if (!atomic_inc_not_zero(&clp->cl_count))
3617 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003618 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003619 if (data == NULL)
3620 return -ENOMEM;
3621 data->client = clp;
3622 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003624 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003627static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
3629 struct rpc_message msg = {
3630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3631 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003632 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 };
3634 unsigned long now = jiffies;
3635 int status;
3636
3637 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3638 if (status < 0)
3639 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003640 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 return 0;
3642}
3643
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003644static inline int nfs4_server_supports_acls(struct nfs_server *server)
3645{
3646 return (server->caps & NFS_CAP_ACLS)
3647 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3648 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3649}
3650
Trond Myklebust21f498c2012-08-24 10:59:25 -04003651/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3652 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003653 * the stack.
3654 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003655#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003656
Neil Hormane9e3d722011-03-04 19:26:03 -05003657static int buf_to_pages_noslab(const void *buf, size_t buflen,
3658 struct page **pages, unsigned int *pgbase)
3659{
3660 struct page *newpage, **spages;
3661 int rc = 0;
3662 size_t len;
3663 spages = pages;
3664
3665 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003666 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003667 newpage = alloc_page(GFP_KERNEL);
3668
3669 if (newpage == NULL)
3670 goto unwind;
3671 memcpy(page_address(newpage), buf, len);
3672 buf += len;
3673 buflen -= len;
3674 *pages++ = newpage;
3675 rc++;
3676 } while (buflen != 0);
3677
3678 return rc;
3679
3680unwind:
3681 for(; rc > 0; rc--)
3682 __free_page(spages[rc-1]);
3683 return -ENOMEM;
3684}
3685
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003686struct nfs4_cached_acl {
3687 int cached;
3688 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003689 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003690};
3691
3692static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003693{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003694 struct nfs_inode *nfsi = NFS_I(inode);
3695
3696 spin_lock(&inode->i_lock);
3697 kfree(nfsi->nfs4_acl);
3698 nfsi->nfs4_acl = acl;
3699 spin_unlock(&inode->i_lock);
3700}
3701
3702static void nfs4_zap_acl_attr(struct inode *inode)
3703{
3704 nfs4_set_cached_acl(inode, NULL);
3705}
3706
3707static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3708{
3709 struct nfs_inode *nfsi = NFS_I(inode);
3710 struct nfs4_cached_acl *acl;
3711 int ret = -ENOENT;
3712
3713 spin_lock(&inode->i_lock);
3714 acl = nfsi->nfs4_acl;
3715 if (acl == NULL)
3716 goto out;
3717 if (buf == NULL) /* user is just asking for length */
3718 goto out_len;
3719 if (acl->cached == 0)
3720 goto out;
3721 ret = -ERANGE; /* see getxattr(2) man page */
3722 if (acl->len > buflen)
3723 goto out;
3724 memcpy(buf, acl->data, acl->len);
3725out_len:
3726 ret = acl->len;
3727out:
3728 spin_unlock(&inode->i_lock);
3729 return ret;
3730}
3731
Sachin Prabhu5794d212012-04-17 14:36:40 +01003732static 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 +00003733{
3734 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003735 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003736
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003737 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003738 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003739 if (acl == NULL)
3740 goto out;
3741 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003742 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003743 } else {
3744 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3745 if (acl == NULL)
3746 goto out;
3747 acl->cached = 0;
3748 }
3749 acl->len = acl_len;
3750out:
3751 nfs4_set_cached_acl(inode, acl);
3752}
3753
Andy Adamsonbf118a32011-12-07 11:55:27 -05003754/*
3755 * The getxattr API returns the required buffer length when called with a
3756 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3757 * the required buf. On a NULL buf, we send a page of data to the server
3758 * guessing that the ACL request can be serviced by a page. If so, we cache
3759 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3760 * the cache. If not so, we throw away the page, and cache the required
3761 * length. The next getxattr call will then produce another round trip to
3762 * the server, this time with the input buf of the required size.
3763 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003764static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003765{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003767 struct nfs_getaclargs args = {
3768 .fh = NFS_FH(inode),
3769 .acl_pages = pages,
3770 .acl_len = buflen,
3771 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003772 struct nfs_getaclres res = {
3773 .acl_len = buflen,
3774 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003775 struct rpc_message msg = {
3776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3777 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003778 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003779 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003780 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3781 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003782
Andy Adamsonbf118a32011-12-07 11:55:27 -05003783 /* As long as we're doing a round trip to the server anyway,
3784 * let's be prepared for a page of acl data. */
3785 if (npages == 0)
3786 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003787 if (npages > ARRAY_SIZE(pages))
3788 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003789
Andy Adamsonbf118a32011-12-07 11:55:27 -05003790 for (i = 0; i < npages; i++) {
3791 pages[i] = alloc_page(GFP_KERNEL);
3792 if (!pages[i])
3793 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003794 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003795
3796 /* for decoding across pages */
3797 res.acl_scratch = alloc_page(GFP_KERNEL);
3798 if (!res.acl_scratch)
3799 goto out_free;
3800
Andy Adamsonbf118a32011-12-07 11:55:27 -05003801 args.acl_len = npages * PAGE_SIZE;
3802 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003803
Peng Taode040be2012-01-10 22:42:47 +08003804 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003805 __func__, buf, buflen, npages, args.acl_len);
3806 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3807 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003808 if (ret)
3809 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003810
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003811 /* Handle the case where the passed-in buffer is too short */
3812 if (res.acl_flags & NFS4_ACL_TRUNC) {
3813 /* Did the user only issue a request for the acl length? */
3814 if (buf == NULL)
3815 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003817 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003818 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003819 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3820 if (buf)
3821 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3822out_ok:
3823 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003824out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003825 for (i = 0; i < npages; i++)
3826 if (pages[i])
3827 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003828 if (res.acl_scratch)
3829 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003830 return ret;
3831}
3832
Trond Myklebust16b42892006-08-24 12:27:15 -04003833static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3834{
3835 struct nfs4_exception exception = { };
3836 ssize_t ret;
3837 do {
3838 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3839 if (ret >= 0)
3840 break;
3841 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3842 } while (exception.retry);
3843 return ret;
3844}
3845
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003846static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3847{
3848 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003849 int ret;
3850
3851 if (!nfs4_server_supports_acls(server))
3852 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003853 ret = nfs_revalidate_inode(server, inode);
3854 if (ret < 0)
3855 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003856 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3857 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003858 ret = nfs4_read_cached_acl(inode, buf, buflen);
3859 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003860 /* -ENOENT is returned if there is no ACL or if there is an ACL
3861 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862 return ret;
3863 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003864}
3865
Trond Myklebust16b42892006-08-24 12:27:15 -04003866static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003867{
3868 struct nfs_server *server = NFS_SERVER(inode);
3869 struct page *pages[NFS4ACL_MAXPAGES];
3870 struct nfs_setaclargs arg = {
3871 .fh = NFS_FH(inode),
3872 .acl_pages = pages,
3873 .acl_len = buflen,
3874 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003875 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003876 struct rpc_message msg = {
3877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3878 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003879 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003880 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003881 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003882 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003883
3884 if (!nfs4_server_supports_acls(server))
3885 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003886 if (npages > ARRAY_SIZE(pages))
3887 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003888 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3889 if (i < 0)
3890 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003891 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003892 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003893
3894 /*
3895 * Free each page after tx, so the only ref left is
3896 * held by the network stack
3897 */
3898 for (; i > 0; i--)
3899 put_page(pages[i-1]);
3900
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003901 /*
3902 * Acl update can result in inode attribute update.
3903 * so mark the attribute cache invalid.
3904 */
3905 spin_lock(&inode->i_lock);
3906 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3907 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003908 nfs_access_zap_cache(inode);
3909 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003910 return ret;
3911}
3912
Trond Myklebust16b42892006-08-24 12:27:15 -04003913static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3914{
3915 struct nfs4_exception exception = { };
3916 int err;
3917 do {
3918 err = nfs4_handle_exception(NFS_SERVER(inode),
3919 __nfs4_proc_set_acl(inode, buf, buflen),
3920 &exception);
3921 } while (exception.retry);
3922 return err;
3923}
3924
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003926nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003928 struct nfs_client *clp = server->nfs_client;
3929
3930 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return 0;
3932 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003933 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003934 case -NFS4ERR_ADMIN_REVOKED:
3935 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003936 if (state == NULL)
3937 break;
3938 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003939 case -NFS4ERR_OPENMODE:
3940 if (state == NULL)
3941 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003942 nfs4_schedule_stateid_recovery(server, state);
3943 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003944 case -NFS4ERR_EXPIRED:
3945 if (state != NULL)
3946 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003948 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003949 nfs4_schedule_lease_recovery(clp);
3950 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003951#if defined(CONFIG_NFS_V4_1)
3952 case -NFS4ERR_BADSESSION:
3953 case -NFS4ERR_BADSLOT:
3954 case -NFS4ERR_BAD_HIGH_SLOT:
3955 case -NFS4ERR_DEADSESSION:
3956 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3957 case -NFS4ERR_SEQ_FALSE_RETRY:
3958 case -NFS4ERR_SEQ_MISORDERED:
3959 dprintk("%s ERROR %d, Reset session\n", __func__,
3960 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003961 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003962 task->tk_status = 0;
3963 return -EAGAIN;
3964#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003966 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003967 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003968 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3970 task->tk_status = 0;
3971 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003972 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 case -NFS4ERR_OLD_STATEID:
3974 task->tk_status = 0;
3975 return -EAGAIN;
3976 }
3977 task->tk_status = nfs4_map_errors(task->tk_status);
3978 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003979wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003980 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003981 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3982 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3983 task->tk_status = 0;
3984 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985}
3986
Chuck Leverf0920752012-05-21 22:45:41 -04003987static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3988 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003989{
3990 __be32 verf[2];
3991
Chuck Lever2c820d92012-05-21 22:45:33 -04003992 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3993 /* An impossible timestamp guarantees this value
3994 * will never match a generated boot time. */
3995 verf[0] = 0;
3996 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3997 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003998 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3999 verf[0] = (__be32)nn->boot_time.tv_sec;
4000 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004001 }
Chuck Levercd937102012-03-02 17:14:31 -05004002 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4003}
4004
Chuck Levere984a552012-09-14 17:24:21 -04004005static unsigned int
4006nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4007 char *buf, size_t len)
4008{
4009 unsigned int result;
4010
4011 rcu_read_lock();
4012 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4013 clp->cl_ipaddr,
4014 rpc_peeraddr2str(clp->cl_rpcclient,
4015 RPC_DISPLAY_ADDR),
4016 rpc_peeraddr2str(clp->cl_rpcclient,
4017 RPC_DISPLAY_PROTO));
4018 rcu_read_unlock();
4019 return result;
4020}
4021
4022static unsigned int
4023nfs4_init_uniform_client_string(const struct nfs_client *clp,
4024 char *buf, size_t len)
4025{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004026 char *nodename = clp->cl_rpcclient->cl_nodename;
4027
4028 if (nfs4_client_id_uniquifier[0] != '\0')
4029 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004030 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4031 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004032 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004033}
4034
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004035/**
4036 * nfs4_proc_setclientid - Negotiate client ID
4037 * @clp: state data structure
4038 * @program: RPC program for NFSv4 callback service
4039 * @port: IP port number for NFS4 callback service
4040 * @cred: RPC credential to use for this call
4041 * @res: where to place the result
4042 *
4043 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4044 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004045int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4046 unsigned short port, struct rpc_cred *cred,
4047 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
4049 nfs4_verifier sc_verifier;
4050 struct nfs4_setclientid setclientid = {
4051 .sc_verifier = &sc_verifier,
4052 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004053 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 };
4055 struct rpc_message msg = {
4056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4057 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004058 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004059 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004061 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Chuck Leverde734832012-07-11 16:30:50 -04004063 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004064 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004065 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4066 setclientid.sc_name_len =
4067 nfs4_init_uniform_client_string(clp,
4068 setclientid.sc_name,
4069 sizeof(setclientid.sc_name));
4070 else
4071 setclientid.sc_name_len =
4072 nfs4_init_nonuniform_client_string(clp,
4073 setclientid.sc_name,
4074 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004075 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004076 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004077 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004078 sizeof(setclientid.sc_netid),
4079 rpc_peeraddr2str(clp->cl_rpcclient,
4080 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004081 rcu_read_unlock();
4082 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004083 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 clp->cl_ipaddr, port >> 8, port & 255);
4085
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004086 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4087 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4088 setclientid.sc_name_len, setclientid.sc_name);
4089 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4090 dprintk("NFS reply setclientid: %d\n", status);
4091 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092}
4093
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004094/**
4095 * nfs4_proc_setclientid_confirm - Confirm client ID
4096 * @clp: state data structure
4097 * @res: result of a previous SETCLIENTID
4098 * @cred: RPC credential to use for this call
4099 *
4100 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4101 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004102int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004103 struct nfs4_setclientid_res *arg,
4104 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
4106 struct nfs_fsinfo fsinfo;
4107 struct rpc_message msg = {
4108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004109 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004111 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 };
4113 unsigned long now;
4114 int status;
4115
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004116 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4117 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4118 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004120 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 if (status == 0) {
4122 spin_lock(&clp->cl_lock);
4123 clp->cl_lease_time = fsinfo.lease_time * HZ;
4124 clp->cl_last_renewal = now;
4125 spin_unlock(&clp->cl_lock);
4126 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004127 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 return status;
4129}
4130
Trond Myklebustfe650402006-01-03 09:55:18 +01004131struct nfs4_delegreturndata {
4132 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004133 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004134 struct nfs_fh fh;
4135 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004136 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004137 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004138 int rpc_status;
4139};
4140
Trond Myklebustfe650402006-01-03 09:55:18 +01004141static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4142{
4143 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004144
Trond Myklebust14516c32010-07-31 14:29:06 -04004145 if (!nfs4_sequence_done(task, &data->res.seq_res))
4146 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004147
Ricardo Labiaga79708862009-12-07 09:23:21 -05004148 switch (task->tk_status) {
4149 case -NFS4ERR_STALE_STATEID:
4150 case -NFS4ERR_EXPIRED:
4151 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004152 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004153 break;
4154 default:
4155 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4156 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004157 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004158 return;
4159 }
4160 }
4161 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004162}
4163
4164static void nfs4_delegreturn_release(void *calldata)
4165{
Trond Myklebustfe650402006-01-03 09:55:18 +01004166 kfree(calldata);
4167}
4168
Andy Adamson938e1012009-04-01 09:22:28 -04004169#if defined(CONFIG_NFS_V4_1)
4170static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4171{
4172 struct nfs4_delegreturndata *d_data;
4173
4174 d_data = (struct nfs4_delegreturndata *)data;
4175
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004176 nfs4_setup_sequence(d_data->res.server,
4177 &d_data->args.seq_args,
4178 &d_data->res.seq_res,
4179 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004180}
4181#endif /* CONFIG_NFS_V4_1 */
4182
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004183static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004184#if defined(CONFIG_NFS_V4_1)
4185 .rpc_call_prepare = nfs4_delegreturn_prepare,
4186#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004187 .rpc_call_done = nfs4_delegreturn_done,
4188 .rpc_release = nfs4_delegreturn_release,
4189};
4190
Trond Myklebuste6f81072008-01-24 18:14:34 -05004191static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004192{
4193 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004194 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004195 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004196 struct rpc_message msg = {
4197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4198 .rpc_cred = cred,
4199 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004200 struct rpc_task_setup task_setup_data = {
4201 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004202 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004203 .callback_ops = &nfs4_delegreturn_ops,
4204 .flags = RPC_TASK_ASYNC,
4205 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004206 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004207
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004208 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004209 if (data == NULL)
4210 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004211 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004212 data->args.fhandle = &data->fh;
4213 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004214 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004215 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004216 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004217 data->res.fattr = &data->fattr;
4218 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004219 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004220 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004221 data->rpc_status = 0;
4222
Trond Myklebustc970aa82007-07-14 15:39:59 -04004223 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004224 msg.rpc_argp = &data->args;
4225 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004226 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004227 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004228 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004229 if (!issync)
4230 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004231 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004232 if (status != 0)
4233 goto out;
4234 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004235 if (status == 0)
4236 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4237 else
4238 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004239out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004240 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004241 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
Trond Myklebuste6f81072008-01-24 18:14:34 -05004244int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
4246 struct nfs_server *server = NFS_SERVER(inode);
4247 struct nfs4_exception exception = { };
4248 int err;
4249 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004250 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 switch (err) {
4252 case -NFS4ERR_STALE_STATEID:
4253 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 case 0:
4255 return 0;
4256 }
4257 err = nfs4_handle_exception(server, err, &exception);
4258 } while (exception.retry);
4259 return err;
4260}
4261
4262#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4263#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4264
4265/*
4266 * sleep, with exponential backoff, and retry the LOCK operation.
4267 */
4268static unsigned long
4269nfs4_set_lock_task_retry(unsigned long timeout)
4270{
Jeff Laytond3103102011-12-01 22:44:39 +01004271 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 timeout <<= 1;
4273 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4274 return NFS4_LOCK_MAXTIMEOUT;
4275 return timeout;
4276}
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4279{
4280 struct inode *inode = state->inode;
4281 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004282 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004283 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004285 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004287 struct nfs_lockt_res res = {
4288 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 };
4290 struct rpc_message msg = {
4291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4292 .rpc_argp = &arg,
4293 .rpc_resp = &res,
4294 .rpc_cred = state->owner->so_cred,
4295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct nfs4_lock_state *lsp;
4297 int status;
4298
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004299 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004300 status = nfs4_set_lock_state(state, request);
4301 if (status != 0)
4302 goto out;
4303 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004304 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004305 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004306 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004307 switch (status) {
4308 case 0:
4309 request->fl_type = F_UNLCK;
4310 break;
4311 case -NFS4ERR_DENIED:
4312 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004314 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004315out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return status;
4317}
4318
4319static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4320{
4321 struct nfs4_exception exception = { };
4322 int err;
4323
4324 do {
4325 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4326 _nfs4_proc_getlk(state, cmd, request),
4327 &exception);
4328 } while (exception.retry);
4329 return err;
4330}
4331
4332static int do_vfs_lock(struct file *file, struct file_lock *fl)
4333{
4334 int res = 0;
4335 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4336 case FL_POSIX:
4337 res = posix_lock_file_wait(file, fl);
4338 break;
4339 case FL_FLOCK:
4340 res = flock_lock_file_wait(file, fl);
4341 break;
4342 default:
4343 BUG();
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return res;
4346}
4347
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004348struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004349 struct nfs_locku_args arg;
4350 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004351 struct nfs4_lock_state *lsp;
4352 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004353 struct file_lock fl;
4354 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004355 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004356};
4357
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004358static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4359 struct nfs_open_context *ctx,
4360 struct nfs4_lock_state *lsp,
4361 struct nfs_seqid *seqid)
4362{
4363 struct nfs4_unlockdata *p;
4364 struct inode *inode = lsp->ls_state->inode;
4365
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004366 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004367 if (p == NULL)
4368 return NULL;
4369 p->arg.fh = NFS_FH(inode);
4370 p->arg.fl = &p->fl;
4371 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004372 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004373 p->arg.stateid = &lsp->ls_stateid;
4374 p->lsp = lsp;
4375 atomic_inc(&lsp->ls_count);
4376 /* Ensure we don't close file until we're done freeing locks! */
4377 p->ctx = get_nfs_open_context(ctx);
4378 memcpy(&p->fl, fl, sizeof(p->fl));
4379 p->server = NFS_SERVER(inode);
4380 return p;
4381}
4382
Trond Myklebust06f814a2006-01-03 09:55:07 +01004383static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004385 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004386 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004387 nfs4_put_lock_state(calldata->lsp);
4388 put_nfs_open_context(calldata->ctx);
4389 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004390}
4391
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004392static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004394 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004395
Trond Myklebust14516c32010-07-31 14:29:06 -04004396 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4397 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398 switch (task->tk_status) {
4399 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004400 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4401 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004402 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004404 case -NFS4ERR_BAD_STATEID:
4405 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 case -NFS4ERR_STALE_STATEID:
4407 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004408 break;
4409 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004410 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004411 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004412 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004413 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004414}
4415
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004416static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004417{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004418 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004420 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004421 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004422 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004423 /* Note: exit _without_ running nfs4_locku_done */
4424 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004425 return;
4426 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004427 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004428 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004429 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004430 &calldata->res.seq_res,
4431 task) != 0)
4432 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433}
4434
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004435static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004436 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004437 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004438 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004439};
4440
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004441static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4442 struct nfs_open_context *ctx,
4443 struct nfs4_lock_state *lsp,
4444 struct nfs_seqid *seqid)
4445{
4446 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004447 struct rpc_message msg = {
4448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4449 .rpc_cred = ctx->cred,
4450 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004451 struct rpc_task_setup task_setup_data = {
4452 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004453 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004454 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004455 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004456 .flags = RPC_TASK_ASYNC,
4457 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004458
Frank Filz137d6ac2007-07-09 15:32:29 -07004459 /* Ensure this is an unlock - when canceling a lock, the
4460 * canceled lock is passed in, and it won't be an unlock.
4461 */
4462 fl->fl_type = F_UNLCK;
4463
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004464 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4465 if (data == NULL) {
4466 nfs_free_seqid(seqid);
4467 return ERR_PTR(-ENOMEM);
4468 }
4469
Trond Myklebust9d12b212012-01-17 22:04:25 -05004470 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004471 msg.rpc_argp = &data->arg;
4472 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004473 task_setup_data.callback_data = data;
4474 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475}
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4478{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004479 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004480 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004481 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004482 struct rpc_task *task;
4483 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004484 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
Trond Myklebust9b073572006-06-29 16:38:34 -04004486 status = nfs4_set_lock_state(state, request);
4487 /* Unlock _before_ we do the RPC call */
4488 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004489 down_read(&nfsi->rwsem);
4490 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4491 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004492 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004493 }
4494 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004495 if (status != 0)
4496 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004497 /* Is this a delegated lock? */
4498 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004499 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004500 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004501 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004502 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004503 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004504 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004505 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004506 status = PTR_ERR(task);
4507 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004508 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004510 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004511out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004512 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004513 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004516struct nfs4_lockdata {
4517 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004518 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519 struct nfs4_lock_state *lsp;
4520 struct nfs_open_context *ctx;
4521 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004522 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523 int rpc_status;
4524 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004525 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004526};
4527
4528static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004529 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4530 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004531{
4532 struct nfs4_lockdata *p;
4533 struct inode *inode = lsp->ls_state->inode;
4534 struct nfs_server *server = NFS_SERVER(inode);
4535
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004536 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004537 if (p == NULL)
4538 return NULL;
4539
4540 p->arg.fh = NFS_FH(inode);
4541 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004542 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004543 if (p->arg.open_seqid == NULL)
4544 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004545 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004546 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004547 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004548 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004549 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004550 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004551 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004552 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004553 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004554 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004555 atomic_inc(&lsp->ls_count);
4556 p->ctx = get_nfs_open_context(ctx);
4557 memcpy(&p->fl, fl, sizeof(p->fl));
4558 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004559out_free_seqid:
4560 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561out_free:
4562 kfree(p);
4563 return NULL;
4564}
4565
4566static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4567{
4568 struct nfs4_lockdata *data = calldata;
4569 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Harvey Harrison3110ff82008-05-02 13:42:44 -07004571 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004572 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4573 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004574 /* Do we need to do an open_to_lock_owner? */
4575 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004576 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004577 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578 data->arg.open_stateid = &state->stateid;
4579 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004580 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004581 } else
4582 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004583 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004584 if (nfs4_setup_sequence(data->server,
4585 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004586 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004587 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004588 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004589 nfs_release_seqid(data->arg.open_seqid);
4590out_release_lock_seqid:
4591 nfs_release_seqid(data->arg.lock_seqid);
4592 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593}
4594
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004595static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4596{
4597 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4598 nfs4_lock_prepare(task, calldata);
4599}
4600
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004601static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4602{
4603 struct nfs4_lockdata *data = calldata;
4604
Harvey Harrison3110ff82008-05-02 13:42:44 -07004605 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606
Trond Myklebust14516c32010-07-31 14:29:06 -04004607 if (!nfs4_sequence_done(task, &data->res.seq_res))
4608 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004609
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004611 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612 if (data->rpc_status == 0)
4613 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4614 else
4615 goto out;
4616 }
4617 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004618 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004619 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004620 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004623 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004624}
4625
4626static void nfs4_lock_release(void *calldata)
4627{
4628 struct nfs4_lockdata *data = calldata;
4629
Harvey Harrison3110ff82008-05-02 13:42:44 -07004630 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004631 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632 if (data->cancelled != 0) {
4633 struct rpc_task *task;
4634 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4635 data->arg.lock_seqid);
4636 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004637 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004638 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004639 } else
4640 nfs_free_seqid(data->arg.lock_seqid);
4641 nfs4_put_lock_state(data->lsp);
4642 put_nfs_open_context(data->ctx);
4643 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004644 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004645}
4646
4647static const struct rpc_call_ops nfs4_lock_ops = {
4648 .rpc_call_prepare = nfs4_lock_prepare,
4649 .rpc_call_done = nfs4_lock_done,
4650 .rpc_release = nfs4_lock_release,
4651};
4652
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004653static const struct rpc_call_ops nfs4_recover_lock_ops = {
4654 .rpc_call_prepare = nfs4_recover_lock_prepare,
4655 .rpc_call_done = nfs4_lock_done,
4656 .rpc_release = nfs4_lock_release,
4657};
4658
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004659static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4660{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004661 switch (error) {
4662 case -NFS4ERR_ADMIN_REVOKED:
4663 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004664 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004665 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004666 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004667 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004668 break;
4669 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004670 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004671 case -NFS4ERR_EXPIRED:
4672 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004673 };
4674}
4675
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004676static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004677{
4678 struct nfs4_lockdata *data;
4679 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004680 struct rpc_message msg = {
4681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4682 .rpc_cred = state->owner->so_cred,
4683 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004684 struct rpc_task_setup task_setup_data = {
4685 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004686 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004687 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004688 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004689 .flags = RPC_TASK_ASYNC,
4690 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004691 int ret;
4692
Harvey Harrison3110ff82008-05-02 13:42:44 -07004693 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004694 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004695 fl->fl_u.nfs4_fl.owner,
4696 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004697 if (data == NULL)
4698 return -ENOMEM;
4699 if (IS_SETLKW(cmd))
4700 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004701 if (recovery_type > NFS_LOCK_NEW) {
4702 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004703 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004704 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4705 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004706 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004707 msg.rpc_argp = &data->arg;
4708 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004709 task_setup_data.callback_data = data;
4710 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004711 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004712 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004713 ret = nfs4_wait_for_completion_rpc_task(task);
4714 if (ret == 0) {
4715 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004716 if (ret)
4717 nfs4_handle_setlk_error(data->server, data->lsp,
4718 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004719 } else
4720 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004721 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004722 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725
4726static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4727{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004728 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004729 struct nfs4_exception exception = {
4730 .inode = state->inode,
4731 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004732 int err;
4733
4734 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004735 /* Cache the lock if possible... */
4736 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4737 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004738 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004739 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004740 break;
4741 nfs4_handle_exception(server, err, &exception);
4742 } while (exception.retry);
4743 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744}
4745
4746static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4747{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004748 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004749 struct nfs4_exception exception = {
4750 .inode = state->inode,
4751 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004752 int err;
4753
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004754 err = nfs4_set_lock_state(state, request);
4755 if (err != 0)
4756 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004757 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004758 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4759 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004760 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004761 switch (err) {
4762 default:
4763 goto out;
4764 case -NFS4ERR_GRACE:
4765 case -NFS4ERR_DELAY:
4766 nfs4_handle_exception(server, err, &exception);
4767 err = 0;
4768 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004769 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004770out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772}
4773
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004774#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004775/**
4776 * nfs41_check_expired_locks - possibly free a lock stateid
4777 *
4778 * @state: NFSv4 state for an inode
4779 *
4780 * Returns NFS_OK if recovery for this stateid is now finished.
4781 * Otherwise a negative NFS4ERR value is returned.
4782 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004783static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004784{
Chuck Levereb64cf92012-07-11 16:30:05 -04004785 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004786 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004787 struct nfs_server *server = NFS_SERVER(state->inode);
4788
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004789 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004790 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004791 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4792 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004793 /* Free the stateid unless the server
4794 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004795 if (status != -NFS4ERR_BAD_STATEID)
4796 nfs41_free_stateid(server,
4797 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004798 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004799 ret = status;
4800 }
4801 }
4802 };
4803
4804 return ret;
4805}
4806
4807static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4808{
4809 int status = NFS_OK;
4810
4811 if (test_bit(LK_STATE_IN_USE, &state->flags))
4812 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004813 if (status != NFS_OK)
4814 status = nfs4_lock_expired(state, request);
4815 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004816}
4817#endif
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4820{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004821 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004822 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004823 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004825 if ((fl_flags & FL_POSIX) &&
4826 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4827 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004828 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004829 status = nfs4_set_lock_state(state, request);
4830 if (status != 0)
4831 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004832 request->fl_flags |= FL_ACCESS;
4833 status = do_vfs_lock(request->fl_file, request);
4834 if (status < 0)
4835 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004836 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004837 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004838 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004839 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004840 request->fl_flags = fl_flags & ~FL_SLEEP;
4841 status = do_vfs_lock(request->fl_file, request);
4842 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004843 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004844 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004845 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004846 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004847 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004848 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004849 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004850 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4851 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004852out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004853 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004854out:
4855 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 return status;
4857}
4858
4859static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4860{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004861 struct nfs4_exception exception = {
4862 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004863 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004864 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 int err;
4866
4867 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004868 err = _nfs4_proc_setlk(state, cmd, request);
4869 if (err == -NFS4ERR_DENIED)
4870 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004872 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 } while (exception.retry);
4874 return err;
4875}
4876
4877static int
4878nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4879{
4880 struct nfs_open_context *ctx;
4881 struct nfs4_state *state;
4882 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4883 int status;
4884
4885 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004886 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 state = ctx->state;
4888
4889 if (request->fl_start < 0 || request->fl_end < 0)
4890 return -EINVAL;
4891
Trond Myklebustd9531262009-07-21 19:22:38 -04004892 if (IS_GETLK(cmd)) {
4893 if (state != NULL)
4894 return nfs4_proc_getlk(state, F_GETLK, request);
4895 return 0;
4896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4899 return -EINVAL;
4900
Trond Myklebustd9531262009-07-21 19:22:38 -04004901 if (request->fl_type == F_UNLCK) {
4902 if (state != NULL)
4903 return nfs4_proc_unlck(state, cmd, request);
4904 return 0;
4905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Trond Myklebustd9531262009-07-21 19:22:38 -04004907 if (state == NULL)
4908 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004909 /*
4910 * Don't rely on the VFS having checked the file open mode,
4911 * since it won't do this for flock() locks.
4912 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004913 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004914 case F_RDLCK:
4915 if (!(filp->f_mode & FMODE_READ))
4916 return -EBADF;
4917 break;
4918 case F_WRLCK:
4919 if (!(filp->f_mode & FMODE_WRITE))
4920 return -EBADF;
4921 }
4922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 do {
4924 status = nfs4_proc_setlk(state, cmd, request);
4925 if ((status != -EAGAIN) || IS_SETLK(cmd))
4926 break;
4927 timeout = nfs4_set_lock_task_retry(timeout);
4928 status = -ERESTARTSYS;
4929 if (signalled())
4930 break;
4931 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 return status;
4933}
4934
Trond Myklebust888e6942005-11-04 15:38:11 -05004935int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4936{
4937 struct nfs_server *server = NFS_SERVER(state->inode);
4938 struct nfs4_exception exception = { };
4939 int err;
4940
4941 err = nfs4_set_lock_state(state, fl);
4942 if (err != 0)
4943 goto out;
4944 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004945 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004946 switch (err) {
4947 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004948 printk(KERN_ERR "NFS: %s: unhandled error "
4949 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004950 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004951 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004952 goto out;
4953 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004954 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004955 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004956 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004957 nfs4_schedule_lease_recovery(server->nfs_client);
4958 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004959 case -NFS4ERR_BADSESSION:
4960 case -NFS4ERR_BADSLOT:
4961 case -NFS4ERR_BAD_HIGH_SLOT:
4962 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4963 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004964 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004965 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004966 case -ERESTARTSYS:
4967 /*
4968 * The show must go on: exit, but mark the
4969 * stateid as needing recovery.
4970 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004971 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004972 case -NFS4ERR_ADMIN_REVOKED:
4973 case -NFS4ERR_BAD_STATEID:
4974 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004975 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004976 err = 0;
4977 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004978 case -EKEYEXPIRED:
4979 /*
4980 * User RPCSEC_GSS context has expired.
4981 * We cannot recover this stateid now, so
4982 * skip it and allow recovery thread to
4983 * proceed.
4984 */
4985 err = 0;
4986 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004987 case -ENOMEM:
4988 case -NFS4ERR_DENIED:
4989 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4990 err = 0;
4991 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004992 case -NFS4ERR_DELAY:
4993 break;
4994 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004995 err = nfs4_handle_exception(server, err, &exception);
4996 } while (exception.retry);
4997out:
4998 return err;
4999}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005000
Trond Myklebustcf470c32012-03-07 13:49:12 -05005001struct nfs_release_lockowner_data {
5002 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005003 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005004 struct nfs_release_lockowner_args args;
5005};
5006
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005007static void nfs4_release_lockowner_release(void *calldata)
5008{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005009 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005010 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005011 kfree(calldata);
5012}
5013
Trond Myklebust17280172012-03-11 13:11:00 -04005014static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005015 .rpc_release = nfs4_release_lockowner_release,
5016};
5017
Trond Myklebustcf470c32012-03-07 13:49:12 -05005018int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005019{
5020 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005021 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005022 struct rpc_message msg = {
5023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5024 };
5025
5026 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005027 return -EINVAL;
5028 data = kmalloc(sizeof(*data), GFP_NOFS);
5029 if (!data)
5030 return -ENOMEM;
5031 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005032 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005033 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5034 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5035 data->args.lock_owner.s_dev = server->s_dev;
5036 msg.rpc_argp = &data->args;
5037 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5038 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005039}
5040
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005041#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5042
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005043static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5044 const void *buf, size_t buflen,
5045 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005046{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005047 if (strcmp(key, "") != 0)
5048 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005049
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005050 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005051}
5052
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005053static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5054 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005055{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005056 if (strcmp(key, "") != 0)
5057 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005058
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005059 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005060}
5061
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005062static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5063 size_t list_len, const char *name,
5064 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005065{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005066 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005067
J. Bruce Fields096455a2006-03-20 23:23:42 -05005068 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5069 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005070
5071 if (list && len <= list_len)
5072 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005073 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005074}
5075
Andy Adamson533eb462011-06-13 18:25:56 -04005076/*
5077 * nfs_fhget will use either the mounted_on_fileid or the fileid
5078 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5080{
Andy Adamson533eb462011-06-13 18:25:56 -04005081 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5082 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5083 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005084 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005085 return;
5086
5087 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005088 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005089 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5090 fattr->nlink = 2;
5091}
5092
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005093static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5094 const struct qstr *name,
5095 struct nfs4_fs_locations *fs_locations,
5096 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005097{
5098 struct nfs_server *server = NFS_SERVER(dir);
5099 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005100 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005101 };
5102 struct nfs4_fs_locations_arg args = {
5103 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005104 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005105 .page = page,
5106 .bitmask = bitmask,
5107 };
Benny Halevy22958462009-04-01 09:22:02 -04005108 struct nfs4_fs_locations_res res = {
5109 .fs_locations = fs_locations,
5110 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005111 struct rpc_message msg = {
5112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5113 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005114 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005115 };
5116 int status;
5117
Harvey Harrison3110ff82008-05-02 13:42:44 -07005118 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005119
5120 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5121 * is not supported */
5122 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5123 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5124 else
5125 bitmask[0] |= FATTR4_WORD0_FILEID;
5126
Trond Myklebustc228fd32007-01-13 02:28:11 -05005127 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005128 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005129 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005130 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005131 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005132 return status;
5133}
5134
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005135int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5136 const struct qstr *name,
5137 struct nfs4_fs_locations *fs_locations,
5138 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005139{
5140 struct nfs4_exception exception = { };
5141 int err;
5142 do {
5143 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005144 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005145 &exception);
5146 } while (exception.retry);
5147 return err;
5148}
5149
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005150static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5151{
5152 int status;
5153 struct nfs4_secinfo_arg args = {
5154 .dir_fh = NFS_FH(dir),
5155 .name = name,
5156 };
5157 struct nfs4_secinfo_res res = {
5158 .flavors = flavors,
5159 };
5160 struct rpc_message msg = {
5161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5162 .rpc_argp = &args,
5163 .rpc_resp = &res,
5164 };
5165
5166 dprintk("NFS call secinfo %s\n", name->name);
5167 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5168 dprintk("NFS reply secinfo: %d\n", status);
5169 return status;
5170}
5171
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005172int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5173 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005174{
5175 struct nfs4_exception exception = { };
5176 int err;
5177 do {
5178 err = nfs4_handle_exception(NFS_SERVER(dir),
5179 _nfs4_proc_secinfo(dir, name, flavors),
5180 &exception);
5181 } while (exception.retry);
5182 return err;
5183}
5184
Andy Adamson557134a2009-04-01 09:21:53 -04005185#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005186/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005187 * Check the exchange flags returned by the server for invalid flags, having
5188 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5189 * DS flags set.
5190 */
5191static int nfs4_check_cl_exchange_flags(u32 flags)
5192{
5193 if (flags & ~EXCHGID4_FLAG_MASK_R)
5194 goto out_inval;
5195 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5196 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5197 goto out_inval;
5198 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5199 goto out_inval;
5200 return NFS_OK;
5201out_inval:
5202 return -NFS4ERR_INVAL;
5203}
5204
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005205static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005206nfs41_same_server_scope(struct nfs41_server_scope *a,
5207 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005208{
5209 if (a->server_scope_sz == b->server_scope_sz &&
5210 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5211 return true;
5212
5213 return false;
5214}
5215
Andy Adamson357f54d2010-12-14 10:11:57 -05005216/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005217 * nfs4_proc_bind_conn_to_session()
5218 *
5219 * The 4.1 client currently uses the same TCP connection for the
5220 * fore and backchannel.
5221 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005222int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005223{
5224 int status;
5225 struct nfs41_bind_conn_to_session_res res;
5226 struct rpc_message msg = {
5227 .rpc_proc =
5228 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5229 .rpc_argp = clp,
5230 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005231 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005232 };
5233
5234 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005235
5236 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5237 if (unlikely(res.session == NULL)) {
5238 status = -ENOMEM;
5239 goto out;
5240 }
5241
5242 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5243 if (status == 0) {
5244 if (memcmp(res.session->sess_id.data,
5245 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5246 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5247 status = -EIO;
5248 goto out_session;
5249 }
5250 if (res.dir != NFS4_CDFS4_BOTH) {
5251 dprintk("NFS: %s: Unexpected direction from server\n",
5252 __func__);
5253 status = -EIO;
5254 goto out_session;
5255 }
5256 if (res.use_conn_in_rdma_mode) {
5257 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5258 __func__);
5259 status = -EIO;
5260 goto out_session;
5261 }
5262 }
5263out_session:
5264 kfree(res.session);
5265out:
5266 dprintk("<-- %s status= %d\n", __func__, status);
5267 return status;
5268}
5269
5270/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005271 * nfs4_proc_exchange_id()
5272 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005273 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5274 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005275 * Since the clientid has expired, all compounds using sessions
5276 * associated with the stale clientid will be returning
5277 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5278 * be in some phase of session reset.
5279 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005280int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005281{
5282 nfs4_verifier verifier;
5283 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005284 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005285 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005286 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005287 };
5288 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005289 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005290 };
5291 int status;
5292 struct rpc_message msg = {
5293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5294 .rpc_argp = &args,
5295 .rpc_resp = &res,
5296 .rpc_cred = cred,
5297 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005298
Chuck Leverf0920752012-05-21 22:45:41 -04005299 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005300 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5301 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005302 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5304 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005305
Chuck Leveracdeb692012-05-21 22:46:16 -04005306 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005307 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005308 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005309 status = -ENOMEM;
5310 goto out;
5311 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005312
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005313 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005314 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005315 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005316 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005317 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005318 }
5319
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005320 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005321 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005322 status = -ENOMEM;
5323 goto out_server_scope;
5324 }
5325
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005326 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005327 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005328 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005329
Chuck Lever177313f2012-05-21 22:44:58 -04005330 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005331 clp->cl_clientid = res.clientid;
5332 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5333 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5334 clp->cl_seqid = res.seqid;
5335
Chuck Leveracdeb692012-05-21 22:46:16 -04005336 kfree(clp->cl_serverowner);
5337 clp->cl_serverowner = res.server_owner;
5338 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005339
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005340 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005341 kfree(clp->cl_implid);
5342 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005343
Chuck Lever177313f2012-05-21 22:44:58 -04005344 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005345 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005346 res.server_scope)) {
5347 dprintk("%s: server_scope mismatch detected\n",
5348 __func__);
5349 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005350 kfree(clp->cl_serverscope);
5351 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005352 }
5353
Chuck Lever177313f2012-05-21 22:44:58 -04005354 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005355 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005356 goto out;
5357 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005358 } else
5359 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005360
Chuck Leveracdeb692012-05-21 22:46:16 -04005361out_server_owner:
5362 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005363out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005364 kfree(res.server_scope);
5365out:
Chuck Lever177313f2012-05-21 22:44:58 -04005366 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005367 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005368 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005369 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005370 clp->cl_implid->date.seconds,
5371 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005372 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005373 return status;
5374}
5375
Trond Myklebust66245532012-05-25 17:18:09 -04005376static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5377 struct rpc_cred *cred)
5378{
5379 struct rpc_message msg = {
5380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5381 .rpc_argp = clp,
5382 .rpc_cred = cred,
5383 };
5384 int status;
5385
5386 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5387 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005388 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005389 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5390 return status;
5391}
5392
5393static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5394 struct rpc_cred *cred)
5395{
5396 unsigned int loop;
5397 int ret;
5398
5399 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5400 ret = _nfs4_proc_destroy_clientid(clp, cred);
5401 switch (ret) {
5402 case -NFS4ERR_DELAY:
5403 case -NFS4ERR_CLIENTID_BUSY:
5404 ssleep(1);
5405 break;
5406 default:
5407 return ret;
5408 }
5409 }
5410 return 0;
5411}
5412
5413int nfs4_destroy_clientid(struct nfs_client *clp)
5414{
5415 struct rpc_cred *cred;
5416 int ret = 0;
5417
5418 if (clp->cl_mvops->minor_version < 1)
5419 goto out;
5420 if (clp->cl_exchange_flags == 0)
5421 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005422 if (clp->cl_preserve_clid)
5423 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005424 cred = nfs4_get_exchange_id_cred(clp);
5425 ret = nfs4_proc_destroy_clientid(clp, cred);
5426 if (cred)
5427 put_rpccred(cred);
5428 switch (ret) {
5429 case 0:
5430 case -NFS4ERR_STALE_CLIENTID:
5431 clp->cl_exchange_flags = 0;
5432 }
5433out:
5434 return ret;
5435}
5436
Andy Adamson2050f0c2009-04-01 09:22:30 -04005437struct nfs4_get_lease_time_data {
5438 struct nfs4_get_lease_time_args *args;
5439 struct nfs4_get_lease_time_res *res;
5440 struct nfs_client *clp;
5441};
5442
5443static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5444 void *calldata)
5445{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005446 struct nfs4_get_lease_time_data *data =
5447 (struct nfs4_get_lease_time_data *)calldata;
5448
5449 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005450 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005451 /* just setup sequence, do not trigger session recovery
5452 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005453 nfs41_setup_sequence(data->clp->cl_session,
5454 &data->args->la_seq_args,
5455 &data->res->lr_seq_res,
5456 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005457 dprintk("<-- %s\n", __func__);
5458}
5459
5460/*
5461 * Called from nfs4_state_manager thread for session setup, so don't recover
5462 * from sequence operation or clientid errors.
5463 */
5464static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5465{
5466 struct nfs4_get_lease_time_data *data =
5467 (struct nfs4_get_lease_time_data *)calldata;
5468
5469 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005470 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5471 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005472 switch (task->tk_status) {
5473 case -NFS4ERR_DELAY:
5474 case -NFS4ERR_GRACE:
5475 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5476 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5477 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005478 /* fall through */
5479 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005480 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005481 return;
5482 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005483 dprintk("<-- %s\n", __func__);
5484}
5485
Trond Myklebust17280172012-03-11 13:11:00 -04005486static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005487 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5488 .rpc_call_done = nfs4_get_lease_time_done,
5489};
5490
5491int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5492{
5493 struct rpc_task *task;
5494 struct nfs4_get_lease_time_args args;
5495 struct nfs4_get_lease_time_res res = {
5496 .lr_fsinfo = fsinfo,
5497 };
5498 struct nfs4_get_lease_time_data data = {
5499 .args = &args,
5500 .res = &res,
5501 .clp = clp,
5502 };
5503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5505 .rpc_argp = &args,
5506 .rpc_resp = &res,
5507 };
5508 struct rpc_task_setup task_setup = {
5509 .rpc_client = clp->cl_rpcclient,
5510 .rpc_message = &msg,
5511 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005512 .callback_data = &data,
5513 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005514 };
5515 int status;
5516
Trond Myklebust9d12b212012-01-17 22:04:25 -05005517 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005518 dprintk("--> %s\n", __func__);
5519 task = rpc_run_task(&task_setup);
5520
5521 if (IS_ERR(task))
5522 status = PTR_ERR(task);
5523 else {
5524 status = task->tk_status;
5525 rpc_put_task(task);
5526 }
5527 dprintk("<-- %s return %d\n", __func__, status);
5528
5529 return status;
5530}
5531
Andy Adamsonfc931582009-04-01 09:22:31 -04005532/*
5533 * Initialize the values to be used by the client in CREATE_SESSION
5534 * If nfs4_init_session set the fore channel request and response sizes,
5535 * use them.
5536 *
5537 * Set the back channel max_resp_sz_cached to zero to force the client to
5538 * always set csa_cachethis to FALSE because the current implementation
5539 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5540 */
5541static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5542{
5543 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005544 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5545 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005546
5547 if (mxrqst_sz == 0)
5548 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5549 if (mxresp_sz == 0)
5550 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5551 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005552 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5553 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005554 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005555 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005556
5557 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005558 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005559 __func__,
5560 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005561 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005562
5563 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005564 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5565 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5566 args->bc_attrs.max_resp_sz_cached = 0;
5567 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5568 args->bc_attrs.max_reqs = 1;
5569
5570 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5571 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5572 __func__,
5573 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5574 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5575 args->bc_attrs.max_reqs);
5576}
5577
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005578static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005579{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005580 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5581 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5582
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005583 if (rcvd->max_resp_sz > sent->max_resp_sz)
5584 return -EINVAL;
5585 /*
5586 * Our requested max_ops is the minimum we need; we're not
5587 * prepared to break up compounds into smaller pieces than that.
5588 * So, no point even trying to continue if the server won't
5589 * cooperate:
5590 */
5591 if (rcvd->max_ops < sent->max_ops)
5592 return -EINVAL;
5593 if (rcvd->max_reqs == 0)
5594 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005595 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5596 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005597 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005598}
5599
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005600static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5601{
5602 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5603 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005604
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005605 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5606 return -EINVAL;
5607 if (rcvd->max_resp_sz < sent->max_resp_sz)
5608 return -EINVAL;
5609 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5610 return -EINVAL;
5611 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005612 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005613 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005614 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005615 return -EINVAL;
5616 return 0;
5617}
Andy Adamson8d353012009-04-01 09:22:32 -04005618
Andy Adamson8d353012009-04-01 09:22:32 -04005619static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5620 struct nfs4_session *session)
5621{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005622 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005623
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005624 ret = nfs4_verify_fore_channel_attrs(args, session);
5625 if (ret)
5626 return ret;
5627 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005628}
5629
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005630static int _nfs4_proc_create_session(struct nfs_client *clp,
5631 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005632{
5633 struct nfs4_session *session = clp->cl_session;
5634 struct nfs41_create_session_args args = {
5635 .client = clp,
5636 .cb_program = NFS4_CALLBACK,
5637 };
5638 struct nfs41_create_session_res res = {
5639 .client = clp,
5640 };
5641 struct rpc_message msg = {
5642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5643 .rpc_argp = &args,
5644 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005645 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005646 };
5647 int status;
5648
5649 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005650 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005651
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005652 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005653
Trond Myklebust43095d32012-11-20 11:13:12 -05005654 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005655 /* Verify the session's negotiated channel_attrs values */
5656 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005657 /* Increment the clientid slot sequence id */
5658 clp->cl_seqid++;
5659 }
5660
5661 return status;
5662}
5663
5664/*
5665 * Issues a CREATE_SESSION operation to the server.
5666 * It is the responsibility of the caller to verify the session is
5667 * expired before calling this routine.
5668 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005669int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005670{
5671 int status;
5672 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005673 struct nfs4_session *session = clp->cl_session;
5674
5675 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5676
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005677 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005678 if (status)
5679 goto out;
5680
Andy Adamsonaacd5532011-11-09 13:58:21 -05005681 /* Init or reset the session slot tables */
5682 status = nfs4_setup_session_slot_tables(session);
5683 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005684 if (status)
5685 goto out;
5686
5687 ptr = (unsigned *)&session->sess_id.data[0];
5688 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5689 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005690out:
5691 dprintk("<-- %s\n", __func__);
5692 return status;
5693}
5694
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005695/*
5696 * Issue the over-the-wire RPC DESTROY_SESSION.
5697 * The caller must serialize access to this routine.
5698 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005699int nfs4_proc_destroy_session(struct nfs4_session *session,
5700 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005701{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005702 struct rpc_message msg = {
5703 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5704 .rpc_argp = session,
5705 .rpc_cred = cred,
5706 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005707 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005708
5709 dprintk("--> nfs4_proc_destroy_session\n");
5710
5711 /* session is still being setup */
5712 if (session->clp->cl_cons_state != NFS_CS_READY)
5713 return status;
5714
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005715 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005716
5717 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005718 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005719 "Session has been destroyed regardless...\n", status);
5720
5721 dprintk("<-- nfs4_proc_destroy_session\n");
5722 return status;
5723}
5724
Trond Myklebust7b38c362012-05-23 13:23:31 -04005725/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005726 * Renew the cl_session lease.
5727 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005728struct nfs4_sequence_data {
5729 struct nfs_client *clp;
5730 struct nfs4_sequence_args args;
5731 struct nfs4_sequence_res res;
5732};
5733
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005734static void nfs41_sequence_release(void *data)
5735{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005736 struct nfs4_sequence_data *calldata = data;
5737 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005738
Alexandros Batsakis71358402010-02-05 03:45:05 -08005739 if (atomic_read(&clp->cl_count) > 1)
5740 nfs4_schedule_state_renewal(clp);
5741 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005742 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005743}
5744
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005745static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5746{
5747 switch(task->tk_status) {
5748 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005749 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5750 return -EAGAIN;
5751 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005752 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005753 }
5754 return 0;
5755}
5756
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005757static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005758{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005759 struct nfs4_sequence_data *calldata = data;
5760 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005761
Trond Myklebust14516c32010-07-31 14:29:06 -04005762 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5763 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005764
5765 if (task->tk_status < 0) {
5766 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005767 if (atomic_read(&clp->cl_count) == 1)
5768 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005769
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005770 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5771 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005772 return;
5773 }
5774 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005775 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005776out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005777 dprintk("<-- %s\n", __func__);
5778}
5779
5780static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5781{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005782 struct nfs4_sequence_data *calldata = data;
5783 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005784 struct nfs4_sequence_args *args;
5785 struct nfs4_sequence_res *res;
5786
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005787 args = task->tk_msg.rpc_argp;
5788 res = task->tk_msg.rpc_resp;
5789
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005790 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005791}
5792
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005793static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5794{
5795 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5796 nfs41_sequence_prepare(task, data);
5797}
5798
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005799static const struct rpc_call_ops nfs41_sequence_ops = {
5800 .rpc_call_done = nfs41_sequence_call_done,
5801 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005802 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005803};
5804
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005805static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5806 .rpc_call_done = nfs41_sequence_call_done,
5807 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5808 .rpc_release = nfs41_sequence_release,
5809};
5810
5811static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5812 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005813{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005814 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005815 struct rpc_message msg = {
5816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5817 .rpc_cred = cred,
5818 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005819 struct rpc_task_setup task_setup_data = {
5820 .rpc_client = clp->cl_rpcclient,
5821 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005822 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005823 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5824 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005825
Alexandros Batsakis71358402010-02-05 03:45:05 -08005826 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005827 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005828 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005829 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005830 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005831 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005832 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005833 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005834 msg.rpc_argp = &calldata->args;
5835 msg.rpc_resp = &calldata->res;
5836 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005837 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005838
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005839 return rpc_run_task(&task_setup_data);
5840}
5841
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005842static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005843{
5844 struct rpc_task *task;
5845 int ret = 0;
5846
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005847 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5848 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005849 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005850 if (IS_ERR(task))
5851 ret = PTR_ERR(task);
5852 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005853 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005854 dprintk("<-- %s status=%d\n", __func__, ret);
5855 return ret;
5856}
5857
5858static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5859{
5860 struct rpc_task *task;
5861 int ret;
5862
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005863 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005864 if (IS_ERR(task)) {
5865 ret = PTR_ERR(task);
5866 goto out;
5867 }
5868 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005869 if (!ret) {
5870 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5871
5872 if (task->tk_status == 0)
5873 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005874 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005875 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005876 rpc_put_task(task);
5877out:
5878 dprintk("<-- %s status=%d\n", __func__, ret);
5879 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005880}
5881
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005882struct nfs4_reclaim_complete_data {
5883 struct nfs_client *clp;
5884 struct nfs41_reclaim_complete_args arg;
5885 struct nfs41_reclaim_complete_res res;
5886};
5887
5888static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5889{
5890 struct nfs4_reclaim_complete_data *calldata = data;
5891
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005892 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005893 nfs41_setup_sequence(calldata->clp->cl_session,
5894 &calldata->arg.seq_args,
5895 &calldata->res.seq_res,
5896 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005897}
5898
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005899static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5900{
5901 switch(task->tk_status) {
5902 case 0:
5903 case -NFS4ERR_COMPLETE_ALREADY:
5904 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5905 break;
5906 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005907 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005908 /* fall through */
5909 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005910 return -EAGAIN;
5911 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005912 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005913 }
5914 return 0;
5915}
5916
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005917static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5918{
5919 struct nfs4_reclaim_complete_data *calldata = data;
5920 struct nfs_client *clp = calldata->clp;
5921 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5922
5923 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005924 if (!nfs41_sequence_done(task, res))
5925 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005926
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005927 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5928 rpc_restart_call_prepare(task);
5929 return;
5930 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005931 dprintk("<-- %s\n", __func__);
5932}
5933
5934static void nfs4_free_reclaim_complete_data(void *data)
5935{
5936 struct nfs4_reclaim_complete_data *calldata = data;
5937
5938 kfree(calldata);
5939}
5940
5941static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5942 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5943 .rpc_call_done = nfs4_reclaim_complete_done,
5944 .rpc_release = nfs4_free_reclaim_complete_data,
5945};
5946
5947/*
5948 * Issue a global reclaim complete.
5949 */
5950static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5951{
5952 struct nfs4_reclaim_complete_data *calldata;
5953 struct rpc_task *task;
5954 struct rpc_message msg = {
5955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5956 };
5957 struct rpc_task_setup task_setup_data = {
5958 .rpc_client = clp->cl_rpcclient,
5959 .rpc_message = &msg,
5960 .callback_ops = &nfs4_reclaim_complete_call_ops,
5961 .flags = RPC_TASK_ASYNC,
5962 };
5963 int status = -ENOMEM;
5964
5965 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005966 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005967 if (calldata == NULL)
5968 goto out;
5969 calldata->clp = clp;
5970 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005971
Trond Myklebust9d12b212012-01-17 22:04:25 -05005972 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005973 msg.rpc_argp = &calldata->arg;
5974 msg.rpc_resp = &calldata->res;
5975 task_setup_data.callback_data = calldata;
5976 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005977 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005978 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005979 goto out;
5980 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005981 status = nfs4_wait_for_completion_rpc_task(task);
5982 if (status == 0)
5983 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005984 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005985 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005986out:
5987 dprintk("<-- %s status=%d\n", __func__, status);
5988 return status;
5989}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005990
5991static void
5992nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5993{
5994 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005995 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005996 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005997
5998 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005999 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6000 * right now covering the LAYOUTGET we are about to send.
6001 * However, that is not so catastrophic, and there seems
6002 * to be no way to prevent it completely.
6003 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006004 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006005 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006006 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006007 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6008 NFS_I(lgp->args.inode)->layout,
6009 lgp->args.ctx->state)) {
6010 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006011 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006012}
6013
6014static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6015{
6016 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006017 struct inode *inode = lgp->args.inode;
6018 struct nfs_server *server = NFS_SERVER(inode);
6019 struct pnfs_layout_hdr *lo;
6020 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006021
6022 dprintk("--> %s\n", __func__);
6023
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006024 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006025 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006026
6027 switch (task->tk_status) {
6028 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006029 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006030 case -NFS4ERR_LAYOUTTRYLATER:
6031 case -NFS4ERR_RECALLCONFLICT:
6032 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006033 break;
6034 case -NFS4ERR_EXPIRED:
6035 case -NFS4ERR_BAD_STATEID:
6036 spin_lock(&inode->i_lock);
6037 lo = NFS_I(inode)->layout;
6038 if (!lo || list_empty(&lo->plh_segs)) {
6039 spin_unlock(&inode->i_lock);
6040 /* If the open stateid was bad, then recover it. */
6041 state = lgp->args.ctx->state;
6042 } else {
6043 LIST_HEAD(head);
6044
6045 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6046 spin_unlock(&inode->i_lock);
6047 /* Mark the bad layout state as invalid, then
6048 * retry using the open stateid. */
6049 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006050 }
6051 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006052 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6053 rpc_restart_call_prepare(task);
6054out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006055 dprintk("<-- %s\n", __func__);
6056}
6057
Idan Kedar85541162012-08-02 11:47:10 +03006058static size_t max_response_pages(struct nfs_server *server)
6059{
6060 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6061 return nfs_page_array_len(0, max_resp_sz);
6062}
6063
6064static void nfs4_free_pages(struct page **pages, size_t size)
6065{
6066 int i;
6067
6068 if (!pages)
6069 return;
6070
6071 for (i = 0; i < size; i++) {
6072 if (!pages[i])
6073 break;
6074 __free_page(pages[i]);
6075 }
6076 kfree(pages);
6077}
6078
6079static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6080{
6081 struct page **pages;
6082 int i;
6083
6084 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6085 if (!pages) {
6086 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6087 return NULL;
6088 }
6089
6090 for (i = 0; i < size; i++) {
6091 pages[i] = alloc_page(gfp_flags);
6092 if (!pages[i]) {
6093 dprintk("%s: failed to allocate page\n", __func__);
6094 nfs4_free_pages(pages, size);
6095 return NULL;
6096 }
6097 }
6098
6099 return pages;
6100}
6101
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006102static void nfs4_layoutget_release(void *calldata)
6103{
6104 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006105 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6106 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006107
6108 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006109 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006110 put_nfs_open_context(lgp->args.ctx);
6111 kfree(calldata);
6112 dprintk("<-- %s\n", __func__);
6113}
6114
6115static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6116 .rpc_call_prepare = nfs4_layoutget_prepare,
6117 .rpc_call_done = nfs4_layoutget_done,
6118 .rpc_release = nfs4_layoutget_release,
6119};
6120
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006121struct pnfs_layout_segment *
6122nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006123{
6124 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006125 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006126 struct rpc_task *task;
6127 struct rpc_message msg = {
6128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6129 .rpc_argp = &lgp->args,
6130 .rpc_resp = &lgp->res,
6131 };
6132 struct rpc_task_setup task_setup_data = {
6133 .rpc_client = server->client,
6134 .rpc_message = &msg,
6135 .callback_ops = &nfs4_layoutget_call_ops,
6136 .callback_data = lgp,
6137 .flags = RPC_TASK_ASYNC,
6138 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006139 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006140 int status = 0;
6141
6142 dprintk("--> %s\n", __func__);
6143
Idan Kedar85541162012-08-02 11:47:10 +03006144 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6145 if (!lgp->args.layout.pages) {
6146 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006147 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006148 }
6149 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6150
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006151 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006152 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006153 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006154 task = rpc_run_task(&task_setup_data);
6155 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006156 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006157 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006158 if (status == 0)
6159 status = task->tk_status;
6160 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006161 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006162 rpc_put_task(task);
6163 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006164 if (status)
6165 return ERR_PTR(status);
6166 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006167}
6168
Benny Halevycbe82602011-05-22 19:52:37 +03006169static void
6170nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6171{
6172 struct nfs4_layoutreturn *lrp = calldata;
6173
6174 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006175 nfs41_setup_sequence(lrp->clp->cl_session,
6176 &lrp->args.seq_args,
6177 &lrp->res.seq_res,
6178 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006179}
6180
6181static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6182{
6183 struct nfs4_layoutreturn *lrp = calldata;
6184 struct nfs_server *server;
6185
6186 dprintk("--> %s\n", __func__);
6187
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006188 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006189 return;
6190
6191 server = NFS_SERVER(lrp->args.inode);
6192 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006193 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006194 return;
6195 }
Benny Halevycbe82602011-05-22 19:52:37 +03006196 dprintk("<-- %s\n", __func__);
6197}
6198
6199static void nfs4_layoutreturn_release(void *calldata)
6200{
6201 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006202 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006203
6204 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006205 spin_lock(&lo->plh_inode->i_lock);
6206 if (lrp->res.lrs_present)
6207 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6208 lo->plh_block_lgets--;
6209 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006210 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006211 kfree(calldata);
6212 dprintk("<-- %s\n", __func__);
6213}
6214
6215static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6216 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6217 .rpc_call_done = nfs4_layoutreturn_done,
6218 .rpc_release = nfs4_layoutreturn_release,
6219};
6220
6221int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6222{
6223 struct rpc_task *task;
6224 struct rpc_message msg = {
6225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6226 .rpc_argp = &lrp->args,
6227 .rpc_resp = &lrp->res,
6228 };
6229 struct rpc_task_setup task_setup_data = {
6230 .rpc_client = lrp->clp->cl_rpcclient,
6231 .rpc_message = &msg,
6232 .callback_ops = &nfs4_layoutreturn_call_ops,
6233 .callback_data = lrp,
6234 };
6235 int status;
6236
6237 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006238 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006239 task = rpc_run_task(&task_setup_data);
6240 if (IS_ERR(task))
6241 return PTR_ERR(task);
6242 status = task->tk_status;
6243 dprintk("<-- %s status=%d\n", __func__, status);
6244 rpc_put_task(task);
6245 return status;
6246}
6247
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006248/*
6249 * Retrieve the list of Data Server devices from the MDS.
6250 */
6251static int _nfs4_getdevicelist(struct nfs_server *server,
6252 const struct nfs_fh *fh,
6253 struct pnfs_devicelist *devlist)
6254{
6255 struct nfs4_getdevicelist_args args = {
6256 .fh = fh,
6257 .layoutclass = server->pnfs_curr_ld->id,
6258 };
6259 struct nfs4_getdevicelist_res res = {
6260 .devlist = devlist,
6261 };
6262 struct rpc_message msg = {
6263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6264 .rpc_argp = &args,
6265 .rpc_resp = &res,
6266 };
6267 int status;
6268
6269 dprintk("--> %s\n", __func__);
6270 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6271 &res.seq_res, 0);
6272 dprintk("<-- %s status=%d\n", __func__, status);
6273 return status;
6274}
6275
6276int nfs4_proc_getdevicelist(struct nfs_server *server,
6277 const struct nfs_fh *fh,
6278 struct pnfs_devicelist *devlist)
6279{
6280 struct nfs4_exception exception = { };
6281 int err;
6282
6283 do {
6284 err = nfs4_handle_exception(server,
6285 _nfs4_getdevicelist(server, fh, devlist),
6286 &exception);
6287 } while (exception.retry);
6288
6289 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6290 err, devlist->num_devs);
6291
6292 return err;
6293}
6294EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6295
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006296static int
6297_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6298{
6299 struct nfs4_getdeviceinfo_args args = {
6300 .pdev = pdev,
6301 };
6302 struct nfs4_getdeviceinfo_res res = {
6303 .pdev = pdev,
6304 };
6305 struct rpc_message msg = {
6306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6307 .rpc_argp = &args,
6308 .rpc_resp = &res,
6309 };
6310 int status;
6311
6312 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006313 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006314 dprintk("<-- %s status=%d\n", __func__, status);
6315
6316 return status;
6317}
6318
6319int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6320{
6321 struct nfs4_exception exception = { };
6322 int err;
6323
6324 do {
6325 err = nfs4_handle_exception(server,
6326 _nfs4_proc_getdeviceinfo(server, pdev),
6327 &exception);
6328 } while (exception.retry);
6329 return err;
6330}
6331EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6332
Andy Adamson863a3c62011-03-23 13:27:54 +00006333static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6334{
6335 struct nfs4_layoutcommit_data *data = calldata;
6336 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006337 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006338
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006339 nfs41_setup_sequence(session,
6340 &data->args.seq_args,
6341 &data->res.seq_res,
6342 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006343}
6344
6345static void
6346nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6347{
6348 struct nfs4_layoutcommit_data *data = calldata;
6349 struct nfs_server *server = NFS_SERVER(data->args.inode);
6350
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006351 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006352 return;
6353
6354 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006355 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6356 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6357 case -NFS4ERR_BADLAYOUT: /* no layout */
6358 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006359 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006360 break;
6361 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006362 nfs_post_op_update_inode_force_wcc(data->args.inode,
6363 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006364 break;
6365 default:
6366 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6367 rpc_restart_call_prepare(task);
6368 return;
6369 }
6370 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006371}
6372
6373static void nfs4_layoutcommit_release(void *calldata)
6374{
6375 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006376 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006377 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006378
Andy Adamsondb29c082011-07-30 20:52:38 -04006379 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006380 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006381 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6382 list_del_init(&lseg->pls_lc_list);
6383 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6384 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006385 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006386 }
Peng Tao92407e72011-10-23 20:21:17 -07006387
6388 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6389 smp_mb__after_clear_bit();
6390 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6391
Andy Adamson863a3c62011-03-23 13:27:54 +00006392 put_rpccred(data->cred);
6393 kfree(data);
6394}
6395
6396static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6397 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6398 .rpc_call_done = nfs4_layoutcommit_done,
6399 .rpc_release = nfs4_layoutcommit_release,
6400};
6401
6402int
Andy Adamsonef311532011-03-12 02:58:10 -05006403nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006404{
6405 struct rpc_message msg = {
6406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6407 .rpc_argp = &data->args,
6408 .rpc_resp = &data->res,
6409 .rpc_cred = data->cred,
6410 };
6411 struct rpc_task_setup task_setup_data = {
6412 .task = &data->task,
6413 .rpc_client = NFS_CLIENT(data->args.inode),
6414 .rpc_message = &msg,
6415 .callback_ops = &nfs4_layoutcommit_ops,
6416 .callback_data = data,
6417 .flags = RPC_TASK_ASYNC,
6418 };
6419 struct rpc_task *task;
6420 int status = 0;
6421
6422 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6423 "lbw: %llu inode %lu\n",
6424 data->task.tk_pid, sync,
6425 data->args.lastbytewritten,
6426 data->args.inode->i_ino);
6427
Trond Myklebust9d12b212012-01-17 22:04:25 -05006428 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006429 task = rpc_run_task(&task_setup_data);
6430 if (IS_ERR(task))
6431 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006432 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006433 goto out;
6434 status = nfs4_wait_for_completion_rpc_task(task);
6435 if (status != 0)
6436 goto out;
6437 status = task->tk_status;
6438out:
6439 dprintk("%s: status %d\n", __func__, status);
6440 rpc_put_task(task);
6441 return status;
6442}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006443
6444static int
6445_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6446 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6447{
6448 struct nfs41_secinfo_no_name_args args = {
6449 .style = SECINFO_STYLE_CURRENT_FH,
6450 };
6451 struct nfs4_secinfo_res res = {
6452 .flavors = flavors,
6453 };
6454 struct rpc_message msg = {
6455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6456 .rpc_argp = &args,
6457 .rpc_resp = &res,
6458 };
6459 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6460}
6461
6462static int
6463nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6464 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6465{
6466 struct nfs4_exception exception = { };
6467 int err;
6468 do {
6469 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6470 switch (err) {
6471 case 0:
6472 case -NFS4ERR_WRONGSEC:
6473 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006474 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006475 default:
6476 err = nfs4_handle_exception(server, err, &exception);
6477 }
6478 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006479out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006480 return err;
6481}
6482
6483static int
6484nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6485 struct nfs_fsinfo *info)
6486{
6487 int err;
6488 struct page *page;
6489 rpc_authflavor_t flavor;
6490 struct nfs4_secinfo_flavors *flavors;
6491
6492 page = alloc_page(GFP_KERNEL);
6493 if (!page) {
6494 err = -ENOMEM;
6495 goto out;
6496 }
6497
6498 flavors = page_address(page);
6499 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6500
6501 /*
6502 * Fall back on "guess and check" method if
6503 * the server doesn't support SECINFO_NO_NAME
6504 */
6505 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6506 err = nfs4_find_root_sec(server, fhandle, info);
6507 goto out_freepage;
6508 }
6509 if (err)
6510 goto out_freepage;
6511
6512 flavor = nfs_find_best_sec(flavors);
6513 if (err == 0)
6514 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6515
6516out_freepage:
6517 put_page(page);
6518 if (err == -EACCES)
6519 return -EPERM;
6520out:
6521 return err;
6522}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006523
6524static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006525{
6526 int status;
6527 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006528 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006529 };
6530 struct nfs41_test_stateid_res res;
6531 struct rpc_message msg = {
6532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6533 .rpc_argp = &args,
6534 .rpc_resp = &res,
6535 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006536
Chuck Lever38527b12012-07-11 16:30:23 -04006537 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006538 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006539 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006540 if (status != NFS_OK) {
6541 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006542 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006543 }
6544 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006545 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006546}
6547
Chuck Lever38527b12012-07-11 16:30:23 -04006548/**
6549 * nfs41_test_stateid - perform a TEST_STATEID operation
6550 *
6551 * @server: server / transport on which to perform the operation
6552 * @stateid: state ID to test
6553 *
6554 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6555 * Otherwise a negative NFS4ERR value is returned if the operation
6556 * failed or the state ID is not currently valid.
6557 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006558static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006559{
6560 struct nfs4_exception exception = { };
6561 int err;
6562 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006563 err = _nfs41_test_stateid(server, stateid);
6564 if (err != -NFS4ERR_DELAY)
6565 break;
6566 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006567 } while (exception.retry);
6568 return err;
6569}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006570
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006571static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006572{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006573 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006574 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006575 };
6576 struct nfs41_free_stateid_res res;
6577 struct rpc_message msg = {
6578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6579 .rpc_argp = &args,
6580 .rpc_resp = &res,
6581 };
Chuck Lever38527b12012-07-11 16:30:23 -04006582 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006583
Chuck Lever38527b12012-07-11 16:30:23 -04006584 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006585 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006586 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006587 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006588 dprintk("NFS reply free_stateid: %d\n", status);
6589 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006590}
6591
Chuck Lever38527b12012-07-11 16:30:23 -04006592/**
6593 * nfs41_free_stateid - perform a FREE_STATEID operation
6594 *
6595 * @server: server / transport on which to perform the operation
6596 * @stateid: state ID to release
6597 *
6598 * Returns NFS_OK if the server freed "stateid". Otherwise a
6599 * negative NFS4ERR value is returned.
6600 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006601static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006602{
6603 struct nfs4_exception exception = { };
6604 int err;
6605 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006606 err = _nfs4_free_stateid(server, stateid);
6607 if (err != -NFS4ERR_DELAY)
6608 break;
6609 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006610 } while (exception.retry);
6611 return err;
6612}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006613
6614static bool nfs41_match_stateid(const nfs4_stateid *s1,
6615 const nfs4_stateid *s2)
6616{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006617 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006618 return false;
6619
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006620 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006621 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006622 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006623 return true;
6624
6625 return false;
6626}
6627
Andy Adamson557134a2009-04-01 09:21:53 -04006628#endif /* CONFIG_NFS_V4_1 */
6629
Trond Myklebust36281ca2012-03-04 18:13:56 -05006630static bool nfs4_match_stateid(const nfs4_stateid *s1,
6631 const nfs4_stateid *s2)
6632{
Trond Myklebustf597c532012-03-04 18:13:56 -05006633 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006634}
6635
6636
Trond Myklebust17280172012-03-11 13:11:00 -04006637static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006638 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006639 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 .recover_open = nfs4_open_reclaim,
6641 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006642 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006643 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006644 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645};
6646
Andy Adamson591d71c2009-04-01 09:22:47 -04006647#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006648static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006649 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6650 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6651 .recover_open = nfs4_open_reclaim,
6652 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006653 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006654 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006655 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006656 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006657};
6658#endif /* CONFIG_NFS_V4_1 */
6659
Trond Myklebust17280172012-03-11 13:11:00 -04006660static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006661 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006662 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 .recover_open = nfs4_open_expired,
6664 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006665 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006666 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667};
6668
Andy Adamson591d71c2009-04-01 09:22:47 -04006669#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006670static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006671 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6672 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006673 .recover_open = nfs41_open_expired,
6674 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006675 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006676 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006677};
6678#endif /* CONFIG_NFS_V4_1 */
6679
Trond Myklebust17280172012-03-11 13:11:00 -04006680static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006681 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006682 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006683 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006684};
6685
6686#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006687static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006688 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006689 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006690 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006691};
6692#endif
6693
Trond Myklebust97dc1352010-06-16 09:52:26 -04006694static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6695 .minor_version = 0,
6696 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006697 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006698 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006699 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6700 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6701 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006702};
6703
6704#if defined(CONFIG_NFS_V4_1)
6705static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6706 .minor_version = 1,
6707 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006708 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006709 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006710 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6711 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6712 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006713};
6714#endif
6715
Trond Myklebust97dc1352010-06-16 09:52:26 -04006716const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6717 [0] = &nfs_v4_0_minor_ops,
6718#if defined(CONFIG_NFS_V4_1)
6719 [1] = &nfs_v4_1_minor_ops,
6720#endif
6721};
6722
Bryan Schumaker73a79702012-07-16 16:39:12 -04006723const struct inode_operations nfs4_dir_inode_operations = {
6724 .create = nfs_create,
6725 .lookup = nfs_lookup,
6726 .atomic_open = nfs_atomic_open,
6727 .link = nfs_link,
6728 .unlink = nfs_unlink,
6729 .symlink = nfs_symlink,
6730 .mkdir = nfs_mkdir,
6731 .rmdir = nfs_rmdir,
6732 .mknod = nfs_mknod,
6733 .rename = nfs_rename,
6734 .permission = nfs_permission,
6735 .getattr = nfs_getattr,
6736 .setattr = nfs_setattr,
6737 .getxattr = generic_getxattr,
6738 .setxattr = generic_setxattr,
6739 .listxattr = generic_listxattr,
6740 .removexattr = generic_removexattr,
6741};
6742
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006743static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006744 .permission = nfs_permission,
6745 .getattr = nfs_getattr,
6746 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006747 .getxattr = generic_getxattr,
6748 .setxattr = generic_setxattr,
6749 .listxattr = generic_listxattr,
6750 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006751};
6752
David Howells509de812006-08-22 20:06:11 -04006753const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 .version = 4, /* protocol version */
6755 .dentry_ops = &nfs4_dentry_operations,
6756 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006757 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006758 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006760 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006761 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 .getattr = nfs4_proc_getattr,
6763 .setattr = nfs4_proc_setattr,
6764 .lookup = nfs4_proc_lookup,
6765 .access = nfs4_proc_access,
6766 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 .create = nfs4_proc_create,
6768 .remove = nfs4_proc_remove,
6769 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006770 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 .unlink_done = nfs4_proc_unlink_done,
6772 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006773 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006774 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006775 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 .link = nfs4_proc_link,
6777 .symlink = nfs4_proc_symlink,
6778 .mkdir = nfs4_proc_mkdir,
6779 .rmdir = nfs4_proc_remove,
6780 .readdir = nfs4_proc_readdir,
6781 .mknod = nfs4_proc_mknod,
6782 .statfs = nfs4_proc_statfs,
6783 .fsinfo = nfs4_proc_fsinfo,
6784 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006785 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 .decode_dirent = nfs4_decode_dirent,
6787 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006788 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006789 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006790 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006792 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006793 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006794 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006796 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006797 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006799 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006800 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006801 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006802 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006803 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006804 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006805 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006806 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006807 .create_server = nfs4_create_server,
6808 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809};
6810
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006811static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6812 .prefix = XATTR_NAME_NFSV4_ACL,
6813 .list = nfs4_xattr_list_nfs4_acl,
6814 .get = nfs4_xattr_get_nfs4_acl,
6815 .set = nfs4_xattr_set_nfs4_acl,
6816};
6817
6818const struct xattr_handler *nfs4_xattr_handlers[] = {
6819 &nfs4_xattr_nfs4_acl_handler,
6820 NULL
6821};
6822
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823/*
6824 * Local variables:
6825 * c-basic-offset: 8
6826 * End:
6827 */