blob: 92584c1ea7258f1fdda104e7b76424906cf57d8c [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 Myklebustb7391f42008-12-23 15:21:52 -0500899 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 return 1;
901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100904{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100906 case FMODE_WRITE:
907 state->n_wronly++;
908 break;
909 case FMODE_READ:
910 state->n_rdonly++;
911 break;
912 case FMODE_READ|FMODE_WRITE:
913 state->n_rdwr++;
914 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100916}
917
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400919{
920 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500921 nfs4_stateid_copy(&state->stateid, stateid);
922 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400924 case FMODE_READ:
925 set_bit(NFS_O_RDONLY_STATE, &state->flags);
926 break;
927 case FMODE_WRITE:
928 set_bit(NFS_O_WRONLY_STATE, &state->flags);
929 break;
930 case FMODE_READ|FMODE_WRITE:
931 set_bit(NFS_O_RDWR_STATE, &state->flags);
932 }
933}
934
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500935static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400936{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400937 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400940}
941
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942static 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 -0700943{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400944 /*
945 * Protect the call to nfs4_state_set_mode_locked and
946 * serialise the stateid update
947 */
948 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400949 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500950 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951 set_bit(NFS_DELEGATED_STATE, &state->flags);
952 }
953 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400955 write_sequnlock(&state->seqlock);
956 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700958 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static 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 -0500962{
963 struct nfs_inode *nfsi = NFS_I(state->inode);
964 struct nfs_delegation *deleg_cur;
965 int ret = 0;
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500968
969 rcu_read_lock();
970 deleg_cur = rcu_dereference(nfsi->delegation);
971 if (deleg_cur == NULL)
972 goto no_delegation;
973
974 spin_lock(&deleg_cur->lock);
975 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500977 goto no_delegation_unlock;
978
979 if (delegation == NULL)
980 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500981 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500982 goto no_delegation_unlock;
983
Trond Myklebustb7391f42008-12-23 15:21:52 -0500984 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500985 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500986 ret = 1;
987no_delegation_unlock:
988 spin_unlock(&deleg_cur->lock);
989no_delegation:
990 rcu_read_unlock();
991
992 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500994 ret = 1;
995 }
996
997 return ret;
998}
999
1000
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002{
1003 struct nfs_delegation *delegation;
1004
1005 rcu_read_lock();
1006 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008 rcu_read_unlock();
1009 return;
1010 }
1011 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001012 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013}
1014
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016{
1017 struct nfs4_state *state = opendata->state;
1018 struct nfs_inode *nfsi = NFS_I(state->inode);
1019 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001020 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022 nfs4_stateid stateid;
1023 int ret = -EAGAIN;
1024
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001027 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 if (can_open_cached(state, fmode, open_mode)) {
1029 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001031 goto out_return_state;
1032 }
1033 spin_unlock(&state->owner->so_lock);
1034 }
Trond Myklebust34310432008-12-23 15:21:38 -05001035 rcu_read_lock();
1036 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001037 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001038 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001040 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001042 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001044 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 if (ret != 0)
1046 goto out;
1047 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001048
1049 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001051 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053out:
1054 return ERR_PTR(ret);
1055out_return_state:
1056 atomic_inc(&state->count);
1057 return state;
1058}
1059
Andy Adamsone23008e2012-10-02 21:07:32 -04001060static void
1061nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1062{
1063 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1064 struct nfs_delegation *delegation;
1065 int delegation_flags = 0;
1066
1067 rcu_read_lock();
1068 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1069 if (delegation)
1070 delegation_flags = delegation->flags;
1071 rcu_read_unlock();
1072 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1073 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1074 "returning a delegation for "
1075 "OPEN(CLAIM_DELEGATE_CUR)\n",
1076 clp->cl_hostname);
1077 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1078 nfs_inode_set_delegation(state->inode,
1079 data->owner->so_cred,
1080 &data->o_res);
1081 else
1082 nfs_inode_reclaim_delegation(state->inode,
1083 data->owner->so_cred,
1084 &data->o_res);
1085}
1086
1087/*
1088 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1089 * and update the nfs4_state.
1090 */
1091static struct nfs4_state *
1092_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1093{
1094 struct inode *inode = data->state->inode;
1095 struct nfs4_state *state = data->state;
1096 int ret;
1097
1098 if (!data->rpc_done) {
1099 ret = data->rpc_status;
1100 goto err;
1101 }
1102
1103 ret = -ESTALE;
1104 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1105 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1106 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1107 goto err;
1108
1109 ret = -ENOMEM;
1110 state = nfs4_get_open_state(inode, data->owner);
1111 if (state == NULL)
1112 goto err;
1113
1114 ret = nfs_refresh_inode(inode, &data->f_attr);
1115 if (ret)
1116 goto err;
1117
1118 if (data->o_res.delegation_type != 0)
1119 nfs4_opendata_check_deleg(data, state);
1120 update_open_stateid(state, &data->o_res.stateid, NULL,
1121 data->o_arg.fmode);
1122
1123 return state;
1124err:
1125 return ERR_PTR(ret);
1126
1127}
1128
1129static struct nfs4_state *
1130_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131{
1132 struct inode *inode;
1133 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001134 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135
Trond Myklebustaac00a82007-07-05 19:02:21 -04001136 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001137 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001138 goto out;
1139 }
1140
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001141 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001143 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001146 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147 goto err;
1148 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149 state = nfs4_get_open_state(inode, data->owner);
1150 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001151 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001152 if (data->o_res.delegation_type != 0)
1153 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001154 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001156 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001157out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001158 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001159err_put_inode:
1160 iput(inode);
1161err:
1162 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001163}
1164
Andy Adamsone23008e2012-10-02 21:07:32 -04001165static struct nfs4_state *
1166nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1167{
1168 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1169 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1170 return _nfs4_opendata_to_nfs4_state(data);
1171}
1172
Trond Myklebust864472e2006-01-03 09:55:15 +01001173static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1174{
1175 struct nfs_inode *nfsi = NFS_I(state->inode);
1176 struct nfs_open_context *ctx;
1177
1178 spin_lock(&state->inode->i_lock);
1179 list_for_each_entry(ctx, &nfsi->open_files, list) {
1180 if (ctx->state != state)
1181 continue;
1182 get_nfs_open_context(ctx);
1183 spin_unlock(&state->inode->i_lock);
1184 return ctx;
1185 }
1186 spin_unlock(&state->inode->i_lock);
1187 return ERR_PTR(-ENOENT);
1188}
1189
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001190static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1191{
1192 struct nfs4_opendata *opendata;
1193
Al Viro3d4ff432011-06-22 18:40:12 -04001194 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001195 if (opendata == NULL)
1196 return ERR_PTR(-ENOMEM);
1197 opendata->state = state;
1198 atomic_inc(&state->count);
1199 return opendata;
1200}
1201
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001202static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001203{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 int ret;
1206
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001207 opendata->o_arg.open_flags = 0;
1208 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1210 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1211 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001212 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 if (ret != 0)
1214 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001216 if (IS_ERR(newstate))
1217 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001218 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 return 0;
1221}
1222
1223static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1224{
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 int ret;
1227
1228 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 smp_rmb();
1231 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001232 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 if (ret != 0)
1235 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 if (newstate != state)
1237 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 }
1239 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001240 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001241 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 if (ret != 0)
1243 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 if (newstate != state)
1245 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 }
1247 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001248 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001249 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 if (ret != 0)
1251 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 if (newstate != state)
1253 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 /*
1256 * We may have performed cached opens for all three recoveries.
1257 * Check if we need to update the current stateid.
1258 */
1259 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001260 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001261 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001263 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001264 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001266 return 0;
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/*
1270 * OPEN_RECLAIM:
1271 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001273static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001275 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001277 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int status;
1279
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001280 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1281 if (IS_ERR(opendata))
1282 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1284 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001285 rcu_read_lock();
1286 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001287 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001288 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001289 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001290 opendata->o_arg.u.delegation_type = delegation_type;
1291 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return status;
1294}
1295
Trond Myklebust539cd032007-06-05 11:46:42 -04001296static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 struct nfs_server *server = NFS_SERVER(state->inode);
1299 struct nfs4_exception exception = { };
1300 int err;
1301 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001303 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001304 break;
1305 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 } while (exception.retry);
1307 return err;
1308}
1309
Trond Myklebust864472e2006-01-03 09:55:15 +01001310static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1311{
1312 struct nfs_open_context *ctx;
1313 int ret;
1314
1315 ctx = nfs4_state_find_open_context(state);
1316 if (IS_ERR(ctx))
1317 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001318 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001319 put_nfs_open_context(ctx);
1320 return ret;
1321}
1322
Trond Myklebust13437e12007-07-06 15:10:43 -04001323static 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 -07001324{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001325 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001326 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001328 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1329 if (IS_ERR(opendata))
1330 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001331 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001332 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001334 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001335 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Trond Myklebust13437e12007-07-06 15:10:43 -04001338int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001341 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int err;
1343 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001344 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 switch (err) {
1346 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347 case -ENOENT:
1348 case -ESTALE:
1349 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001350 case -NFS4ERR_BADSESSION:
1351 case -NFS4ERR_BADSLOT:
1352 case -NFS4ERR_BAD_HIGH_SLOT:
1353 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1354 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001355 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 case -NFS4ERR_STALE_CLIENTID:
1358 case -NFS4ERR_STALE_STATEID:
1359 case -NFS4ERR_EXPIRED:
1360 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001361 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001362 goto out;
1363 case -ERESTARTSYS:
1364 /*
1365 * The show must go on: exit, but mark the
1366 * stateid as needing recovery.
1367 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001368 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001369 case -NFS4ERR_ADMIN_REVOKED:
1370 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001371 nfs_inode_find_state_and_recover(state->inode,
1372 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001373 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001374 case -ENOMEM:
1375 err = 0;
1376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 err = nfs4_handle_exception(server, err, &exception);
1379 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001380out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return err;
1382}
1383
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1385{
1386 struct nfs4_opendata *data = calldata;
1387
1388 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001389 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001390 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001391 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001392 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001393 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001394 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395}
1396
1397static void nfs4_open_confirm_release(void *calldata)
1398{
1399 struct nfs4_opendata *data = calldata;
1400 struct nfs4_state *state = NULL;
1401
1402 /* If this request hasn't been cancelled, do nothing */
1403 if (data->cancelled == 0)
1404 goto out_free;
1405 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001406 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001407 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001408 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001409 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001410 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001411out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001412 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001413}
1414
1415static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416 .rpc_call_done = nfs4_open_confirm_done,
1417 .rpc_release = nfs4_open_confirm_release,
1418};
1419
1420/*
1421 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1422 */
1423static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1424{
1425 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1426 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001427 struct rpc_message msg = {
1428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1429 .rpc_argp = &data->c_arg,
1430 .rpc_resp = &data->c_res,
1431 .rpc_cred = data->owner->so_cred,
1432 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001433 struct rpc_task_setup task_setup_data = {
1434 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001435 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001436 .callback_ops = &nfs4_open_confirm_ops,
1437 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001438 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 .flags = RPC_TASK_ASYNC,
1440 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 int status;
1442
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001443 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001444 data->rpc_done = 0;
1445 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001446 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001447 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001448 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001449 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001450 status = nfs4_wait_for_completion_rpc_task(task);
1451 if (status != 0) {
1452 data->cancelled = 1;
1453 smp_wmb();
1454 } else
1455 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001456 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return status;
1458}
1459
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 struct nfs4_opendata *data = calldata;
1463 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001464
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1466 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001467 /*
1468 * Check if we still need to send an OPEN call, or if we can use
1469 * a delegation instead.
1470 */
1471 if (data->state != NULL) {
1472 struct nfs_delegation *delegation;
1473
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001474 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001475 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001476 rcu_read_lock();
1477 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001478 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1479 can_open_delegated(delegation, data->o_arg.fmode))
1480 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001481 rcu_read_unlock();
1482 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001483 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001484 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001485 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001486 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001487 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001488 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1489 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001490 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001491 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001492 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001493 &data->o_res.seq_res,
1494 task) != 0)
1495 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001496 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001497unlock_no_action:
1498 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001499out_no_action:
1500 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001501 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001502}
1503
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504static void nfs4_open_done(struct rpc_task *task, void *calldata)
1505{
1506 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001509
Trond Myklebust14516c32010-07-31 14:29:06 -04001510 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1511 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001512
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001514 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1515 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001516 case S_IFREG:
1517 break;
1518 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001520 break;
1521 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 break;
1524 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001526 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001527 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001528 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001529 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1530 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001531 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001532 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535static void nfs4_open_release(void *calldata)
1536{
1537 struct nfs4_opendata *data = calldata;
1538 struct nfs4_state *state = NULL;
1539
1540 /* If this request hasn't been cancelled, do nothing */
1541 if (data->cancelled == 0)
1542 goto out_free;
1543 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001544 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 goto out_free;
1546 /* In case we need an open_confirm, no cleanup! */
1547 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1548 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001550 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001551 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001553 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554}
1555
1556static const struct rpc_call_ops nfs4_open_ops = {
1557 .rpc_call_prepare = nfs4_open_prepare,
1558 .rpc_call_done = nfs4_open_done,
1559 .rpc_release = nfs4_open_release,
1560};
1561
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001562static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563{
1564 struct inode *dir = data->dir->d_inode;
1565 struct nfs_server *server = NFS_SERVER(dir);
1566 struct nfs_openargs *o_arg = &data->o_arg;
1567 struct nfs_openres *o_res = &data->o_res;
1568 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001569 struct rpc_message msg = {
1570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1571 .rpc_argp = o_arg,
1572 .rpc_resp = o_res,
1573 .rpc_cred = data->owner->so_cred,
1574 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001575 struct rpc_task_setup task_setup_data = {
1576 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001577 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001578 .callback_ops = &nfs4_open_ops,
1579 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001580 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 .flags = RPC_TASK_ASYNC,
1582 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583 int status;
1584
Trond Myklebust9d12b212012-01-17 22:04:25 -05001585 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001586 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001587 data->rpc_done = 0;
1588 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001589 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001590 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001591 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001592 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001593 if (IS_ERR(task))
1594 return PTR_ERR(task);
1595 status = nfs4_wait_for_completion_rpc_task(task);
1596 if (status != 0) {
1597 data->cancelled = 1;
1598 smp_wmb();
1599 } else
1600 status = data->rpc_status;
1601 rpc_put_task(task);
1602
1603 return status;
1604}
1605
1606static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1607{
1608 struct inode *dir = data->dir->d_inode;
1609 struct nfs_openres *o_res = &data->o_res;
1610 int status;
1611
1612 status = nfs4_run_open_task(data, 1);
1613 if (status != 0 || !data->rpc_done)
1614 return status;
1615
Trond Myklebust6926afd2012-01-07 13:22:46 -05001616 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1617
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001618 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1619 status = _nfs4_proc_open_confirm(data);
1620 if (status != 0)
1621 return status;
1622 }
1623
1624 return status;
1625}
1626
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001627static int nfs4_opendata_access(struct rpc_cred *cred,
1628 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001629 struct nfs4_state *state, fmode_t fmode,
1630 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001631{
1632 struct nfs_access_entry cache;
1633 u32 mask;
1634
1635 /* access call failed or for some reason the server doesn't
1636 * support any access modes -- defer access call until later */
1637 if (opendata->o_res.access_supported == 0)
1638 return 0;
1639
1640 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001641 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001642 * write mode bits, but POSIX allows the creating process to write.
1643 * use openflags to check for exec, because fmode won't
1644 * always have FMODE_EXEC set when file open for exec. */
1645 if (openflags & __FMODE_EXEC) {
1646 /* ONLY check for exec rights */
1647 mask = MAY_EXEC;
1648 } else if (fmode & FMODE_READ)
1649 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001650
1651 cache.cred = cred;
1652 cache.jiffies = jiffies;
1653 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1654 nfs_access_add_cache(state->inode, &cache);
1655
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001656 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001657 return 0;
1658
1659 /* even though OPEN succeeded, access is denied. Close the file */
1660 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001661 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001662}
1663
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001664/*
1665 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1666 */
1667static int _nfs4_proc_open(struct nfs4_opendata *data)
1668{
1669 struct inode *dir = data->dir->d_inode;
1670 struct nfs_server *server = NFS_SERVER(dir);
1671 struct nfs_openargs *o_arg = &data->o_arg;
1672 struct nfs_openres *o_res = &data->o_res;
1673 int status;
1674
1675 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001676 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001677 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001678 if (status != 0) {
1679 if (status == -NFS4ERR_BADNAME &&
1680 !(o_arg->open_flags & O_CREAT))
1681 return -ENOENT;
1682 return status;
1683 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684
Trond Myklebust6926afd2012-01-07 13:22:46 -05001685 nfs_fattr_map_and_free_names(server, &data->f_attr);
1686
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001687 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001688 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001689 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1690 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001692 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001697 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Andy Adamsond83217c2011-03-01 01:34:17 +00001701static int nfs4_recover_expired_lease(struct nfs_server *server)
1702{
1703 return nfs4_client_recover_expired_lease(server->nfs_client);
1704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * OPEN_EXPIRED:
1708 * reclaim state on the server after a network partition.
1709 * Assumes caller holds the appropriate lock
1710 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001711static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001713 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001716 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1717 if (IS_ERR(opendata))
1718 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001720 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001721 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001722 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001726static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727{
Trond Myklebust539cd032007-06-05 11:46:42 -04001728 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729 struct nfs4_exception exception = { };
1730 int err;
1731
1732 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001733 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001734 switch (err) {
1735 default:
1736 goto out;
1737 case -NFS4ERR_GRACE:
1738 case -NFS4ERR_DELAY:
1739 nfs4_handle_exception(server, err, &exception);
1740 err = 0;
1741 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001742 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001743out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001744 return err;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001750 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 ctx = nfs4_state_find_open_context(state);
1753 if (IS_ERR(ctx))
1754 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001755 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001756 put_nfs_open_context(ctx);
1757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001761static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001764 nfs4_stateid *stateid = &state->stateid;
1765 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001766
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001767 /* If a state reset has been done, test_stateid is unneeded */
1768 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1769 return;
1770
1771 status = nfs41_test_stateid(server, stateid);
1772 if (status != NFS_OK) {
1773 /* Free the stateid unless the server explicitly
1774 * informs us the stateid is unrecognized. */
1775 if (status != -NFS4ERR_BAD_STATEID)
1776 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001777 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001778
Bryan Schumaker69388672012-09-26 15:25:52 -04001779 write_seqlock(&state->seqlock);
1780 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1781 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001782 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1783 }
1784}
1785
1786/**
1787 * nfs41_check_open_stateid - possibly free an open stateid
1788 *
1789 * @state: NFSv4 state for an inode
1790 *
1791 * Returns NFS_OK if recovery for this stateid is now finished.
1792 * Otherwise a negative NFS4ERR value is returned.
1793 */
1794static int nfs41_check_open_stateid(struct nfs4_state *state)
1795{
1796 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001797 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001798 int status;
1799
1800 /* If a state reset has been done, test_stateid is unneeded */
1801 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1802 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1803 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1804 return -NFS4ERR_BAD_STATEID;
1805
1806 status = nfs41_test_stateid(server, stateid);
1807 if (status != NFS_OK) {
1808 /* Free the stateid unless the server explicitly
1809 * informs us the stateid is unrecognized. */
1810 if (status != -NFS4ERR_BAD_STATEID)
1811 nfs41_free_stateid(server, stateid);
1812
1813 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1814 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1815 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001816 }
1817 return status;
1818}
1819
1820static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1821{
Chuck Levereb64cf92012-07-11 16:30:05 -04001822 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001823
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001824 nfs41_clear_delegation_stateid(state);
1825 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001826 if (status != NFS_OK)
1827 status = nfs4_open_expired(sp, state);
1828 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001829}
1830#endif
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001833 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1834 * fields corresponding to attributes that were used to store the verifier.
1835 * Make sure we clobber those fields in the later setattr call
1836 */
1837static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1838{
1839 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1840 !(sattr->ia_valid & ATTR_ATIME_SET))
1841 sattr->ia_valid |= ATTR_ATIME;
1842
1843 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1844 !(sattr->ia_valid & ATTR_MTIME_SET))
1845 sattr->ia_valid |= ATTR_MTIME;
1846}
1847
1848/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001849 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 */
Andy Adamson82be4172012-05-23 05:02:35 -04001851static int _nfs4_do_open(struct inode *dir,
1852 struct dentry *dentry,
1853 fmode_t fmode,
1854 int flags,
1855 struct iattr *sattr,
1856 struct rpc_cred *cred,
1857 struct nfs4_state **res,
1858 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 struct nfs4_state_owner *sp;
1861 struct nfs4_state *state = NULL;
1862 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001863 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001864 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001868 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1869 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1871 goto out_err;
1872 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001873 status = nfs4_recover_expired_lease(server);
1874 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001875 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001876 if (dentry->d_inode != NULL)
1877 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001878 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001879 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001880 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001881 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Andy Adamson82be4172012-05-23 05:02:35 -04001883 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1884 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1885 if (!opendata->f_attr.mdsthreshold)
1886 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001887 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001888 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001889 if (dentry->d_inode != NULL)
1890 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001891
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001892 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001894 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001896 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001897 status = PTR_ERR(state);
1898 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001899 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001900 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001901 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001902
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001903 status = nfs4_opendata_access(cred, opendata, state, fmode, flags);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001904 if (status != 0)
1905 goto err_opendata_put;
1906
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001907 if (opendata->o_arg.open_flags & O_EXCL) {
1908 nfs4_exclusive_attrset(opendata, sattr);
1909
1910 nfs_fattr_init(opendata->o_res.f_attr);
1911 status = nfs4_do_setattr(state->inode, cred,
1912 opendata->o_res.f_attr, sattr,
1913 state);
1914 if (status == 0)
1915 nfs_setattr_update_inode(state->inode, sattr);
1916 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1917 }
Andy Adamson82be4172012-05-23 05:02:35 -04001918
1919 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1920 *ctx_th = opendata->f_attr.mdsthreshold;
1921 else
1922 kfree(opendata->f_attr.mdsthreshold);
1923 opendata->f_attr.mdsthreshold = NULL;
1924
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001925 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *res = state;
1928 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001929err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001930 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001931 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001932err_put_state_owner:
1933 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 *res = NULL;
1936 return status;
1937}
1938
1939
Andy Adamson82be4172012-05-23 05:02:35 -04001940static struct nfs4_state *nfs4_do_open(struct inode *dir,
1941 struct dentry *dentry,
1942 fmode_t fmode,
1943 int flags,
1944 struct iattr *sattr,
1945 struct rpc_cred *cred,
1946 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct nfs4_exception exception = { };
1949 struct nfs4_state *res;
1950 int status;
1951
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001952 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001954 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1955 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (status == 0)
1957 break;
1958 /* NOTE: BAD_SEQID means the server and client disagree about the
1959 * book-keeping w.r.t. state-changing operations
1960 * (OPEN/CLOSE/LOCK/LOCKU...)
1961 * It is actually a sign of a bug on the client or on the server.
1962 *
1963 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001964 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 * have unhashed the old state_owner for us, and that we can
1966 * therefore safely retry using a new one. We should still warn
1967 * the user though...
1968 */
1969 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001970 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001971 " returned a bad sequence-id error!\n",
1972 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 exception.retry = 1;
1974 continue;
1975 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001976 /*
1977 * BAD_STATEID on OPEN means that the server cancelled our
1978 * state before it received the OPEN_CONFIRM.
1979 * Recover by retrying the request as per the discussion
1980 * on Page 181 of RFC3530.
1981 */
1982 if (status == -NFS4ERR_BAD_STATEID) {
1983 exception.retry = 1;
1984 continue;
1985 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001986 if (status == -EAGAIN) {
1987 /* We must have found a delegation */
1988 exception.retry = 1;
1989 continue;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1992 status, &exception));
1993 } while (exception.retry);
1994 return res;
1995}
1996
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001997static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1998 struct nfs_fattr *fattr, struct iattr *sattr,
1999 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002001 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002003 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 .iap = sattr,
2005 .server = server,
2006 .bitmask = server->attr_bitmask,
2007 };
2008 struct nfs_setattrres res = {
2009 .fattr = fattr,
2010 .server = server,
2011 };
2012 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2014 .rpc_argp = &arg,
2015 .rpc_resp = &res,
2016 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002018 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002019 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Trond Myklebust0e574af2005-10-27 22:12:38 -04002021 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Trond Myklebust4fc87962012-03-08 17:42:01 -05002023 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002024 struct nfs_lockowner lockowner = {
2025 .l_owner = current->files,
2026 .l_pid = current->tgid,
2027 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002028 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002029 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002030 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2031 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002032 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002033 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002034 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Bryan Schumaker7c513052011-03-24 17:12:24 +00002036 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002037 if (status == 0 && state != NULL)
2038 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002039 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002042static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2043 struct nfs_fattr *fattr, struct iattr *sattr,
2044 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002046 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002047 struct nfs4_exception exception = {
2048 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002049 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002050 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int err;
2052 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002053 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2054 switch (err) {
2055 case -NFS4ERR_OPENMODE:
2056 if (state && !(state->state & FMODE_WRITE)) {
2057 err = -EBADF;
2058 if (sattr->ia_valid & ATTR_OPEN)
2059 err = -EACCES;
2060 goto out;
2061 }
2062 }
2063 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002065out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return err;
2067}
2068
2069struct nfs4_closedata {
2070 struct inode *inode;
2071 struct nfs4_state *state;
2072 struct nfs_closeargs arg;
2073 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002074 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002075 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002076 bool roc;
2077 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078};
2079
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002080static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002081{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002082 struct nfs4_closedata *calldata = data;
2083 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002084 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002085
Fred Isamanf7e89172011-01-06 11:36:32 +00002086 if (calldata->roc)
2087 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002088 nfs4_put_open_state(calldata->state);
2089 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002090 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002091 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002092 kfree(calldata);
2093}
2094
Trond Myklebust88069f72009-12-08 08:33:16 -05002095static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2096 fmode_t fmode)
2097{
2098 spin_lock(&state->owner->so_lock);
2099 if (!(fmode & FMODE_READ))
2100 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2101 if (!(fmode & FMODE_WRITE))
2102 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2103 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2104 spin_unlock(&state->owner->so_lock);
2105}
2106
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002107static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002109 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct nfs_server *server = NFS_SERVER(calldata->inode);
2112
Chuck Levera3ca5652012-03-01 17:00:40 -05002113 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002114 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* hmm. we are done with the inode, and in the process of freeing
2117 * the state_owner. we keep this around to process errors
2118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 switch (task->tk_status) {
2120 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002121 if (calldata->roc)
2122 pnfs_roc_set_barrier(state->inode,
2123 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002124 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002125 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002126 nfs4_close_clear_stateid_flags(state,
2127 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 break;
2129 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002130 case -NFS4ERR_OLD_STATEID:
2131 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002133 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002136 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2137 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002139 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002140 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002141 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002144static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002146 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002147 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002148 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002149 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002150
Chuck Levera3ca5652012-03-01 17:00:40 -05002151 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002152 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002153 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002154
Trond Myklebust88069f72009-12-08 08:33:16 -05002155 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2156 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002157 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002158 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002159 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002160 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002161 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2162 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2163 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002164 }
2165 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002166 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2167 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2168 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002169 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002170 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002171 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002172
2173 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002174 /* Note: exit _without_ calling nfs4_close_done */
2175 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002176 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002177 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002178 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002179
Fred Isamanf7e89172011-01-06 11:36:32 +00002180 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002181 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002182 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002183 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002184 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002185 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002186
Trond Myklebust516a6af2005-10-27 22:12:41 -04002187 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002188 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002189 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002190 &calldata->arg.seq_args,
2191 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002192 task) != 0)
2193 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002194out:
2195 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002198static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002199 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002200 .rpc_call_done = nfs4_close_done,
2201 .rpc_release = nfs4_free_closedata,
2202};
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204/*
2205 * It is possible for data to be read/written from a mem-mapped file
2206 * after the sys_close call (which hits the vfs layer as a flush).
2207 * This means that we can't safely call nfsv4 close on a file until
2208 * the inode is cleared. This in turn means that we are not good
2209 * NFSv4 citizens - we do not indicate to the server to update the file's
2210 * share state even when we are done with one of the three share
2211 * stateid's in the inode.
2212 *
2213 * NOTE: Caller must be holding the sp->so_owner semaphore!
2214 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002215int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002217 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002219 struct nfs4_state_owner *sp = state->owner;
2220 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002221 struct rpc_message msg = {
2222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2223 .rpc_cred = state->owner->so_cred,
2224 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002225 struct rpc_task_setup task_setup_data = {
2226 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002227 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002228 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002229 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002230 .flags = RPC_TASK_ASYNC,
2231 };
Trond Myklebust95121352005-10-18 14:20:12 -07002232 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002234 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002236 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002237 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002238 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002240 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002241 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002243 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002244 if (calldata->arg.seqid == NULL)
2245 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002246 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002247 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002248 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002249 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002250 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002251 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002252 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002253
Trond Myklebust1174dd12010-12-21 10:52:24 -05002254 msg.rpc_argp = &calldata->arg;
2255 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002256 task_setup_data.callback_data = calldata;
2257 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002258 if (IS_ERR(task))
2259 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002260 status = 0;
2261 if (wait)
2262 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002263 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002264 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002265out_free_calldata:
2266 kfree(calldata);
2267out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002268 nfs4_put_open_state(state);
2269 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002270 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Trond Myklebust2b484292010-09-17 10:56:51 -04002273static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002274nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 struct nfs4_state *state;
2277
Trond Myklebust565277f2007-10-15 18:17:53 -04002278 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002279 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2280 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002281 if (IS_ERR(state))
2282 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002283 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002284 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Trond Myklebust1185a552009-12-03 15:54:02 -05002287static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002288{
2289 if (ctx->state == NULL)
2290 return;
2291 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002292 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002293 else
Al Viro643168c2011-06-22 18:20:23 -04002294 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2298{
Benny Halevy43652ad2009-04-01 09:21:54 -04002299 struct nfs4_server_caps_arg args = {
2300 .fhandle = fhandle,
2301 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct nfs4_server_caps_res res = {};
2303 struct rpc_message msg = {
2304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002305 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 .rpc_resp = &res,
2307 };
2308 int status;
2309
Bryan Schumaker7c513052011-03-24 17:12:24 +00002310 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (status == 0) {
2312 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002313 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2314 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2315 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2316 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2317 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2319 server->caps |= NFS_CAP_ACLS;
2320 if (res.has_links != 0)
2321 server->caps |= NFS_CAP_HARDLINKS;
2322 if (res.has_symlinks != 0)
2323 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002324 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2325 server->caps |= NFS_CAP_FILEID;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2327 server->caps |= NFS_CAP_MODE;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2329 server->caps |= NFS_CAP_NLINK;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2331 server->caps |= NFS_CAP_OWNER;
2332 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2333 server->caps |= NFS_CAP_OWNER_GROUP;
2334 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2335 server->caps |= NFS_CAP_ATIME;
2336 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2337 server->caps |= NFS_CAP_CTIME;
2338 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2339 server->caps |= NFS_CAP_MTIME;
2340
Trond Myklebusta65318b2009-03-11 14:10:28 -04002341 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2342 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2343 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002345 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return status;
2349}
2350
Trond Myklebust55a97592006-06-09 09:34:19 -04002351int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 struct nfs4_exception exception = { };
2354 int err;
2355 do {
2356 err = nfs4_handle_exception(server,
2357 _nfs4_server_capabilities(server, fhandle),
2358 &exception);
2359 } while (exception.retry);
2360 return err;
2361}
2362
2363static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2364 struct nfs_fsinfo *info)
2365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct nfs4_lookup_root_arg args = {
2367 .bitmask = nfs4_fattr_bitmap,
2368 };
2369 struct nfs4_lookup_res res = {
2370 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002371 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 .fh = fhandle,
2373 };
2374 struct rpc_message msg = {
2375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2376 .rpc_argp = &args,
2377 .rpc_resp = &res,
2378 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002379
Trond Myklebust0e574af2005-10-27 22:12:38 -04002380 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002381 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
2384static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2385 struct nfs_fsinfo *info)
2386{
2387 struct nfs4_exception exception = { };
2388 int err;
2389 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002390 err = _nfs4_lookup_root(server, fhandle, info);
2391 switch (err) {
2392 case 0:
2393 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002394 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002395 default:
2396 err = nfs4_handle_exception(server, err, &exception);
2397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002399out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return err;
2401}
2402
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002403static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2404 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2405{
2406 struct rpc_auth *auth;
2407 int ret;
2408
2409 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002410 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002411 ret = -EIO;
2412 goto out;
2413 }
2414 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002415out:
2416 return ret;
2417}
2418
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002419static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002420 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002422 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002423 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002425 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002426 if (len < 0)
2427 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002428
2429 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002430 /* AUTH_UNIX is the default flavor if none was specified,
2431 * thus has already been tried. */
2432 if (flav_array[i] == RPC_AUTH_UNIX)
2433 continue;
2434
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002435 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002436 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002437 continue;
2438 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002439 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002440 /*
2441 * -EACCESS could mean that the user doesn't have correct permissions
2442 * to access the mount. It could also mean that we tried to mount
2443 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2444 * existing mount programs don't handle -EACCES very well so it should
2445 * be mapped to -EPERM instead.
2446 */
2447 if (status == -EACCES)
2448 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002449 return status;
2450}
2451
2452/*
2453 * get the file handle for the "/" directory on the server
2454 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002455int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2456 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002457{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002458 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002459 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002460 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2461 /*
2462 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2463 * by nfs4_map_errors() as this function exits.
2464 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002465 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (status == 0)
2467 status = nfs4_server_capabilities(server, fhandle);
2468 if (status == 0)
2469 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002470 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Bryan Schumakerbae36242012-05-10 15:07:31 -04002473static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2474 struct nfs_fsinfo *info)
2475{
2476 int error;
2477 struct nfs_fattr *fattr = info->fattr;
2478
2479 error = nfs4_server_capabilities(server, mntfh);
2480 if (error < 0) {
2481 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2482 return error;
2483 }
2484
2485 error = nfs4_proc_getattr(server, mntfh, fattr);
2486 if (error < 0) {
2487 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2488 return error;
2489 }
2490
2491 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2492 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2493 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2494
2495 return error;
2496}
2497
Manoj Naik6b97fd32006-06-09 09:34:29 -04002498/*
2499 * Get locations and (maybe) other attributes of a referral.
2500 * Note that we'll actually follow the referral later when
2501 * we detect fsid mismatch in inode revalidation
2502 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002503static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2504 const struct qstr *name, struct nfs_fattr *fattr,
2505 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002506{
2507 int status = -ENOMEM;
2508 struct page *page = NULL;
2509 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002510
2511 page = alloc_page(GFP_KERNEL);
2512 if (page == NULL)
2513 goto out;
2514 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2515 if (locations == NULL)
2516 goto out;
2517
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002518 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002519 if (status != 0)
2520 goto out;
2521 /* Make sure server returned a different fsid for the referral */
2522 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002523 dprintk("%s: server did not return a different fsid for"
2524 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002525 status = -EIO;
2526 goto out;
2527 }
Andy Adamson533eb462011-06-13 18:25:56 -04002528 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2529 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002530
Andy Adamson533eb462011-06-13 18:25:56 -04002531 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002532 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002533 memset(fhandle, 0, sizeof(struct nfs_fh));
2534out:
2535 if (page)
2536 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002537 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002538 return status;
2539}
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2542{
2543 struct nfs4_getattr_arg args = {
2544 .fh = fhandle,
2545 .bitmask = server->attr_bitmask,
2546 };
2547 struct nfs4_getattr_res res = {
2548 .fattr = fattr,
2549 .server = server,
2550 };
2551 struct rpc_message msg = {
2552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2553 .rpc_argp = &args,
2554 .rpc_resp = &res,
2555 };
2556
Trond Myklebust0e574af2005-10-27 22:12:38 -04002557 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002558 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
2561static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2562{
2563 struct nfs4_exception exception = { };
2564 int err;
2565 do {
2566 err = nfs4_handle_exception(server,
2567 _nfs4_proc_getattr(server, fhandle, fattr),
2568 &exception);
2569 } while (exception.retry);
2570 return err;
2571}
2572
2573/*
2574 * The file is not closed if it is opened due to the a request to change
2575 * the size of the file. The open call will not be needed once the
2576 * VFS layer lookup-intents are implemented.
2577 *
2578 * Close is called when the inode is destroyed.
2579 * If we haven't opened the file for O_WRONLY, we
2580 * need to in the size_change case to obtain a stateid.
2581 *
2582 * Got race?
2583 * Because OPEN is always done by name in nfsv4, it is
2584 * possible that we opened a different file by the same
2585 * name. We can recognize this race condition, but we
2586 * can't do anything about it besides returning an error.
2587 *
2588 * This will be fixed with VFS changes (lookup-intent).
2589 */
2590static int
2591nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2592 struct iattr *sattr)
2593{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002594 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002595 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002596 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 int status;
2598
Benny Halevy8a1636c2010-07-14 15:43:57 -04002599 if (pnfs_ld_layoutret_on_setattr(inode))
2600 pnfs_return_layout(inode);
2601
Trond Myklebust0e574af2005-10-27 22:12:38 -04002602 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Andy Adamson26699402012-05-30 16:12:24 -04002604 /* Deal with open(O_TRUNC) */
2605 if (sattr->ia_valid & ATTR_OPEN)
2606 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2607
2608 /* Optimization: if the end result is no change, don't RPC */
2609 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2610 return 0;
2611
Trond Myklebustd5308382005-11-04 15:33:38 -05002612 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002613 if (sattr->ia_valid & ATTR_FILE) {
2614 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002615
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002616 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002617 if (ctx) {
2618 cred = ctx->cred;
2619 state = ctx->state;
2620 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002621 }
2622
2623 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002624 if (status == 0)
2625 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 return status;
2627}
2628
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002629static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2630 const struct qstr *name, struct nfs_fh *fhandle,
2631 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002632{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002633 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002634 int status;
2635 struct nfs4_lookup_arg args = {
2636 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002637 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002638 .name = name,
2639 };
2640 struct nfs4_lookup_res res = {
2641 .server = server,
2642 .fattr = fattr,
2643 .fh = fhandle,
2644 };
2645 struct rpc_message msg = {
2646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2647 .rpc_argp = &args,
2648 .rpc_resp = &res,
2649 };
2650
2651 nfs_fattr_init(fattr);
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002654 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 dprintk("NFS reply lookup: %d\n", status);
2656 return status;
2657}
2658
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002659static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002660{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002661 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002662 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002663 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2664 fattr->nlink = 2;
2665}
2666
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002667static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2668 struct qstr *name, struct nfs_fh *fhandle,
2669 struct nfs_fattr *fattr)
2670{
2671 struct nfs4_exception exception = { };
2672 struct rpc_clnt *client = *clnt;
2673 int err;
2674 do {
2675 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2676 switch (err) {
2677 case -NFS4ERR_BADNAME:
2678 err = -ENOENT;
2679 goto out;
2680 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002681 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002682 goto out;
2683 case -NFS4ERR_WRONGSEC:
2684 err = -EPERM;
2685 if (client != *clnt)
2686 goto out;
2687
2688 client = nfs4_create_sec_client(client, dir, name);
2689 if (IS_ERR(client))
2690 return PTR_ERR(client);
2691
2692 exception.retry = 1;
2693 break;
2694 default:
2695 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2696 }
2697 } while (exception.retry);
2698
2699out:
2700 if (err == 0)
2701 *clnt = client;
2702 else if (client != *clnt)
2703 rpc_shutdown_client(client);
2704
2705 return err;
2706}
2707
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002708static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002709 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002711 int status;
2712 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002713
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002714 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2715 if (client != NFS_CLIENT(dir)) {
2716 rpc_shutdown_client(client);
2717 nfs_fixup_secinfo_attributes(fattr);
2718 }
2719 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720}
2721
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002722struct rpc_clnt *
2723nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2724 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2725{
2726 int status;
2727 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2728
2729 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2730 if (status < 0) {
2731 rpc_shutdown_client(client);
2732 return ERR_PTR(status);
2733 }
2734 return client;
2735}
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2738{
Trond Myklebust76b32992007-08-10 17:45:11 -04002739 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct nfs4_accessargs args = {
2741 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002742 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002744 struct nfs4_accessres res = {
2745 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002746 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 struct rpc_message msg = {
2748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2749 .rpc_argp = &args,
2750 .rpc_resp = &res,
2751 .rpc_cred = entry->cred,
2752 };
2753 int mode = entry->mask;
2754 int status;
2755
2756 /*
2757 * Determine which access bits we want to ask for...
2758 */
2759 if (mode & MAY_READ)
2760 args.access |= NFS4_ACCESS_READ;
2761 if (S_ISDIR(inode->i_mode)) {
2762 if (mode & MAY_WRITE)
2763 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2764 if (mode & MAY_EXEC)
2765 args.access |= NFS4_ACCESS_LOOKUP;
2766 } else {
2767 if (mode & MAY_WRITE)
2768 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2769 if (mode & MAY_EXEC)
2770 args.access |= NFS4_ACCESS_EXECUTE;
2771 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002772
2773 res.fattr = nfs_alloc_fattr();
2774 if (res.fattr == NULL)
2775 return -ENOMEM;
2776
Bryan Schumaker7c513052011-03-24 17:12:24 +00002777 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002779 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002780 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002782 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return status;
2784}
2785
2786static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2787{
2788 struct nfs4_exception exception = { };
2789 int err;
2790 do {
2791 err = nfs4_handle_exception(NFS_SERVER(inode),
2792 _nfs4_proc_access(inode, entry),
2793 &exception);
2794 } while (exception.retry);
2795 return err;
2796}
2797
2798/*
2799 * TODO: For the time being, we don't try to get any attributes
2800 * along with any of the zero-copy operations READ, READDIR,
2801 * READLINK, WRITE.
2802 *
2803 * In the case of the first three, we want to put the GETATTR
2804 * after the read-type operation -- this is because it is hard
2805 * to predict the length of a GETATTR response in v4, and thus
2806 * align the READ data correctly. This means that the GETATTR
2807 * may end up partially falling into the page cache, and we should
2808 * shift it into the 'tail' of the xdr_buf before processing.
2809 * To do this efficiently, we need to know the total length
2810 * of data received, which doesn't seem to be available outside
2811 * of the RPC layer.
2812 *
2813 * In the case of WRITE, we also want to put the GETATTR after
2814 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002815 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 *
2817 * Both of these changes to the XDR layer would in fact be quite
2818 * minor, but I decided to leave them for a subsequent patch.
2819 */
2820static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2821 unsigned int pgbase, unsigned int pglen)
2822{
2823 struct nfs4_readlink args = {
2824 .fh = NFS_FH(inode),
2825 .pgbase = pgbase,
2826 .pglen = pglen,
2827 .pages = &page,
2828 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002829 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 struct rpc_message msg = {
2831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2832 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002833 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 };
2835
Bryan Schumaker7c513052011-03-24 17:12:24 +00002836 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 -07002837}
2838
2839static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2840 unsigned int pgbase, unsigned int pglen)
2841{
2842 struct nfs4_exception exception = { };
2843 int err;
2844 do {
2845 err = nfs4_handle_exception(NFS_SERVER(inode),
2846 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2847 &exception);
2848 } while (exception.retry);
2849 return err;
2850}
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002853 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855static int
2856nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002857 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002859 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 int status = 0;
2862
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002863 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2864 if (IS_ERR(ctx))
2865 return PTR_ERR(ctx);
2866
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002867 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002868 state = nfs4_do_open(dir, dentry, ctx->mode,
2869 flags, sattr, ctx->cred,
2870 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002871 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (IS_ERR(state)) {
2873 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002874 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002876 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002877 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002878 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002880 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return status;
2882}
2883
2884static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2885{
Trond Myklebust16e42952005-10-27 22:12:44 -04002886 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002887 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002889 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002891 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002892 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002893 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2896 .rpc_argp = &args,
2897 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002899 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002900
Bryan Schumaker7c513052011-03-24 17:12:24 +00002901 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002902 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002903 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return status;
2905}
2906
2907static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2908{
2909 struct nfs4_exception exception = { };
2910 int err;
2911 do {
2912 err = nfs4_handle_exception(NFS_SERVER(dir),
2913 _nfs4_proc_remove(dir, name),
2914 &exception);
2915 } while (exception.retry);
2916 return err;
2917}
2918
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002919static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002921 struct nfs_server *server = NFS_SERVER(dir);
2922 struct nfs_removeargs *args = msg->rpc_argp;
2923 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002925 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002927 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002930static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2931{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002932 nfs4_setup_sequence(NFS_SERVER(data->dir),
2933 &data->args.seq_args,
2934 &data->res.seq_res,
2935 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002938static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002940 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2941
Trond Myklebust14516c32010-07-31 14:29:06 -04002942 if (!nfs4_sequence_done(task, &res->seq_res))
2943 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002944 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002945 return 0;
2946 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002947 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
Jeff Laytond3d41522010-09-17 17:31:57 -04002950static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2951{
2952 struct nfs_server *server = NFS_SERVER(dir);
2953 struct nfs_renameargs *arg = msg->rpc_argp;
2954 struct nfs_renameres *res = msg->rpc_resp;
2955
2956 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002957 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002958 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002959}
2960
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002961static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2962{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002963 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2964 &data->args.seq_args,
2965 &data->res.seq_res,
2966 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002967}
2968
2969static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2970 struct inode *new_dir)
2971{
2972 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2973
2974 if (!nfs4_sequence_done(task, &res->seq_res))
2975 return 0;
2976 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2977 return 0;
2978
2979 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002980 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002981 return 1;
2982}
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2985 struct inode *new_dir, struct qstr *new_name)
2986{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002987 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002988 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 .old_dir = NFS_FH(old_dir),
2990 .new_dir = NFS_FH(new_dir),
2991 .old_name = old_name,
2992 .new_name = new_name,
2993 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002994 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002995 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002996 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 struct rpc_message msg = {
2998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2999 .rpc_argp = &arg,
3000 .rpc_resp = &res,
3001 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003002 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Bryan Schumaker7c513052011-03-24 17:12:24 +00003004 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (!status) {
3006 update_changeattr(old_dir, &res.old_cinfo);
3007 update_changeattr(new_dir, &res.new_cinfo);
3008 }
3009 return status;
3010}
3011
3012static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3013 struct inode *new_dir, struct qstr *new_name)
3014{
3015 struct nfs4_exception exception = { };
3016 int err;
3017 do {
3018 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3019 _nfs4_proc_rename(old_dir, old_name,
3020 new_dir, new_name),
3021 &exception);
3022 } while (exception.retry);
3023 return err;
3024}
3025
3026static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3027{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003028 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 struct nfs4_link_arg arg = {
3030 .fh = NFS_FH(inode),
3031 .dir_fh = NFS_FH(dir),
3032 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003033 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003035 struct nfs4_link_res res = {
3036 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 struct rpc_message msg = {
3039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3040 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003041 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003043 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Trond Myklebust136f2622010-04-16 16:22:49 -04003045 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003046 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003047 goto out;
3048
Bryan Schumaker7c513052011-03-24 17:12:24 +00003049 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003050 if (!status) {
3051 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003052 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003053 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003054out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003055 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return status;
3057}
3058
3059static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3060{
3061 struct nfs4_exception exception = { };
3062 int err;
3063 do {
3064 err = nfs4_handle_exception(NFS_SERVER(inode),
3065 _nfs4_proc_link(inode, dir, name),
3066 &exception);
3067 } while (exception.retry);
3068 return err;
3069}
3070
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071struct nfs4_createdata {
3072 struct rpc_message msg;
3073 struct nfs4_create_arg arg;
3074 struct nfs4_create_res res;
3075 struct nfs_fh fh;
3076 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077};
3078
3079static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3080 struct qstr *name, struct iattr *sattr, u32 ftype)
3081{
3082 struct nfs4_createdata *data;
3083
3084 data = kzalloc(sizeof(*data), GFP_KERNEL);
3085 if (data != NULL) {
3086 struct nfs_server *server = NFS_SERVER(dir);
3087
3088 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3089 data->msg.rpc_argp = &data->arg;
3090 data->msg.rpc_resp = &data->res;
3091 data->arg.dir_fh = NFS_FH(dir);
3092 data->arg.server = server;
3093 data->arg.name = name;
3094 data->arg.attrs = sattr;
3095 data->arg.ftype = ftype;
3096 data->arg.bitmask = server->attr_bitmask;
3097 data->res.server = server;
3098 data->res.fh = &data->fh;
3099 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003100 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 }
3102 return data;
3103}
3104
3105static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3106{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003107 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003108 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003109 if (status == 0) {
3110 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003111 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3112 }
3113 return status;
3114}
3115
3116static void nfs4_free_createdata(struct nfs4_createdata *data)
3117{
3118 kfree(data);
3119}
3120
Chuck Lever4f390c12006-08-22 20:06:22 -04003121static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003122 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003124 struct nfs4_createdata *data;
3125 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Chuck Lever94a6d752006-08-22 20:06:23 -04003127 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003128 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003129
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003130 status = -ENOMEM;
3131 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3132 if (data == NULL)
3133 goto out;
3134
3135 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3136 data->arg.u.symlink.pages = &page;
3137 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003139 status = nfs4_do_create(dir, dentry, data);
3140
3141 nfs4_free_createdata(data);
3142out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return status;
3144}
3145
Chuck Lever4f390c12006-08-22 20:06:22 -04003146static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003147 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
3149 struct nfs4_exception exception = { };
3150 int err;
3151 do {
3152 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003153 _nfs4_proc_symlink(dir, dentry, page,
3154 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 &exception);
3156 } while (exception.retry);
3157 return err;
3158}
3159
3160static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3161 struct iattr *sattr)
3162{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003163 struct nfs4_createdata *data;
3164 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003166 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3167 if (data == NULL)
3168 goto out;
3169
3170 status = nfs4_do_create(dir, dentry, data);
3171
3172 nfs4_free_createdata(data);
3173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return status;
3175}
3176
3177static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3178 struct iattr *sattr)
3179{
3180 struct nfs4_exception exception = { };
3181 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003182
3183 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 do {
3185 err = nfs4_handle_exception(NFS_SERVER(dir),
3186 _nfs4_proc_mkdir(dir, dentry, sattr),
3187 &exception);
3188 } while (exception.retry);
3189 return err;
3190}
3191
3192static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003193 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
3195 struct inode *dir = dentry->d_inode;
3196 struct nfs4_readdir_arg args = {
3197 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003198 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 .pgbase = 0,
3200 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003201 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003202 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 };
3204 struct nfs4_readdir_res res;
3205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3207 .rpc_argp = &args,
3208 .rpc_resp = &res,
3209 .rpc_cred = cred,
3210 };
3211 int status;
3212
Harvey Harrison3110ff82008-05-02 13:42:44 -07003213 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003214 dentry->d_parent->d_name.name,
3215 dentry->d_name.name,
3216 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003217 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003219 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 -05003220 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003221 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003222 status += args.pgbase;
3223 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003224
3225 nfs_invalidate_atime(dir);
3226
Harvey Harrison3110ff82008-05-02 13:42:44 -07003227 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return status;
3229}
3230
3231static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003232 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
3234 struct nfs4_exception exception = { };
3235 int err;
3236 do {
3237 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3238 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003239 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 &exception);
3241 } while (exception.retry);
3242 return err;
3243}
3244
3245static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3246 struct iattr *sattr, dev_t rdev)
3247{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003248 struct nfs4_createdata *data;
3249 int mode = sattr->ia_mode;
3250 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003252 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3253 if (data == NULL)
3254 goto out;
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003257 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003259 data->arg.ftype = NF4BLK;
3260 data->arg.u.device.specdata1 = MAJOR(rdev);
3261 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
3263 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003264 data->arg.ftype = NF4CHR;
3265 data->arg.u.device.specdata1 = MAJOR(rdev);
3266 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003267 } else if (!S_ISSOCK(mode)) {
3268 status = -EINVAL;
3269 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003272 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003273out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003274 nfs4_free_createdata(data);
3275out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return status;
3277}
3278
3279static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3280 struct iattr *sattr, dev_t rdev)
3281{
3282 struct nfs4_exception exception = { };
3283 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003284
3285 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 do {
3287 err = nfs4_handle_exception(NFS_SERVER(dir),
3288 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3289 &exception);
3290 } while (exception.retry);
3291 return err;
3292}
3293
3294static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3295 struct nfs_fsstat *fsstat)
3296{
3297 struct nfs4_statfs_arg args = {
3298 .fh = fhandle,
3299 .bitmask = server->attr_bitmask,
3300 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003301 struct nfs4_statfs_res res = {
3302 .fsstat = fsstat,
3303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3306 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003307 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 };
3309
Trond Myklebust0e574af2005-10-27 22:12:38 -04003310 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003311 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
3314static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3315{
3316 struct nfs4_exception exception = { };
3317 int err;
3318 do {
3319 err = nfs4_handle_exception(server,
3320 _nfs4_proc_statfs(server, fhandle, fsstat),
3321 &exception);
3322 } while (exception.retry);
3323 return err;
3324}
3325
3326static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3327 struct nfs_fsinfo *fsinfo)
3328{
3329 struct nfs4_fsinfo_arg args = {
3330 .fh = fhandle,
3331 .bitmask = server->attr_bitmask,
3332 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003333 struct nfs4_fsinfo_res res = {
3334 .fsinfo = fsinfo,
3335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 struct rpc_message msg = {
3337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3338 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003339 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 };
3341
Bryan Schumaker7c513052011-03-24 17:12:24 +00003342 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
3345static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3346{
3347 struct nfs4_exception exception = { };
3348 int err;
3349
3350 do {
3351 err = nfs4_handle_exception(server,
3352 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3353 &exception);
3354 } while (exception.retry);
3355 return err;
3356}
3357
3358static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3359{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003360 int error;
3361
Trond Myklebust0e574af2005-10-27 22:12:38 -04003362 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003363 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003364 if (error == 0) {
3365 /* block layout checks this! */
3366 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003367 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003368 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003369
3370 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
3373static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3374 struct nfs_pathconf *pathconf)
3375{
3376 struct nfs4_pathconf_arg args = {
3377 .fh = fhandle,
3378 .bitmask = server->attr_bitmask,
3379 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003380 struct nfs4_pathconf_res res = {
3381 .pathconf = pathconf,
3382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3385 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003386 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 };
3388
3389 /* None of the pathconf attributes are mandatory to implement */
3390 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3391 memset(pathconf, 0, sizeof(*pathconf));
3392 return 0;
3393 }
3394
Trond Myklebust0e574af2005-10-27 22:12:38 -04003395 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003396 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3400 struct nfs_pathconf *pathconf)
3401{
3402 struct nfs4_exception exception = { };
3403 int err;
3404
3405 do {
3406 err = nfs4_handle_exception(server,
3407 _nfs4_proc_pathconf(server, fhandle, pathconf),
3408 &exception);
3409 } while (exception.retry);
3410 return err;
3411}
3412
Benny Halevyd20581a2011-05-22 19:52:03 +03003413void __nfs4_read_done_cb(struct nfs_read_data *data)
3414{
Fred Isamancd841602012-04-20 14:47:44 -04003415 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003416}
3417
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003418static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Fred Isamancd841602012-04-20 14:47:44 -04003420 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003422 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003423 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003424 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003426
Benny Halevyd20581a2011-05-22 19:52:03 +03003427 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003429 renew_lease(server, data->timestamp);
3430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003433static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3434{
3435
3436 dprintk("--> %s\n", __func__);
3437
3438 if (!nfs4_sequence_done(task, &data->res.seq_res))
3439 return -EAGAIN;
3440
Benny Halevyd20581a2011-05-22 19:52:03 +03003441 return data->read_done_cb ? data->read_done_cb(task, data) :
3442 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003443}
3444
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003445static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003448 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003449 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003450 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003453static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3454{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003455 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3456 &data->args.seq_args,
3457 &data->res.seq_res,
3458 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
Fred Isamanb029bc92011-03-03 15:13:42 +00003461static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Fred Isamancd841602012-04-20 14:47:44 -04003463 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003465 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003466 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003467 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003469 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003471 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003472 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Fred Isamanb029bc92011-03-03 15:13:42 +00003476static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3477{
3478 if (!nfs4_sequence_done(task, &data->res.seq_res))
3479 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003480 return data->write_done_cb ? data->write_done_cb(task, data) :
3481 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003482}
3483
Trond Myklebust5a37f852012-04-28 14:55:16 -04003484static
3485bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003486{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003487 const struct nfs_pgio_header *hdr = data->header;
3488
3489 /* Don't request attributes for pNFS or O_DIRECT writes */
3490 if (data->ds_clp != NULL || hdr->dreq != NULL)
3491 return false;
3492 /* Otherwise, request attributes if and only if we don't hold
3493 * a delegation
3494 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003495 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003496}
Fred Isamana69aef12011-03-03 15:13:47 +00003497
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003498static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Fred Isamancd841602012-04-20 14:47:44 -04003500 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003501
Trond Myklebust5a37f852012-04-28 14:55:16 -04003502 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003503 data->args.bitmask = NULL;
3504 data->res.fattr = NULL;
3505 } else
3506 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003507
Fred Isamanb029bc92011-03-03 15:13:42 +00003508 if (!data->write_done_cb)
3509 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003510 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 data->timestamp = jiffies;
3512
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003514 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003517static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3518{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003519 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3520 &data->args.seq_args,
3521 &data->res.seq_res,
3522 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
Fred Isaman0b7c0152012-04-20 14:47:39 -04003525static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3526{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003527 nfs4_setup_sequence(NFS_SERVER(data->inode),
3528 &data->args.seq_args,
3529 &data->res.seq_res,
3530 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Fred Isaman0b7c0152012-04-20 14:47:39 -04003533static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003536
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003537 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003538 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003539 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Fred Isaman0b7c0152012-04-20 14:47:39 -04003544static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003545{
3546 if (!nfs4_sequence_done(task, &data->res.seq_res))
3547 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003548 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003549}
3550
Fred Isaman0b7c0152012-04-20 14:47:39 -04003551static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003553 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003554
Fred Isaman0b7c0152012-04-20 14:47:39 -04003555 if (data->commit_done_cb == NULL)
3556 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003557 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003558 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003559 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003562struct nfs4_renewdata {
3563 struct nfs_client *client;
3564 unsigned long timestamp;
3565};
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567/*
3568 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3569 * standalone procedure for queueing an asynchronous RENEW.
3570 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003571static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003572{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003573 struct nfs4_renewdata *data = calldata;
3574 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003575
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003576 if (atomic_read(&clp->cl_count) > 1)
3577 nfs4_schedule_state_renewal(clp);
3578 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003579 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003580}
3581
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003582static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003584 struct nfs4_renewdata *data = calldata;
3585 struct nfs_client *clp = data->client;
3586 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003589 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003590 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3591 return;
3592 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003593 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003594 return;
3595 }
3596 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003598 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003601static const struct rpc_call_ops nfs4_renew_ops = {
3602 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003603 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003604};
3605
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003606static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
3608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3610 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003611 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003613 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003615 if (renew_flags == 0)
3616 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003617 if (!atomic_inc_not_zero(&clp->cl_count))
3618 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003619 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003620 if (data == NULL)
3621 return -ENOMEM;
3622 data->client = clp;
3623 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003625 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003628static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 struct rpc_message msg = {
3631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3632 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003633 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 };
3635 unsigned long now = jiffies;
3636 int status;
3637
3638 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3639 if (status < 0)
3640 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003641 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 return 0;
3643}
3644
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003645static inline int nfs4_server_supports_acls(struct nfs_server *server)
3646{
3647 return (server->caps & NFS_CAP_ACLS)
3648 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3649 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3650}
3651
Trond Myklebust21f498c2012-08-24 10:59:25 -04003652/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3653 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003654 * the stack.
3655 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003656#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003657
Neil Hormane9e3d722011-03-04 19:26:03 -05003658static int buf_to_pages_noslab(const void *buf, size_t buflen,
3659 struct page **pages, unsigned int *pgbase)
3660{
3661 struct page *newpage, **spages;
3662 int rc = 0;
3663 size_t len;
3664 spages = pages;
3665
3666 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003667 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003668 newpage = alloc_page(GFP_KERNEL);
3669
3670 if (newpage == NULL)
3671 goto unwind;
3672 memcpy(page_address(newpage), buf, len);
3673 buf += len;
3674 buflen -= len;
3675 *pages++ = newpage;
3676 rc++;
3677 } while (buflen != 0);
3678
3679 return rc;
3680
3681unwind:
3682 for(; rc > 0; rc--)
3683 __free_page(spages[rc-1]);
3684 return -ENOMEM;
3685}
3686
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003687struct nfs4_cached_acl {
3688 int cached;
3689 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003690 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003691};
3692
3693static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003694{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003695 struct nfs_inode *nfsi = NFS_I(inode);
3696
3697 spin_lock(&inode->i_lock);
3698 kfree(nfsi->nfs4_acl);
3699 nfsi->nfs4_acl = acl;
3700 spin_unlock(&inode->i_lock);
3701}
3702
3703static void nfs4_zap_acl_attr(struct inode *inode)
3704{
3705 nfs4_set_cached_acl(inode, NULL);
3706}
3707
3708static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3709{
3710 struct nfs_inode *nfsi = NFS_I(inode);
3711 struct nfs4_cached_acl *acl;
3712 int ret = -ENOENT;
3713
3714 spin_lock(&inode->i_lock);
3715 acl = nfsi->nfs4_acl;
3716 if (acl == NULL)
3717 goto out;
3718 if (buf == NULL) /* user is just asking for length */
3719 goto out_len;
3720 if (acl->cached == 0)
3721 goto out;
3722 ret = -ERANGE; /* see getxattr(2) man page */
3723 if (acl->len > buflen)
3724 goto out;
3725 memcpy(buf, acl->data, acl->len);
3726out_len:
3727 ret = acl->len;
3728out:
3729 spin_unlock(&inode->i_lock);
3730 return ret;
3731}
3732
Sachin Prabhu5794d212012-04-17 14:36:40 +01003733static 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 +00003734{
3735 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003736 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003737
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003738 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003739 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740 if (acl == NULL)
3741 goto out;
3742 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003743 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744 } else {
3745 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3746 if (acl == NULL)
3747 goto out;
3748 acl->cached = 0;
3749 }
3750 acl->len = acl_len;
3751out:
3752 nfs4_set_cached_acl(inode, acl);
3753}
3754
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755/*
3756 * The getxattr API returns the required buffer length when called with a
3757 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3758 * the required buf. On a NULL buf, we send a page of data to the server
3759 * guessing that the ACL request can be serviced by a page. If so, we cache
3760 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3761 * the cache. If not so, we throw away the page, and cache the required
3762 * length. The next getxattr call will then produce another round trip to
3763 * the server, this time with the input buf of the required size.
3764 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003765static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003766{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003767 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003768 struct nfs_getaclargs args = {
3769 .fh = NFS_FH(inode),
3770 .acl_pages = pages,
3771 .acl_len = buflen,
3772 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003773 struct nfs_getaclres res = {
3774 .acl_len = buflen,
3775 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003776 struct rpc_message msg = {
3777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3778 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003779 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003780 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003781 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3782 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003783
Andy Adamsonbf118a32011-12-07 11:55:27 -05003784 /* As long as we're doing a round trip to the server anyway,
3785 * let's be prepared for a page of acl data. */
3786 if (npages == 0)
3787 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003788 if (npages > ARRAY_SIZE(pages))
3789 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003790
Andy Adamsonbf118a32011-12-07 11:55:27 -05003791 for (i = 0; i < npages; i++) {
3792 pages[i] = alloc_page(GFP_KERNEL);
3793 if (!pages[i])
3794 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003795 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003796
3797 /* for decoding across pages */
3798 res.acl_scratch = alloc_page(GFP_KERNEL);
3799 if (!res.acl_scratch)
3800 goto out_free;
3801
Andy Adamsonbf118a32011-12-07 11:55:27 -05003802 args.acl_len = npages * PAGE_SIZE;
3803 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003804
Peng Taode040be2012-01-10 22:42:47 +08003805 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003806 __func__, buf, buflen, npages, args.acl_len);
3807 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3808 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003809 if (ret)
3810 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003811
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003812 /* Handle the case where the passed-in buffer is too short */
3813 if (res.acl_flags & NFS4_ACL_TRUNC) {
3814 /* Did the user only issue a request for the acl length? */
3815 if (buf == NULL)
3816 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003817 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003818 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003819 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003820 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003821 if (buf) {
3822 if (res.acl_len > buflen) {
3823 ret = -ERANGE;
3824 goto out_free;
3825 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003826 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003827 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003828out_ok:
3829 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003830out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003831 for (i = 0; i < npages; i++)
3832 if (pages[i])
3833 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003834 if (res.acl_scratch)
3835 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003836 return ret;
3837}
3838
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003839static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3840{
3841 struct nfs4_exception exception = { };
3842 ssize_t ret;
3843 do {
3844 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3845 if (ret >= 0)
3846 break;
3847 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3848 } while (exception.retry);
3849 return ret;
3850}
3851
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003852static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3853{
3854 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003855 int ret;
3856
3857 if (!nfs4_server_supports_acls(server))
3858 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003859 ret = nfs_revalidate_inode(server, inode);
3860 if (ret < 0)
3861 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003862 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3863 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003864 ret = nfs4_read_cached_acl(inode, buf, buflen);
3865 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003866 /* -ENOENT is returned if there is no ACL or if there is an ACL
3867 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003868 return ret;
3869 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003870}
3871
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003872static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003873{
3874 struct nfs_server *server = NFS_SERVER(inode);
3875 struct page *pages[NFS4ACL_MAXPAGES];
3876 struct nfs_setaclargs arg = {
3877 .fh = NFS_FH(inode),
3878 .acl_pages = pages,
3879 .acl_len = buflen,
3880 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003881 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003882 struct rpc_message msg = {
3883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3884 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003885 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003886 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003887 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003888 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003889
3890 if (!nfs4_server_supports_acls(server))
3891 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003892 if (npages > ARRAY_SIZE(pages))
3893 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003894 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3895 if (i < 0)
3896 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003897 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003898 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003899
3900 /*
3901 * Free each page after tx, so the only ref left is
3902 * held by the network stack
3903 */
3904 for (; i > 0; i--)
3905 put_page(pages[i-1]);
3906
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003907 /*
3908 * Acl update can result in inode attribute update.
3909 * so mark the attribute cache invalid.
3910 */
3911 spin_lock(&inode->i_lock);
3912 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3913 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003914 nfs_access_zap_cache(inode);
3915 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003916 return ret;
3917}
3918
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003919static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3920{
3921 struct nfs4_exception exception = { };
3922 int err;
3923 do {
3924 err = nfs4_handle_exception(NFS_SERVER(inode),
3925 __nfs4_proc_set_acl(inode, buf, buflen),
3926 &exception);
3927 } while (exception.retry);
3928 return err;
3929}
3930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003932nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003934 struct nfs_client *clp = server->nfs_client;
3935
3936 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 return 0;
3938 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003939 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003940 case -NFS4ERR_ADMIN_REVOKED:
3941 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003942 if (state == NULL)
3943 break;
3944 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003945 case -NFS4ERR_OPENMODE:
3946 if (state == NULL)
3947 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003948 nfs4_schedule_stateid_recovery(server, state);
3949 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003950 case -NFS4ERR_EXPIRED:
3951 if (state != NULL)
3952 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003954 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003955 nfs4_schedule_lease_recovery(clp);
3956 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003957#if defined(CONFIG_NFS_V4_1)
3958 case -NFS4ERR_BADSESSION:
3959 case -NFS4ERR_BADSLOT:
3960 case -NFS4ERR_BAD_HIGH_SLOT:
3961 case -NFS4ERR_DEADSESSION:
3962 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3963 case -NFS4ERR_SEQ_FALSE_RETRY:
3964 case -NFS4ERR_SEQ_MISORDERED:
3965 dprintk("%s ERROR %d, Reset session\n", __func__,
3966 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003967 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003968 task->tk_status = 0;
3969 return -EAGAIN;
3970#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003972 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003973 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3975 task->tk_status = 0;
3976 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003977 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 case -NFS4ERR_OLD_STATEID:
3979 task->tk_status = 0;
3980 return -EAGAIN;
3981 }
3982 task->tk_status = nfs4_map_errors(task->tk_status);
3983 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003984wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003985 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003986 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3987 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3988 task->tk_status = 0;
3989 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990}
3991
Chuck Leverf0920752012-05-21 22:45:41 -04003992static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3993 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003994{
3995 __be32 verf[2];
3996
Chuck Lever2c820d92012-05-21 22:45:33 -04003997 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3998 /* An impossible timestamp guarantees this value
3999 * will never match a generated boot time. */
4000 verf[0] = 0;
4001 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4002 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004003 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4004 verf[0] = (__be32)nn->boot_time.tv_sec;
4005 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004006 }
Chuck Levercd937102012-03-02 17:14:31 -05004007 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4008}
4009
Chuck Levere984a552012-09-14 17:24:21 -04004010static unsigned int
4011nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4012 char *buf, size_t len)
4013{
4014 unsigned int result;
4015
4016 rcu_read_lock();
4017 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4018 clp->cl_ipaddr,
4019 rpc_peeraddr2str(clp->cl_rpcclient,
4020 RPC_DISPLAY_ADDR),
4021 rpc_peeraddr2str(clp->cl_rpcclient,
4022 RPC_DISPLAY_PROTO));
4023 rcu_read_unlock();
4024 return result;
4025}
4026
4027static unsigned int
4028nfs4_init_uniform_client_string(const struct nfs_client *clp,
4029 char *buf, size_t len)
4030{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004031 char *nodename = clp->cl_rpcclient->cl_nodename;
4032
4033 if (nfs4_client_id_uniquifier[0] != '\0')
4034 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004035 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4036 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004037 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004038}
4039
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004040/**
4041 * nfs4_proc_setclientid - Negotiate client ID
4042 * @clp: state data structure
4043 * @program: RPC program for NFSv4 callback service
4044 * @port: IP port number for NFS4 callback service
4045 * @cred: RPC credential to use for this call
4046 * @res: where to place the result
4047 *
4048 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4049 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004050int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4051 unsigned short port, struct rpc_cred *cred,
4052 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
4054 nfs4_verifier sc_verifier;
4055 struct nfs4_setclientid setclientid = {
4056 .sc_verifier = &sc_verifier,
4057 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004058 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 };
4060 struct rpc_message msg = {
4061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4062 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004063 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004064 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004066 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Chuck Leverde734832012-07-11 16:30:50 -04004068 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004069 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004070 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4071 setclientid.sc_name_len =
4072 nfs4_init_uniform_client_string(clp,
4073 setclientid.sc_name,
4074 sizeof(setclientid.sc_name));
4075 else
4076 setclientid.sc_name_len =
4077 nfs4_init_nonuniform_client_string(clp,
4078 setclientid.sc_name,
4079 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004080 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004081 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004082 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004083 sizeof(setclientid.sc_netid),
4084 rpc_peeraddr2str(clp->cl_rpcclient,
4085 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004086 rcu_read_unlock();
4087 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004088 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 clp->cl_ipaddr, port >> 8, port & 255);
4090
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004091 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4092 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4093 setclientid.sc_name_len, setclientid.sc_name);
4094 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4095 dprintk("NFS reply setclientid: %d\n", status);
4096 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097}
4098
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004099/**
4100 * nfs4_proc_setclientid_confirm - Confirm client ID
4101 * @clp: state data structure
4102 * @res: result of a previous SETCLIENTID
4103 * @cred: RPC credential to use for this call
4104 *
4105 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4106 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004107int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004108 struct nfs4_setclientid_res *arg,
4109 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 struct nfs_fsinfo fsinfo;
4112 struct rpc_message msg = {
4113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004114 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004116 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 };
4118 unsigned long now;
4119 int status;
4120
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004121 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4122 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4123 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004125 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 if (status == 0) {
4127 spin_lock(&clp->cl_lock);
4128 clp->cl_lease_time = fsinfo.lease_time * HZ;
4129 clp->cl_last_renewal = now;
4130 spin_unlock(&clp->cl_lock);
4131 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004132 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 return status;
4134}
4135
Trond Myklebustfe650402006-01-03 09:55:18 +01004136struct nfs4_delegreturndata {
4137 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004138 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004139 struct nfs_fh fh;
4140 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004141 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004142 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004143 int rpc_status;
4144};
4145
Trond Myklebustfe650402006-01-03 09:55:18 +01004146static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4147{
4148 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004149
Trond Myklebust14516c32010-07-31 14:29:06 -04004150 if (!nfs4_sequence_done(task, &data->res.seq_res))
4151 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004152
Ricardo Labiaga79708862009-12-07 09:23:21 -05004153 switch (task->tk_status) {
4154 case -NFS4ERR_STALE_STATEID:
4155 case -NFS4ERR_EXPIRED:
4156 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004157 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004158 break;
4159 default:
4160 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4161 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004162 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004163 return;
4164 }
4165 }
4166 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004167}
4168
4169static void nfs4_delegreturn_release(void *calldata)
4170{
Trond Myklebustfe650402006-01-03 09:55:18 +01004171 kfree(calldata);
4172}
4173
Andy Adamson938e1012009-04-01 09:22:28 -04004174#if defined(CONFIG_NFS_V4_1)
4175static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4176{
4177 struct nfs4_delegreturndata *d_data;
4178
4179 d_data = (struct nfs4_delegreturndata *)data;
4180
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004181 nfs4_setup_sequence(d_data->res.server,
4182 &d_data->args.seq_args,
4183 &d_data->res.seq_res,
4184 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004185}
4186#endif /* CONFIG_NFS_V4_1 */
4187
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004188static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004189#if defined(CONFIG_NFS_V4_1)
4190 .rpc_call_prepare = nfs4_delegreturn_prepare,
4191#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004192 .rpc_call_done = nfs4_delegreturn_done,
4193 .rpc_release = nfs4_delegreturn_release,
4194};
4195
Trond Myklebuste6f81072008-01-24 18:14:34 -05004196static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004197{
4198 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004199 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004200 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004201 struct rpc_message msg = {
4202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4203 .rpc_cred = cred,
4204 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004205 struct rpc_task_setup task_setup_data = {
4206 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004207 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004208 .callback_ops = &nfs4_delegreturn_ops,
4209 .flags = RPC_TASK_ASYNC,
4210 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004211 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004212
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004213 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004214 if (data == NULL)
4215 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004216 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004217 data->args.fhandle = &data->fh;
4218 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004219 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004220 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004221 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004222 data->res.fattr = &data->fattr;
4223 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004224 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004225 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004226 data->rpc_status = 0;
4227
Trond Myklebustc970aa82007-07-14 15:39:59 -04004228 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004229 msg.rpc_argp = &data->args;
4230 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004231 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004232 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004233 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004234 if (!issync)
4235 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004236 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004237 if (status != 0)
4238 goto out;
4239 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004240 if (status == 0)
4241 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4242 else
4243 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004244out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004245 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004246 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
Trond Myklebuste6f81072008-01-24 18:14:34 -05004249int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
4251 struct nfs_server *server = NFS_SERVER(inode);
4252 struct nfs4_exception exception = { };
4253 int err;
4254 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004255 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 switch (err) {
4257 case -NFS4ERR_STALE_STATEID:
4258 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 case 0:
4260 return 0;
4261 }
4262 err = nfs4_handle_exception(server, err, &exception);
4263 } while (exception.retry);
4264 return err;
4265}
4266
4267#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4268#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4269
4270/*
4271 * sleep, with exponential backoff, and retry the LOCK operation.
4272 */
4273static unsigned long
4274nfs4_set_lock_task_retry(unsigned long timeout)
4275{
Jeff Laytond3103102011-12-01 22:44:39 +01004276 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 timeout <<= 1;
4278 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4279 return NFS4_LOCK_MAXTIMEOUT;
4280 return timeout;
4281}
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4284{
4285 struct inode *inode = state->inode;
4286 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004287 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004288 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004290 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004292 struct nfs_lockt_res res = {
4293 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 };
4295 struct rpc_message msg = {
4296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4297 .rpc_argp = &arg,
4298 .rpc_resp = &res,
4299 .rpc_cred = state->owner->so_cred,
4300 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 struct nfs4_lock_state *lsp;
4302 int status;
4303
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004304 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004305 status = nfs4_set_lock_state(state, request);
4306 if (status != 0)
4307 goto out;
4308 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004309 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004310 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004311 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004312 switch (status) {
4313 case 0:
4314 request->fl_type = F_UNLCK;
4315 break;
4316 case -NFS4ERR_DENIED:
4317 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004319 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004320out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return status;
4322}
4323
4324static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4325{
4326 struct nfs4_exception exception = { };
4327 int err;
4328
4329 do {
4330 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4331 _nfs4_proc_getlk(state, cmd, request),
4332 &exception);
4333 } while (exception.retry);
4334 return err;
4335}
4336
4337static int do_vfs_lock(struct file *file, struct file_lock *fl)
4338{
4339 int res = 0;
4340 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4341 case FL_POSIX:
4342 res = posix_lock_file_wait(file, fl);
4343 break;
4344 case FL_FLOCK:
4345 res = flock_lock_file_wait(file, fl);
4346 break;
4347 default:
4348 BUG();
4349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return res;
4351}
4352
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004353struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004354 struct nfs_locku_args arg;
4355 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004356 struct nfs4_lock_state *lsp;
4357 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004358 struct file_lock fl;
4359 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004360 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004361};
4362
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004363static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4364 struct nfs_open_context *ctx,
4365 struct nfs4_lock_state *lsp,
4366 struct nfs_seqid *seqid)
4367{
4368 struct nfs4_unlockdata *p;
4369 struct inode *inode = lsp->ls_state->inode;
4370
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004371 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004372 if (p == NULL)
4373 return NULL;
4374 p->arg.fh = NFS_FH(inode);
4375 p->arg.fl = &p->fl;
4376 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004377 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004378 p->arg.stateid = &lsp->ls_stateid;
4379 p->lsp = lsp;
4380 atomic_inc(&lsp->ls_count);
4381 /* Ensure we don't close file until we're done freeing locks! */
4382 p->ctx = get_nfs_open_context(ctx);
4383 memcpy(&p->fl, fl, sizeof(p->fl));
4384 p->server = NFS_SERVER(inode);
4385 return p;
4386}
4387
Trond Myklebust06f814a2006-01-03 09:55:07 +01004388static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004389{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004390 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004391 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004392 nfs4_put_lock_state(calldata->lsp);
4393 put_nfs_open_context(calldata->ctx);
4394 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004395}
4396
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004397static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004399 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004400
Trond Myklebust14516c32010-07-31 14:29:06 -04004401 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4402 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004403 switch (task->tk_status) {
4404 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004405 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4406 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004407 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004408 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004409 case -NFS4ERR_BAD_STATEID:
4410 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004411 case -NFS4ERR_STALE_STATEID:
4412 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004413 break;
4414 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004415 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004416 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004417 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004418 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004419}
4420
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004421static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004422{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004423 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004425 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004426 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004427 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004428 /* Note: exit _without_ running nfs4_locku_done */
4429 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004430 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004431 return;
4432 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004433 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004434 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004435 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004436 &calldata->res.seq_res,
4437 task) != 0)
4438 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439}
4440
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004441static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004442 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004443 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004444 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004445};
4446
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4448 struct nfs_open_context *ctx,
4449 struct nfs4_lock_state *lsp,
4450 struct nfs_seqid *seqid)
4451{
4452 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004453 struct rpc_message msg = {
4454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4455 .rpc_cred = ctx->cred,
4456 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004457 struct rpc_task_setup task_setup_data = {
4458 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004459 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004460 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004461 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004462 .flags = RPC_TASK_ASYNC,
4463 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004464
Frank Filz137d6ac2007-07-09 15:32:29 -07004465 /* Ensure this is an unlock - when canceling a lock, the
4466 * canceled lock is passed in, and it won't be an unlock.
4467 */
4468 fl->fl_type = F_UNLCK;
4469
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004470 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4471 if (data == NULL) {
4472 nfs_free_seqid(seqid);
4473 return ERR_PTR(-ENOMEM);
4474 }
4475
Trond Myklebust9d12b212012-01-17 22:04:25 -05004476 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004477 msg.rpc_argp = &data->arg;
4478 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004479 task_setup_data.callback_data = data;
4480 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481}
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4484{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004485 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004486 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004487 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004488 struct rpc_task *task;
4489 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004490 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Trond Myklebust9b073572006-06-29 16:38:34 -04004492 status = nfs4_set_lock_state(state, request);
4493 /* Unlock _before_ we do the RPC call */
4494 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004495 down_read(&nfsi->rwsem);
4496 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4497 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004498 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004499 }
4500 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004501 if (status != 0)
4502 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004503 /* Is this a delegated lock? */
4504 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004505 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004506 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004507 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004508 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004509 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004510 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004511 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 status = PTR_ERR(task);
4513 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004514 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004516 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004517out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004518 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004519 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522struct nfs4_lockdata {
4523 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004524 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 struct nfs4_lock_state *lsp;
4526 struct nfs_open_context *ctx;
4527 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004528 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529 int rpc_status;
4530 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004531 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532};
4533
4534static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004535 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4536 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004537{
4538 struct nfs4_lockdata *p;
4539 struct inode *inode = lsp->ls_state->inode;
4540 struct nfs_server *server = NFS_SERVER(inode);
4541
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004542 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004543 if (p == NULL)
4544 return NULL;
4545
4546 p->arg.fh = NFS_FH(inode);
4547 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004548 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004549 if (p->arg.open_seqid == NULL)
4550 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004551 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004552 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004553 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004555 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004556 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004557 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004558 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004559 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004560 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561 atomic_inc(&lsp->ls_count);
4562 p->ctx = get_nfs_open_context(ctx);
4563 memcpy(&p->fl, fl, sizeof(p->fl));
4564 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004565out_free_seqid:
4566 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567out_free:
4568 kfree(p);
4569 return NULL;
4570}
4571
4572static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4573{
4574 struct nfs4_lockdata *data = calldata;
4575 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Harvey Harrison3110ff82008-05-02 13:42:44 -07004577 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004578 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4579 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580 /* Do we need to do an open_to_lock_owner? */
4581 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004582 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004583 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004584 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585 data->arg.open_stateid = &state->stateid;
4586 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004587 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004588 } else
4589 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004590 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004591 if (nfs4_setup_sequence(data->server,
4592 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004593 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004594 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004595 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004596 nfs_release_seqid(data->arg.open_seqid);
4597out_release_lock_seqid:
4598 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004599 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004600}
4601
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4603{
4604 struct nfs4_lockdata *data = calldata;
4605
Harvey Harrison3110ff82008-05-02 13:42:44 -07004606 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004607
Trond Myklebust14516c32010-07-31 14:29:06 -04004608 if (!nfs4_sequence_done(task, &data->res.seq_res))
4609 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004610
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004611 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004613 if (data->rpc_status == 0)
4614 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4615 else
4616 goto out;
4617 }
4618 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004619 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004620 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004621 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004624 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625}
4626
4627static void nfs4_lock_release(void *calldata)
4628{
4629 struct nfs4_lockdata *data = calldata;
4630
Harvey Harrison3110ff82008-05-02 13:42:44 -07004631 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004632 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004633 if (data->cancelled != 0) {
4634 struct rpc_task *task;
4635 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4636 data->arg.lock_seqid);
4637 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004638 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004639 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640 } else
4641 nfs_free_seqid(data->arg.lock_seqid);
4642 nfs4_put_lock_state(data->lsp);
4643 put_nfs_open_context(data->ctx);
4644 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004645 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646}
4647
4648static const struct rpc_call_ops nfs4_lock_ops = {
4649 .rpc_call_prepare = nfs4_lock_prepare,
4650 .rpc_call_done = nfs4_lock_done,
4651 .rpc_release = nfs4_lock_release,
4652};
4653
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004654static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4655{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004656 switch (error) {
4657 case -NFS4ERR_ADMIN_REVOKED:
4658 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004659 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004660 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004661 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004662 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004663 break;
4664 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004665 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004666 case -NFS4ERR_EXPIRED:
4667 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004668 };
4669}
4670
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004671static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672{
4673 struct nfs4_lockdata *data;
4674 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004675 struct rpc_message msg = {
4676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4677 .rpc_cred = state->owner->so_cred,
4678 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004679 struct rpc_task_setup task_setup_data = {
4680 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004681 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004682 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004683 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004684 .flags = RPC_TASK_ASYNC,
4685 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004686 int ret;
4687
Harvey Harrison3110ff82008-05-02 13:42:44 -07004688 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004689 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004690 fl->fl_u.nfs4_fl.owner,
4691 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004692 if (data == NULL)
4693 return -ENOMEM;
4694 if (IS_SETLKW(cmd))
4695 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004696 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004697 msg.rpc_argp = &data->arg;
4698 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004699 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004700 if (recovery_type > NFS_LOCK_NEW) {
4701 if (recovery_type == NFS_LOCK_RECLAIM)
4702 data->arg.reclaim = NFS_LOCK_RECLAIM;
4703 nfs4_set_sequence_privileged(&data->arg.seq_args);
4704 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004705 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004706 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004707 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004708 ret = nfs4_wait_for_completion_rpc_task(task);
4709 if (ret == 0) {
4710 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004711 if (ret)
4712 nfs4_handle_setlk_error(data->server, data->lsp,
4713 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004714 } else
4715 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004716 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004717 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004718 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
4721static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4722{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004723 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004724 struct nfs4_exception exception = {
4725 .inode = state->inode,
4726 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004727 int err;
4728
4729 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004730 /* Cache the lock if possible... */
4731 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4732 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004733 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004734 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004735 break;
4736 nfs4_handle_exception(server, err, &exception);
4737 } while (exception.retry);
4738 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
4741static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4742{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004743 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004744 struct nfs4_exception exception = {
4745 .inode = state->inode,
4746 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004747 int err;
4748
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004749 err = nfs4_set_lock_state(state, request);
4750 if (err != 0)
4751 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004752 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004753 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4754 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004755 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004756 switch (err) {
4757 default:
4758 goto out;
4759 case -NFS4ERR_GRACE:
4760 case -NFS4ERR_DELAY:
4761 nfs4_handle_exception(server, err, &exception);
4762 err = 0;
4763 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004764 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004765out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004766 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004769#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004770/**
4771 * nfs41_check_expired_locks - possibly free a lock stateid
4772 *
4773 * @state: NFSv4 state for an inode
4774 *
4775 * Returns NFS_OK if recovery for this stateid is now finished.
4776 * Otherwise a negative NFS4ERR value is returned.
4777 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004778static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004779{
Chuck Levereb64cf92012-07-11 16:30:05 -04004780 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004781 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004782 struct nfs_server *server = NFS_SERVER(state->inode);
4783
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004784 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004785 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004786 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4787 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004788 /* Free the stateid unless the server
4789 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004790 if (status != -NFS4ERR_BAD_STATEID)
4791 nfs41_free_stateid(server,
4792 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004793 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004794 ret = status;
4795 }
4796 }
4797 };
4798
4799 return ret;
4800}
4801
4802static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4803{
4804 int status = NFS_OK;
4805
4806 if (test_bit(LK_STATE_IN_USE, &state->flags))
4807 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004808 if (status != NFS_OK)
4809 status = nfs4_lock_expired(state, request);
4810 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004811}
4812#endif
4813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4815{
Trond Myklebust9a99af492013-02-04 20:17:49 -05004816 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004817 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004818 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004819 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004820 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004822 if ((fl_flags & FL_POSIX) &&
4823 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4824 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004825 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004826 status = nfs4_set_lock_state(state, request);
4827 if (status != 0)
4828 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004829 request->fl_flags |= FL_ACCESS;
4830 status = do_vfs_lock(request->fl_file, request);
4831 if (status < 0)
4832 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004833 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004834 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004835 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004836 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004837 request->fl_flags = fl_flags & ~FL_SLEEP;
4838 status = do_vfs_lock(request->fl_file, request);
4839 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004840 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05004841 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4842 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004843 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004844 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05004845 goto out;
4846 down_read(&nfsi->rwsem);
4847 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4848 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004849 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004850 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004851 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004852 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004853 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004854 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4855 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004856out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004857 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004858out:
4859 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 return status;
4861}
4862
4863static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4864{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004865 struct nfs4_exception exception = {
4866 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004867 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004868 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 int err;
4870
4871 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004872 err = _nfs4_proc_setlk(state, cmd, request);
4873 if (err == -NFS4ERR_DENIED)
4874 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004876 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 } while (exception.retry);
4878 return err;
4879}
4880
4881static int
4882nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4883{
4884 struct nfs_open_context *ctx;
4885 struct nfs4_state *state;
4886 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4887 int status;
4888
4889 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004890 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 state = ctx->state;
4892
4893 if (request->fl_start < 0 || request->fl_end < 0)
4894 return -EINVAL;
4895
Trond Myklebustd9531262009-07-21 19:22:38 -04004896 if (IS_GETLK(cmd)) {
4897 if (state != NULL)
4898 return nfs4_proc_getlk(state, F_GETLK, request);
4899 return 0;
4900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4903 return -EINVAL;
4904
Trond Myklebustd9531262009-07-21 19:22:38 -04004905 if (request->fl_type == F_UNLCK) {
4906 if (state != NULL)
4907 return nfs4_proc_unlck(state, cmd, request);
4908 return 0;
4909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Trond Myklebustd9531262009-07-21 19:22:38 -04004911 if (state == NULL)
4912 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004913 /*
4914 * Don't rely on the VFS having checked the file open mode,
4915 * since it won't do this for flock() locks.
4916 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004917 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004918 case F_RDLCK:
4919 if (!(filp->f_mode & FMODE_READ))
4920 return -EBADF;
4921 break;
4922 case F_WRLCK:
4923 if (!(filp->f_mode & FMODE_WRITE))
4924 return -EBADF;
4925 }
4926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 do {
4928 status = nfs4_proc_setlk(state, cmd, request);
4929 if ((status != -EAGAIN) || IS_SETLK(cmd))
4930 break;
4931 timeout = nfs4_set_lock_task_retry(timeout);
4932 status = -ERESTARTSYS;
4933 if (signalled())
4934 break;
4935 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 return status;
4937}
4938
Trond Myklebust888e6942005-11-04 15:38:11 -05004939int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4940{
4941 struct nfs_server *server = NFS_SERVER(state->inode);
4942 struct nfs4_exception exception = { };
4943 int err;
4944
4945 err = nfs4_set_lock_state(state, fl);
4946 if (err != 0)
4947 goto out;
4948 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004949 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004950 switch (err) {
4951 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004952 printk(KERN_ERR "NFS: %s: unhandled error "
4953 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004954 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004955 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004956 goto out;
4957 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004958 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004959 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004960 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004961 nfs4_schedule_lease_recovery(server->nfs_client);
4962 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004963 case -NFS4ERR_BADSESSION:
4964 case -NFS4ERR_BADSLOT:
4965 case -NFS4ERR_BAD_HIGH_SLOT:
4966 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4967 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004968 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004969 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004970 case -ERESTARTSYS:
4971 /*
4972 * The show must go on: exit, but mark the
4973 * stateid as needing recovery.
4974 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004975 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004976 case -NFS4ERR_ADMIN_REVOKED:
4977 case -NFS4ERR_BAD_STATEID:
4978 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004979 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004980 err = 0;
4981 goto out;
4982 case -ENOMEM:
4983 case -NFS4ERR_DENIED:
4984 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4985 err = 0;
4986 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004987 case -NFS4ERR_DELAY:
4988 break;
4989 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004990 err = nfs4_handle_exception(server, err, &exception);
4991 } while (exception.retry);
4992out:
4993 return err;
4994}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004995
Trond Myklebustcf470c32012-03-07 13:49:12 -05004996struct nfs_release_lockowner_data {
4997 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004998 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004999 struct nfs_release_lockowner_args args;
5000};
5001
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005002static void nfs4_release_lockowner_release(void *calldata)
5003{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005004 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005005 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005006 kfree(calldata);
5007}
5008
Trond Myklebust17280172012-03-11 13:11:00 -04005009static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005010 .rpc_release = nfs4_release_lockowner_release,
5011};
5012
Trond Myklebustcf470c32012-03-07 13:49:12 -05005013int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005014{
5015 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005016 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005017 struct rpc_message msg = {
5018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5019 };
5020
5021 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005022 return -EINVAL;
5023 data = kmalloc(sizeof(*data), GFP_NOFS);
5024 if (!data)
5025 return -ENOMEM;
5026 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005027 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005028 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5029 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5030 data->args.lock_owner.s_dev = server->s_dev;
5031 msg.rpc_argp = &data->args;
5032 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5033 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005034}
5035
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005036#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5037
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005038static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5039 const void *buf, size_t buflen,
5040 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005041{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005042 if (strcmp(key, "") != 0)
5043 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005044
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005045 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005046}
5047
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005048static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5049 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005050{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005051 if (strcmp(key, "") != 0)
5052 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005053
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005054 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005055}
5056
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005057static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5058 size_t list_len, const char *name,
5059 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005060{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005061 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005062
J. Bruce Fields096455a2006-03-20 23:23:42 -05005063 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5064 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005065
5066 if (list && len <= list_len)
5067 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005068 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005069}
5070
Andy Adamson533eb462011-06-13 18:25:56 -04005071/*
5072 * nfs_fhget will use either the mounted_on_fileid or the fileid
5073 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005074static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5075{
Andy Adamson533eb462011-06-13 18:25:56 -04005076 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5077 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5078 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005079 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005080 return;
5081
5082 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005083 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005084 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5085 fattr->nlink = 2;
5086}
5087
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005088static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5089 const struct qstr *name,
5090 struct nfs4_fs_locations *fs_locations,
5091 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005092{
5093 struct nfs_server *server = NFS_SERVER(dir);
5094 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005095 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005096 };
5097 struct nfs4_fs_locations_arg args = {
5098 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005099 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005100 .page = page,
5101 .bitmask = bitmask,
5102 };
Benny Halevy22958462009-04-01 09:22:02 -04005103 struct nfs4_fs_locations_res res = {
5104 .fs_locations = fs_locations,
5105 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005106 struct rpc_message msg = {
5107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5108 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005109 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005110 };
5111 int status;
5112
Harvey Harrison3110ff82008-05-02 13:42:44 -07005113 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005114
5115 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5116 * is not supported */
5117 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5118 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5119 else
5120 bitmask[0] |= FATTR4_WORD0_FILEID;
5121
Trond Myklebustc228fd32007-01-13 02:28:11 -05005122 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005123 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005124 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005125 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005126 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005127 return status;
5128}
5129
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005130int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5131 const struct qstr *name,
5132 struct nfs4_fs_locations *fs_locations,
5133 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005134{
5135 struct nfs4_exception exception = { };
5136 int err;
5137 do {
5138 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005139 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005140 &exception);
5141 } while (exception.retry);
5142 return err;
5143}
5144
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005145static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5146{
5147 int status;
5148 struct nfs4_secinfo_arg args = {
5149 .dir_fh = NFS_FH(dir),
5150 .name = name,
5151 };
5152 struct nfs4_secinfo_res res = {
5153 .flavors = flavors,
5154 };
5155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5157 .rpc_argp = &args,
5158 .rpc_resp = &res,
5159 };
5160
5161 dprintk("NFS call secinfo %s\n", name->name);
5162 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5163 dprintk("NFS reply secinfo: %d\n", status);
5164 return status;
5165}
5166
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005167int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5168 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005169{
5170 struct nfs4_exception exception = { };
5171 int err;
5172 do {
5173 err = nfs4_handle_exception(NFS_SERVER(dir),
5174 _nfs4_proc_secinfo(dir, name, flavors),
5175 &exception);
5176 } while (exception.retry);
5177 return err;
5178}
5179
Andy Adamson557134a2009-04-01 09:21:53 -04005180#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005181/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005182 * Check the exchange flags returned by the server for invalid flags, having
5183 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5184 * DS flags set.
5185 */
5186static int nfs4_check_cl_exchange_flags(u32 flags)
5187{
5188 if (flags & ~EXCHGID4_FLAG_MASK_R)
5189 goto out_inval;
5190 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5191 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5192 goto out_inval;
5193 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5194 goto out_inval;
5195 return NFS_OK;
5196out_inval:
5197 return -NFS4ERR_INVAL;
5198}
5199
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005200static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005201nfs41_same_server_scope(struct nfs41_server_scope *a,
5202 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005203{
5204 if (a->server_scope_sz == b->server_scope_sz &&
5205 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5206 return true;
5207
5208 return false;
5209}
5210
Andy Adamson357f54d2010-12-14 10:11:57 -05005211/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005212 * nfs4_proc_bind_conn_to_session()
5213 *
5214 * The 4.1 client currently uses the same TCP connection for the
5215 * fore and backchannel.
5216 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005217int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005218{
5219 int status;
5220 struct nfs41_bind_conn_to_session_res res;
5221 struct rpc_message msg = {
5222 .rpc_proc =
5223 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5224 .rpc_argp = clp,
5225 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005226 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005227 };
5228
5229 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005230
5231 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5232 if (unlikely(res.session == NULL)) {
5233 status = -ENOMEM;
5234 goto out;
5235 }
5236
5237 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5238 if (status == 0) {
5239 if (memcmp(res.session->sess_id.data,
5240 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5241 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5242 status = -EIO;
5243 goto out_session;
5244 }
5245 if (res.dir != NFS4_CDFS4_BOTH) {
5246 dprintk("NFS: %s: Unexpected direction from server\n",
5247 __func__);
5248 status = -EIO;
5249 goto out_session;
5250 }
5251 if (res.use_conn_in_rdma_mode) {
5252 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5253 __func__);
5254 status = -EIO;
5255 goto out_session;
5256 }
5257 }
5258out_session:
5259 kfree(res.session);
5260out:
5261 dprintk("<-- %s status= %d\n", __func__, status);
5262 return status;
5263}
5264
5265/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005266 * nfs4_proc_exchange_id()
5267 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005268 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5269 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005270 * Since the clientid has expired, all compounds using sessions
5271 * associated with the stale clientid will be returning
5272 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5273 * be in some phase of session reset.
5274 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005275int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005276{
5277 nfs4_verifier verifier;
5278 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005279 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005280 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005281 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005282 };
5283 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005284 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005285 };
5286 int status;
5287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5289 .rpc_argp = &args,
5290 .rpc_resp = &res,
5291 .rpc_cred = cred,
5292 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005293
Chuck Leverf0920752012-05-21 22:45:41 -04005294 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005295 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5296 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005297 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5298 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5299 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005300
Chuck Leveracdeb692012-05-21 22:46:16 -04005301 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005302 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005303 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005304 status = -ENOMEM;
5305 goto out;
5306 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005307
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005308 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005309 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005310 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005311 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005312 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005313 }
5314
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005315 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005316 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005317 status = -ENOMEM;
5318 goto out_server_scope;
5319 }
5320
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005321 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005322 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005323 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005324
Chuck Lever177313f2012-05-21 22:44:58 -04005325 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005326 clp->cl_clientid = res.clientid;
5327 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5328 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5329 clp->cl_seqid = res.seqid;
5330
Chuck Leveracdeb692012-05-21 22:46:16 -04005331 kfree(clp->cl_serverowner);
5332 clp->cl_serverowner = res.server_owner;
5333 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005334
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005335 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005336 kfree(clp->cl_implid);
5337 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005338
Chuck Lever177313f2012-05-21 22:44:58 -04005339 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005340 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005341 res.server_scope)) {
5342 dprintk("%s: server_scope mismatch detected\n",
5343 __func__);
5344 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005345 kfree(clp->cl_serverscope);
5346 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005347 }
5348
Chuck Lever177313f2012-05-21 22:44:58 -04005349 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005350 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005351 goto out;
5352 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005353 } else
5354 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005355
Chuck Leveracdeb692012-05-21 22:46:16 -04005356out_server_owner:
5357 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005358out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005359 kfree(res.server_scope);
5360out:
Chuck Lever177313f2012-05-21 22:44:58 -04005361 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005362 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005363 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005364 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005365 clp->cl_implid->date.seconds,
5366 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005367 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005368 return status;
5369}
5370
Trond Myklebust66245532012-05-25 17:18:09 -04005371static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5372 struct rpc_cred *cred)
5373{
5374 struct rpc_message msg = {
5375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5376 .rpc_argp = clp,
5377 .rpc_cred = cred,
5378 };
5379 int status;
5380
5381 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5382 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005383 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005384 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5385 return status;
5386}
5387
5388static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5389 struct rpc_cred *cred)
5390{
5391 unsigned int loop;
5392 int ret;
5393
5394 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5395 ret = _nfs4_proc_destroy_clientid(clp, cred);
5396 switch (ret) {
5397 case -NFS4ERR_DELAY:
5398 case -NFS4ERR_CLIENTID_BUSY:
5399 ssleep(1);
5400 break;
5401 default:
5402 return ret;
5403 }
5404 }
5405 return 0;
5406}
5407
5408int nfs4_destroy_clientid(struct nfs_client *clp)
5409{
5410 struct rpc_cred *cred;
5411 int ret = 0;
5412
5413 if (clp->cl_mvops->minor_version < 1)
5414 goto out;
5415 if (clp->cl_exchange_flags == 0)
5416 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005417 if (clp->cl_preserve_clid)
5418 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005419 cred = nfs4_get_exchange_id_cred(clp);
5420 ret = nfs4_proc_destroy_clientid(clp, cred);
5421 if (cred)
5422 put_rpccred(cred);
5423 switch (ret) {
5424 case 0:
5425 case -NFS4ERR_STALE_CLIENTID:
5426 clp->cl_exchange_flags = 0;
5427 }
5428out:
5429 return ret;
5430}
5431
Andy Adamson2050f0c2009-04-01 09:22:30 -04005432struct nfs4_get_lease_time_data {
5433 struct nfs4_get_lease_time_args *args;
5434 struct nfs4_get_lease_time_res *res;
5435 struct nfs_client *clp;
5436};
5437
5438static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5439 void *calldata)
5440{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005441 struct nfs4_get_lease_time_data *data =
5442 (struct nfs4_get_lease_time_data *)calldata;
5443
5444 dprintk("--> %s\n", __func__);
5445 /* just setup sequence, do not trigger session recovery
5446 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005447 nfs41_setup_sequence(data->clp->cl_session,
5448 &data->args->la_seq_args,
5449 &data->res->lr_seq_res,
5450 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005451 dprintk("<-- %s\n", __func__);
5452}
5453
5454/*
5455 * Called from nfs4_state_manager thread for session setup, so don't recover
5456 * from sequence operation or clientid errors.
5457 */
5458static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5459{
5460 struct nfs4_get_lease_time_data *data =
5461 (struct nfs4_get_lease_time_data *)calldata;
5462
5463 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005464 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5465 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005466 switch (task->tk_status) {
5467 case -NFS4ERR_DELAY:
5468 case -NFS4ERR_GRACE:
5469 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5470 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5471 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005472 /* fall through */
5473 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005474 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005475 return;
5476 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005477 dprintk("<-- %s\n", __func__);
5478}
5479
Trond Myklebust17280172012-03-11 13:11:00 -04005480static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005481 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5482 .rpc_call_done = nfs4_get_lease_time_done,
5483};
5484
5485int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5486{
5487 struct rpc_task *task;
5488 struct nfs4_get_lease_time_args args;
5489 struct nfs4_get_lease_time_res res = {
5490 .lr_fsinfo = fsinfo,
5491 };
5492 struct nfs4_get_lease_time_data data = {
5493 .args = &args,
5494 .res = &res,
5495 .clp = clp,
5496 };
5497 struct rpc_message msg = {
5498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5499 .rpc_argp = &args,
5500 .rpc_resp = &res,
5501 };
5502 struct rpc_task_setup task_setup = {
5503 .rpc_client = clp->cl_rpcclient,
5504 .rpc_message = &msg,
5505 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005506 .callback_data = &data,
5507 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005508 };
5509 int status;
5510
Trond Myklebust9d12b212012-01-17 22:04:25 -05005511 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005512 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005513 dprintk("--> %s\n", __func__);
5514 task = rpc_run_task(&task_setup);
5515
5516 if (IS_ERR(task))
5517 status = PTR_ERR(task);
5518 else {
5519 status = task->tk_status;
5520 rpc_put_task(task);
5521 }
5522 dprintk("<-- %s return %d\n", __func__, status);
5523
5524 return status;
5525}
5526
Andy Adamsonfc931582009-04-01 09:22:31 -04005527/*
5528 * Initialize the values to be used by the client in CREATE_SESSION
5529 * If nfs4_init_session set the fore channel request and response sizes,
5530 * use them.
5531 *
5532 * Set the back channel max_resp_sz_cached to zero to force the client to
5533 * always set csa_cachethis to FALSE because the current implementation
5534 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5535 */
5536static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5537{
5538 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005539 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5540 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005541
5542 if (mxrqst_sz == 0)
5543 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5544 if (mxresp_sz == 0)
5545 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5546 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005547 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5548 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005549 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005550 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005551
5552 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005553 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005554 __func__,
5555 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005556 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005557
5558 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005559 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5560 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5561 args->bc_attrs.max_resp_sz_cached = 0;
5562 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5563 args->bc_attrs.max_reqs = 1;
5564
5565 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5566 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5567 __func__,
5568 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5569 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5570 args->bc_attrs.max_reqs);
5571}
5572
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005573static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005574{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005575 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5576 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5577
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005578 if (rcvd->max_resp_sz > sent->max_resp_sz)
5579 return -EINVAL;
5580 /*
5581 * Our requested max_ops is the minimum we need; we're not
5582 * prepared to break up compounds into smaller pieces than that.
5583 * So, no point even trying to continue if the server won't
5584 * cooperate:
5585 */
5586 if (rcvd->max_ops < sent->max_ops)
5587 return -EINVAL;
5588 if (rcvd->max_reqs == 0)
5589 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005590 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5591 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005592 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005593}
5594
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005595static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5596{
5597 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5598 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005599
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005600 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5601 return -EINVAL;
5602 if (rcvd->max_resp_sz < sent->max_resp_sz)
5603 return -EINVAL;
5604 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5605 return -EINVAL;
5606 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005607 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005608 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005609 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005610 return -EINVAL;
5611 return 0;
5612}
Andy Adamson8d353012009-04-01 09:22:32 -04005613
Andy Adamson8d353012009-04-01 09:22:32 -04005614static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5615 struct nfs4_session *session)
5616{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005617 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005618
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005619 ret = nfs4_verify_fore_channel_attrs(args, session);
5620 if (ret)
5621 return ret;
5622 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005623}
5624
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005625static int _nfs4_proc_create_session(struct nfs_client *clp,
5626 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005627{
5628 struct nfs4_session *session = clp->cl_session;
5629 struct nfs41_create_session_args args = {
5630 .client = clp,
5631 .cb_program = NFS4_CALLBACK,
5632 };
5633 struct nfs41_create_session_res res = {
5634 .client = clp,
5635 };
5636 struct rpc_message msg = {
5637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5638 .rpc_argp = &args,
5639 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005640 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005641 };
5642 int status;
5643
5644 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005645 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005646
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005647 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005648
Trond Myklebust43095d32012-11-20 11:13:12 -05005649 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005650 /* Verify the session's negotiated channel_attrs values */
5651 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005652 /* Increment the clientid slot sequence id */
5653 clp->cl_seqid++;
5654 }
5655
5656 return status;
5657}
5658
5659/*
5660 * Issues a CREATE_SESSION operation to the server.
5661 * It is the responsibility of the caller to verify the session is
5662 * expired before calling this routine.
5663 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005664int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005665{
5666 int status;
5667 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005668 struct nfs4_session *session = clp->cl_session;
5669
5670 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5671
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005672 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005673 if (status)
5674 goto out;
5675
Andy Adamsonaacd5532011-11-09 13:58:21 -05005676 /* Init or reset the session slot tables */
5677 status = nfs4_setup_session_slot_tables(session);
5678 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005679 if (status)
5680 goto out;
5681
5682 ptr = (unsigned *)&session->sess_id.data[0];
5683 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5684 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005685out:
5686 dprintk("<-- %s\n", __func__);
5687 return status;
5688}
5689
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005690/*
5691 * Issue the over-the-wire RPC DESTROY_SESSION.
5692 * The caller must serialize access to this routine.
5693 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005694int nfs4_proc_destroy_session(struct nfs4_session *session,
5695 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005696{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005697 struct rpc_message msg = {
5698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5699 .rpc_argp = session,
5700 .rpc_cred = cred,
5701 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005702 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005703
5704 dprintk("--> nfs4_proc_destroy_session\n");
5705
5706 /* session is still being setup */
5707 if (session->clp->cl_cons_state != NFS_CS_READY)
5708 return status;
5709
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005710 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005711
5712 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005713 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005714 "Session has been destroyed regardless...\n", status);
5715
5716 dprintk("<-- nfs4_proc_destroy_session\n");
5717 return status;
5718}
5719
Trond Myklebust7b38c362012-05-23 13:23:31 -04005720/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005721 * Renew the cl_session lease.
5722 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005723struct nfs4_sequence_data {
5724 struct nfs_client *clp;
5725 struct nfs4_sequence_args args;
5726 struct nfs4_sequence_res res;
5727};
5728
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005729static void nfs41_sequence_release(void *data)
5730{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005731 struct nfs4_sequence_data *calldata = data;
5732 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005733
Alexandros Batsakis71358402010-02-05 03:45:05 -08005734 if (atomic_read(&clp->cl_count) > 1)
5735 nfs4_schedule_state_renewal(clp);
5736 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005737 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005738}
5739
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005740static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5741{
5742 switch(task->tk_status) {
5743 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005744 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5745 return -EAGAIN;
5746 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005747 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005748 }
5749 return 0;
5750}
5751
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005752static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005753{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005754 struct nfs4_sequence_data *calldata = data;
5755 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005756
Trond Myklebust14516c32010-07-31 14:29:06 -04005757 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5758 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005759
5760 if (task->tk_status < 0) {
5761 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005762 if (atomic_read(&clp->cl_count) == 1)
5763 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005764
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005765 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5766 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005767 return;
5768 }
5769 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005770 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005771out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005772 dprintk("<-- %s\n", __func__);
5773}
5774
5775static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5776{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005777 struct nfs4_sequence_data *calldata = data;
5778 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005779 struct nfs4_sequence_args *args;
5780 struct nfs4_sequence_res *res;
5781
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005782 args = task->tk_msg.rpc_argp;
5783 res = task->tk_msg.rpc_resp;
5784
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005785 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005786}
5787
5788static const struct rpc_call_ops nfs41_sequence_ops = {
5789 .rpc_call_done = nfs41_sequence_call_done,
5790 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005791 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005792};
5793
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005794static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5795 struct rpc_cred *cred,
5796 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005797{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005798 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005799 struct rpc_message msg = {
5800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5801 .rpc_cred = cred,
5802 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005803 struct rpc_task_setup task_setup_data = {
5804 .rpc_client = clp->cl_rpcclient,
5805 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005806 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005807 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5808 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005809
Alexandros Batsakis71358402010-02-05 03:45:05 -08005810 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005811 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005812 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005813 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005814 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005815 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005816 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005817 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005818 if (is_privileged)
5819 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005820 msg.rpc_argp = &calldata->args;
5821 msg.rpc_resp = &calldata->res;
5822 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005823 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005824
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005825 return rpc_run_task(&task_setup_data);
5826}
5827
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005828static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005829{
5830 struct rpc_task *task;
5831 int ret = 0;
5832
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005833 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5834 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005835 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005836 if (IS_ERR(task))
5837 ret = PTR_ERR(task);
5838 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005839 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005840 dprintk("<-- %s status=%d\n", __func__, ret);
5841 return ret;
5842}
5843
5844static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5845{
5846 struct rpc_task *task;
5847 int ret;
5848
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005849 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005850 if (IS_ERR(task)) {
5851 ret = PTR_ERR(task);
5852 goto out;
5853 }
5854 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005855 if (!ret) {
5856 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5857
5858 if (task->tk_status == 0)
5859 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005860 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005861 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005862 rpc_put_task(task);
5863out:
5864 dprintk("<-- %s status=%d\n", __func__, ret);
5865 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005866}
5867
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005868struct nfs4_reclaim_complete_data {
5869 struct nfs_client *clp;
5870 struct nfs41_reclaim_complete_args arg;
5871 struct nfs41_reclaim_complete_res res;
5872};
5873
5874static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5875{
5876 struct nfs4_reclaim_complete_data *calldata = data;
5877
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005878 nfs41_setup_sequence(calldata->clp->cl_session,
5879 &calldata->arg.seq_args,
5880 &calldata->res.seq_res,
5881 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005882}
5883
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005884static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5885{
5886 switch(task->tk_status) {
5887 case 0:
5888 case -NFS4ERR_COMPLETE_ALREADY:
5889 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5890 break;
5891 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005892 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005893 /* fall through */
5894 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005895 return -EAGAIN;
5896 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005897 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005898 }
5899 return 0;
5900}
5901
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005902static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5903{
5904 struct nfs4_reclaim_complete_data *calldata = data;
5905 struct nfs_client *clp = calldata->clp;
5906 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5907
5908 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005909 if (!nfs41_sequence_done(task, res))
5910 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005911
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005912 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5913 rpc_restart_call_prepare(task);
5914 return;
5915 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005916 dprintk("<-- %s\n", __func__);
5917}
5918
5919static void nfs4_free_reclaim_complete_data(void *data)
5920{
5921 struct nfs4_reclaim_complete_data *calldata = data;
5922
5923 kfree(calldata);
5924}
5925
5926static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5927 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5928 .rpc_call_done = nfs4_reclaim_complete_done,
5929 .rpc_release = nfs4_free_reclaim_complete_data,
5930};
5931
5932/*
5933 * Issue a global reclaim complete.
5934 */
5935static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5936{
5937 struct nfs4_reclaim_complete_data *calldata;
5938 struct rpc_task *task;
5939 struct rpc_message msg = {
5940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5941 };
5942 struct rpc_task_setup task_setup_data = {
5943 .rpc_client = clp->cl_rpcclient,
5944 .rpc_message = &msg,
5945 .callback_ops = &nfs4_reclaim_complete_call_ops,
5946 .flags = RPC_TASK_ASYNC,
5947 };
5948 int status = -ENOMEM;
5949
5950 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005951 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005952 if (calldata == NULL)
5953 goto out;
5954 calldata->clp = clp;
5955 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005956
Trond Myklebust9d12b212012-01-17 22:04:25 -05005957 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005958 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005959 msg.rpc_argp = &calldata->arg;
5960 msg.rpc_resp = &calldata->res;
5961 task_setup_data.callback_data = calldata;
5962 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005963 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005964 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005965 goto out;
5966 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005967 status = nfs4_wait_for_completion_rpc_task(task);
5968 if (status == 0)
5969 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005970 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005971 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005972out:
5973 dprintk("<-- %s status=%d\n", __func__, status);
5974 return status;
5975}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005976
5977static void
5978nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5979{
5980 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005981 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005982 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005983
5984 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005985 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5986 * right now covering the LAYOUTGET we are about to send.
5987 * However, that is not so catastrophic, and there seems
5988 * to be no way to prevent it completely.
5989 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005990 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005991 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005992 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005993 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5994 NFS_I(lgp->args.inode)->layout,
5995 lgp->args.ctx->state)) {
5996 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005997 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005998}
5999
6000static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6001{
6002 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006003 struct inode *inode = lgp->args.inode;
6004 struct nfs_server *server = NFS_SERVER(inode);
6005 struct pnfs_layout_hdr *lo;
6006 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006007
6008 dprintk("--> %s\n", __func__);
6009
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006010 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006011 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006012
6013 switch (task->tk_status) {
6014 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006015 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006016 case -NFS4ERR_LAYOUTTRYLATER:
6017 case -NFS4ERR_RECALLCONFLICT:
6018 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006019 break;
6020 case -NFS4ERR_EXPIRED:
6021 case -NFS4ERR_BAD_STATEID:
6022 spin_lock(&inode->i_lock);
6023 lo = NFS_I(inode)->layout;
6024 if (!lo || list_empty(&lo->plh_segs)) {
6025 spin_unlock(&inode->i_lock);
6026 /* If the open stateid was bad, then recover it. */
6027 state = lgp->args.ctx->state;
6028 } else {
6029 LIST_HEAD(head);
6030
6031 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6032 spin_unlock(&inode->i_lock);
6033 /* Mark the bad layout state as invalid, then
6034 * retry using the open stateid. */
6035 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006036 }
6037 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006038 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6039 rpc_restart_call_prepare(task);
6040out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006041 dprintk("<-- %s\n", __func__);
6042}
6043
Idan Kedar85541162012-08-02 11:47:10 +03006044static size_t max_response_pages(struct nfs_server *server)
6045{
6046 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6047 return nfs_page_array_len(0, max_resp_sz);
6048}
6049
6050static void nfs4_free_pages(struct page **pages, size_t size)
6051{
6052 int i;
6053
6054 if (!pages)
6055 return;
6056
6057 for (i = 0; i < size; i++) {
6058 if (!pages[i])
6059 break;
6060 __free_page(pages[i]);
6061 }
6062 kfree(pages);
6063}
6064
6065static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6066{
6067 struct page **pages;
6068 int i;
6069
6070 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6071 if (!pages) {
6072 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6073 return NULL;
6074 }
6075
6076 for (i = 0; i < size; i++) {
6077 pages[i] = alloc_page(gfp_flags);
6078 if (!pages[i]) {
6079 dprintk("%s: failed to allocate page\n", __func__);
6080 nfs4_free_pages(pages, size);
6081 return NULL;
6082 }
6083 }
6084
6085 return pages;
6086}
6087
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006088static void nfs4_layoutget_release(void *calldata)
6089{
6090 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006091 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6092 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006093
6094 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006095 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006096 put_nfs_open_context(lgp->args.ctx);
6097 kfree(calldata);
6098 dprintk("<-- %s\n", __func__);
6099}
6100
6101static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6102 .rpc_call_prepare = nfs4_layoutget_prepare,
6103 .rpc_call_done = nfs4_layoutget_done,
6104 .rpc_release = nfs4_layoutget_release,
6105};
6106
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006107struct pnfs_layout_segment *
6108nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006109{
6110 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006111 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006112 struct rpc_task *task;
6113 struct rpc_message msg = {
6114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6115 .rpc_argp = &lgp->args,
6116 .rpc_resp = &lgp->res,
6117 };
6118 struct rpc_task_setup task_setup_data = {
6119 .rpc_client = server->client,
6120 .rpc_message = &msg,
6121 .callback_ops = &nfs4_layoutget_call_ops,
6122 .callback_data = lgp,
6123 .flags = RPC_TASK_ASYNC,
6124 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006125 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006126 int status = 0;
6127
6128 dprintk("--> %s\n", __func__);
6129
Idan Kedar85541162012-08-02 11:47:10 +03006130 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6131 if (!lgp->args.layout.pages) {
6132 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006133 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006134 }
6135 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6136
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006137 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006138 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006139 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006140 task = rpc_run_task(&task_setup_data);
6141 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006142 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006143 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006144 if (status == 0)
6145 status = task->tk_status;
6146 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006147 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006148 rpc_put_task(task);
6149 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006150 if (status)
6151 return ERR_PTR(status);
6152 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006153}
6154
Benny Halevycbe82602011-05-22 19:52:37 +03006155static void
6156nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6157{
6158 struct nfs4_layoutreturn *lrp = calldata;
6159
6160 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006161 nfs41_setup_sequence(lrp->clp->cl_session,
6162 &lrp->args.seq_args,
6163 &lrp->res.seq_res,
6164 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006165}
6166
6167static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6168{
6169 struct nfs4_layoutreturn *lrp = calldata;
6170 struct nfs_server *server;
6171
6172 dprintk("--> %s\n", __func__);
6173
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006174 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006175 return;
6176
6177 server = NFS_SERVER(lrp->args.inode);
6178 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006179 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006180 return;
6181 }
Benny Halevycbe82602011-05-22 19:52:37 +03006182 dprintk("<-- %s\n", __func__);
6183}
6184
6185static void nfs4_layoutreturn_release(void *calldata)
6186{
6187 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006188 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006189
6190 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006191 spin_lock(&lo->plh_inode->i_lock);
6192 if (lrp->res.lrs_present)
6193 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6194 lo->plh_block_lgets--;
6195 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006196 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006197 kfree(calldata);
6198 dprintk("<-- %s\n", __func__);
6199}
6200
6201static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6202 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6203 .rpc_call_done = nfs4_layoutreturn_done,
6204 .rpc_release = nfs4_layoutreturn_release,
6205};
6206
6207int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6208{
6209 struct rpc_task *task;
6210 struct rpc_message msg = {
6211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6212 .rpc_argp = &lrp->args,
6213 .rpc_resp = &lrp->res,
6214 };
6215 struct rpc_task_setup task_setup_data = {
6216 .rpc_client = lrp->clp->cl_rpcclient,
6217 .rpc_message = &msg,
6218 .callback_ops = &nfs4_layoutreturn_call_ops,
6219 .callback_data = lrp,
6220 };
6221 int status;
6222
6223 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006224 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006225 task = rpc_run_task(&task_setup_data);
6226 if (IS_ERR(task))
6227 return PTR_ERR(task);
6228 status = task->tk_status;
6229 dprintk("<-- %s status=%d\n", __func__, status);
6230 rpc_put_task(task);
6231 return status;
6232}
6233
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006234/*
6235 * Retrieve the list of Data Server devices from the MDS.
6236 */
6237static int _nfs4_getdevicelist(struct nfs_server *server,
6238 const struct nfs_fh *fh,
6239 struct pnfs_devicelist *devlist)
6240{
6241 struct nfs4_getdevicelist_args args = {
6242 .fh = fh,
6243 .layoutclass = server->pnfs_curr_ld->id,
6244 };
6245 struct nfs4_getdevicelist_res res = {
6246 .devlist = devlist,
6247 };
6248 struct rpc_message msg = {
6249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6250 .rpc_argp = &args,
6251 .rpc_resp = &res,
6252 };
6253 int status;
6254
6255 dprintk("--> %s\n", __func__);
6256 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6257 &res.seq_res, 0);
6258 dprintk("<-- %s status=%d\n", __func__, status);
6259 return status;
6260}
6261
6262int nfs4_proc_getdevicelist(struct nfs_server *server,
6263 const struct nfs_fh *fh,
6264 struct pnfs_devicelist *devlist)
6265{
6266 struct nfs4_exception exception = { };
6267 int err;
6268
6269 do {
6270 err = nfs4_handle_exception(server,
6271 _nfs4_getdevicelist(server, fh, devlist),
6272 &exception);
6273 } while (exception.retry);
6274
6275 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6276 err, devlist->num_devs);
6277
6278 return err;
6279}
6280EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6281
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006282static int
6283_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6284{
6285 struct nfs4_getdeviceinfo_args args = {
6286 .pdev = pdev,
6287 };
6288 struct nfs4_getdeviceinfo_res res = {
6289 .pdev = pdev,
6290 };
6291 struct rpc_message msg = {
6292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6293 .rpc_argp = &args,
6294 .rpc_resp = &res,
6295 };
6296 int status;
6297
6298 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006299 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006300 dprintk("<-- %s status=%d\n", __func__, status);
6301
6302 return status;
6303}
6304
6305int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6306{
6307 struct nfs4_exception exception = { };
6308 int err;
6309
6310 do {
6311 err = nfs4_handle_exception(server,
6312 _nfs4_proc_getdeviceinfo(server, pdev),
6313 &exception);
6314 } while (exception.retry);
6315 return err;
6316}
6317EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6318
Andy Adamson863a3c62011-03-23 13:27:54 +00006319static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6320{
6321 struct nfs4_layoutcommit_data *data = calldata;
6322 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006323 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006324
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006325 nfs41_setup_sequence(session,
6326 &data->args.seq_args,
6327 &data->res.seq_res,
6328 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006329}
6330
6331static void
6332nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6333{
6334 struct nfs4_layoutcommit_data *data = calldata;
6335 struct nfs_server *server = NFS_SERVER(data->args.inode);
6336
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006337 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006338 return;
6339
6340 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006341 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6342 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6343 case -NFS4ERR_BADLAYOUT: /* no layout */
6344 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006345 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006346 break;
6347 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006348 nfs_post_op_update_inode_force_wcc(data->args.inode,
6349 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006350 break;
6351 default:
6352 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6353 rpc_restart_call_prepare(task);
6354 return;
6355 }
6356 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006357}
6358
6359static void nfs4_layoutcommit_release(void *calldata)
6360{
6361 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006362 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006363 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006364
Andy Adamsondb29c082011-07-30 20:52:38 -04006365 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006366 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006367 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6368 list_del_init(&lseg->pls_lc_list);
6369 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6370 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006371 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006372 }
Peng Tao92407e72011-10-23 20:21:17 -07006373
6374 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6375 smp_mb__after_clear_bit();
6376 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6377
Andy Adamson863a3c62011-03-23 13:27:54 +00006378 put_rpccred(data->cred);
6379 kfree(data);
6380}
6381
6382static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6383 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6384 .rpc_call_done = nfs4_layoutcommit_done,
6385 .rpc_release = nfs4_layoutcommit_release,
6386};
6387
6388int
Andy Adamsonef311532011-03-12 02:58:10 -05006389nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006390{
6391 struct rpc_message msg = {
6392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6393 .rpc_argp = &data->args,
6394 .rpc_resp = &data->res,
6395 .rpc_cred = data->cred,
6396 };
6397 struct rpc_task_setup task_setup_data = {
6398 .task = &data->task,
6399 .rpc_client = NFS_CLIENT(data->args.inode),
6400 .rpc_message = &msg,
6401 .callback_ops = &nfs4_layoutcommit_ops,
6402 .callback_data = data,
6403 .flags = RPC_TASK_ASYNC,
6404 };
6405 struct rpc_task *task;
6406 int status = 0;
6407
6408 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6409 "lbw: %llu inode %lu\n",
6410 data->task.tk_pid, sync,
6411 data->args.lastbytewritten,
6412 data->args.inode->i_ino);
6413
Trond Myklebust9d12b212012-01-17 22:04:25 -05006414 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006415 task = rpc_run_task(&task_setup_data);
6416 if (IS_ERR(task))
6417 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006418 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006419 goto out;
6420 status = nfs4_wait_for_completion_rpc_task(task);
6421 if (status != 0)
6422 goto out;
6423 status = task->tk_status;
6424out:
6425 dprintk("%s: status %d\n", __func__, status);
6426 rpc_put_task(task);
6427 return status;
6428}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006429
6430static int
6431_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6432 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6433{
6434 struct nfs41_secinfo_no_name_args args = {
6435 .style = SECINFO_STYLE_CURRENT_FH,
6436 };
6437 struct nfs4_secinfo_res res = {
6438 .flavors = flavors,
6439 };
6440 struct rpc_message msg = {
6441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6442 .rpc_argp = &args,
6443 .rpc_resp = &res,
6444 };
6445 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6446}
6447
6448static int
6449nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6450 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6451{
6452 struct nfs4_exception exception = { };
6453 int err;
6454 do {
6455 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6456 switch (err) {
6457 case 0:
6458 case -NFS4ERR_WRONGSEC:
6459 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006460 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006461 default:
6462 err = nfs4_handle_exception(server, err, &exception);
6463 }
6464 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006465out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006466 return err;
6467}
6468
6469static int
6470nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6471 struct nfs_fsinfo *info)
6472{
6473 int err;
6474 struct page *page;
6475 rpc_authflavor_t flavor;
6476 struct nfs4_secinfo_flavors *flavors;
6477
6478 page = alloc_page(GFP_KERNEL);
6479 if (!page) {
6480 err = -ENOMEM;
6481 goto out;
6482 }
6483
6484 flavors = page_address(page);
6485 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6486
6487 /*
6488 * Fall back on "guess and check" method if
6489 * the server doesn't support SECINFO_NO_NAME
6490 */
6491 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6492 err = nfs4_find_root_sec(server, fhandle, info);
6493 goto out_freepage;
6494 }
6495 if (err)
6496 goto out_freepage;
6497
6498 flavor = nfs_find_best_sec(flavors);
6499 if (err == 0)
6500 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6501
6502out_freepage:
6503 put_page(page);
6504 if (err == -EACCES)
6505 return -EPERM;
6506out:
6507 return err;
6508}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006509
6510static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006511{
6512 int status;
6513 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006514 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006515 };
6516 struct nfs41_test_stateid_res res;
6517 struct rpc_message msg = {
6518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6519 .rpc_argp = &args,
6520 .rpc_resp = &res,
6521 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006522
Chuck Lever38527b12012-07-11 16:30:23 -04006523 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006524 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006525 nfs4_set_sequence_privileged(&args.seq_args);
6526 status = nfs4_call_sync_sequence(server->client, server, &msg,
6527 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006528 if (status != NFS_OK) {
6529 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006530 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006531 }
6532 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006533 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006534}
6535
Chuck Lever38527b12012-07-11 16:30:23 -04006536/**
6537 * nfs41_test_stateid - perform a TEST_STATEID operation
6538 *
6539 * @server: server / transport on which to perform the operation
6540 * @stateid: state ID to test
6541 *
6542 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6543 * Otherwise a negative NFS4ERR value is returned if the operation
6544 * failed or the state ID is not currently valid.
6545 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006546static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006547{
6548 struct nfs4_exception exception = { };
6549 int err;
6550 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006551 err = _nfs41_test_stateid(server, stateid);
6552 if (err != -NFS4ERR_DELAY)
6553 break;
6554 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006555 } while (exception.retry);
6556 return err;
6557}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006558
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006559static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006560{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006561 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006562 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006563 };
6564 struct nfs41_free_stateid_res res;
6565 struct rpc_message msg = {
6566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6567 .rpc_argp = &args,
6568 .rpc_resp = &res,
6569 };
Chuck Lever38527b12012-07-11 16:30:23 -04006570 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006571
Chuck Lever38527b12012-07-11 16:30:23 -04006572 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006573 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006574 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006575 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006576 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006577 dprintk("NFS reply free_stateid: %d\n", status);
6578 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006579}
6580
Chuck Lever38527b12012-07-11 16:30:23 -04006581/**
6582 * nfs41_free_stateid - perform a FREE_STATEID operation
6583 *
6584 * @server: server / transport on which to perform the operation
6585 * @stateid: state ID to release
6586 *
6587 * Returns NFS_OK if the server freed "stateid". Otherwise a
6588 * negative NFS4ERR value is returned.
6589 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006590static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006591{
6592 struct nfs4_exception exception = { };
6593 int err;
6594 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006595 err = _nfs4_free_stateid(server, stateid);
6596 if (err != -NFS4ERR_DELAY)
6597 break;
6598 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006599 } while (exception.retry);
6600 return err;
6601}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006602
6603static bool nfs41_match_stateid(const nfs4_stateid *s1,
6604 const nfs4_stateid *s2)
6605{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006606 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006607 return false;
6608
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006609 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006610 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006611 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006612 return true;
6613
6614 return false;
6615}
6616
Andy Adamson557134a2009-04-01 09:21:53 -04006617#endif /* CONFIG_NFS_V4_1 */
6618
Trond Myklebust36281ca2012-03-04 18:13:56 -05006619static bool nfs4_match_stateid(const nfs4_stateid *s1,
6620 const nfs4_stateid *s2)
6621{
Trond Myklebustf597c532012-03-04 18:13:56 -05006622 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006623}
6624
6625
Trond Myklebust17280172012-03-11 13:11:00 -04006626static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006627 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006628 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 .recover_open = nfs4_open_reclaim,
6630 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006631 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006632 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006633 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634};
6635
Andy Adamson591d71c2009-04-01 09:22:47 -04006636#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006637static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006638 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6639 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6640 .recover_open = nfs4_open_reclaim,
6641 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006642 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006643 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006644 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006645 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006646};
6647#endif /* CONFIG_NFS_V4_1 */
6648
Trond Myklebust17280172012-03-11 13:11:00 -04006649static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006650 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006651 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 .recover_open = nfs4_open_expired,
6653 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006654 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006655 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656};
6657
Andy Adamson591d71c2009-04-01 09:22:47 -04006658#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006659static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006660 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6661 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006662 .recover_open = nfs41_open_expired,
6663 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006664 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006665 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006666};
6667#endif /* CONFIG_NFS_V4_1 */
6668
Trond Myklebust17280172012-03-11 13:11:00 -04006669static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006670 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006671 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006672 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006673};
6674
6675#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006676static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006677 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006678 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006679 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006680};
6681#endif
6682
Trond Myklebust97dc1352010-06-16 09:52:26 -04006683static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6684 .minor_version = 0,
6685 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006686 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006687 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006688 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6689 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6690 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006691};
6692
6693#if defined(CONFIG_NFS_V4_1)
6694static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6695 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006696 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006697 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006698 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006699 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6700 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6701 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006702};
6703#endif
6704
Trond Myklebust97dc1352010-06-16 09:52:26 -04006705const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6706 [0] = &nfs_v4_0_minor_ops,
6707#if defined(CONFIG_NFS_V4_1)
6708 [1] = &nfs_v4_1_minor_ops,
6709#endif
6710};
6711
Bryan Schumaker73a79702012-07-16 16:39:12 -04006712const struct inode_operations nfs4_dir_inode_operations = {
6713 .create = nfs_create,
6714 .lookup = nfs_lookup,
6715 .atomic_open = nfs_atomic_open,
6716 .link = nfs_link,
6717 .unlink = nfs_unlink,
6718 .symlink = nfs_symlink,
6719 .mkdir = nfs_mkdir,
6720 .rmdir = nfs_rmdir,
6721 .mknod = nfs_mknod,
6722 .rename = nfs_rename,
6723 .permission = nfs_permission,
6724 .getattr = nfs_getattr,
6725 .setattr = nfs_setattr,
6726 .getxattr = generic_getxattr,
6727 .setxattr = generic_setxattr,
6728 .listxattr = generic_listxattr,
6729 .removexattr = generic_removexattr,
6730};
6731
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006732static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006733 .permission = nfs_permission,
6734 .getattr = nfs_getattr,
6735 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006736 .getxattr = generic_getxattr,
6737 .setxattr = generic_setxattr,
6738 .listxattr = generic_listxattr,
6739 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006740};
6741
David Howells509de812006-08-22 20:06:11 -04006742const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 .version = 4, /* protocol version */
6744 .dentry_ops = &nfs4_dentry_operations,
6745 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006746 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006747 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006749 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006750 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 .getattr = nfs4_proc_getattr,
6752 .setattr = nfs4_proc_setattr,
6753 .lookup = nfs4_proc_lookup,
6754 .access = nfs4_proc_access,
6755 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 .create = nfs4_proc_create,
6757 .remove = nfs4_proc_remove,
6758 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006759 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 .unlink_done = nfs4_proc_unlink_done,
6761 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006762 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006763 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006764 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 .link = nfs4_proc_link,
6766 .symlink = nfs4_proc_symlink,
6767 .mkdir = nfs4_proc_mkdir,
6768 .rmdir = nfs4_proc_remove,
6769 .readdir = nfs4_proc_readdir,
6770 .mknod = nfs4_proc_mknod,
6771 .statfs = nfs4_proc_statfs,
6772 .fsinfo = nfs4_proc_fsinfo,
6773 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006774 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 .decode_dirent = nfs4_decode_dirent,
6776 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006777 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006778 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006779 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006781 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006782 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006783 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006785 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006786 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006788 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006789 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006790 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006791 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006792 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006793 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006794 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006795 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006796 .create_server = nfs4_create_server,
6797 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798};
6799
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006800static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6801 .prefix = XATTR_NAME_NFSV4_ACL,
6802 .list = nfs4_xattr_list_nfs4_acl,
6803 .get = nfs4_xattr_get_nfs4_acl,
6804 .set = nfs4_xattr_set_nfs4_acl,
6805};
6806
6807const struct xattr_handler *nfs4_xattr_handlers[] = {
6808 &nfs4_xattr_nfs4_acl_handler,
6809 NULL
6810};
6811
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812/*
6813 * Local variables:
6814 * c-basic-offset: 8
6815 * End:
6816 */