blob: a1e89e270ad89057e121a852932ff7e9a365a89e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
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:
336 ret = nfs4_delay(server->client, &exception->timeout);
337 if (ret != 0)
338 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400339 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500340 case -NFS4ERR_OLD_STATEID:
341 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800342 break;
343 case -NFS4ERR_BADOWNER:
344 /* The following works around a Linux server bug! */
345 case -NFS4ERR_BADNAME:
346 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348 exception->retry = 1;
349 printk(KERN_WARNING "NFS: v4 server %s "
350 "does not accept raw "
351 "uid/gids. "
352 "Reenabling the idmapper.\n",
353 server->nfs_client->cl_hostname);
354 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 }
356 /* We failed to handle the error */
357 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500358wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500359 ret = nfs4_wait_clnt_recover(clp);
360 if (ret == 0)
361 exception->retry = 1;
362 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500363}
364
365
Trond Myklebust452e9352010-07-31 14:29:06 -0400366static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_lock(&clp->cl_lock);
369 if (time_before(clp->cl_last_renewal,timestamp))
370 clp->cl_last_renewal = timestamp;
371 spin_unlock(&clp->cl_lock);
372}
373
Trond Myklebust452e9352010-07-31 14:29:06 -0400374static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
375{
376 do_renew_lease(server->nfs_client, timestamp);
377}
378
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400379#if defined(CONFIG_NFS_V4_1)
380
Trond Myklebustd185a332010-06-16 09:52:25 -0400381static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400382{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500383 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400384 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500385 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400386
Benny Halevydfb4f3092010-09-24 09:17:01 -0400387 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400388 /* just wake up the next guy waiting since
389 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500390 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500391 return;
Andy Adamson13615872009-04-01 09:22:17 -0400392 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500393 tbl = res->sr_slot->table;
394 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500395
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500396 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500397 /* Be nice to the server: try to ensure that the last transmitted
398 * value for highest_user_slotid <= target_highest_slotid
399 */
400 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
401 send_new_highest_used_slotid = true;
402
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500403 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
404 send_new_highest_used_slotid = false;
405 goto out_unlock;
406 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500407 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500408
409 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
410 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500411out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500412 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400413 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500414 if (send_new_highest_used_slotid)
415 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400416}
417
Trond Myklebust14516c32010-07-31 14:29:06 -0400418static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400419{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500420 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500421 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400422 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500423 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500424 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400425
Bryan Schumaker468f8612011-04-18 15:57:32 -0400426 /* don't increment the sequence number if the task wasn't sent */
427 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400428 goto out;
429
Trond Myklebust933602e2012-11-16 12:12:38 -0500430 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500431 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500432
Trond Myklebustac20d162012-12-15 15:36:07 -0500433 if (slot->interrupted) {
434 slot->interrupted = 0;
435 interrupted = true;
436 }
437
Andy Adamson691daf32009-12-04 15:55:39 -0500438 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400439 switch (res->sr_status) {
440 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500442 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500443 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500444 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500445 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500446 if (res->sr_status_flags != 0)
447 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500448 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500450 case 1:
451 /*
452 * sr_status remains 1 if an RPC level error occurred.
453 * The server may or may not have processed the sequence
454 * operation..
455 * Mark the slot as having hosted an interrupted RPC call.
456 */
457 slot->interrupted = 1;
458 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 case -NFS4ERR_DELAY:
460 /* The server detected a resend of the RPC call and
461 * returned NFS4ERR_DELAY as per Section 2.10.6.2
462 * of RFC5661.
463 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500464 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400465 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500466 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500467 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500469 case -NFS4ERR_BADSLOT:
470 /*
471 * The slot id we used was probably retired. Try again
472 * using a different slot id.
473 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500474 goto retry_nowait;
475 case -NFS4ERR_SEQ_MISORDERED:
476 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500477 * Was the last operation on this sequence interrupted?
478 * If so, retry after bumping the sequence number.
479 */
480 if (interrupted) {
481 ++slot->seq_nr;
482 goto retry_nowait;
483 }
484 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500485 * Could this slot have been previously retired?
486 * If so, then the server may be expecting seq_nr = 1!
487 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500488 if (slot->seq_nr != 1) {
489 slot->seq_nr = 1;
490 goto retry_nowait;
491 }
492 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500493 case -NFS4ERR_SEQ_FALSE_RETRY:
494 ++slot->seq_nr;
495 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400496 default:
497 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500498 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400499 }
500out:
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400503 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500504 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500505retry_nowait:
506 if (rpc_restart_call_prepare(task)) {
507 task->tk_status = 0;
508 ret = 0;
509 }
510 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400511out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400512 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 goto out;
514 rpc_delay(task, NFS4_POLL_RETRY_MAX);
515 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400516}
517
Trond Myklebust14516c32010-07-31 14:29:06 -0400518static int nfs4_sequence_done(struct rpc_task *task,
519 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400520{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500521 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400522 return 1;
523 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400524}
525
Trond Myklebust9d12b212012-01-17 22:04:25 -0500526static void nfs41_init_sequence(struct nfs4_sequence_args *args,
527 struct nfs4_sequence_res *res, int cache_reply)
528{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500529 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500530 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400531 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500532 if (cache_reply)
533 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500534 res->sr_slot = NULL;
535}
536
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400537static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
538{
539 args->sa_privileged = 1;
540}
541
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000542int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400543 struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400545 struct rpc_task *task)
546{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 struct nfs4_slot *slot;
548 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549
550 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400551 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400552 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400553 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 tbl = &session->fc_slot_table;
556
Trond Myklebust69d206b2012-11-22 13:21:02 -0500557 task->tk_timeout = 0;
558
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400559 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400560 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400561 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500562 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500563 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400564 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400565 }
566
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500567 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500568 if (IS_ERR(slot)) {
569 /* If out of memory, try again in 1/4 second */
570 if (slot == ERR_PTR(-ENOMEM))
571 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400572 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400573 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 }
575 spin_unlock(&tbl->slot_tbl_lock);
576
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500577 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400578
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
580 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400581
Benny Halevydfb4f3092010-09-24 09:17:01 -0400582 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500583 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400584 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 /*
586 * sr_status is only set in decode_sequence, and so will remain
587 * set to 1 if an rpc level failure occurs.
588 */
589 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400590out_success:
591 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400593out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400594 /* Privileged tasks are queued with top priority */
595 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400596 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
597 NULL, RPC_PRIORITY_PRIVILEGED);
598 else
599 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400600 spin_unlock(&tbl->slot_tbl_lock);
601 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000603EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604
Trond Myklebust035168a2010-06-16 09:52:26 -0400605int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 struct rpc_task *task)
609{
Trond Myklebust035168a2010-06-16 09:52:26 -0400610 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611 int ret = 0;
612
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400613 if (session == NULL) {
614 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400616 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400617
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500618 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400619 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500620 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400621
Trond Myklebust9d12b212012-01-17 22:04:25 -0500622 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623out:
624 dprintk("<-- %s status=%d\n", __func__, ret);
625 return ret;
626}
627
628struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400629 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct nfs4_sequence_args *seq_args;
631 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632};
633
634static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
635{
636 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400637 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638
Trond Myklebust035168a2010-06-16 09:52:26 -0400639 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
640
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400641 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642}
643
Andy Adamson69ab40c2009-04-01 09:22:19 -0400644static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
645{
646 struct nfs41_call_sync_data *data = calldata;
647
Trond Myklebust14516c32010-07-31 14:29:06 -0400648 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400649}
650
Trond Myklebust17280172012-03-11 13:11:00 -0400651static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400653 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654};
655
Bryan Schumaker7c513052011-03-24 17:12:24 +0000656static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
657 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 struct rpc_message *msg,
659 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400660 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661{
662 int ret;
663 struct rpc_task *task;
664 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400665 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666 .seq_args = args,
667 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 };
669 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000670 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671 .rpc_message = msg,
672 .callback_ops = &nfs41_call_sync_ops,
673 .callback_data = &data
674 };
675
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676 task = rpc_run_task(&task_setup);
677 if (IS_ERR(task))
678 ret = PTR_ERR(task);
679 else {
680 ret = task->tk_status;
681 rpc_put_task(task);
682 }
683 return ret;
684}
685
Trond Myklebustdf896452010-06-16 09:52:26 -0400686#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400687static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500688void nfs41_init_sequence(struct nfs4_sequence_args *args,
689 struct nfs4_sequence_res *res, int cache_reply)
690{
691}
692
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400693static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
694{
695}
696
697
Trond Myklebust14516c32010-07-31 14:29:06 -0400698static int nfs4_sequence_done(struct rpc_task *task,
699 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400700{
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400702}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400703#endif /* CONFIG_NFS_V4_1 */
704
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400705static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000706int _nfs4_call_sync(struct rpc_clnt *clnt,
707 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400710 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000712 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713}
714
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400715static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000716int nfs4_call_sync(struct rpc_clnt *clnt,
717 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000718 struct rpc_message *msg,
719 struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res,
721 int cache_reply)
722{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400723 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400725 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000726}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400727
Trond Myklebust38478b22006-05-25 01:40:57 -0400728static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Trond Myklebust38478b22006-05-25 01:40:57 -0400730 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Trond Myklebust38478b22006-05-25 01:40:57 -0400732 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400733 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700734 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400735 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700736 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000737 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400738 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400742 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743 struct nfs_openargs o_arg;
744 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100745 struct nfs_open_confirmargs c_arg;
746 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500747 struct nfs4_string owner_name;
748 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400751 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400753 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100755 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400756 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100757 int rpc_status;
758 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759};
760
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400761
762static void nfs4_init_opendata_res(struct nfs4_opendata *p)
763{
764 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400765 p->o_res.seqid = p->o_arg.seqid;
766 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400767 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400768 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400769 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500770 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400771}
772
Al Viro82a2c1b2011-06-22 18:30:55 -0400773static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500774 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400775 const struct iattr *attrs,
776 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777{
Al Viro82a2c1b2011-06-22 18:30:55 -0400778 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 struct inode *dir = parent->d_inode;
780 struct nfs_server *server = NFS_SERVER(dir);
781 struct nfs4_opendata *p;
782
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400783 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 if (p == NULL)
785 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400786 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 if (p->o_arg.seqid == NULL)
788 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400789 nfs_sb_active(dentry->d_sb);
790 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 p->dir = parent;
792 p->owner = sp;
793 atomic_inc(&sp->so_count);
794 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500795 p->o_arg.open_flags = flags;
796 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700797 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
798 * will return permission denied for all bits until close */
799 if (!(flags & O_EXCL)) {
800 /* ask server to check for all possible rights as results
801 * are cached */
802 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
803 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700804 }
David Howells7539bba2006-08-22 20:06:09 -0400805 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400806 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
807 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400808 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 p->o_arg.server = server;
810 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400811 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500813 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500814 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400815
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 p->o_arg.u.attrs = &p->attrs;
817 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500818
819 verf[0] = jiffies;
820 verf[1] = current->pid;
821 memcpy(p->o_arg.u.verifier.data, verf,
822 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100824 p->c_arg.fh = &p->o_res.fh;
825 p->c_arg.stateid = &p->o_res.stateid;
826 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400827 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400828 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 return p;
830err_free:
831 kfree(p);
832err:
833 dput(parent);
834 return NULL;
835}
836
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400837static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400839 struct nfs4_opendata *p = container_of(kref,
840 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400841 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400842
843 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400844 if (p->state != NULL)
845 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846 nfs4_put_state_owner(p->owner);
847 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 dput(p->dentry);
849 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500850 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400851 kfree(p);
852}
853
854static void nfs4_opendata_put(struct nfs4_opendata *p)
855{
856 if (p != NULL)
857 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858}
859
Trond Myklebust06f814a2006-01-03 09:55:07 +0100860static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
861{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100862 int ret;
863
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100865 return ret;
866}
867
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400869{
870 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871
Trond Myklebust536e43d2012-01-17 22:04:26 -0500872 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873 goto out;
874 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400875 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500876 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
877 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400878 break;
879 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500880 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
881 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400882 break;
883 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500884 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
885 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400886 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888 return ret;
889}
890
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400892{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500893 if (delegation == NULL)
894 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500897 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500899 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
900 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500901 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 return 1;
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100906{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100908 case FMODE_WRITE:
909 state->n_wronly++;
910 break;
911 case FMODE_READ:
912 state->n_rdonly++;
913 break;
914 case FMODE_READ|FMODE_WRITE:
915 state->n_rdwr++;
916 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500923 nfs4_stateid_copy(&state->stateid, stateid);
924 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400926 case FMODE_READ:
927 set_bit(NFS_O_RDONLY_STATE, &state->flags);
928 break;
929 case FMODE_WRITE:
930 set_bit(NFS_O_WRONLY_STATE, &state->flags);
931 break;
932 case FMODE_READ|FMODE_WRITE:
933 set_bit(NFS_O_RDWR_STATE, &state->flags);
934 }
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400938{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static 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 -0700945{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400946 /*
947 * Protect the call to nfs4_state_set_mode_locked and
948 * serialise the stateid update
949 */
950 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500952 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400953 set_bit(NFS_DELEGATED_STATE, &state->flags);
954 }
955 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400957 write_sequnlock(&state->seqlock);
958 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700960 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static 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 -0500964{
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *deleg_cur;
967 int ret = 0;
968
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500970
971 rcu_read_lock();
972 deleg_cur = rcu_dereference(nfsi->delegation);
973 if (deleg_cur == NULL)
974 goto no_delegation;
975
976 spin_lock(&deleg_cur->lock);
977 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -0500978 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500980 goto no_delegation_unlock;
981
982 if (delegation == NULL)
983 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500984 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500985 goto no_delegation_unlock;
986
Trond Myklebustb7391f42008-12-23 15:21:52 -0500987 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500989 ret = 1;
990no_delegation_unlock:
991 spin_unlock(&deleg_cur->lock);
992no_delegation:
993 rcu_read_unlock();
994
995 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500997 ret = 1;
998 }
999
1000 return ret;
1001}
1002
1003
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005{
1006 struct nfs_delegation *delegation;
1007
1008 rcu_read_lock();
1009 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 rcu_read_unlock();
1012 return;
1013 }
1014 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001015 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016}
1017
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019{
1020 struct nfs4_state *state = opendata->state;
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001023 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 nfs4_stateid stateid;
1026 int ret = -EAGAIN;
1027
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 if (can_open_cached(state, fmode, open_mode)) {
1032 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034 goto out_return_state;
1035 }
1036 spin_unlock(&state->owner->so_lock);
1037 }
Trond Myklebust34310432008-12-23 15:21:38 -05001038 rcu_read_lock();
1039 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001040 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001041 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001042 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001043 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001045 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001047 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 if (ret != 0)
1049 goto out;
1050 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001051
1052 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001054 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056out:
1057 return ERR_PTR(ret);
1058out_return_state:
1059 atomic_inc(&state->count);
1060 return state;
1061}
1062
Andy Adamsone23008e2012-10-02 21:07:32 -04001063static void
1064nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1065{
1066 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1067 struct nfs_delegation *delegation;
1068 int delegation_flags = 0;
1069
1070 rcu_read_lock();
1071 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1072 if (delegation)
1073 delegation_flags = delegation->flags;
1074 rcu_read_unlock();
1075 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1076 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1077 "returning a delegation for "
1078 "OPEN(CLAIM_DELEGATE_CUR)\n",
1079 clp->cl_hostname);
1080 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1081 nfs_inode_set_delegation(state->inode,
1082 data->owner->so_cred,
1083 &data->o_res);
1084 else
1085 nfs_inode_reclaim_delegation(state->inode,
1086 data->owner->so_cred,
1087 &data->o_res);
1088}
1089
1090/*
1091 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1092 * and update the nfs4_state.
1093 */
1094static struct nfs4_state *
1095_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1096{
1097 struct inode *inode = data->state->inode;
1098 struct nfs4_state *state = data->state;
1099 int ret;
1100
1101 if (!data->rpc_done) {
1102 ret = data->rpc_status;
1103 goto err;
1104 }
1105
1106 ret = -ESTALE;
1107 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1108 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1109 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1110 goto err;
1111
1112 ret = -ENOMEM;
1113 state = nfs4_get_open_state(inode, data->owner);
1114 if (state == NULL)
1115 goto err;
1116
1117 ret = nfs_refresh_inode(inode, &data->f_attr);
1118 if (ret)
1119 goto err;
1120
1121 if (data->o_res.delegation_type != 0)
1122 nfs4_opendata_check_deleg(data, state);
1123 update_open_stateid(state, &data->o_res.stateid, NULL,
1124 data->o_arg.fmode);
1125
1126 return state;
1127err:
1128 return ERR_PTR(ret);
1129
1130}
1131
1132static struct nfs4_state *
1133_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134{
1135 struct inode *inode;
1136 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001137 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138
Trond Myklebustaac00a82007-07-05 19:02:21 -04001139 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001140 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001141 goto out;
1142 }
1143
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001144 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001145 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001146 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001148 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001149 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001150 goto err;
1151 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001152 state = nfs4_get_open_state(inode, data->owner);
1153 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001154 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001155 if (data->o_res.delegation_type != 0)
1156 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001157 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001159 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001160out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001161 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001162err_put_inode:
1163 iput(inode);
1164err:
1165 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001166}
1167
Andy Adamsone23008e2012-10-02 21:07:32 -04001168static struct nfs4_state *
1169nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1170{
1171 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1172 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1173 return _nfs4_opendata_to_nfs4_state(data);
1174}
1175
Trond Myklebust864472e2006-01-03 09:55:15 +01001176static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1177{
1178 struct nfs_inode *nfsi = NFS_I(state->inode);
1179 struct nfs_open_context *ctx;
1180
1181 spin_lock(&state->inode->i_lock);
1182 list_for_each_entry(ctx, &nfsi->open_files, list) {
1183 if (ctx->state != state)
1184 continue;
1185 get_nfs_open_context(ctx);
1186 spin_unlock(&state->inode->i_lock);
1187 return ctx;
1188 }
1189 spin_unlock(&state->inode->i_lock);
1190 return ERR_PTR(-ENOENT);
1191}
1192
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001193static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1194{
1195 struct nfs4_opendata *opendata;
1196
Al Viro3d4ff432011-06-22 18:40:12 -04001197 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001198 if (opendata == NULL)
1199 return ERR_PTR(-ENOMEM);
1200 opendata->state = state;
1201 atomic_inc(&state->count);
1202 return opendata;
1203}
1204
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001205static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001206{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 int ret;
1209
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 opendata->o_arg.open_flags = 0;
1211 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1213 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1214 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001215 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 if (ret != 0)
1217 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219 if (IS_ERR(newstate))
1220 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001221 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 return 0;
1224}
1225
1226static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1227{
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 int ret;
1230
1231 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 smp_rmb();
1234 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001235 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 if (ret != 0)
1238 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001239 if (newstate != state)
1240 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 }
1242 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001243 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 if (ret != 0)
1246 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 if (newstate != state)
1248 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 }
1250 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001251 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 if (ret != 0)
1254 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 if (newstate != state)
1256 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001258 /*
1259 * We may have performed cached opens for all three recoveries.
1260 * Check if we need to update the current stateid.
1261 */
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001263 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001264 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001266 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001267 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 return 0;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
1273 * OPEN_RECLAIM:
1274 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001278 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001280 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int status;
1282
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001283 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1284 if (IS_ERR(opendata))
1285 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1287 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001290 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001291 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001292 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 opendata->o_arg.u.delegation_type = delegation_type;
1294 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return status;
1297}
1298
Trond Myklebust539cd032007-06-05 11:46:42 -04001299static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs_server *server = NFS_SERVER(state->inode);
1302 struct nfs4_exception exception = { };
1303 int err;
1304 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001305 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001306 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001307 break;
1308 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 } while (exception.retry);
1310 return err;
1311}
1312
Trond Myklebust864472e2006-01-03 09:55:15 +01001313static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1314{
1315 struct nfs_open_context *ctx;
1316 int ret;
1317
1318 ctx = nfs4_state_find_open_context(state);
1319 if (IS_ERR(ctx))
1320 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 put_nfs_open_context(ctx);
1323 return ret;
1324}
1325
Trond Myklebust13437e12007-07-06 15:10:43 -04001326static 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 -07001327{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001328 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001331 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1332 if (IS_ERR(opendata))
1333 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001334 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001336 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001337 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Trond Myklebust13437e12007-07-06 15:10:43 -04001341int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001344 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int err;
1346 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001347 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 switch (err) {
1349 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350 case -ENOENT:
1351 case -ESTALE:
1352 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001353 case -NFS4ERR_BADSESSION:
1354 case -NFS4ERR_BADSLOT:
1355 case -NFS4ERR_BAD_HIGH_SLOT:
1356 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1357 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001358 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 case -NFS4ERR_STALE_CLIENTID:
1361 case -NFS4ERR_STALE_STATEID:
1362 case -NFS4ERR_EXPIRED:
1363 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001364 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001365 goto out;
1366 case -ERESTARTSYS:
1367 /*
1368 * The show must go on: exit, but mark the
1369 * stateid as needing recovery.
1370 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001371 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001372 case -NFS4ERR_ADMIN_REVOKED:
1373 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001374 nfs_inode_find_state_and_recover(state->inode,
1375 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001376 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001377 case -ENOMEM:
1378 err = 0;
1379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381 err = nfs4_handle_exception(server, err, &exception);
1382 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001383out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return err;
1385}
1386
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001387static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1388{
1389 struct nfs4_opendata *data = calldata;
1390
1391 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001392 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001393 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001394 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001395 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001396 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001397 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398}
1399
1400static void nfs4_open_confirm_release(void *calldata)
1401{
1402 struct nfs4_opendata *data = calldata;
1403 struct nfs4_state *state = NULL;
1404
1405 /* If this request hasn't been cancelled, do nothing */
1406 if (data->cancelled == 0)
1407 goto out_free;
1408 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001409 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001410 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001411 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001412 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001413 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001414out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001415 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416}
1417
1418static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419 .rpc_call_done = nfs4_open_confirm_done,
1420 .rpc_release = nfs4_open_confirm_release,
1421};
1422
1423/*
1424 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1425 */
1426static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1427{
1428 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1429 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001430 struct rpc_message msg = {
1431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1432 .rpc_argp = &data->c_arg,
1433 .rpc_resp = &data->c_res,
1434 .rpc_cred = data->owner->so_cred,
1435 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001436 struct rpc_task_setup task_setup_data = {
1437 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001438 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 .callback_ops = &nfs4_open_confirm_ops,
1440 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001441 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001442 .flags = RPC_TASK_ASYNC,
1443 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int status;
1445
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001446 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001447 data->rpc_done = 0;
1448 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001449 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001450 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001451 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001452 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001453 status = nfs4_wait_for_completion_rpc_task(task);
1454 if (status != 0) {
1455 data->cancelled = 1;
1456 smp_wmb();
1457 } else
1458 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001459 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return status;
1461}
1462
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001463static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 struct nfs4_opendata *data = calldata;
1466 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1469 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001470 /*
1471 * Check if we still need to send an OPEN call, or if we can use
1472 * a delegation instead.
1473 */
1474 if (data->state != NULL) {
1475 struct nfs_delegation *delegation;
1476
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001478 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001479 rcu_read_lock();
1480 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001481 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1482 can_open_delegated(delegation, data->o_arg.fmode))
1483 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001484 rcu_read_unlock();
1485 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001486 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001487 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001488 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001489 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001490 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001491 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1492 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001493 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001494 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001495 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001496 &data->o_res.seq_res,
1497 task) != 0)
1498 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001499 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001500unlock_no_action:
1501 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001502out_no_action:
1503 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001504 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001505}
1506
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507static void nfs4_open_done(struct rpc_task *task, void *calldata)
1508{
1509 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001512
Trond Myklebust14516c32010-07-31 14:29:06 -04001513 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1514 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001515
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001517 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1518 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519 case S_IFREG:
1520 break;
1521 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 break;
1524 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001526 break;
1527 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001529 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001531 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001532 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1533 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001535 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001537
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538static void nfs4_open_release(void *calldata)
1539{
1540 struct nfs4_opendata *data = calldata;
1541 struct nfs4_state *state = NULL;
1542
1543 /* If this request hasn't been cancelled, do nothing */
1544 if (data->cancelled == 0)
1545 goto out_free;
1546 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001547 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 goto out_free;
1549 /* In case we need an open_confirm, no cleanup! */
1550 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1551 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001553 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001554 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001555out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001556 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557}
1558
1559static const struct rpc_call_ops nfs4_open_ops = {
1560 .rpc_call_prepare = nfs4_open_prepare,
1561 .rpc_call_done = nfs4_open_done,
1562 .rpc_release = nfs4_open_release,
1563};
1564
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001565static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001566{
1567 struct inode *dir = data->dir->d_inode;
1568 struct nfs_server *server = NFS_SERVER(dir);
1569 struct nfs_openargs *o_arg = &data->o_arg;
1570 struct nfs_openres *o_res = &data->o_res;
1571 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001572 struct rpc_message msg = {
1573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1574 .rpc_argp = o_arg,
1575 .rpc_resp = o_res,
1576 .rpc_cred = data->owner->so_cred,
1577 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001578 struct rpc_task_setup task_setup_data = {
1579 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001580 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 .callback_ops = &nfs4_open_ops,
1582 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001583 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001584 .flags = RPC_TASK_ASYNC,
1585 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586 int status;
1587
Trond Myklebust9d12b212012-01-17 22:04:25 -05001588 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001589 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001590 data->rpc_done = 0;
1591 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001592 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001593 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001594 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001596 if (IS_ERR(task))
1597 return PTR_ERR(task);
1598 status = nfs4_wait_for_completion_rpc_task(task);
1599 if (status != 0) {
1600 data->cancelled = 1;
1601 smp_wmb();
1602 } else
1603 status = data->rpc_status;
1604 rpc_put_task(task);
1605
1606 return status;
1607}
1608
1609static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1610{
1611 struct inode *dir = data->dir->d_inode;
1612 struct nfs_openres *o_res = &data->o_res;
1613 int status;
1614
1615 status = nfs4_run_open_task(data, 1);
1616 if (status != 0 || !data->rpc_done)
1617 return status;
1618
Trond Myklebust6926afd2012-01-07 13:22:46 -05001619 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1620
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001621 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1622 status = _nfs4_proc_open_confirm(data);
1623 if (status != 0)
1624 return status;
1625 }
1626
1627 return status;
1628}
1629
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001630static int nfs4_opendata_access(struct rpc_cred *cred,
1631 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001632 struct nfs4_state *state, fmode_t fmode,
1633 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001634{
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
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001645 * write mode bits, but POSIX allows the creating process to write.
1646 * use openflags to check for exec, because fmode won't
1647 * always have FMODE_EXEC set when file open for exec. */
1648 if (openflags & __FMODE_EXEC) {
1649 /* ONLY check for exec rights */
1650 mask = MAY_EXEC;
1651 } else if (fmode & FMODE_READ)
1652 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001653
1654 cache.cred = cred;
1655 cache.jiffies = jiffies;
1656 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1657 nfs_access_add_cache(state->inode, &cache);
1658
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001659 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001660 return 0;
1661
1662 /* even though OPEN succeeded, access is denied. Close the file */
1663 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001664 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001665}
1666
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001667/*
1668 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1669 */
1670static int _nfs4_proc_open(struct nfs4_opendata *data)
1671{
1672 struct inode *dir = data->dir->d_inode;
1673 struct nfs_server *server = NFS_SERVER(dir);
1674 struct nfs_openargs *o_arg = &data->o_arg;
1675 struct nfs_openres *o_res = &data->o_res;
1676 int status;
1677
1678 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001679 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001681 if (status != 0) {
1682 if (status == -NFS4ERR_BADNAME &&
1683 !(o_arg->open_flags & O_CREAT))
1684 return -ENOENT;
1685 return status;
1686 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687
Trond Myklebust6926afd2012-01-07 13:22:46 -05001688 nfs_fattr_map_and_free_names(server, &data->f_attr);
1689
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001690 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001691 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001692 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1693 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001695 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001697 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001700 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Andy Adamsond83217c2011-03-01 01:34:17 +00001704static int nfs4_recover_expired_lease(struct nfs_server *server)
1705{
1706 return nfs4_client_recover_expired_lease(server->nfs_client);
1707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/*
1710 * OPEN_EXPIRED:
1711 * reclaim state on the server after a network partition.
1712 * Assumes caller holds the appropriate lock
1713 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001714static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001716 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001717 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001719 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1720 if (IS_ERR(opendata))
1721 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001722 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001723 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001724 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001725 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001726 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001729static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001730{
Trond Myklebust539cd032007-06-05 11:46:42 -04001731 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001732 struct nfs4_exception exception = { };
1733 int err;
1734
1735 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001736 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001737 switch (err) {
1738 default:
1739 goto out;
1740 case -NFS4ERR_GRACE:
1741 case -NFS4ERR_DELAY:
1742 nfs4_handle_exception(server, err, &exception);
1743 err = 0;
1744 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001745 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001746out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001747 return err;
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001753 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Trond Myklebust864472e2006-01-03 09:55:15 +01001755 ctx = nfs4_state_find_open_context(state);
1756 if (IS_ERR(ctx))
1757 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001758 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001759 put_nfs_open_context(ctx);
1760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001764static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001765{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001766 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001767 nfs4_stateid *stateid = &state->stateid;
1768 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001769
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001770 /* If a state reset has been done, test_stateid is unneeded */
1771 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1772 return;
1773
1774 status = nfs41_test_stateid(server, stateid);
1775 if (status != NFS_OK) {
1776 /* Free the stateid unless the server explicitly
1777 * informs us the stateid is unrecognized. */
1778 if (status != -NFS4ERR_BAD_STATEID)
1779 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001780 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001781
Bryan Schumaker69388672012-09-26 15:25:52 -04001782 write_seqlock(&state->seqlock);
1783 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1784 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001785 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1786 }
1787}
1788
1789/**
1790 * nfs41_check_open_stateid - possibly free an open stateid
1791 *
1792 * @state: NFSv4 state for an inode
1793 *
1794 * Returns NFS_OK if recovery for this stateid is now finished.
1795 * Otherwise a negative NFS4ERR value is returned.
1796 */
1797static int nfs41_check_open_stateid(struct nfs4_state *state)
1798{
1799 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001800 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001801 int status;
1802
1803 /* If a state reset has been done, test_stateid is unneeded */
1804 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1805 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1806 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1807 return -NFS4ERR_BAD_STATEID;
1808
1809 status = nfs41_test_stateid(server, stateid);
1810 if (status != NFS_OK) {
1811 /* Free the stateid unless the server explicitly
1812 * informs us the stateid is unrecognized. */
1813 if (status != -NFS4ERR_BAD_STATEID)
1814 nfs41_free_stateid(server, stateid);
1815
1816 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1817 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1818 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001819 }
1820 return status;
1821}
1822
1823static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1824{
Chuck Levereb64cf92012-07-11 16:30:05 -04001825 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001826
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001827 nfs41_clear_delegation_stateid(state);
1828 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001829 if (status != NFS_OK)
1830 status = nfs4_open_expired(sp, state);
1831 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001832}
1833#endif
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001836 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1837 * fields corresponding to attributes that were used to store the verifier.
1838 * Make sure we clobber those fields in the later setattr call
1839 */
1840static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1841{
1842 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1843 !(sattr->ia_valid & ATTR_ATIME_SET))
1844 sattr->ia_valid |= ATTR_ATIME;
1845
1846 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1847 !(sattr->ia_valid & ATTR_MTIME_SET))
1848 sattr->ia_valid |= ATTR_MTIME;
1849}
1850
1851/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Andy Adamson82be4172012-05-23 05:02:35 -04001854static int _nfs4_do_open(struct inode *dir,
1855 struct dentry *dentry,
1856 fmode_t fmode,
1857 int flags,
1858 struct iattr *sattr,
1859 struct rpc_cred *cred,
1860 struct nfs4_state **res,
1861 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct nfs4_state_owner *sp;
1864 struct nfs4_state *state = NULL;
1865 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001866 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001867 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001871 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1872 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1874 goto out_err;
1875 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001876 status = nfs4_recover_expired_lease(server);
1877 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001878 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001879 if (dentry->d_inode != NULL)
1880 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001881 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001882 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001883 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001884 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Andy Adamson82be4172012-05-23 05:02:35 -04001886 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1887 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1888 if (!opendata->f_attr.mdsthreshold)
1889 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001890 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001891 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001892 if (dentry->d_inode != NULL)
1893 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001894
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001895 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001897 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001899 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001900 status = PTR_ERR(state);
1901 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001902 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001903 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001904 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001905
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001906 status = nfs4_opendata_access(cred, opendata, state, fmode, flags);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001907 if (status != 0)
1908 goto err_opendata_put;
1909
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001910 if (opendata->o_arg.open_flags & O_EXCL) {
1911 nfs4_exclusive_attrset(opendata, sattr);
1912
1913 nfs_fattr_init(opendata->o_res.f_attr);
1914 status = nfs4_do_setattr(state->inode, cred,
1915 opendata->o_res.f_attr, sattr,
1916 state);
1917 if (status == 0)
1918 nfs_setattr_update_inode(state->inode, sattr);
1919 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1920 }
Andy Adamson82be4172012-05-23 05:02:35 -04001921
1922 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1923 *ctx_th = opendata->f_attr.mdsthreshold;
1924 else
1925 kfree(opendata->f_attr.mdsthreshold);
1926 opendata->f_attr.mdsthreshold = NULL;
1927
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001928 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 *res = state;
1931 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001932err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001933 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001934 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001935err_put_state_owner:
1936 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 *res = NULL;
1939 return status;
1940}
1941
1942
Andy Adamson82be4172012-05-23 05:02:35 -04001943static struct nfs4_state *nfs4_do_open(struct inode *dir,
1944 struct dentry *dentry,
1945 fmode_t fmode,
1946 int flags,
1947 struct iattr *sattr,
1948 struct rpc_cred *cred,
1949 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 struct nfs4_exception exception = { };
1952 struct nfs4_state *res;
1953 int status;
1954
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001955 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001957 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1958 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (status == 0)
1960 break;
1961 /* NOTE: BAD_SEQID means the server and client disagree about the
1962 * book-keeping w.r.t. state-changing operations
1963 * (OPEN/CLOSE/LOCK/LOCKU...)
1964 * It is actually a sign of a bug on the client or on the server.
1965 *
1966 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001967 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 * have unhashed the old state_owner for us, and that we can
1969 * therefore safely retry using a new one. We should still warn
1970 * the user though...
1971 */
1972 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001973 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001974 " returned a bad sequence-id error!\n",
1975 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 exception.retry = 1;
1977 continue;
1978 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001979 /*
1980 * BAD_STATEID on OPEN means that the server cancelled our
1981 * state before it received the OPEN_CONFIRM.
1982 * Recover by retrying the request as per the discussion
1983 * on Page 181 of RFC3530.
1984 */
1985 if (status == -NFS4ERR_BAD_STATEID) {
1986 exception.retry = 1;
1987 continue;
1988 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001989 if (status == -EAGAIN) {
1990 /* We must have found a delegation */
1991 exception.retry = 1;
1992 continue;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1995 status, &exception));
1996 } while (exception.retry);
1997 return res;
1998}
1999
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002000static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2001 struct nfs_fattr *fattr, struct iattr *sattr,
2002 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002004 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002006 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 .iap = sattr,
2008 .server = server,
2009 .bitmask = server->attr_bitmask,
2010 };
2011 struct nfs_setattrres res = {
2012 .fattr = fattr,
2013 .server = server,
2014 };
2015 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2017 .rpc_argp = &arg,
2018 .rpc_resp = &res,
2019 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002021 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002022 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Trond Myklebust0e574af2005-10-27 22:12:38 -04002024 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Trond Myklebust4fc87962012-03-08 17:42:01 -05002026 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002027 struct nfs_lockowner lockowner = {
2028 .l_owner = current->files,
2029 .l_pid = current->tgid,
2030 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002031 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002032 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002033 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2034 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002035 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002036 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002037 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Bryan Schumaker7c513052011-03-24 17:12:24 +00002039 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002040 if (status == 0 && state != NULL)
2041 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002042 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002045static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2046 struct nfs_fattr *fattr, struct iattr *sattr,
2047 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002049 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002050 struct nfs4_exception exception = {
2051 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002052 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002053 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int err;
2055 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002056 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2057 switch (err) {
2058 case -NFS4ERR_OPENMODE:
2059 if (state && !(state->state & FMODE_WRITE)) {
2060 err = -EBADF;
2061 if (sattr->ia_valid & ATTR_OPEN)
2062 err = -EACCES;
2063 goto out;
2064 }
2065 }
2066 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002068out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return err;
2070}
2071
2072struct nfs4_closedata {
2073 struct inode *inode;
2074 struct nfs4_state *state;
2075 struct nfs_closeargs arg;
2076 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002077 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002078 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002079 bool roc;
2080 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081};
2082
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002083static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002084{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002085 struct nfs4_closedata *calldata = data;
2086 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002087 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002088
Fred Isamanf7e89172011-01-06 11:36:32 +00002089 if (calldata->roc)
2090 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002091 nfs4_put_open_state(calldata->state);
2092 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002093 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002094 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002095 kfree(calldata);
2096}
2097
Trond Myklebust88069f72009-12-08 08:33:16 -05002098static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2099 fmode_t fmode)
2100{
2101 spin_lock(&state->owner->so_lock);
2102 if (!(fmode & FMODE_READ))
2103 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2104 if (!(fmode & FMODE_WRITE))
2105 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2106 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2107 spin_unlock(&state->owner->so_lock);
2108}
2109
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002110static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002112 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct nfs_server *server = NFS_SERVER(calldata->inode);
2115
Chuck Levera3ca5652012-03-01 17:00:40 -05002116 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002117 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /* hmm. we are done with the inode, and in the process of freeing
2120 * the state_owner. we keep this around to process errors
2121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 switch (task->tk_status) {
2123 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002124 if (calldata->roc)
2125 pnfs_roc_set_barrier(state->inode,
2126 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002127 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002129 nfs4_close_clear_stateid_flags(state,
2130 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 break;
2132 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002133 case -NFS4ERR_OLD_STATEID:
2134 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002136 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002137 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002139 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2140 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002142 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002143 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002144 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002147static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002149 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002150 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002151 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002152 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002153
Chuck Levera3ca5652012-03-01 17:00:40 -05002154 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002155 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002156 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002157
Trond Myklebust88069f72009-12-08 08:33:16 -05002158 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2159 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002160 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002161 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002162 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002163 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002164 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2165 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2166 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002167 }
2168 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002169 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2170 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2171 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002172 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002173 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002174 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002175
2176 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002177 /* Note: exit _without_ calling nfs4_close_done */
2178 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002179 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002180 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002181 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002182
Fred Isamanf7e89172011-01-06 11:36:32 +00002183 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002184 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002185 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002186 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002187 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002188 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002189
Trond Myklebust516a6af2005-10-27 22:12:41 -04002190 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002191 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002192 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002193 &calldata->arg.seq_args,
2194 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002195 task) != 0)
2196 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002197out:
2198 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002201static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002202 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002203 .rpc_call_done = nfs4_close_done,
2204 .rpc_release = nfs4_free_closedata,
2205};
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/*
2208 * It is possible for data to be read/written from a mem-mapped file
2209 * after the sys_close call (which hits the vfs layer as a flush).
2210 * This means that we can't safely call nfsv4 close on a file until
2211 * the inode is cleared. This in turn means that we are not good
2212 * NFSv4 citizens - we do not indicate to the server to update the file's
2213 * share state even when we are done with one of the three share
2214 * stateid's in the inode.
2215 *
2216 * NOTE: Caller must be holding the sp->so_owner semaphore!
2217 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002218int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002220 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002222 struct nfs4_state_owner *sp = state->owner;
2223 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2226 .rpc_cred = state->owner->so_cred,
2227 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002228 struct rpc_task_setup task_setup_data = {
2229 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002230 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002231 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002232 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002233 .flags = RPC_TASK_ASYNC,
2234 };
Trond Myklebust95121352005-10-18 14:20:12 -07002235 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002237 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002239 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002240 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002241 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002243 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002244 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002246 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002247 if (calldata->arg.seqid == NULL)
2248 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002249 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002250 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002251 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002252 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002253 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002254 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002255 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002256
Trond Myklebust1174dd12010-12-21 10:52:24 -05002257 msg.rpc_argp = &calldata->arg;
2258 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002259 task_setup_data.callback_data = calldata;
2260 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002261 if (IS_ERR(task))
2262 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002263 status = 0;
2264 if (wait)
2265 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002266 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002267 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002268out_free_calldata:
2269 kfree(calldata);
2270out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002271 nfs4_put_open_state(state);
2272 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002273 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Trond Myklebust2b484292010-09-17 10:56:51 -04002276static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002277nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct nfs4_state *state;
2280
Trond Myklebust565277f2007-10-15 18:17:53 -04002281 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002282 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2283 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002284 if (IS_ERR(state))
2285 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002286 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002287 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Trond Myklebust1185a552009-12-03 15:54:02 -05002290static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002291{
2292 if (ctx->state == NULL)
2293 return;
2294 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002295 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002296 else
Al Viro643168c2011-06-22 18:20:23 -04002297 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2301{
Benny Halevy43652ad2009-04-01 09:21:54 -04002302 struct nfs4_server_caps_arg args = {
2303 .fhandle = fhandle,
2304 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct nfs4_server_caps_res res = {};
2306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002308 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 .rpc_resp = &res,
2310 };
2311 int status;
2312
Bryan Schumaker7c513052011-03-24 17:12:24 +00002313 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (status == 0) {
2315 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002316 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2317 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2318 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2319 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2320 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2322 server->caps |= NFS_CAP_ACLS;
2323 if (res.has_links != 0)
2324 server->caps |= NFS_CAP_HARDLINKS;
2325 if (res.has_symlinks != 0)
2326 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002327 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2328 server->caps |= NFS_CAP_FILEID;
2329 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2330 server->caps |= NFS_CAP_MODE;
2331 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2332 server->caps |= NFS_CAP_NLINK;
2333 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2334 server->caps |= NFS_CAP_OWNER;
2335 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2336 server->caps |= NFS_CAP_OWNER_GROUP;
2337 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2338 server->caps |= NFS_CAP_ATIME;
2339 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2340 server->caps |= NFS_CAP_CTIME;
2341 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2342 server->caps |= NFS_CAP_MTIME;
2343
Trond Myklebusta65318b2009-03-11 14:10:28 -04002344 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2345 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2346 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002348 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return status;
2352}
2353
Trond Myklebust55a97592006-06-09 09:34:19 -04002354int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 struct nfs4_exception exception = { };
2357 int err;
2358 do {
2359 err = nfs4_handle_exception(server,
2360 _nfs4_server_capabilities(server, fhandle),
2361 &exception);
2362 } while (exception.retry);
2363 return err;
2364}
2365
2366static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2367 struct nfs_fsinfo *info)
2368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 struct nfs4_lookup_root_arg args = {
2370 .bitmask = nfs4_fattr_bitmap,
2371 };
2372 struct nfs4_lookup_res res = {
2373 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002374 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 .fh = fhandle,
2376 };
2377 struct rpc_message msg = {
2378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2379 .rpc_argp = &args,
2380 .rpc_resp = &res,
2381 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002382
Trond Myklebust0e574af2005-10-27 22:12:38 -04002383 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002384 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
2387static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2388 struct nfs_fsinfo *info)
2389{
2390 struct nfs4_exception exception = { };
2391 int err;
2392 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002393 err = _nfs4_lookup_root(server, fhandle, info);
2394 switch (err) {
2395 case 0:
2396 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002397 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002398 default:
2399 err = nfs4_handle_exception(server, err, &exception);
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002402out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return err;
2404}
2405
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002406static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2407 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2408{
2409 struct rpc_auth *auth;
2410 int ret;
2411
2412 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002413 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002414 ret = -EIO;
2415 goto out;
2416 }
2417 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002418out:
2419 return ret;
2420}
2421
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002422static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002423 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002425 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002426 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002428 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002429 if (len < 0)
2430 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002431
2432 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002433 /* AUTH_UNIX is the default flavor if none was specified,
2434 * thus has already been tried. */
2435 if (flav_array[i] == RPC_AUTH_UNIX)
2436 continue;
2437
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002438 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002439 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002440 continue;
2441 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002442 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002443 /*
2444 * -EACCESS could mean that the user doesn't have correct permissions
2445 * to access the mount. It could also mean that we tried to mount
2446 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2447 * existing mount programs don't handle -EACCES very well so it should
2448 * be mapped to -EPERM instead.
2449 */
2450 if (status == -EACCES)
2451 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002452 return status;
2453}
2454
2455/*
2456 * get the file handle for the "/" directory on the server
2457 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002458int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2459 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002460{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002461 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002462 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002463 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2464 /*
2465 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2466 * by nfs4_map_errors() as this function exits.
2467 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002468 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (status == 0)
2470 status = nfs4_server_capabilities(server, fhandle);
2471 if (status == 0)
2472 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002473 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Bryan Schumakerbae36242012-05-10 15:07:31 -04002476static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2477 struct nfs_fsinfo *info)
2478{
2479 int error;
2480 struct nfs_fattr *fattr = info->fattr;
2481
2482 error = nfs4_server_capabilities(server, mntfh);
2483 if (error < 0) {
2484 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2485 return error;
2486 }
2487
2488 error = nfs4_proc_getattr(server, mntfh, fattr);
2489 if (error < 0) {
2490 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2491 return error;
2492 }
2493
2494 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2495 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2496 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2497
2498 return error;
2499}
2500
Manoj Naik6b97fd32006-06-09 09:34:29 -04002501/*
2502 * Get locations and (maybe) other attributes of a referral.
2503 * Note that we'll actually follow the referral later when
2504 * we detect fsid mismatch in inode revalidation
2505 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002506static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2507 const struct qstr *name, struct nfs_fattr *fattr,
2508 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002509{
2510 int status = -ENOMEM;
2511 struct page *page = NULL;
2512 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002513
2514 page = alloc_page(GFP_KERNEL);
2515 if (page == NULL)
2516 goto out;
2517 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2518 if (locations == NULL)
2519 goto out;
2520
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002521 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002522 if (status != 0)
2523 goto out;
2524 /* Make sure server returned a different fsid for the referral */
2525 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002526 dprintk("%s: server did not return a different fsid for"
2527 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002528 status = -EIO;
2529 goto out;
2530 }
Andy Adamson533eb462011-06-13 18:25:56 -04002531 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2532 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002533
Andy Adamson533eb462011-06-13 18:25:56 -04002534 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002535 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002536 memset(fhandle, 0, sizeof(struct nfs_fh));
2537out:
2538 if (page)
2539 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002540 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002541 return status;
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2545{
2546 struct nfs4_getattr_arg args = {
2547 .fh = fhandle,
2548 .bitmask = server->attr_bitmask,
2549 };
2550 struct nfs4_getattr_res res = {
2551 .fattr = fattr,
2552 .server = server,
2553 };
2554 struct rpc_message msg = {
2555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2556 .rpc_argp = &args,
2557 .rpc_resp = &res,
2558 };
2559
Trond Myklebust0e574af2005-10-27 22:12:38 -04002560 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002561 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
2564static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2565{
2566 struct nfs4_exception exception = { };
2567 int err;
2568 do {
2569 err = nfs4_handle_exception(server,
2570 _nfs4_proc_getattr(server, fhandle, fattr),
2571 &exception);
2572 } while (exception.retry);
2573 return err;
2574}
2575
2576/*
2577 * The file is not closed if it is opened due to the a request to change
2578 * the size of the file. The open call will not be needed once the
2579 * VFS layer lookup-intents are implemented.
2580 *
2581 * Close is called when the inode is destroyed.
2582 * If we haven't opened the file for O_WRONLY, we
2583 * need to in the size_change case to obtain a stateid.
2584 *
2585 * Got race?
2586 * Because OPEN is always done by name in nfsv4, it is
2587 * possible that we opened a different file by the same
2588 * name. We can recognize this race condition, but we
2589 * can't do anything about it besides returning an error.
2590 *
2591 * This will be fixed with VFS changes (lookup-intent).
2592 */
2593static int
2594nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2595 struct iattr *sattr)
2596{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002597 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002598 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002599 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 int status;
2601
Benny Halevy8a1636c2010-07-14 15:43:57 -04002602 if (pnfs_ld_layoutret_on_setattr(inode))
2603 pnfs_return_layout(inode);
2604
Trond Myklebust0e574af2005-10-27 22:12:38 -04002605 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Andy Adamson26699402012-05-30 16:12:24 -04002607 /* Deal with open(O_TRUNC) */
2608 if (sattr->ia_valid & ATTR_OPEN)
2609 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2610
2611 /* Optimization: if the end result is no change, don't RPC */
2612 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2613 return 0;
2614
Trond Myklebustd5308382005-11-04 15:33:38 -05002615 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002616 if (sattr->ia_valid & ATTR_FILE) {
2617 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002618
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002619 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002620 if (ctx) {
2621 cred = ctx->cred;
2622 state = ctx->state;
2623 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002624 }
2625
2626 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002627 if (status == 0)
2628 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return status;
2630}
2631
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002632static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2633 const struct qstr *name, struct nfs_fh *fhandle,
2634 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002635{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002636 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002637 int status;
2638 struct nfs4_lookup_arg args = {
2639 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002640 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002641 .name = name,
2642 };
2643 struct nfs4_lookup_res res = {
2644 .server = server,
2645 .fattr = fattr,
2646 .fh = fhandle,
2647 };
2648 struct rpc_message msg = {
2649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2650 .rpc_argp = &args,
2651 .rpc_resp = &res,
2652 };
2653
2654 nfs_fattr_init(fattr);
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002657 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 dprintk("NFS reply lookup: %d\n", status);
2659 return status;
2660}
2661
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002662static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002663{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002664 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002665 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002666 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2667 fattr->nlink = 2;
2668}
2669
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002670static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2671 struct qstr *name, struct nfs_fh *fhandle,
2672 struct nfs_fattr *fattr)
2673{
2674 struct nfs4_exception exception = { };
2675 struct rpc_clnt *client = *clnt;
2676 int err;
2677 do {
2678 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2679 switch (err) {
2680 case -NFS4ERR_BADNAME:
2681 err = -ENOENT;
2682 goto out;
2683 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002684 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002685 goto out;
2686 case -NFS4ERR_WRONGSEC:
2687 err = -EPERM;
2688 if (client != *clnt)
2689 goto out;
2690
2691 client = nfs4_create_sec_client(client, dir, name);
2692 if (IS_ERR(client))
2693 return PTR_ERR(client);
2694
2695 exception.retry = 1;
2696 break;
2697 default:
2698 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2699 }
2700 } while (exception.retry);
2701
2702out:
2703 if (err == 0)
2704 *clnt = client;
2705 else if (client != *clnt)
2706 rpc_shutdown_client(client);
2707
2708 return err;
2709}
2710
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002711static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002712 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002714 int status;
2715 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002716
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002717 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2718 if (client != NFS_CLIENT(dir)) {
2719 rpc_shutdown_client(client);
2720 nfs_fixup_secinfo_attributes(fattr);
2721 }
2722 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002725struct rpc_clnt *
2726nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2727 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2728{
2729 int status;
2730 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2731
2732 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2733 if (status < 0) {
2734 rpc_shutdown_client(client);
2735 return ERR_PTR(status);
2736 }
2737 return client;
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2741{
Trond Myklebust76b32992007-08-10 17:45:11 -04002742 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct nfs4_accessargs args = {
2744 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002745 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002747 struct nfs4_accessres res = {
2748 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 struct rpc_message msg = {
2751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2752 .rpc_argp = &args,
2753 .rpc_resp = &res,
2754 .rpc_cred = entry->cred,
2755 };
2756 int mode = entry->mask;
2757 int status;
2758
2759 /*
2760 * Determine which access bits we want to ask for...
2761 */
2762 if (mode & MAY_READ)
2763 args.access |= NFS4_ACCESS_READ;
2764 if (S_ISDIR(inode->i_mode)) {
2765 if (mode & MAY_WRITE)
2766 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2767 if (mode & MAY_EXEC)
2768 args.access |= NFS4_ACCESS_LOOKUP;
2769 } else {
2770 if (mode & MAY_WRITE)
2771 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2772 if (mode & MAY_EXEC)
2773 args.access |= NFS4_ACCESS_EXECUTE;
2774 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002775
2776 res.fattr = nfs_alloc_fattr();
2777 if (res.fattr == NULL)
2778 return -ENOMEM;
2779
Bryan Schumaker7c513052011-03-24 17:12:24 +00002780 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002782 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002783 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002785 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return status;
2787}
2788
2789static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2790{
2791 struct nfs4_exception exception = { };
2792 int err;
2793 do {
2794 err = nfs4_handle_exception(NFS_SERVER(inode),
2795 _nfs4_proc_access(inode, entry),
2796 &exception);
2797 } while (exception.retry);
2798 return err;
2799}
2800
2801/*
2802 * TODO: For the time being, we don't try to get any attributes
2803 * along with any of the zero-copy operations READ, READDIR,
2804 * READLINK, WRITE.
2805 *
2806 * In the case of the first three, we want to put the GETATTR
2807 * after the read-type operation -- this is because it is hard
2808 * to predict the length of a GETATTR response in v4, and thus
2809 * align the READ data correctly. This means that the GETATTR
2810 * may end up partially falling into the page cache, and we should
2811 * shift it into the 'tail' of the xdr_buf before processing.
2812 * To do this efficiently, we need to know the total length
2813 * of data received, which doesn't seem to be available outside
2814 * of the RPC layer.
2815 *
2816 * In the case of WRITE, we also want to put the GETATTR after
2817 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002818 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 *
2820 * Both of these changes to the XDR layer would in fact be quite
2821 * minor, but I decided to leave them for a subsequent patch.
2822 */
2823static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2824 unsigned int pgbase, unsigned int pglen)
2825{
2826 struct nfs4_readlink args = {
2827 .fh = NFS_FH(inode),
2828 .pgbase = pgbase,
2829 .pglen = pglen,
2830 .pages = &page,
2831 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002832 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct rpc_message msg = {
2834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2835 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002836 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 };
2838
Bryan Schumaker7c513052011-03-24 17:12:24 +00002839 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 -07002840}
2841
2842static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2843 unsigned int pgbase, unsigned int pglen)
2844{
2845 struct nfs4_exception exception = { };
2846 int err;
2847 do {
2848 err = nfs4_handle_exception(NFS_SERVER(inode),
2849 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2850 &exception);
2851 } while (exception.retry);
2852 return err;
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858static int
2859nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002860 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002862 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 int status = 0;
2865
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002866 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2867 if (IS_ERR(ctx))
2868 return PTR_ERR(ctx);
2869
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002870 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002871 state = nfs4_do_open(dir, dentry, ctx->mode,
2872 flags, sattr, ctx->cred,
2873 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002874 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (IS_ERR(state)) {
2876 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002879 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002880 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002881 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002883 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return status;
2885}
2886
2887static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2888{
Trond Myklebust16e42952005-10-27 22:12:44 -04002889 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002890 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002892 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002894 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002895 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002896 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2899 .rpc_argp = &args,
2900 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002902 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002903
Bryan Schumaker7c513052011-03-24 17:12:24 +00002904 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002905 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002906 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 return status;
2908}
2909
2910static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2911{
2912 struct nfs4_exception exception = { };
2913 int err;
2914 do {
2915 err = nfs4_handle_exception(NFS_SERVER(dir),
2916 _nfs4_proc_remove(dir, name),
2917 &exception);
2918 } while (exception.retry);
2919 return err;
2920}
2921
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002922static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002924 struct nfs_server *server = NFS_SERVER(dir);
2925 struct nfs_removeargs *args = msg->rpc_argp;
2926 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002928 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002930 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002933static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2934{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002935 nfs4_setup_sequence(NFS_SERVER(data->dir),
2936 &data->args.seq_args,
2937 &data->res.seq_res,
2938 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002941static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002943 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2944
Trond Myklebust14516c32010-07-31 14:29:06 -04002945 if (!nfs4_sequence_done(task, &res->seq_res))
2946 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002947 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002948 return 0;
2949 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002950 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
Jeff Laytond3d41522010-09-17 17:31:57 -04002953static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2954{
2955 struct nfs_server *server = NFS_SERVER(dir);
2956 struct nfs_renameargs *arg = msg->rpc_argp;
2957 struct nfs_renameres *res = msg->rpc_resp;
2958
2959 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002960 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002961 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002962}
2963
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002964static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2965{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002966 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2967 &data->args.seq_args,
2968 &data->res.seq_res,
2969 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002970}
2971
2972static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2973 struct inode *new_dir)
2974{
2975 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2976
2977 if (!nfs4_sequence_done(task, &res->seq_res))
2978 return 0;
2979 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2980 return 0;
2981
2982 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002983 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002984 return 1;
2985}
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2988 struct inode *new_dir, struct qstr *new_name)
2989{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002990 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002991 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .old_dir = NFS_FH(old_dir),
2993 .new_dir = NFS_FH(new_dir),
2994 .old_name = old_name,
2995 .new_name = new_name,
2996 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002997 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002998 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3002 .rpc_argp = &arg,
3003 .rpc_resp = &res,
3004 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003005 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Bryan Schumaker7c513052011-03-24 17:12:24 +00003007 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (!status) {
3009 update_changeattr(old_dir, &res.old_cinfo);
3010 update_changeattr(new_dir, &res.new_cinfo);
3011 }
3012 return status;
3013}
3014
3015static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3016 struct inode *new_dir, struct qstr *new_name)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020 do {
3021 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3022 _nfs4_proc_rename(old_dir, old_name,
3023 new_dir, new_name),
3024 &exception);
3025 } while (exception.retry);
3026 return err;
3027}
3028
3029static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3030{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003031 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct nfs4_link_arg arg = {
3033 .fh = NFS_FH(inode),
3034 .dir_fh = NFS_FH(dir),
3035 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003036 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003038 struct nfs4_link_res res = {
3039 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 struct rpc_message msg = {
3042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3043 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003044 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003046 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Trond Myklebust136f2622010-04-16 16:22:49 -04003048 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003049 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003050 goto out;
3051
Bryan Schumaker7c513052011-03-24 17:12:24 +00003052 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003053 if (!status) {
3054 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003055 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003056 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003057out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003058 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return status;
3060}
3061
3062static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3063{
3064 struct nfs4_exception exception = { };
3065 int err;
3066 do {
3067 err = nfs4_handle_exception(NFS_SERVER(inode),
3068 _nfs4_proc_link(inode, dir, name),
3069 &exception);
3070 } while (exception.retry);
3071 return err;
3072}
3073
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003074struct nfs4_createdata {
3075 struct rpc_message msg;
3076 struct nfs4_create_arg arg;
3077 struct nfs4_create_res res;
3078 struct nfs_fh fh;
3079 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003080};
3081
3082static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3083 struct qstr *name, struct iattr *sattr, u32 ftype)
3084{
3085 struct nfs4_createdata *data;
3086
3087 data = kzalloc(sizeof(*data), GFP_KERNEL);
3088 if (data != NULL) {
3089 struct nfs_server *server = NFS_SERVER(dir);
3090
3091 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3092 data->msg.rpc_argp = &data->arg;
3093 data->msg.rpc_resp = &data->res;
3094 data->arg.dir_fh = NFS_FH(dir);
3095 data->arg.server = server;
3096 data->arg.name = name;
3097 data->arg.attrs = sattr;
3098 data->arg.ftype = ftype;
3099 data->arg.bitmask = server->attr_bitmask;
3100 data->res.server = server;
3101 data->res.fh = &data->fh;
3102 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003104 }
3105 return data;
3106}
3107
3108static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3109{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003110 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003111 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 if (status == 0) {
3113 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003114 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3115 }
3116 return status;
3117}
3118
3119static void nfs4_free_createdata(struct nfs4_createdata *data)
3120{
3121 kfree(data);
3122}
3123
Chuck Lever4f390c12006-08-22 20:06:22 -04003124static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003125 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 struct nfs4_createdata *data;
3128 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Chuck Lever94a6d752006-08-22 20:06:23 -04003130 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003132
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003133 status = -ENOMEM;
3134 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3135 if (data == NULL)
3136 goto out;
3137
3138 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3139 data->arg.u.symlink.pages = &page;
3140 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003142 status = nfs4_do_create(dir, dentry, data);
3143
3144 nfs4_free_createdata(data);
3145out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return status;
3147}
3148
Chuck Lever4f390c12006-08-22 20:06:22 -04003149static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003150 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 struct nfs4_exception exception = { };
3153 int err;
3154 do {
3155 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003156 _nfs4_proc_symlink(dir, dentry, page,
3157 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3164 struct iattr *sattr)
3165{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003166 struct nfs4_createdata *data;
3167 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3170 if (data == NULL)
3171 goto out;
3172
3173 status = nfs4_do_create(dir, dentry, data);
3174
3175 nfs4_free_createdata(data);
3176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return status;
3178}
3179
3180static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3181 struct iattr *sattr)
3182{
3183 struct nfs4_exception exception = { };
3184 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003185
3186 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 do {
3188 err = nfs4_handle_exception(NFS_SERVER(dir),
3189 _nfs4_proc_mkdir(dir, dentry, sattr),
3190 &exception);
3191 } while (exception.retry);
3192 return err;
3193}
3194
3195static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003196 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct inode *dir = dentry->d_inode;
3199 struct nfs4_readdir_arg args = {
3200 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003201 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 .pgbase = 0,
3203 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003204 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003205 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 };
3207 struct nfs4_readdir_res res;
3208 struct rpc_message msg = {
3209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3210 .rpc_argp = &args,
3211 .rpc_resp = &res,
3212 .rpc_cred = cred,
3213 };
3214 int status;
3215
Harvey Harrison3110ff82008-05-02 13:42:44 -07003216 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003217 dentry->d_parent->d_name.name,
3218 dentry->d_name.name,
3219 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003220 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003222 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 -05003223 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003224 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003225 status += args.pgbase;
3226 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003227
3228 nfs_invalidate_atime(dir);
3229
Harvey Harrison3110ff82008-05-02 13:42:44 -07003230 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return status;
3232}
3233
3234static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003235 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 struct nfs4_exception exception = { };
3238 int err;
3239 do {
3240 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3241 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003242 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 &exception);
3244 } while (exception.retry);
3245 return err;
3246}
3247
3248static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3249 struct iattr *sattr, dev_t rdev)
3250{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251 struct nfs4_createdata *data;
3252 int mode = sattr->ia_mode;
3253 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003255 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3256 if (data == NULL)
3257 goto out;
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003260 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003262 data->arg.ftype = NF4BLK;
3263 data->arg.u.device.specdata1 = MAJOR(rdev);
3264 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 data->arg.ftype = NF4CHR;
3268 data->arg.u.device.specdata1 = MAJOR(rdev);
3269 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003270 } else if (!S_ISSOCK(mode)) {
3271 status = -EINVAL;
3272 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003275 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003276out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003277 nfs4_free_createdata(data);
3278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return status;
3280}
3281
3282static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3283 struct iattr *sattr, dev_t rdev)
3284{
3285 struct nfs4_exception exception = { };
3286 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003287
3288 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 do {
3290 err = nfs4_handle_exception(NFS_SERVER(dir),
3291 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3292 &exception);
3293 } while (exception.retry);
3294 return err;
3295}
3296
3297static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3298 struct nfs_fsstat *fsstat)
3299{
3300 struct nfs4_statfs_arg args = {
3301 .fh = fhandle,
3302 .bitmask = server->attr_bitmask,
3303 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003304 struct nfs4_statfs_res res = {
3305 .fsstat = fsstat,
3306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3309 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003310 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 };
3312
Trond Myklebust0e574af2005-10-27 22:12:38 -04003313 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003314 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3318{
3319 struct nfs4_exception exception = { };
3320 int err;
3321 do {
3322 err = nfs4_handle_exception(server,
3323 _nfs4_proc_statfs(server, fhandle, fsstat),
3324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
3329static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3330 struct nfs_fsinfo *fsinfo)
3331{
3332 struct nfs4_fsinfo_arg args = {
3333 .fh = fhandle,
3334 .bitmask = server->attr_bitmask,
3335 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003336 struct nfs4_fsinfo_res res = {
3337 .fsinfo = fsinfo,
3338 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct rpc_message msg = {
3340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3341 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003342 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 };
3344
Bryan Schumaker7c513052011-03-24 17:12:24 +00003345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
3348static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3349{
3350 struct nfs4_exception exception = { };
3351 int err;
3352
3353 do {
3354 err = nfs4_handle_exception(server,
3355 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3356 &exception);
3357 } while (exception.retry);
3358 return err;
3359}
3360
3361static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3362{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003363 int error;
3364
Trond Myklebust0e574af2005-10-27 22:12:38 -04003365 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003366 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003367 if (error == 0) {
3368 /* block layout checks this! */
3369 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003370 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003371 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003372
3373 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
3376static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3377 struct nfs_pathconf *pathconf)
3378{
3379 struct nfs4_pathconf_arg args = {
3380 .fh = fhandle,
3381 .bitmask = server->attr_bitmask,
3382 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003383 struct nfs4_pathconf_res res = {
3384 .pathconf = pathconf,
3385 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3388 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003389 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 };
3391
3392 /* None of the pathconf attributes are mandatory to implement */
3393 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3394 memset(pathconf, 0, sizeof(*pathconf));
3395 return 0;
3396 }
3397
Trond Myklebust0e574af2005-10-27 22:12:38 -04003398 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003399 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
3402static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3403 struct nfs_pathconf *pathconf)
3404{
3405 struct nfs4_exception exception = { };
3406 int err;
3407
3408 do {
3409 err = nfs4_handle_exception(server,
3410 _nfs4_proc_pathconf(server, fhandle, pathconf),
3411 &exception);
3412 } while (exception.retry);
3413 return err;
3414}
3415
Benny Halevyd20581a2011-05-22 19:52:03 +03003416void __nfs4_read_done_cb(struct nfs_read_data *data)
3417{
Fred Isamancd841602012-04-20 14:47:44 -04003418 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003419}
3420
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003421static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
Fred Isamancd841602012-04-20 14:47:44 -04003423 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003425 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003426 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003427 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003429
Benny Halevyd20581a2011-05-22 19:52:03 +03003430 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003432 renew_lease(server, data->timestamp);
3433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003436static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3437{
3438
3439 dprintk("--> %s\n", __func__);
3440
3441 if (!nfs4_sequence_done(task, &data->res.seq_res))
3442 return -EAGAIN;
3443
Benny Halevyd20581a2011-05-22 19:52:03 +03003444 return data->read_done_cb ? data->read_done_cb(task, data) :
3445 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003446}
3447
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003448static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003451 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003452 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003453 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003456static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3457{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003458 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3459 &data->args.seq_args,
3460 &data->res.seq_res,
3461 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Fred Isamanb029bc92011-03-03 15:13:42 +00003464static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
Fred Isamancd841602012-04-20 14:47:44 -04003466 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003468 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003469 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003470 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003472 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003474 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003475 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
Fred Isamanb029bc92011-03-03 15:13:42 +00003479static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3480{
3481 if (!nfs4_sequence_done(task, &data->res.seq_res))
3482 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003483 return data->write_done_cb ? data->write_done_cb(task, data) :
3484 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003485}
3486
Trond Myklebust5a37f852012-04-28 14:55:16 -04003487static
3488bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003489{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003490 const struct nfs_pgio_header *hdr = data->header;
3491
3492 /* Don't request attributes for pNFS or O_DIRECT writes */
3493 if (data->ds_clp != NULL || hdr->dreq != NULL)
3494 return false;
3495 /* Otherwise, request attributes if and only if we don't hold
3496 * a delegation
3497 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003498 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003499}
Fred Isamana69aef12011-03-03 15:13:47 +00003500
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003501static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Fred Isamancd841602012-04-20 14:47:44 -04003503 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003504
Trond Myklebust5a37f852012-04-28 14:55:16 -04003505 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003506 data->args.bitmask = NULL;
3507 data->res.fattr = NULL;
3508 } else
3509 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003510
Fred Isamanb029bc92011-03-03 15:13:42 +00003511 if (!data->write_done_cb)
3512 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003513 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 data->timestamp = jiffies;
3515
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003516 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003517 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003520static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3521{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003522 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3523 &data->args.seq_args,
3524 &data->res.seq_res,
3525 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Fred Isaman0b7c0152012-04-20 14:47:39 -04003528static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3529{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003530 nfs4_setup_sequence(NFS_SERVER(data->inode),
3531 &data->args.seq_args,
3532 &data->res.seq_res,
3533 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Fred Isaman0b7c0152012-04-20 14:47:39 -04003536static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003539
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003540 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003541 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003542 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Fred Isaman0b7c0152012-04-20 14:47:39 -04003547static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003548{
3549 if (!nfs4_sequence_done(task, &data->res.seq_res))
3550 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003551 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003552}
3553
Fred Isaman0b7c0152012-04-20 14:47:39 -04003554static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003556 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003557
Fred Isaman0b7c0152012-04-20 14:47:39 -04003558 if (data->commit_done_cb == NULL)
3559 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003560 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003561 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003562 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003565struct nfs4_renewdata {
3566 struct nfs_client *client;
3567 unsigned long timestamp;
3568};
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570/*
3571 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3572 * standalone procedure for queueing an asynchronous RENEW.
3573 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003574static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003575{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003576 struct nfs4_renewdata *data = calldata;
3577 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003578
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003579 if (atomic_read(&clp->cl_count) > 1)
3580 nfs4_schedule_state_renewal(clp);
3581 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003582 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003583}
3584
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003585static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003587 struct nfs4_renewdata *data = calldata;
3588 struct nfs_client *clp = data->client;
3589 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003592 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003593 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3594 return;
3595 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003596 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003597 return;
3598 }
3599 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003601 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003604static const struct rpc_call_ops nfs4_renew_ops = {
3605 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003606 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003607};
3608
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003609static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 struct rpc_message msg = {
3612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3613 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003614 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003616 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003618 if (renew_flags == 0)
3619 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003620 if (!atomic_inc_not_zero(&clp->cl_count))
3621 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003622 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003623 if (data == NULL)
3624 return -ENOMEM;
3625 data->client = clp;
3626 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003628 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003631static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
3633 struct rpc_message msg = {
3634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3635 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003636 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 };
3638 unsigned long now = jiffies;
3639 int status;
3640
3641 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3642 if (status < 0)
3643 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003644 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return 0;
3646}
3647
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003648static inline int nfs4_server_supports_acls(struct nfs_server *server)
3649{
3650 return (server->caps & NFS_CAP_ACLS)
3651 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3652 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3653}
3654
Trond Myklebust21f498c2012-08-24 10:59:25 -04003655/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3656 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003657 * the stack.
3658 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003659#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003660
Neil Hormane9e3d722011-03-04 19:26:03 -05003661static int buf_to_pages_noslab(const void *buf, size_t buflen,
3662 struct page **pages, unsigned int *pgbase)
3663{
3664 struct page *newpage, **spages;
3665 int rc = 0;
3666 size_t len;
3667 spages = pages;
3668
3669 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003670 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003671 newpage = alloc_page(GFP_KERNEL);
3672
3673 if (newpage == NULL)
3674 goto unwind;
3675 memcpy(page_address(newpage), buf, len);
3676 buf += len;
3677 buflen -= len;
3678 *pages++ = newpage;
3679 rc++;
3680 } while (buflen != 0);
3681
3682 return rc;
3683
3684unwind:
3685 for(; rc > 0; rc--)
3686 __free_page(spages[rc-1]);
3687 return -ENOMEM;
3688}
3689
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003690struct nfs4_cached_acl {
3691 int cached;
3692 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003693 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003694};
3695
3696static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003697{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003698 struct nfs_inode *nfsi = NFS_I(inode);
3699
3700 spin_lock(&inode->i_lock);
3701 kfree(nfsi->nfs4_acl);
3702 nfsi->nfs4_acl = acl;
3703 spin_unlock(&inode->i_lock);
3704}
3705
3706static void nfs4_zap_acl_attr(struct inode *inode)
3707{
3708 nfs4_set_cached_acl(inode, NULL);
3709}
3710
3711static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3712{
3713 struct nfs_inode *nfsi = NFS_I(inode);
3714 struct nfs4_cached_acl *acl;
3715 int ret = -ENOENT;
3716
3717 spin_lock(&inode->i_lock);
3718 acl = nfsi->nfs4_acl;
3719 if (acl == NULL)
3720 goto out;
3721 if (buf == NULL) /* user is just asking for length */
3722 goto out_len;
3723 if (acl->cached == 0)
3724 goto out;
3725 ret = -ERANGE; /* see getxattr(2) man page */
3726 if (acl->len > buflen)
3727 goto out;
3728 memcpy(buf, acl->data, acl->len);
3729out_len:
3730 ret = acl->len;
3731out:
3732 spin_unlock(&inode->i_lock);
3733 return ret;
3734}
3735
Sachin Prabhu5794d212012-04-17 14:36:40 +01003736static 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 +00003737{
3738 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003739 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003741 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003742 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003743 if (acl == NULL)
3744 goto out;
3745 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003746 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003747 } else {
3748 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3749 if (acl == NULL)
3750 goto out;
3751 acl->cached = 0;
3752 }
3753 acl->len = acl_len;
3754out:
3755 nfs4_set_cached_acl(inode, acl);
3756}
3757
Andy Adamsonbf118a32011-12-07 11:55:27 -05003758/*
3759 * The getxattr API returns the required buffer length when called with a
3760 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3761 * the required buf. On a NULL buf, we send a page of data to the server
3762 * guessing that the ACL request can be serviced by a page. If so, we cache
3763 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3764 * the cache. If not so, we throw away the page, and cache the required
3765 * length. The next getxattr call will then produce another round trip to
3766 * the server, this time with the input buf of the required size.
3767 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003768static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003770 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003771 struct nfs_getaclargs args = {
3772 .fh = NFS_FH(inode),
3773 .acl_pages = pages,
3774 .acl_len = buflen,
3775 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003776 struct nfs_getaclres res = {
3777 .acl_len = buflen,
3778 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003779 struct rpc_message msg = {
3780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3781 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003782 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003783 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003784 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3785 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003786
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787 /* As long as we're doing a round trip to the server anyway,
3788 * let's be prepared for a page of acl data. */
3789 if (npages == 0)
3790 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003791 if (npages > ARRAY_SIZE(pages))
3792 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003793
Andy Adamsonbf118a32011-12-07 11:55:27 -05003794 for (i = 0; i < npages; i++) {
3795 pages[i] = alloc_page(GFP_KERNEL);
3796 if (!pages[i])
3797 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003798 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003799
3800 /* for decoding across pages */
3801 res.acl_scratch = alloc_page(GFP_KERNEL);
3802 if (!res.acl_scratch)
3803 goto out_free;
3804
Andy Adamsonbf118a32011-12-07 11:55:27 -05003805 args.acl_len = npages * PAGE_SIZE;
3806 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003807
Peng Taode040be2012-01-10 22:42:47 +08003808 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003809 __func__, buf, buflen, npages, args.acl_len);
3810 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3811 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812 if (ret)
3813 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003814
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003815 /* Handle the case where the passed-in buffer is too short */
3816 if (res.acl_flags & NFS4_ACL_TRUNC) {
3817 /* Did the user only issue a request for the acl length? */
3818 if (buf == NULL)
3819 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003820 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003821 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003822 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003823 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003824 if (buf) {
3825 if (res.acl_len > buflen) {
3826 ret = -ERANGE;
3827 goto out_free;
3828 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003829 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003830 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003831out_ok:
3832 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003833out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003834 for (i = 0; i < npages; i++)
3835 if (pages[i])
3836 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003837 if (res.acl_scratch)
3838 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003839 return ret;
3840}
3841
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003842static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3843{
3844 struct nfs4_exception exception = { };
3845 ssize_t ret;
3846 do {
3847 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3848 if (ret >= 0)
3849 break;
3850 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3851 } while (exception.retry);
3852 return ret;
3853}
3854
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003855static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3856{
3857 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003858 int ret;
3859
3860 if (!nfs4_server_supports_acls(server))
3861 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862 ret = nfs_revalidate_inode(server, inode);
3863 if (ret < 0)
3864 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003865 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3866 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867 ret = nfs4_read_cached_acl(inode, buf, buflen);
3868 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003869 /* -ENOENT is returned if there is no ACL or if there is an ACL
3870 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003871 return ret;
3872 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003873}
3874
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003875static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003876{
3877 struct nfs_server *server = NFS_SERVER(inode);
3878 struct page *pages[NFS4ACL_MAXPAGES];
3879 struct nfs_setaclargs arg = {
3880 .fh = NFS_FH(inode),
3881 .acl_pages = pages,
3882 .acl_len = buflen,
3883 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003884 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003885 struct rpc_message msg = {
3886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3887 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003888 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003889 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003890 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003891 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003892
3893 if (!nfs4_server_supports_acls(server))
3894 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003895 if (npages > ARRAY_SIZE(pages))
3896 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003897 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3898 if (i < 0)
3899 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003900 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003901 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003902
3903 /*
3904 * Free each page after tx, so the only ref left is
3905 * held by the network stack
3906 */
3907 for (; i > 0; i--)
3908 put_page(pages[i-1]);
3909
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003910 /*
3911 * Acl update can result in inode attribute update.
3912 * so mark the attribute cache invalid.
3913 */
3914 spin_lock(&inode->i_lock);
3915 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3916 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003917 nfs_access_zap_cache(inode);
3918 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003919 return ret;
3920}
3921
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003922static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3923{
3924 struct nfs4_exception exception = { };
3925 int err;
3926 do {
3927 err = nfs4_handle_exception(NFS_SERVER(inode),
3928 __nfs4_proc_set_acl(inode, buf, buflen),
3929 &exception);
3930 } while (exception.retry);
3931 return err;
3932}
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003935nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003937 struct nfs_client *clp = server->nfs_client;
3938
3939 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 return 0;
3941 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003942 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003943 case -NFS4ERR_ADMIN_REVOKED:
3944 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003945 if (state == NULL)
3946 break;
3947 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003948 case -NFS4ERR_OPENMODE:
3949 if (state == NULL)
3950 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003951 nfs4_schedule_stateid_recovery(server, state);
3952 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003953 case -NFS4ERR_EXPIRED:
3954 if (state != NULL)
3955 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003957 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003958 nfs4_schedule_lease_recovery(clp);
3959 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003960#if defined(CONFIG_NFS_V4_1)
3961 case -NFS4ERR_BADSESSION:
3962 case -NFS4ERR_BADSLOT:
3963 case -NFS4ERR_BAD_HIGH_SLOT:
3964 case -NFS4ERR_DEADSESSION:
3965 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3966 case -NFS4ERR_SEQ_FALSE_RETRY:
3967 case -NFS4ERR_SEQ_MISORDERED:
3968 dprintk("%s ERROR %d, Reset session\n", __func__,
3969 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003970 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003971 task->tk_status = 0;
3972 return -EAGAIN;
3973#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003975 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003976 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3978 task->tk_status = 0;
3979 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003980 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 case -NFS4ERR_OLD_STATEID:
3982 task->tk_status = 0;
3983 return -EAGAIN;
3984 }
3985 task->tk_status = nfs4_map_errors(task->tk_status);
3986 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003987wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003988 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003989 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3990 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3991 task->tk_status = 0;
3992 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993}
3994
Chuck Leverf0920752012-05-21 22:45:41 -04003995static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3996 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003997{
3998 __be32 verf[2];
3999
Chuck Lever2c820d92012-05-21 22:45:33 -04004000 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4001 /* An impossible timestamp guarantees this value
4002 * will never match a generated boot time. */
4003 verf[0] = 0;
4004 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4005 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004006 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4007 verf[0] = (__be32)nn->boot_time.tv_sec;
4008 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004009 }
Chuck Levercd937102012-03-02 17:14:31 -05004010 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4011}
4012
Chuck Levere984a552012-09-14 17:24:21 -04004013static unsigned int
4014nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4015 char *buf, size_t len)
4016{
4017 unsigned int result;
4018
4019 rcu_read_lock();
4020 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4021 clp->cl_ipaddr,
4022 rpc_peeraddr2str(clp->cl_rpcclient,
4023 RPC_DISPLAY_ADDR),
4024 rpc_peeraddr2str(clp->cl_rpcclient,
4025 RPC_DISPLAY_PROTO));
4026 rcu_read_unlock();
4027 return result;
4028}
4029
4030static unsigned int
4031nfs4_init_uniform_client_string(const struct nfs_client *clp,
4032 char *buf, size_t len)
4033{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004034 char *nodename = clp->cl_rpcclient->cl_nodename;
4035
4036 if (nfs4_client_id_uniquifier[0] != '\0')
4037 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004038 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4039 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004040 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004041}
4042
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004043/**
4044 * nfs4_proc_setclientid - Negotiate client ID
4045 * @clp: state data structure
4046 * @program: RPC program for NFSv4 callback service
4047 * @port: IP port number for NFS4 callback service
4048 * @cred: RPC credential to use for this call
4049 * @res: where to place the result
4050 *
4051 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4052 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004053int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4054 unsigned short port, struct rpc_cred *cred,
4055 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
4057 nfs4_verifier sc_verifier;
4058 struct nfs4_setclientid setclientid = {
4059 .sc_verifier = &sc_verifier,
4060 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004061 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 };
4063 struct rpc_message msg = {
4064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4065 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004066 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004067 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004069 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Chuck Leverde734832012-07-11 16:30:50 -04004071 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004072 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004073 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4074 setclientid.sc_name_len =
4075 nfs4_init_uniform_client_string(clp,
4076 setclientid.sc_name,
4077 sizeof(setclientid.sc_name));
4078 else
4079 setclientid.sc_name_len =
4080 nfs4_init_nonuniform_client_string(clp,
4081 setclientid.sc_name,
4082 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004083 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004084 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004085 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004086 sizeof(setclientid.sc_netid),
4087 rpc_peeraddr2str(clp->cl_rpcclient,
4088 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004089 rcu_read_unlock();
4090 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004091 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 clp->cl_ipaddr, port >> 8, port & 255);
4093
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004094 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4095 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4096 setclientid.sc_name_len, setclientid.sc_name);
4097 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4098 dprintk("NFS reply setclientid: %d\n", status);
4099 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004102/**
4103 * nfs4_proc_setclientid_confirm - Confirm client ID
4104 * @clp: state data structure
4105 * @res: result of a previous SETCLIENTID
4106 * @cred: RPC credential to use for this call
4107 *
4108 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4109 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004110int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004111 struct nfs4_setclientid_res *arg,
4112 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
4114 struct nfs_fsinfo fsinfo;
4115 struct rpc_message msg = {
4116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004117 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004119 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 };
4121 unsigned long now;
4122 int status;
4123
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004124 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4125 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4126 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004128 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (status == 0) {
4130 spin_lock(&clp->cl_lock);
4131 clp->cl_lease_time = fsinfo.lease_time * HZ;
4132 clp->cl_last_renewal = now;
4133 spin_unlock(&clp->cl_lock);
4134 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004135 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return status;
4137}
4138
Trond Myklebustfe650402006-01-03 09:55:18 +01004139struct nfs4_delegreturndata {
4140 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004141 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004142 struct nfs_fh fh;
4143 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004144 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004145 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004146 int rpc_status;
4147};
4148
Trond Myklebustfe650402006-01-03 09:55:18 +01004149static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4150{
4151 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004152
Trond Myklebust14516c32010-07-31 14:29:06 -04004153 if (!nfs4_sequence_done(task, &data->res.seq_res))
4154 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004155
Ricardo Labiaga79708862009-12-07 09:23:21 -05004156 switch (task->tk_status) {
4157 case -NFS4ERR_STALE_STATEID:
4158 case -NFS4ERR_EXPIRED:
4159 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004160 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004161 break;
4162 default:
4163 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4164 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004165 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004166 return;
4167 }
4168 }
4169 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004170}
4171
4172static void nfs4_delegreturn_release(void *calldata)
4173{
Trond Myklebustfe650402006-01-03 09:55:18 +01004174 kfree(calldata);
4175}
4176
Andy Adamson938e1012009-04-01 09:22:28 -04004177#if defined(CONFIG_NFS_V4_1)
4178static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4179{
4180 struct nfs4_delegreturndata *d_data;
4181
4182 d_data = (struct nfs4_delegreturndata *)data;
4183
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004184 nfs4_setup_sequence(d_data->res.server,
4185 &d_data->args.seq_args,
4186 &d_data->res.seq_res,
4187 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004188}
4189#endif /* CONFIG_NFS_V4_1 */
4190
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004191static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004192#if defined(CONFIG_NFS_V4_1)
4193 .rpc_call_prepare = nfs4_delegreturn_prepare,
4194#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004195 .rpc_call_done = nfs4_delegreturn_done,
4196 .rpc_release = nfs4_delegreturn_release,
4197};
4198
Trond Myklebuste6f81072008-01-24 18:14:34 -05004199static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004200{
4201 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004202 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004203 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004204 struct rpc_message msg = {
4205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4206 .rpc_cred = cred,
4207 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004208 struct rpc_task_setup task_setup_data = {
4209 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004210 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004211 .callback_ops = &nfs4_delegreturn_ops,
4212 .flags = RPC_TASK_ASYNC,
4213 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004214 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004215
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004216 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004217 if (data == NULL)
4218 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004219 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004220 data->args.fhandle = &data->fh;
4221 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004222 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004223 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004224 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004225 data->res.fattr = &data->fattr;
4226 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004227 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004228 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004229 data->rpc_status = 0;
4230
Trond Myklebustc970aa82007-07-14 15:39:59 -04004231 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004232 msg.rpc_argp = &data->args;
4233 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004234 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004235 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004236 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004237 if (!issync)
4238 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004239 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004240 if (status != 0)
4241 goto out;
4242 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004243 if (status == 0)
4244 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4245 else
4246 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004247out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004248 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004249 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
4251
Trond Myklebuste6f81072008-01-24 18:14:34 -05004252int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
4254 struct nfs_server *server = NFS_SERVER(inode);
4255 struct nfs4_exception exception = { };
4256 int err;
4257 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004258 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 switch (err) {
4260 case -NFS4ERR_STALE_STATEID:
4261 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 case 0:
4263 return 0;
4264 }
4265 err = nfs4_handle_exception(server, err, &exception);
4266 } while (exception.retry);
4267 return err;
4268}
4269
4270#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4271#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4272
4273/*
4274 * sleep, with exponential backoff, and retry the LOCK operation.
4275 */
4276static unsigned long
4277nfs4_set_lock_task_retry(unsigned long timeout)
4278{
Jeff Laytond3103102011-12-01 22:44:39 +01004279 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 timeout <<= 1;
4281 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4282 return NFS4_LOCK_MAXTIMEOUT;
4283 return timeout;
4284}
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4287{
4288 struct inode *inode = state->inode;
4289 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004290 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004291 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004293 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004295 struct nfs_lockt_res res = {
4296 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 };
4298 struct rpc_message msg = {
4299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4300 .rpc_argp = &arg,
4301 .rpc_resp = &res,
4302 .rpc_cred = state->owner->so_cred,
4303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 struct nfs4_lock_state *lsp;
4305 int status;
4306
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004307 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004308 status = nfs4_set_lock_state(state, request);
4309 if (status != 0)
4310 goto out;
4311 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004312 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004313 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004314 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004315 switch (status) {
4316 case 0:
4317 request->fl_type = F_UNLCK;
4318 break;
4319 case -NFS4ERR_DENIED:
4320 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004322 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004323out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 return status;
4325}
4326
4327static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4328{
4329 struct nfs4_exception exception = { };
4330 int err;
4331
4332 do {
4333 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4334 _nfs4_proc_getlk(state, cmd, request),
4335 &exception);
4336 } while (exception.retry);
4337 return err;
4338}
4339
4340static int do_vfs_lock(struct file *file, struct file_lock *fl)
4341{
4342 int res = 0;
4343 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4344 case FL_POSIX:
4345 res = posix_lock_file_wait(file, fl);
4346 break;
4347 case FL_FLOCK:
4348 res = flock_lock_file_wait(file, fl);
4349 break;
4350 default:
4351 BUG();
4352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return res;
4354}
4355
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004356struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004357 struct nfs_locku_args arg;
4358 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004359 struct nfs4_lock_state *lsp;
4360 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004361 struct file_lock fl;
4362 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004363 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004364};
4365
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004366static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4367 struct nfs_open_context *ctx,
4368 struct nfs4_lock_state *lsp,
4369 struct nfs_seqid *seqid)
4370{
4371 struct nfs4_unlockdata *p;
4372 struct inode *inode = lsp->ls_state->inode;
4373
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004374 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004375 if (p == NULL)
4376 return NULL;
4377 p->arg.fh = NFS_FH(inode);
4378 p->arg.fl = &p->fl;
4379 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004380 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004381 p->arg.stateid = &lsp->ls_stateid;
4382 p->lsp = lsp;
4383 atomic_inc(&lsp->ls_count);
4384 /* Ensure we don't close file until we're done freeing locks! */
4385 p->ctx = get_nfs_open_context(ctx);
4386 memcpy(&p->fl, fl, sizeof(p->fl));
4387 p->server = NFS_SERVER(inode);
4388 return p;
4389}
4390
Trond Myklebust06f814a2006-01-03 09:55:07 +01004391static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004392{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004393 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004394 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004395 nfs4_put_lock_state(calldata->lsp);
4396 put_nfs_open_context(calldata->ctx);
4397 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398}
4399
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004400static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004401{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004402 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403
Trond Myklebust14516c32010-07-31 14:29:06 -04004404 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4405 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 switch (task->tk_status) {
4407 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004408 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4409 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004410 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004411 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004412 case -NFS4ERR_BAD_STATEID:
4413 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004414 case -NFS4ERR_STALE_STATEID:
4415 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004416 break;
4417 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004418 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004419 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004420 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004421 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004422}
4423
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004424static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004425{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004426 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004428 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004429 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004430 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004431 /* Note: exit _without_ running nfs4_locku_done */
4432 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004433 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004434 return;
4435 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004436 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004437 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004438 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004439 &calldata->res.seq_res,
4440 task) != 0)
4441 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004444static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004445 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004446 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004447 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004448};
4449
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004450static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4451 struct nfs_open_context *ctx,
4452 struct nfs4_lock_state *lsp,
4453 struct nfs_seqid *seqid)
4454{
4455 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004456 struct rpc_message msg = {
4457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4458 .rpc_cred = ctx->cred,
4459 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004460 struct rpc_task_setup task_setup_data = {
4461 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004462 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004463 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004464 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004465 .flags = RPC_TASK_ASYNC,
4466 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004467
Frank Filz137d6ac2007-07-09 15:32:29 -07004468 /* Ensure this is an unlock - when canceling a lock, the
4469 * canceled lock is passed in, and it won't be an unlock.
4470 */
4471 fl->fl_type = F_UNLCK;
4472
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004473 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4474 if (data == NULL) {
4475 nfs_free_seqid(seqid);
4476 return ERR_PTR(-ENOMEM);
4477 }
4478
Trond Myklebust9d12b212012-01-17 22:04:25 -05004479 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004480 msg.rpc_argp = &data->arg;
4481 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004482 task_setup_data.callback_data = data;
4483 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004484}
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4487{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004488 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004489 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004490 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004491 struct rpc_task *task;
4492 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004493 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Trond Myklebust9b073572006-06-29 16:38:34 -04004495 status = nfs4_set_lock_state(state, request);
4496 /* Unlock _before_ we do the RPC call */
4497 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004498 down_read(&nfsi->rwsem);
4499 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4500 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004501 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004502 }
4503 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004504 if (status != 0)
4505 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004506 /* Is this a delegated lock? */
4507 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004508 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004509 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004510 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004511 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004512 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004513 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004514 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515 status = PTR_ERR(task);
4516 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004517 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004519 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004520out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004521 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004522 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523}
4524
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525struct nfs4_lockdata {
4526 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004527 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004528 struct nfs4_lock_state *lsp;
4529 struct nfs_open_context *ctx;
4530 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004531 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532 int rpc_status;
4533 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004534 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535};
4536
4537static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004538 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4539 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540{
4541 struct nfs4_lockdata *p;
4542 struct inode *inode = lsp->ls_state->inode;
4543 struct nfs_server *server = NFS_SERVER(inode);
4544
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004545 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004546 if (p == NULL)
4547 return NULL;
4548
4549 p->arg.fh = NFS_FH(inode);
4550 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004551 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004552 if (p->arg.open_seqid == NULL)
4553 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004554 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004555 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004556 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004558 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004559 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004560 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004561 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004563 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564 atomic_inc(&lsp->ls_count);
4565 p->ctx = get_nfs_open_context(ctx);
4566 memcpy(&p->fl, fl, sizeof(p->fl));
4567 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004568out_free_seqid:
4569 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004570out_free:
4571 kfree(p);
4572 return NULL;
4573}
4574
4575static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4576{
4577 struct nfs4_lockdata *data = calldata;
4578 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Harvey Harrison3110ff82008-05-02 13:42:44 -07004580 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004581 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4582 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004583 /* Do we need to do an open_to_lock_owner? */
4584 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004585 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004586 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004587 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588 data->arg.open_stateid = &state->stateid;
4589 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004590 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004591 } else
4592 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004593 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004594 if (nfs4_setup_sequence(data->server,
4595 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004596 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004597 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004598 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004599 nfs_release_seqid(data->arg.open_seqid);
4600out_release_lock_seqid:
4601 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004602 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004603}
4604
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004605static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4606{
4607 struct nfs4_lockdata *data = calldata;
4608
Harvey Harrison3110ff82008-05-02 13:42:44 -07004609 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610
Trond Myklebust14516c32010-07-31 14:29:06 -04004611 if (!nfs4_sequence_done(task, &data->res.seq_res))
4612 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004613
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004615 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 if (data->rpc_status == 0)
4617 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4618 else
4619 goto out;
4620 }
4621 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004622 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004623 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004624 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004626out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004627 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004628}
4629
4630static void nfs4_lock_release(void *calldata)
4631{
4632 struct nfs4_lockdata *data = calldata;
4633
Harvey Harrison3110ff82008-05-02 13:42:44 -07004634 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004635 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004636 if (data->cancelled != 0) {
4637 struct rpc_task *task;
4638 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4639 data->arg.lock_seqid);
4640 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004641 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004642 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643 } else
4644 nfs_free_seqid(data->arg.lock_seqid);
4645 nfs4_put_lock_state(data->lsp);
4646 put_nfs_open_context(data->ctx);
4647 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004648 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649}
4650
4651static const struct rpc_call_ops nfs4_lock_ops = {
4652 .rpc_call_prepare = nfs4_lock_prepare,
4653 .rpc_call_done = nfs4_lock_done,
4654 .rpc_release = nfs4_lock_release,
4655};
4656
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004657static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4658{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004659 switch (error) {
4660 case -NFS4ERR_ADMIN_REVOKED:
4661 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004662 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004663 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004664 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004665 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004666 break;
4667 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004668 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004669 case -NFS4ERR_EXPIRED:
4670 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004671 };
4672}
4673
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004674static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675{
4676 struct nfs4_lockdata *data;
4677 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004678 struct rpc_message msg = {
4679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4680 .rpc_cred = state->owner->so_cred,
4681 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004682 struct rpc_task_setup task_setup_data = {
4683 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004684 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004685 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004686 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004687 .flags = RPC_TASK_ASYNC,
4688 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004689 int ret;
4690
Harvey Harrison3110ff82008-05-02 13:42:44 -07004691 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004692 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004693 fl->fl_u.nfs4_fl.owner,
4694 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695 if (data == NULL)
4696 return -ENOMEM;
4697 if (IS_SETLKW(cmd))
4698 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004699 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004700 msg.rpc_argp = &data->arg;
4701 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004702 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004703 if (recovery_type > NFS_LOCK_NEW) {
4704 if (recovery_type == NFS_LOCK_RECLAIM)
4705 data->arg.reclaim = NFS_LOCK_RECLAIM;
4706 nfs4_set_sequence_privileged(&data->arg.seq_args);
4707 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004708 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004709 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004710 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004711 ret = nfs4_wait_for_completion_rpc_task(task);
4712 if (ret == 0) {
4713 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004714 if (ret)
4715 nfs4_handle_setlk_error(data->server, data->lsp,
4716 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717 } else
4718 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004719 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004720 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
4724static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4725{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004726 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004727 struct nfs4_exception exception = {
4728 .inode = state->inode,
4729 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004730 int err;
4731
4732 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004733 /* Cache the lock if possible... */
4734 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4735 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004736 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004737 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004738 break;
4739 nfs4_handle_exception(server, err, &exception);
4740 } while (exception.retry);
4741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743
4744static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4745{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004746 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004747 struct nfs4_exception exception = {
4748 .inode = state->inode,
4749 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004750 int err;
4751
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004752 err = nfs4_set_lock_state(state, request);
4753 if (err != 0)
4754 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004755 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004756 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4757 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004758 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004759 switch (err) {
4760 default:
4761 goto out;
4762 case -NFS4ERR_GRACE:
4763 case -NFS4ERR_DELAY:
4764 nfs4_handle_exception(server, err, &exception);
4765 err = 0;
4766 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004767 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004768out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004769 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004772#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004773/**
4774 * nfs41_check_expired_locks - possibly free a lock stateid
4775 *
4776 * @state: NFSv4 state for an inode
4777 *
4778 * Returns NFS_OK if recovery for this stateid is now finished.
4779 * Otherwise a negative NFS4ERR value is returned.
4780 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004781static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004782{
Chuck Levereb64cf92012-07-11 16:30:05 -04004783 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004784 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004785 struct nfs_server *server = NFS_SERVER(state->inode);
4786
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004787 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004788 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004789 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4790 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004791 /* Free the stateid unless the server
4792 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004793 if (status != -NFS4ERR_BAD_STATEID)
4794 nfs41_free_stateid(server,
4795 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004796 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004797 ret = status;
4798 }
4799 }
4800 };
4801
4802 return ret;
4803}
4804
4805static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4806{
4807 int status = NFS_OK;
4808
4809 if (test_bit(LK_STATE_IN_USE, &state->flags))
4810 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004811 if (status != NFS_OK)
4812 status = nfs4_lock_expired(state, request);
4813 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004814}
4815#endif
4816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4818{
Trond Myklebust9a99af492013-02-04 20:17:49 -05004819 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004820 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004821 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004822 unsigned int seq;
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 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05004844 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4845 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004846 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004847 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05004848 goto out;
4849 down_read(&nfsi->rwsem);
4850 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4851 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004852 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004853 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004854 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004855 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004856 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004857 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4858 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004859out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004860 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004861out:
4862 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 return status;
4864}
4865
4866static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4867{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004868 struct nfs4_exception exception = {
4869 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004870 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004871 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 int err;
4873
4874 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004875 err = _nfs4_proc_setlk(state, cmd, request);
4876 if (err == -NFS4ERR_DENIED)
4877 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004879 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 } while (exception.retry);
4881 return err;
4882}
4883
4884static int
4885nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4886{
4887 struct nfs_open_context *ctx;
4888 struct nfs4_state *state;
4889 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4890 int status;
4891
4892 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004893 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 state = ctx->state;
4895
4896 if (request->fl_start < 0 || request->fl_end < 0)
4897 return -EINVAL;
4898
Trond Myklebustd9531262009-07-21 19:22:38 -04004899 if (IS_GETLK(cmd)) {
4900 if (state != NULL)
4901 return nfs4_proc_getlk(state, F_GETLK, request);
4902 return 0;
4903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
4905 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4906 return -EINVAL;
4907
Trond Myklebustd9531262009-07-21 19:22:38 -04004908 if (request->fl_type == F_UNLCK) {
4909 if (state != NULL)
4910 return nfs4_proc_unlck(state, cmd, request);
4911 return 0;
4912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
Trond Myklebustd9531262009-07-21 19:22:38 -04004914 if (state == NULL)
4915 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004916 /*
4917 * Don't rely on the VFS having checked the file open mode,
4918 * since it won't do this for flock() locks.
4919 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004920 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004921 case F_RDLCK:
4922 if (!(filp->f_mode & FMODE_READ))
4923 return -EBADF;
4924 break;
4925 case F_WRLCK:
4926 if (!(filp->f_mode & FMODE_WRITE))
4927 return -EBADF;
4928 }
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 do {
4931 status = nfs4_proc_setlk(state, cmd, request);
4932 if ((status != -EAGAIN) || IS_SETLK(cmd))
4933 break;
4934 timeout = nfs4_set_lock_task_retry(timeout);
4935 status = -ERESTARTSYS;
4936 if (signalled())
4937 break;
4938 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return status;
4940}
4941
Trond Myklebust888e6942005-11-04 15:38:11 -05004942int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4943{
4944 struct nfs_server *server = NFS_SERVER(state->inode);
4945 struct nfs4_exception exception = { };
4946 int err;
4947
4948 err = nfs4_set_lock_state(state, fl);
4949 if (err != 0)
4950 goto out;
4951 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004952 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004953 switch (err) {
4954 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004955 printk(KERN_ERR "NFS: %s: unhandled error "
4956 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004957 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004958 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004959 goto out;
4960 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004961 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004962 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004963 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004964 nfs4_schedule_lease_recovery(server->nfs_client);
4965 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004966 case -NFS4ERR_BADSESSION:
4967 case -NFS4ERR_BADSLOT:
4968 case -NFS4ERR_BAD_HIGH_SLOT:
4969 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4970 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004971 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004972 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004973 case -ERESTARTSYS:
4974 /*
4975 * The show must go on: exit, but mark the
4976 * stateid as needing recovery.
4977 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004978 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004979 case -NFS4ERR_ADMIN_REVOKED:
4980 case -NFS4ERR_BAD_STATEID:
4981 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004982 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004983 err = 0;
4984 goto out;
4985 case -ENOMEM:
4986 case -NFS4ERR_DENIED:
4987 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4988 err = 0;
4989 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004990 case -NFS4ERR_DELAY:
4991 break;
4992 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004993 err = nfs4_handle_exception(server, err, &exception);
4994 } while (exception.retry);
4995out:
4996 return err;
4997}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004998
Trond Myklebustcf470c32012-03-07 13:49:12 -05004999struct nfs_release_lockowner_data {
5000 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005001 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005002 struct nfs_release_lockowner_args args;
5003};
5004
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005005static void nfs4_release_lockowner_release(void *calldata)
5006{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005007 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005008 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005009 kfree(calldata);
5010}
5011
Trond Myklebust17280172012-03-11 13:11:00 -04005012static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005013 .rpc_release = nfs4_release_lockowner_release,
5014};
5015
Trond Myklebustcf470c32012-03-07 13:49:12 -05005016int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005017{
5018 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005019 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005020 struct rpc_message msg = {
5021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5022 };
5023
5024 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005025 return -EINVAL;
5026 data = kmalloc(sizeof(*data), GFP_NOFS);
5027 if (!data)
5028 return -ENOMEM;
5029 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005030 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005031 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5032 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5033 data->args.lock_owner.s_dev = server->s_dev;
5034 msg.rpc_argp = &data->args;
5035 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5036 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005037}
5038
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005039#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5040
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005041static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5042 const void *buf, size_t buflen,
5043 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005044{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005045 if (strcmp(key, "") != 0)
5046 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005047
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005048 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005049}
5050
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005051static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5052 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005053{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005054 if (strcmp(key, "") != 0)
5055 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005056
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005057 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005058}
5059
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005060static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5061 size_t list_len, const char *name,
5062 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005063{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005064 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005065
J. Bruce Fields096455a2006-03-20 23:23:42 -05005066 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5067 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005068
5069 if (list && len <= list_len)
5070 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005071 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005072}
5073
Andy Adamson533eb462011-06-13 18:25:56 -04005074/*
5075 * nfs_fhget will use either the mounted_on_fileid or the fileid
5076 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005077static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5078{
Andy Adamson533eb462011-06-13 18:25:56 -04005079 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5080 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5081 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005082 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005083 return;
5084
5085 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005086 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005087 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5088 fattr->nlink = 2;
5089}
5090
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005091static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5092 const struct qstr *name,
5093 struct nfs4_fs_locations *fs_locations,
5094 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005095{
5096 struct nfs_server *server = NFS_SERVER(dir);
5097 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005098 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005099 };
5100 struct nfs4_fs_locations_arg args = {
5101 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005102 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005103 .page = page,
5104 .bitmask = bitmask,
5105 };
Benny Halevy22958462009-04-01 09:22:02 -04005106 struct nfs4_fs_locations_res res = {
5107 .fs_locations = fs_locations,
5108 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005109 struct rpc_message msg = {
5110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5111 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005112 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005113 };
5114 int status;
5115
Harvey Harrison3110ff82008-05-02 13:42:44 -07005116 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005117
5118 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5119 * is not supported */
5120 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5121 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5122 else
5123 bitmask[0] |= FATTR4_WORD0_FILEID;
5124
Trond Myklebustc228fd32007-01-13 02:28:11 -05005125 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005126 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005127 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005128 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005129 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005130 return status;
5131}
5132
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005133int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5134 const struct qstr *name,
5135 struct nfs4_fs_locations *fs_locations,
5136 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005137{
5138 struct nfs4_exception exception = { };
5139 int err;
5140 do {
5141 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005142 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005143 &exception);
5144 } while (exception.retry);
5145 return err;
5146}
5147
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005148static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5149{
5150 int status;
5151 struct nfs4_secinfo_arg args = {
5152 .dir_fh = NFS_FH(dir),
5153 .name = name,
5154 };
5155 struct nfs4_secinfo_res res = {
5156 .flavors = flavors,
5157 };
5158 struct rpc_message msg = {
5159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5160 .rpc_argp = &args,
5161 .rpc_resp = &res,
5162 };
5163
5164 dprintk("NFS call secinfo %s\n", name->name);
5165 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5166 dprintk("NFS reply secinfo: %d\n", status);
5167 return status;
5168}
5169
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005170int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5171 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005172{
5173 struct nfs4_exception exception = { };
5174 int err;
5175 do {
5176 err = nfs4_handle_exception(NFS_SERVER(dir),
5177 _nfs4_proc_secinfo(dir, name, flavors),
5178 &exception);
5179 } while (exception.retry);
5180 return err;
5181}
5182
Andy Adamson557134a2009-04-01 09:21:53 -04005183#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005184/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005185 * Check the exchange flags returned by the server for invalid flags, having
5186 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5187 * DS flags set.
5188 */
5189static int nfs4_check_cl_exchange_flags(u32 flags)
5190{
5191 if (flags & ~EXCHGID4_FLAG_MASK_R)
5192 goto out_inval;
5193 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5194 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5195 goto out_inval;
5196 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5197 goto out_inval;
5198 return NFS_OK;
5199out_inval:
5200 return -NFS4ERR_INVAL;
5201}
5202
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005203static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005204nfs41_same_server_scope(struct nfs41_server_scope *a,
5205 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005206{
5207 if (a->server_scope_sz == b->server_scope_sz &&
5208 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5209 return true;
5210
5211 return false;
5212}
5213
Andy Adamson357f54d2010-12-14 10:11:57 -05005214/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005215 * nfs4_proc_bind_conn_to_session()
5216 *
5217 * The 4.1 client currently uses the same TCP connection for the
5218 * fore and backchannel.
5219 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005220int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005221{
5222 int status;
5223 struct nfs41_bind_conn_to_session_res res;
5224 struct rpc_message msg = {
5225 .rpc_proc =
5226 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5227 .rpc_argp = clp,
5228 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005229 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005230 };
5231
5232 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005233
5234 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5235 if (unlikely(res.session == NULL)) {
5236 status = -ENOMEM;
5237 goto out;
5238 }
5239
5240 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5241 if (status == 0) {
5242 if (memcmp(res.session->sess_id.data,
5243 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5244 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5245 status = -EIO;
5246 goto out_session;
5247 }
5248 if (res.dir != NFS4_CDFS4_BOTH) {
5249 dprintk("NFS: %s: Unexpected direction from server\n",
5250 __func__);
5251 status = -EIO;
5252 goto out_session;
5253 }
5254 if (res.use_conn_in_rdma_mode) {
5255 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5256 __func__);
5257 status = -EIO;
5258 goto out_session;
5259 }
5260 }
5261out_session:
5262 kfree(res.session);
5263out:
5264 dprintk("<-- %s status= %d\n", __func__, status);
5265 return status;
5266}
5267
5268/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005269 * nfs4_proc_exchange_id()
5270 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005271 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5272 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005273 * Since the clientid has expired, all compounds using sessions
5274 * associated with the stale clientid will be returning
5275 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5276 * be in some phase of session reset.
5277 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005278int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005279{
5280 nfs4_verifier verifier;
5281 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005282 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005283 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005284 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005285 };
5286 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005287 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005288 };
5289 int status;
5290 struct rpc_message msg = {
5291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5292 .rpc_argp = &args,
5293 .rpc_resp = &res,
5294 .rpc_cred = cred,
5295 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005296
Chuck Leverf0920752012-05-21 22:45:41 -04005297 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005298 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5299 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005300 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5301 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5302 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005303
Chuck Leveracdeb692012-05-21 22:46:16 -04005304 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005305 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005306 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005307 status = -ENOMEM;
5308 goto out;
5309 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005310
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005311 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005312 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005313 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005314 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005315 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005316 }
5317
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005318 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005319 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005320 status = -ENOMEM;
5321 goto out_server_scope;
5322 }
5323
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005324 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005325 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005326 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005327
Chuck Lever177313f2012-05-21 22:44:58 -04005328 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005329 clp->cl_clientid = res.clientid;
5330 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5331 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5332 clp->cl_seqid = res.seqid;
5333
Chuck Leveracdeb692012-05-21 22:46:16 -04005334 kfree(clp->cl_serverowner);
5335 clp->cl_serverowner = res.server_owner;
5336 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005337
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005338 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005339 kfree(clp->cl_implid);
5340 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005341
Chuck Lever177313f2012-05-21 22:44:58 -04005342 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005343 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005344 res.server_scope)) {
5345 dprintk("%s: server_scope mismatch detected\n",
5346 __func__);
5347 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005348 kfree(clp->cl_serverscope);
5349 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005350 }
5351
Chuck Lever177313f2012-05-21 22:44:58 -04005352 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005353 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005354 goto out;
5355 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005356 } else
5357 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005358
Chuck Leveracdeb692012-05-21 22:46:16 -04005359out_server_owner:
5360 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005361out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005362 kfree(res.server_scope);
5363out:
Chuck Lever177313f2012-05-21 22:44:58 -04005364 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005365 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005366 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005367 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005368 clp->cl_implid->date.seconds,
5369 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005370 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005371 return status;
5372}
5373
Trond Myklebust66245532012-05-25 17:18:09 -04005374static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5375 struct rpc_cred *cred)
5376{
5377 struct rpc_message msg = {
5378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5379 .rpc_argp = clp,
5380 .rpc_cred = cred,
5381 };
5382 int status;
5383
5384 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5385 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005386 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005387 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5388 return status;
5389}
5390
5391static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5392 struct rpc_cred *cred)
5393{
5394 unsigned int loop;
5395 int ret;
5396
5397 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5398 ret = _nfs4_proc_destroy_clientid(clp, cred);
5399 switch (ret) {
5400 case -NFS4ERR_DELAY:
5401 case -NFS4ERR_CLIENTID_BUSY:
5402 ssleep(1);
5403 break;
5404 default:
5405 return ret;
5406 }
5407 }
5408 return 0;
5409}
5410
5411int nfs4_destroy_clientid(struct nfs_client *clp)
5412{
5413 struct rpc_cred *cred;
5414 int ret = 0;
5415
5416 if (clp->cl_mvops->minor_version < 1)
5417 goto out;
5418 if (clp->cl_exchange_flags == 0)
5419 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005420 if (clp->cl_preserve_clid)
5421 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005422 cred = nfs4_get_exchange_id_cred(clp);
5423 ret = nfs4_proc_destroy_clientid(clp, cred);
5424 if (cred)
5425 put_rpccred(cred);
5426 switch (ret) {
5427 case 0:
5428 case -NFS4ERR_STALE_CLIENTID:
5429 clp->cl_exchange_flags = 0;
5430 }
5431out:
5432 return ret;
5433}
5434
Andy Adamson2050f0c2009-04-01 09:22:30 -04005435struct nfs4_get_lease_time_data {
5436 struct nfs4_get_lease_time_args *args;
5437 struct nfs4_get_lease_time_res *res;
5438 struct nfs_client *clp;
5439};
5440
5441static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5442 void *calldata)
5443{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005444 struct nfs4_get_lease_time_data *data =
5445 (struct nfs4_get_lease_time_data *)calldata;
5446
5447 dprintk("--> %s\n", __func__);
5448 /* just setup sequence, do not trigger session recovery
5449 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005450 nfs41_setup_sequence(data->clp->cl_session,
5451 &data->args->la_seq_args,
5452 &data->res->lr_seq_res,
5453 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005454 dprintk("<-- %s\n", __func__);
5455}
5456
5457/*
5458 * Called from nfs4_state_manager thread for session setup, so don't recover
5459 * from sequence operation or clientid errors.
5460 */
5461static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5462{
5463 struct nfs4_get_lease_time_data *data =
5464 (struct nfs4_get_lease_time_data *)calldata;
5465
5466 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005467 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5468 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005469 switch (task->tk_status) {
5470 case -NFS4ERR_DELAY:
5471 case -NFS4ERR_GRACE:
5472 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5473 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5474 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005475 /* fall through */
5476 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005477 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005478 return;
5479 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005480 dprintk("<-- %s\n", __func__);
5481}
5482
Trond Myklebust17280172012-03-11 13:11:00 -04005483static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005484 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5485 .rpc_call_done = nfs4_get_lease_time_done,
5486};
5487
5488int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5489{
5490 struct rpc_task *task;
5491 struct nfs4_get_lease_time_args args;
5492 struct nfs4_get_lease_time_res res = {
5493 .lr_fsinfo = fsinfo,
5494 };
5495 struct nfs4_get_lease_time_data data = {
5496 .args = &args,
5497 .res = &res,
5498 .clp = clp,
5499 };
5500 struct rpc_message msg = {
5501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5502 .rpc_argp = &args,
5503 .rpc_resp = &res,
5504 };
5505 struct rpc_task_setup task_setup = {
5506 .rpc_client = clp->cl_rpcclient,
5507 .rpc_message = &msg,
5508 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005509 .callback_data = &data,
5510 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005511 };
5512 int status;
5513
Trond Myklebust9d12b212012-01-17 22:04:25 -05005514 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005515 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005516 dprintk("--> %s\n", __func__);
5517 task = rpc_run_task(&task_setup);
5518
5519 if (IS_ERR(task))
5520 status = PTR_ERR(task);
5521 else {
5522 status = task->tk_status;
5523 rpc_put_task(task);
5524 }
5525 dprintk("<-- %s return %d\n", __func__, status);
5526
5527 return status;
5528}
5529
Andy Adamsonfc931582009-04-01 09:22:31 -04005530/*
5531 * Initialize the values to be used by the client in CREATE_SESSION
5532 * If nfs4_init_session set the fore channel request and response sizes,
5533 * use them.
5534 *
5535 * Set the back channel max_resp_sz_cached to zero to force the client to
5536 * always set csa_cachethis to FALSE because the current implementation
5537 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5538 */
5539static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5540{
5541 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005542 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5543 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005544
5545 if (mxrqst_sz == 0)
5546 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5547 if (mxresp_sz == 0)
5548 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5549 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005550 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5551 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005552 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005553 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005554
5555 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005556 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005557 __func__,
5558 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005559 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005560
5561 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005562 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5563 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5564 args->bc_attrs.max_resp_sz_cached = 0;
5565 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5566 args->bc_attrs.max_reqs = 1;
5567
5568 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5569 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5570 __func__,
5571 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5572 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5573 args->bc_attrs.max_reqs);
5574}
5575
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005576static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005577{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005578 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5579 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5580
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005581 if (rcvd->max_resp_sz > sent->max_resp_sz)
5582 return -EINVAL;
5583 /*
5584 * Our requested max_ops is the minimum we need; we're not
5585 * prepared to break up compounds into smaller pieces than that.
5586 * So, no point even trying to continue if the server won't
5587 * cooperate:
5588 */
5589 if (rcvd->max_ops < sent->max_ops)
5590 return -EINVAL;
5591 if (rcvd->max_reqs == 0)
5592 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005593 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5594 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005595 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005596}
5597
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005598static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5599{
5600 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5601 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005602
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005603 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5604 return -EINVAL;
5605 if (rcvd->max_resp_sz < sent->max_resp_sz)
5606 return -EINVAL;
5607 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5608 return -EINVAL;
5609 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005610 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005611 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005612 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005613 return -EINVAL;
5614 return 0;
5615}
Andy Adamson8d353012009-04-01 09:22:32 -04005616
Andy Adamson8d353012009-04-01 09:22:32 -04005617static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5618 struct nfs4_session *session)
5619{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005620 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005621
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005622 ret = nfs4_verify_fore_channel_attrs(args, session);
5623 if (ret)
5624 return ret;
5625 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005626}
5627
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005628static int _nfs4_proc_create_session(struct nfs_client *clp,
5629 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005630{
5631 struct nfs4_session *session = clp->cl_session;
5632 struct nfs41_create_session_args args = {
5633 .client = clp,
5634 .cb_program = NFS4_CALLBACK,
5635 };
5636 struct nfs41_create_session_res res = {
5637 .client = clp,
5638 };
5639 struct rpc_message msg = {
5640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5641 .rpc_argp = &args,
5642 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005643 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005644 };
5645 int status;
5646
5647 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005648 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005649
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005650 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005651
Trond Myklebust43095d32012-11-20 11:13:12 -05005652 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005653 /* Verify the session's negotiated channel_attrs values */
5654 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005655 /* Increment the clientid slot sequence id */
5656 clp->cl_seqid++;
5657 }
5658
5659 return status;
5660}
5661
5662/*
5663 * Issues a CREATE_SESSION operation to the server.
5664 * It is the responsibility of the caller to verify the session is
5665 * expired before calling this routine.
5666 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005667int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005668{
5669 int status;
5670 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005671 struct nfs4_session *session = clp->cl_session;
5672
5673 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5674
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005675 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005676 if (status)
5677 goto out;
5678
Andy Adamsonaacd5532011-11-09 13:58:21 -05005679 /* Init or reset the session slot tables */
5680 status = nfs4_setup_session_slot_tables(session);
5681 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005682 if (status)
5683 goto out;
5684
5685 ptr = (unsigned *)&session->sess_id.data[0];
5686 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5687 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005688out:
5689 dprintk("<-- %s\n", __func__);
5690 return status;
5691}
5692
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005693/*
5694 * Issue the over-the-wire RPC DESTROY_SESSION.
5695 * The caller must serialize access to this routine.
5696 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005697int nfs4_proc_destroy_session(struct nfs4_session *session,
5698 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005699{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005700 struct rpc_message msg = {
5701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5702 .rpc_argp = session,
5703 .rpc_cred = cred,
5704 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005705 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005706
5707 dprintk("--> nfs4_proc_destroy_session\n");
5708
5709 /* session is still being setup */
5710 if (session->clp->cl_cons_state != NFS_CS_READY)
5711 return status;
5712
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005713 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005714
5715 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005716 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005717 "Session has been destroyed regardless...\n", status);
5718
5719 dprintk("<-- nfs4_proc_destroy_session\n");
5720 return status;
5721}
5722
Trond Myklebust7b38c362012-05-23 13:23:31 -04005723/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005724 * Renew the cl_session lease.
5725 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005726struct nfs4_sequence_data {
5727 struct nfs_client *clp;
5728 struct nfs4_sequence_args args;
5729 struct nfs4_sequence_res res;
5730};
5731
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005732static void nfs41_sequence_release(void *data)
5733{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005734 struct nfs4_sequence_data *calldata = data;
5735 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005736
Alexandros Batsakis71358402010-02-05 03:45:05 -08005737 if (atomic_read(&clp->cl_count) > 1)
5738 nfs4_schedule_state_renewal(clp);
5739 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005740 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005741}
5742
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005743static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5744{
5745 switch(task->tk_status) {
5746 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005747 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5748 return -EAGAIN;
5749 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005750 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005751 }
5752 return 0;
5753}
5754
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005755static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005756{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005757 struct nfs4_sequence_data *calldata = data;
5758 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005759
Trond Myklebust14516c32010-07-31 14:29:06 -04005760 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5761 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762
5763 if (task->tk_status < 0) {
5764 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005765 if (atomic_read(&clp->cl_count) == 1)
5766 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005767
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005768 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5769 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005770 return;
5771 }
5772 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005773 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005774out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005775 dprintk("<-- %s\n", __func__);
5776}
5777
5778static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5779{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005780 struct nfs4_sequence_data *calldata = data;
5781 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005782 struct nfs4_sequence_args *args;
5783 struct nfs4_sequence_res *res;
5784
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005785 args = task->tk_msg.rpc_argp;
5786 res = task->tk_msg.rpc_resp;
5787
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005788 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005789}
5790
5791static const struct rpc_call_ops nfs41_sequence_ops = {
5792 .rpc_call_done = nfs41_sequence_call_done,
5793 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005794 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005795};
5796
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005797static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5798 struct rpc_cred *cred,
5799 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005800{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005801 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005802 struct rpc_message msg = {
5803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5804 .rpc_cred = cred,
5805 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005806 struct rpc_task_setup task_setup_data = {
5807 .rpc_client = clp->cl_rpcclient,
5808 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005809 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005810 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5811 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005812
Alexandros Batsakis71358402010-02-05 03:45:05 -08005813 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005814 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005815 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005816 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005817 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005818 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005819 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005820 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005821 if (is_privileged)
5822 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005823 msg.rpc_argp = &calldata->args;
5824 msg.rpc_resp = &calldata->res;
5825 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005826 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005827
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005828 return rpc_run_task(&task_setup_data);
5829}
5830
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005831static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005832{
5833 struct rpc_task *task;
5834 int ret = 0;
5835
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005836 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5837 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005838 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005839 if (IS_ERR(task))
5840 ret = PTR_ERR(task);
5841 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005842 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005843 dprintk("<-- %s status=%d\n", __func__, ret);
5844 return ret;
5845}
5846
5847static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5848{
5849 struct rpc_task *task;
5850 int ret;
5851
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005852 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005853 if (IS_ERR(task)) {
5854 ret = PTR_ERR(task);
5855 goto out;
5856 }
5857 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005858 if (!ret) {
5859 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5860
5861 if (task->tk_status == 0)
5862 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005863 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005864 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005865 rpc_put_task(task);
5866out:
5867 dprintk("<-- %s status=%d\n", __func__, ret);
5868 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005869}
5870
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005871struct nfs4_reclaim_complete_data {
5872 struct nfs_client *clp;
5873 struct nfs41_reclaim_complete_args arg;
5874 struct nfs41_reclaim_complete_res res;
5875};
5876
5877static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5878{
5879 struct nfs4_reclaim_complete_data *calldata = data;
5880
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005881 nfs41_setup_sequence(calldata->clp->cl_session,
5882 &calldata->arg.seq_args,
5883 &calldata->res.seq_res,
5884 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005885}
5886
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005887static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5888{
5889 switch(task->tk_status) {
5890 case 0:
5891 case -NFS4ERR_COMPLETE_ALREADY:
5892 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5893 break;
5894 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005895 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005896 /* fall through */
5897 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005898 return -EAGAIN;
5899 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005900 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005901 }
5902 return 0;
5903}
5904
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005905static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5906{
5907 struct nfs4_reclaim_complete_data *calldata = data;
5908 struct nfs_client *clp = calldata->clp;
5909 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5910
5911 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005912 if (!nfs41_sequence_done(task, res))
5913 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005914
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005915 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5916 rpc_restart_call_prepare(task);
5917 return;
5918 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005919 dprintk("<-- %s\n", __func__);
5920}
5921
5922static void nfs4_free_reclaim_complete_data(void *data)
5923{
5924 struct nfs4_reclaim_complete_data *calldata = data;
5925
5926 kfree(calldata);
5927}
5928
5929static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5930 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5931 .rpc_call_done = nfs4_reclaim_complete_done,
5932 .rpc_release = nfs4_free_reclaim_complete_data,
5933};
5934
5935/*
5936 * Issue a global reclaim complete.
5937 */
5938static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5939{
5940 struct nfs4_reclaim_complete_data *calldata;
5941 struct rpc_task *task;
5942 struct rpc_message msg = {
5943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5944 };
5945 struct rpc_task_setup task_setup_data = {
5946 .rpc_client = clp->cl_rpcclient,
5947 .rpc_message = &msg,
5948 .callback_ops = &nfs4_reclaim_complete_call_ops,
5949 .flags = RPC_TASK_ASYNC,
5950 };
5951 int status = -ENOMEM;
5952
5953 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005954 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005955 if (calldata == NULL)
5956 goto out;
5957 calldata->clp = clp;
5958 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005959
Trond Myklebust9d12b212012-01-17 22:04:25 -05005960 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005961 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005962 msg.rpc_argp = &calldata->arg;
5963 msg.rpc_resp = &calldata->res;
5964 task_setup_data.callback_data = calldata;
5965 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005966 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005967 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005968 goto out;
5969 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005970 status = nfs4_wait_for_completion_rpc_task(task);
5971 if (status == 0)
5972 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005973 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005974 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005975out:
5976 dprintk("<-- %s status=%d\n", __func__, status);
5977 return status;
5978}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005979
5980static void
5981nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5982{
5983 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005984 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005985 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005986
5987 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005988 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5989 * right now covering the LAYOUTGET we are about to send.
5990 * However, that is not so catastrophic, and there seems
5991 * to be no way to prevent it completely.
5992 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005993 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005994 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005995 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005996 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5997 NFS_I(lgp->args.inode)->layout,
5998 lgp->args.ctx->state)) {
5999 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006000 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006001}
6002
6003static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6004{
6005 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006006 struct inode *inode = lgp->args.inode;
6007 struct nfs_server *server = NFS_SERVER(inode);
6008 struct pnfs_layout_hdr *lo;
6009 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006010
6011 dprintk("--> %s\n", __func__);
6012
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006013 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006014 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006015
6016 switch (task->tk_status) {
6017 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006018 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006019 case -NFS4ERR_LAYOUTTRYLATER:
6020 case -NFS4ERR_RECALLCONFLICT:
6021 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006022 break;
6023 case -NFS4ERR_EXPIRED:
6024 case -NFS4ERR_BAD_STATEID:
6025 spin_lock(&inode->i_lock);
6026 lo = NFS_I(inode)->layout;
6027 if (!lo || list_empty(&lo->plh_segs)) {
6028 spin_unlock(&inode->i_lock);
6029 /* If the open stateid was bad, then recover it. */
6030 state = lgp->args.ctx->state;
6031 } else {
6032 LIST_HEAD(head);
6033
6034 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6035 spin_unlock(&inode->i_lock);
6036 /* Mark the bad layout state as invalid, then
6037 * retry using the open stateid. */
6038 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006039 }
6040 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006041 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6042 rpc_restart_call_prepare(task);
6043out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006044 dprintk("<-- %s\n", __func__);
6045}
6046
Idan Kedar85541162012-08-02 11:47:10 +03006047static size_t max_response_pages(struct nfs_server *server)
6048{
6049 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6050 return nfs_page_array_len(0, max_resp_sz);
6051}
6052
6053static void nfs4_free_pages(struct page **pages, size_t size)
6054{
6055 int i;
6056
6057 if (!pages)
6058 return;
6059
6060 for (i = 0; i < size; i++) {
6061 if (!pages[i])
6062 break;
6063 __free_page(pages[i]);
6064 }
6065 kfree(pages);
6066}
6067
6068static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6069{
6070 struct page **pages;
6071 int i;
6072
6073 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6074 if (!pages) {
6075 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6076 return NULL;
6077 }
6078
6079 for (i = 0; i < size; i++) {
6080 pages[i] = alloc_page(gfp_flags);
6081 if (!pages[i]) {
6082 dprintk("%s: failed to allocate page\n", __func__);
6083 nfs4_free_pages(pages, size);
6084 return NULL;
6085 }
6086 }
6087
6088 return pages;
6089}
6090
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006091static void nfs4_layoutget_release(void *calldata)
6092{
6093 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006094 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6095 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006096
6097 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006098 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006099 put_nfs_open_context(lgp->args.ctx);
6100 kfree(calldata);
6101 dprintk("<-- %s\n", __func__);
6102}
6103
6104static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6105 .rpc_call_prepare = nfs4_layoutget_prepare,
6106 .rpc_call_done = nfs4_layoutget_done,
6107 .rpc_release = nfs4_layoutget_release,
6108};
6109
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006110struct pnfs_layout_segment *
6111nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006112{
6113 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006114 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006115 struct rpc_task *task;
6116 struct rpc_message msg = {
6117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6118 .rpc_argp = &lgp->args,
6119 .rpc_resp = &lgp->res,
6120 };
6121 struct rpc_task_setup task_setup_data = {
6122 .rpc_client = server->client,
6123 .rpc_message = &msg,
6124 .callback_ops = &nfs4_layoutget_call_ops,
6125 .callback_data = lgp,
6126 .flags = RPC_TASK_ASYNC,
6127 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006128 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006129 int status = 0;
6130
6131 dprintk("--> %s\n", __func__);
6132
Idan Kedar85541162012-08-02 11:47:10 +03006133 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6134 if (!lgp->args.layout.pages) {
6135 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006136 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006137 }
6138 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6139
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006140 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006141 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006142 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006143 task = rpc_run_task(&task_setup_data);
6144 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006145 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006146 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006147 if (status == 0)
6148 status = task->tk_status;
6149 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006150 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006151 rpc_put_task(task);
6152 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006153 if (status)
6154 return ERR_PTR(status);
6155 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006156}
6157
Benny Halevycbe82602011-05-22 19:52:37 +03006158static void
6159nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6160{
6161 struct nfs4_layoutreturn *lrp = calldata;
6162
6163 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006164 nfs41_setup_sequence(lrp->clp->cl_session,
6165 &lrp->args.seq_args,
6166 &lrp->res.seq_res,
6167 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006168}
6169
6170static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6171{
6172 struct nfs4_layoutreturn *lrp = calldata;
6173 struct nfs_server *server;
6174
6175 dprintk("--> %s\n", __func__);
6176
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006177 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006178 return;
6179
6180 server = NFS_SERVER(lrp->args.inode);
6181 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006182 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006183 return;
6184 }
Benny Halevycbe82602011-05-22 19:52:37 +03006185 dprintk("<-- %s\n", __func__);
6186}
6187
6188static void nfs4_layoutreturn_release(void *calldata)
6189{
6190 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006191 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006192
6193 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006194 spin_lock(&lo->plh_inode->i_lock);
6195 if (lrp->res.lrs_present)
6196 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6197 lo->plh_block_lgets--;
6198 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006199 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006200 kfree(calldata);
6201 dprintk("<-- %s\n", __func__);
6202}
6203
6204static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6205 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6206 .rpc_call_done = nfs4_layoutreturn_done,
6207 .rpc_release = nfs4_layoutreturn_release,
6208};
6209
6210int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6211{
6212 struct rpc_task *task;
6213 struct rpc_message msg = {
6214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6215 .rpc_argp = &lrp->args,
6216 .rpc_resp = &lrp->res,
6217 };
6218 struct rpc_task_setup task_setup_data = {
6219 .rpc_client = lrp->clp->cl_rpcclient,
6220 .rpc_message = &msg,
6221 .callback_ops = &nfs4_layoutreturn_call_ops,
6222 .callback_data = lrp,
6223 };
6224 int status;
6225
6226 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006227 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006228 task = rpc_run_task(&task_setup_data);
6229 if (IS_ERR(task))
6230 return PTR_ERR(task);
6231 status = task->tk_status;
6232 dprintk("<-- %s status=%d\n", __func__, status);
6233 rpc_put_task(task);
6234 return status;
6235}
6236
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006237/*
6238 * Retrieve the list of Data Server devices from the MDS.
6239 */
6240static int _nfs4_getdevicelist(struct nfs_server *server,
6241 const struct nfs_fh *fh,
6242 struct pnfs_devicelist *devlist)
6243{
6244 struct nfs4_getdevicelist_args args = {
6245 .fh = fh,
6246 .layoutclass = server->pnfs_curr_ld->id,
6247 };
6248 struct nfs4_getdevicelist_res res = {
6249 .devlist = devlist,
6250 };
6251 struct rpc_message msg = {
6252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6253 .rpc_argp = &args,
6254 .rpc_resp = &res,
6255 };
6256 int status;
6257
6258 dprintk("--> %s\n", __func__);
6259 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6260 &res.seq_res, 0);
6261 dprintk("<-- %s status=%d\n", __func__, status);
6262 return status;
6263}
6264
6265int nfs4_proc_getdevicelist(struct nfs_server *server,
6266 const struct nfs_fh *fh,
6267 struct pnfs_devicelist *devlist)
6268{
6269 struct nfs4_exception exception = { };
6270 int err;
6271
6272 do {
6273 err = nfs4_handle_exception(server,
6274 _nfs4_getdevicelist(server, fh, devlist),
6275 &exception);
6276 } while (exception.retry);
6277
6278 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6279 err, devlist->num_devs);
6280
6281 return err;
6282}
6283EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6284
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006285static int
6286_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6287{
6288 struct nfs4_getdeviceinfo_args args = {
6289 .pdev = pdev,
6290 };
6291 struct nfs4_getdeviceinfo_res res = {
6292 .pdev = pdev,
6293 };
6294 struct rpc_message msg = {
6295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6296 .rpc_argp = &args,
6297 .rpc_resp = &res,
6298 };
6299 int status;
6300
6301 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006302 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006303 dprintk("<-- %s status=%d\n", __func__, status);
6304
6305 return status;
6306}
6307
6308int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6309{
6310 struct nfs4_exception exception = { };
6311 int err;
6312
6313 do {
6314 err = nfs4_handle_exception(server,
6315 _nfs4_proc_getdeviceinfo(server, pdev),
6316 &exception);
6317 } while (exception.retry);
6318 return err;
6319}
6320EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6321
Andy Adamson863a3c62011-03-23 13:27:54 +00006322static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6323{
6324 struct nfs4_layoutcommit_data *data = calldata;
6325 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006326 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006327
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006328 nfs41_setup_sequence(session,
6329 &data->args.seq_args,
6330 &data->res.seq_res,
6331 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006332}
6333
6334static void
6335nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6336{
6337 struct nfs4_layoutcommit_data *data = calldata;
6338 struct nfs_server *server = NFS_SERVER(data->args.inode);
6339
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006340 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006341 return;
6342
6343 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006344 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6345 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6346 case -NFS4ERR_BADLAYOUT: /* no layout */
6347 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006348 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006349 break;
6350 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006351 nfs_post_op_update_inode_force_wcc(data->args.inode,
6352 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006353 break;
6354 default:
6355 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6356 rpc_restart_call_prepare(task);
6357 return;
6358 }
6359 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006360}
6361
6362static void nfs4_layoutcommit_release(void *calldata)
6363{
6364 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006365 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006366 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006367
Andy Adamsondb29c082011-07-30 20:52:38 -04006368 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006369 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006370 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6371 list_del_init(&lseg->pls_lc_list);
6372 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6373 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006374 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006375 }
Peng Tao92407e72011-10-23 20:21:17 -07006376
6377 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6378 smp_mb__after_clear_bit();
6379 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6380
Andy Adamson863a3c62011-03-23 13:27:54 +00006381 put_rpccred(data->cred);
6382 kfree(data);
6383}
6384
6385static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6386 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6387 .rpc_call_done = nfs4_layoutcommit_done,
6388 .rpc_release = nfs4_layoutcommit_release,
6389};
6390
6391int
Andy Adamsonef311532011-03-12 02:58:10 -05006392nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006393{
6394 struct rpc_message msg = {
6395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6396 .rpc_argp = &data->args,
6397 .rpc_resp = &data->res,
6398 .rpc_cred = data->cred,
6399 };
6400 struct rpc_task_setup task_setup_data = {
6401 .task = &data->task,
6402 .rpc_client = NFS_CLIENT(data->args.inode),
6403 .rpc_message = &msg,
6404 .callback_ops = &nfs4_layoutcommit_ops,
6405 .callback_data = data,
6406 .flags = RPC_TASK_ASYNC,
6407 };
6408 struct rpc_task *task;
6409 int status = 0;
6410
6411 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6412 "lbw: %llu inode %lu\n",
6413 data->task.tk_pid, sync,
6414 data->args.lastbytewritten,
6415 data->args.inode->i_ino);
6416
Trond Myklebust9d12b212012-01-17 22:04:25 -05006417 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006418 task = rpc_run_task(&task_setup_data);
6419 if (IS_ERR(task))
6420 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006421 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006422 goto out;
6423 status = nfs4_wait_for_completion_rpc_task(task);
6424 if (status != 0)
6425 goto out;
6426 status = task->tk_status;
6427out:
6428 dprintk("%s: status %d\n", __func__, status);
6429 rpc_put_task(task);
6430 return status;
6431}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006432
6433static int
6434_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6435 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6436{
6437 struct nfs41_secinfo_no_name_args args = {
6438 .style = SECINFO_STYLE_CURRENT_FH,
6439 };
6440 struct nfs4_secinfo_res res = {
6441 .flavors = flavors,
6442 };
6443 struct rpc_message msg = {
6444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6445 .rpc_argp = &args,
6446 .rpc_resp = &res,
6447 };
6448 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6449}
6450
6451static int
6452nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6453 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6454{
6455 struct nfs4_exception exception = { };
6456 int err;
6457 do {
6458 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6459 switch (err) {
6460 case 0:
6461 case -NFS4ERR_WRONGSEC:
6462 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006463 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006464 default:
6465 err = nfs4_handle_exception(server, err, &exception);
6466 }
6467 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006468out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006469 return err;
6470}
6471
6472static int
6473nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6474 struct nfs_fsinfo *info)
6475{
6476 int err;
6477 struct page *page;
6478 rpc_authflavor_t flavor;
6479 struct nfs4_secinfo_flavors *flavors;
6480
6481 page = alloc_page(GFP_KERNEL);
6482 if (!page) {
6483 err = -ENOMEM;
6484 goto out;
6485 }
6486
6487 flavors = page_address(page);
6488 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6489
6490 /*
6491 * Fall back on "guess and check" method if
6492 * the server doesn't support SECINFO_NO_NAME
6493 */
6494 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6495 err = nfs4_find_root_sec(server, fhandle, info);
6496 goto out_freepage;
6497 }
6498 if (err)
6499 goto out_freepage;
6500
6501 flavor = nfs_find_best_sec(flavors);
6502 if (err == 0)
6503 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6504
6505out_freepage:
6506 put_page(page);
6507 if (err == -EACCES)
6508 return -EPERM;
6509out:
6510 return err;
6511}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006512
6513static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006514{
6515 int status;
6516 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006517 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006518 };
6519 struct nfs41_test_stateid_res res;
6520 struct rpc_message msg = {
6521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6522 .rpc_argp = &args,
6523 .rpc_resp = &res,
6524 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006525
Chuck Lever38527b12012-07-11 16:30:23 -04006526 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006527 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006528 nfs4_set_sequence_privileged(&args.seq_args);
6529 status = nfs4_call_sync_sequence(server->client, server, &msg,
6530 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006531 if (status != NFS_OK) {
6532 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006533 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006534 }
6535 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006536 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006537}
6538
Chuck Lever38527b12012-07-11 16:30:23 -04006539/**
6540 * nfs41_test_stateid - perform a TEST_STATEID operation
6541 *
6542 * @server: server / transport on which to perform the operation
6543 * @stateid: state ID to test
6544 *
6545 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6546 * Otherwise a negative NFS4ERR value is returned if the operation
6547 * failed or the state ID is not currently valid.
6548 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006549static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006550{
6551 struct nfs4_exception exception = { };
6552 int err;
6553 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006554 err = _nfs41_test_stateid(server, stateid);
6555 if (err != -NFS4ERR_DELAY)
6556 break;
6557 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006558 } while (exception.retry);
6559 return err;
6560}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006561
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006562static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006563{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006564 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006565 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006566 };
6567 struct nfs41_free_stateid_res res;
6568 struct rpc_message msg = {
6569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6570 .rpc_argp = &args,
6571 .rpc_resp = &res,
6572 };
Chuck Lever38527b12012-07-11 16:30:23 -04006573 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006574
Chuck Lever38527b12012-07-11 16:30:23 -04006575 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006576 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006577 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006578 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006579 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006580 dprintk("NFS reply free_stateid: %d\n", status);
6581 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006582}
6583
Chuck Lever38527b12012-07-11 16:30:23 -04006584/**
6585 * nfs41_free_stateid - perform a FREE_STATEID operation
6586 *
6587 * @server: server / transport on which to perform the operation
6588 * @stateid: state ID to release
6589 *
6590 * Returns NFS_OK if the server freed "stateid". Otherwise a
6591 * negative NFS4ERR value is returned.
6592 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006593static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006594{
6595 struct nfs4_exception exception = { };
6596 int err;
6597 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006598 err = _nfs4_free_stateid(server, stateid);
6599 if (err != -NFS4ERR_DELAY)
6600 break;
6601 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006602 } while (exception.retry);
6603 return err;
6604}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006605
6606static bool nfs41_match_stateid(const nfs4_stateid *s1,
6607 const nfs4_stateid *s2)
6608{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006609 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006610 return false;
6611
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006612 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006613 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006614 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006615 return true;
6616
6617 return false;
6618}
6619
Andy Adamson557134a2009-04-01 09:21:53 -04006620#endif /* CONFIG_NFS_V4_1 */
6621
Trond Myklebust36281ca2012-03-04 18:13:56 -05006622static bool nfs4_match_stateid(const nfs4_stateid *s1,
6623 const nfs4_stateid *s2)
6624{
Trond Myklebustf597c532012-03-04 18:13:56 -05006625 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006626}
6627
6628
Trond Myklebust17280172012-03-11 13:11:00 -04006629static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006630 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006631 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 .recover_open = nfs4_open_reclaim,
6633 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006634 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006635 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006636 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637};
6638
Andy Adamson591d71c2009-04-01 09:22:47 -04006639#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006640static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006641 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6642 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6643 .recover_open = nfs4_open_reclaim,
6644 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006645 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006646 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006647 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006648 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006649};
6650#endif /* CONFIG_NFS_V4_1 */
6651
Trond Myklebust17280172012-03-11 13:11:00 -04006652static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006653 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006654 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 .recover_open = nfs4_open_expired,
6656 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006657 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006658 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659};
6660
Andy Adamson591d71c2009-04-01 09:22:47 -04006661#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006662static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006663 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6664 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006665 .recover_open = nfs41_open_expired,
6666 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006667 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006668 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006669};
6670#endif /* CONFIG_NFS_V4_1 */
6671
Trond Myklebust17280172012-03-11 13:11:00 -04006672static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006673 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006674 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006675 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006676};
6677
6678#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006679static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006680 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006681 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006682 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006683};
6684#endif
6685
Trond Myklebust97dc1352010-06-16 09:52:26 -04006686static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6687 .minor_version = 0,
6688 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006689 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006690 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006691 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6692 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6693 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006694};
6695
6696#if defined(CONFIG_NFS_V4_1)
6697static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6698 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006699 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006700 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006701 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006702 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6703 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6704 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006705};
6706#endif
6707
Trond Myklebust97dc1352010-06-16 09:52:26 -04006708const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6709 [0] = &nfs_v4_0_minor_ops,
6710#if defined(CONFIG_NFS_V4_1)
6711 [1] = &nfs_v4_1_minor_ops,
6712#endif
6713};
6714
Bryan Schumaker73a79702012-07-16 16:39:12 -04006715const struct inode_operations nfs4_dir_inode_operations = {
6716 .create = nfs_create,
6717 .lookup = nfs_lookup,
6718 .atomic_open = nfs_atomic_open,
6719 .link = nfs_link,
6720 .unlink = nfs_unlink,
6721 .symlink = nfs_symlink,
6722 .mkdir = nfs_mkdir,
6723 .rmdir = nfs_rmdir,
6724 .mknod = nfs_mknod,
6725 .rename = nfs_rename,
6726 .permission = nfs_permission,
6727 .getattr = nfs_getattr,
6728 .setattr = nfs_setattr,
6729 .getxattr = generic_getxattr,
6730 .setxattr = generic_setxattr,
6731 .listxattr = generic_listxattr,
6732 .removexattr = generic_removexattr,
6733};
6734
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006735static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006736 .permission = nfs_permission,
6737 .getattr = nfs_getattr,
6738 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006739 .getxattr = generic_getxattr,
6740 .setxattr = generic_setxattr,
6741 .listxattr = generic_listxattr,
6742 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006743};
6744
David Howells509de812006-08-22 20:06:11 -04006745const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 .version = 4, /* protocol version */
6747 .dentry_ops = &nfs4_dentry_operations,
6748 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006749 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006750 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006752 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006753 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 .getattr = nfs4_proc_getattr,
6755 .setattr = nfs4_proc_setattr,
6756 .lookup = nfs4_proc_lookup,
6757 .access = nfs4_proc_access,
6758 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 .create = nfs4_proc_create,
6760 .remove = nfs4_proc_remove,
6761 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006762 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 .unlink_done = nfs4_proc_unlink_done,
6764 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006765 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006766 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006767 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 .link = nfs4_proc_link,
6769 .symlink = nfs4_proc_symlink,
6770 .mkdir = nfs4_proc_mkdir,
6771 .rmdir = nfs4_proc_remove,
6772 .readdir = nfs4_proc_readdir,
6773 .mknod = nfs4_proc_mknod,
6774 .statfs = nfs4_proc_statfs,
6775 .fsinfo = nfs4_proc_fsinfo,
6776 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006777 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 .decode_dirent = nfs4_decode_dirent,
6779 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006780 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006781 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006782 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006784 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006785 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006786 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006788 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006789 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006791 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006792 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006793 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006794 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006795 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006796 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006797 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006798 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006799 .create_server = nfs4_create_server,
6800 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801};
6802
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006803static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6804 .prefix = XATTR_NAME_NFSV4_ACL,
6805 .list = nfs4_xattr_list_nfs4_acl,
6806 .get = nfs4_xattr_get_nfs4_acl,
6807 .set = nfs4_xattr_set_nfs4_acl,
6808};
6809
6810const struct xattr_handler *nfs4_xattr_handlers[] = {
6811 &nfs4_xattr_nfs4_acl_handler,
6812 NULL
6813};
6814
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815/*
6816 * Local variables:
6817 * c-basic-offset: 8
6818 * End:
6819 */