blob: 992233561dbd3cc6c6c1a47481998000bacf798c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond Myklebustd185a332010-06-16 09:52:25 -0400382static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400383{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500384 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400385 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500386 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400387
Benny Halevydfb4f3092010-09-24 09:17:01 -0400388 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400389 /* just wake up the next guy waiting since
390 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500391 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500392 return;
Andy Adamson13615872009-04-01 09:22:17 -0400393 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500394 tbl = res->sr_slot->table;
395 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500396
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500397 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500398 /* Be nice to the server: try to ensure that the last transmitted
399 * value for highest_user_slotid <= target_highest_slotid
400 */
401 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
402 send_new_highest_used_slotid = true;
403
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500404 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
405 send_new_highest_used_slotid = false;
406 goto out_unlock;
407 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500408 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500409
410 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
411 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500412out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500413 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400414 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500415 if (send_new_highest_used_slotid)
416 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400417}
418
Trond Myklebust14516c32010-07-31 14:29:06 -0400419static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400420{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500421 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500422 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400423 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400424 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400425
426 /*
427 * sr_status remains 1 if an RPC level error occurred. The server
428 * may or may not have processed the sequence operation..
429 * Proceed as if the server received and processed the sequence
430 * operation.
431 */
432 if (res->sr_status == 1)
433 res->sr_status = NFS_OK;
434
Bryan Schumaker468f8612011-04-18 15:57:32 -0400435 /* don't increment the sequence number if the task wasn't sent */
436 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400437 goto out;
438
Trond Myklebust933602e2012-11-16 12:12:38 -0500439 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500440 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500441
Andy Adamson691daf32009-12-04 15:55:39 -0500442 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 switch (res->sr_status) {
444 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400445 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500446 ++slot->seq_nr;
447 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500448 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400449 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500450 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500451 if (res->sr_status_flags != 0)
452 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500453 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400454 break;
455 case -NFS4ERR_DELAY:
456 /* The server detected a resend of the RPC call and
457 * returned NFS4ERR_DELAY as per Section 2.10.6.2
458 * of RFC5661.
459 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500460 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400461 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500462 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500463 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 goto out_retry;
465 default:
466 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500467 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400468 }
469out:
470 /* The session may be reset by one of the error handlers. */
471 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400472 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400473 return 1;
474out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400475 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 goto out;
477 rpc_delay(task, NFS4_POLL_RETRY_MAX);
478 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400479}
480
Trond Myklebust14516c32010-07-31 14:29:06 -0400481static int nfs4_sequence_done(struct rpc_task *task,
482 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400483{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500484 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 return 1;
486 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400487}
488
Trond Myklebust9d12b212012-01-17 22:04:25 -0500489static void nfs41_init_sequence(struct nfs4_sequence_args *args,
490 struct nfs4_sequence_res *res, int cache_reply)
491{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500492 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500493 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400494 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500495 if (cache_reply)
496 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500497 res->sr_slot = NULL;
498}
499
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400500static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
501{
502 args->sa_privileged = 1;
503}
504
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000505int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 struct rpc_task *task)
509{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 struct nfs4_slot *slot;
511 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512
513 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400514 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400515 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400516 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400517
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 tbl = &session->fc_slot_table;
519
Trond Myklebust69d206b2012-11-22 13:21:02 -0500520 task->tk_timeout = 0;
521
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400523 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400524 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500525 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500526 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400527 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400528 }
529
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500530 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500531 if (IS_ERR(slot)) {
532 /* If out of memory, try again in 1/4 second */
533 if (slot == ERR_PTR(-ENOMEM))
534 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400535 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400536 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400537 }
538 spin_unlock(&tbl->slot_tbl_lock);
539
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500540 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400541
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500542 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
543 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400544
Benny Halevydfb4f3092010-09-24 09:17:01 -0400545 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400546 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 /*
548 * sr_status is only set in decode_sequence, and so will remain
549 * set to 1 if an rpc level failure occurs.
550 */
551 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400552out_success:
553 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400555out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400556 /* Privileged tasks are queued with top priority */
557 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400558 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
559 NULL, RPC_PRIORITY_PRIVILEGED);
560 else
561 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400562 spin_unlock(&tbl->slot_tbl_lock);
563 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400564}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000565EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566
Trond Myklebust035168a2010-06-16 09:52:26 -0400567int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 struct nfs4_sequence_args *args,
569 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400570 struct rpc_task *task)
571{
Trond Myklebust035168a2010-06-16 09:52:26 -0400572 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 int ret = 0;
574
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400575 if (session == NULL) {
576 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400578 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400579
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500580 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400581 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500582 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400583
Trond Myklebust9d12b212012-01-17 22:04:25 -0500584 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400585out:
586 dprintk("<-- %s status=%d\n", __func__, ret);
587 return ret;
588}
589
590struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400591 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592 struct nfs4_sequence_args *seq_args;
593 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400594};
595
596static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
597{
598 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400599 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600
Trond Myklebust035168a2010-06-16 09:52:26 -0400601 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
602
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400603 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604}
605
Andy Adamson69ab40c2009-04-01 09:22:19 -0400606static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
607{
608 struct nfs41_call_sync_data *data = calldata;
609
Trond Myklebust14516c32010-07-31 14:29:06 -0400610 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400611}
612
Trond Myklebust17280172012-03-11 13:11:00 -0400613static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400615 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616};
617
Bryan Schumaker7c513052011-03-24 17:12:24 +0000618static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
619 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 struct rpc_message *msg,
621 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400622 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623{
624 int ret;
625 struct rpc_task *task;
626 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400627 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628 .seq_args = args,
629 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 };
631 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000632 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 .rpc_message = msg,
634 .callback_ops = &nfs41_call_sync_ops,
635 .callback_data = &data
636 };
637
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 task = rpc_run_task(&task_setup);
639 if (IS_ERR(task))
640 ret = PTR_ERR(task);
641 else {
642 ret = task->tk_status;
643 rpc_put_task(task);
644 }
645 return ret;
646}
647
Trond Myklebustdf896452010-06-16 09:52:26 -0400648#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400649static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500650void nfs41_init_sequence(struct nfs4_sequence_args *args,
651 struct nfs4_sequence_res *res, int cache_reply)
652{
653}
654
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400655static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
656{
657}
658
659
Trond Myklebust14516c32010-07-31 14:29:06 -0400660static int nfs4_sequence_done(struct rpc_task *task,
661 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400662{
Trond Myklebust14516c32010-07-31 14:29:06 -0400663 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400664}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400665#endif /* CONFIG_NFS_V4_1 */
666
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400667static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000668int _nfs4_call_sync(struct rpc_clnt *clnt,
669 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400670 struct rpc_message *msg,
671 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400672 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400673{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000674 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400675}
676
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400677static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000678int nfs4_call_sync(struct rpc_clnt *clnt,
679 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000680 struct rpc_message *msg,
681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
683 int cache_reply)
684{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400685 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000686 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400687 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000688}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689
Trond Myklebust38478b22006-05-25 01:40:57 -0400690static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Trond Myklebust38478b22006-05-25 01:40:57 -0400692 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Trond Myklebust38478b22006-05-25 01:40:57 -0400694 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400695 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700696 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400697 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700698 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400699 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100702struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400703 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100704 struct nfs_openargs o_arg;
705 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100706 struct nfs_open_confirmargs c_arg;
707 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500708 struct nfs4_string owner_name;
709 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100710 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400712 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400714 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100716 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400717 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100718 int rpc_status;
719 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720};
721
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400722
723static void nfs4_init_opendata_res(struct nfs4_opendata *p)
724{
725 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400726 p->o_res.seqid = p->o_arg.seqid;
727 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400728 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400729 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400730 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500731 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400732}
733
Al Viro82a2c1b2011-06-22 18:30:55 -0400734static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500735 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400736 const struct iattr *attrs,
737 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738{
Al Viro82a2c1b2011-06-22 18:30:55 -0400739 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100740 struct inode *dir = parent->d_inode;
741 struct nfs_server *server = NFS_SERVER(dir);
742 struct nfs4_opendata *p;
743
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400744 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745 if (p == NULL)
746 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400747 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100748 if (p->o_arg.seqid == NULL)
749 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400750 nfs_sb_active(dentry->d_sb);
751 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 p->dir = parent;
753 p->owner = sp;
754 atomic_inc(&sp->so_count);
755 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500756 p->o_arg.open_flags = flags;
757 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700758 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
759 * will return permission denied for all bits until close */
760 if (!(flags & O_EXCL)) {
761 /* ask server to check for all possible rights as results
762 * are cached */
763 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
764 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700765 }
David Howells7539bba2006-08-22 20:06:09 -0400766 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400767 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
768 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400769 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 p->o_arg.server = server;
771 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400772 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500774 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500775 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400776
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 p->o_arg.u.attrs = &p->attrs;
778 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500779
780 verf[0] = jiffies;
781 verf[1] = current->pid;
782 memcpy(p->o_arg.u.verifier.data, verf,
783 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100785 p->c_arg.fh = &p->o_res.fh;
786 p->c_arg.stateid = &p->o_res.stateid;
787 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400788 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400789 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 return p;
791err_free:
792 kfree(p);
793err:
794 dput(parent);
795 return NULL;
796}
797
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400798static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400800 struct nfs4_opendata *p = container_of(kref,
801 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400802 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400803
804 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400805 if (p->state != NULL)
806 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400807 nfs4_put_state_owner(p->owner);
808 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400809 dput(p->dentry);
810 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500811 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400812 kfree(p);
813}
814
815static void nfs4_opendata_put(struct nfs4_opendata *p)
816{
817 if (p != NULL)
818 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819}
820
Trond Myklebust06f814a2006-01-03 09:55:07 +0100821static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
822{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100823 int ret;
824
Trond Myklebust06f814a2006-01-03 09:55:07 +0100825 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100826 return ret;
827}
828
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500829static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400830{
831 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832
Trond Myklebust536e43d2012-01-17 22:04:26 -0500833 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500834 goto out;
835 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400836 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500837 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
838 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839 break;
840 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500841 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
842 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400843 break;
844 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500845 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
846 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400847 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500848out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400849 return ret;
850}
851
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400853{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500854 if (delegation == NULL)
855 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400857 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500858 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400859 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500860 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400861 return 1;
862}
863
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100865{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100867 case FMODE_WRITE:
868 state->n_wronly++;
869 break;
870 case FMODE_READ:
871 state->n_rdonly++;
872 break;
873 case FMODE_READ|FMODE_WRITE:
874 state->n_rdwr++;
875 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100877}
878
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400880{
881 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500882 nfs4_stateid_copy(&state->stateid, stateid);
883 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400885 case FMODE_READ:
886 set_bit(NFS_O_RDONLY_STATE, &state->flags);
887 break;
888 case FMODE_WRITE:
889 set_bit(NFS_O_WRONLY_STATE, &state->flags);
890 break;
891 case FMODE_READ|FMODE_WRITE:
892 set_bit(NFS_O_RDWR_STATE, &state->flags);
893 }
894}
895
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400897{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400898 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400900 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400905 /*
906 * Protect the call to nfs4_state_set_mode_locked and
907 * serialise the stateid update
908 */
909 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400910 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500911 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400912 set_bit(NFS_DELEGATED_STATE, &state->flags);
913 }
914 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400916 write_sequnlock(&state->seqlock);
917 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700919 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500923{
924 struct nfs_inode *nfsi = NFS_I(state->inode);
925 struct nfs_delegation *deleg_cur;
926 int ret = 0;
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500929
930 rcu_read_lock();
931 deleg_cur = rcu_dereference(nfsi->delegation);
932 if (deleg_cur == NULL)
933 goto no_delegation;
934
935 spin_lock(&deleg_cur->lock);
936 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500938 goto no_delegation_unlock;
939
940 if (delegation == NULL)
941 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500942 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500943 goto no_delegation_unlock;
944
Trond Myklebustb7391f42008-12-23 15:21:52 -0500945 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500947 ret = 1;
948no_delegation_unlock:
949 spin_unlock(&deleg_cur->lock);
950no_delegation:
951 rcu_read_unlock();
952
953 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500955 ret = 1;
956 }
957
958 return ret;
959}
960
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963{
964 struct nfs_delegation *delegation;
965
966 rcu_read_lock();
967 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 rcu_read_unlock();
970 return;
971 }
972 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400973 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974}
975
Trond Myklebust6ee41262007-07-08 14:11:36 -0400976static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400977{
978 struct nfs4_state *state = opendata->state;
979 struct nfs_inode *nfsi = NFS_I(state->inode);
980 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500981 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400983 nfs4_stateid stateid;
984 int ret = -EAGAIN;
985
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400988 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 if (can_open_cached(state, fmode, open_mode)) {
990 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400991 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400992 goto out_return_state;
993 }
994 spin_unlock(&state->owner->so_lock);
995 }
Trond Myklebust34310432008-12-23 15:21:38 -0500996 rcu_read_lock();
997 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -0500998 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500999 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001000 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001001 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001003 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001005 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001006 if (ret != 0)
1007 goto out;
1008 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001009
1010 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001012 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001014out:
1015 return ERR_PTR(ret);
1016out_return_state:
1017 atomic_inc(&state->count);
1018 return state;
1019}
1020
Andy Adamsone23008e2012-10-02 21:07:32 -04001021static void
1022nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1023{
1024 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1025 struct nfs_delegation *delegation;
1026 int delegation_flags = 0;
1027
1028 rcu_read_lock();
1029 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1030 if (delegation)
1031 delegation_flags = delegation->flags;
1032 rcu_read_unlock();
1033 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1034 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1035 "returning a delegation for "
1036 "OPEN(CLAIM_DELEGATE_CUR)\n",
1037 clp->cl_hostname);
1038 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1039 nfs_inode_set_delegation(state->inode,
1040 data->owner->so_cred,
1041 &data->o_res);
1042 else
1043 nfs_inode_reclaim_delegation(state->inode,
1044 data->owner->so_cred,
1045 &data->o_res);
1046}
1047
1048/*
1049 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1050 * and update the nfs4_state.
1051 */
1052static struct nfs4_state *
1053_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1054{
1055 struct inode *inode = data->state->inode;
1056 struct nfs4_state *state = data->state;
1057 int ret;
1058
1059 if (!data->rpc_done) {
1060 ret = data->rpc_status;
1061 goto err;
1062 }
1063
1064 ret = -ESTALE;
1065 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1066 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1067 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1068 goto err;
1069
1070 ret = -ENOMEM;
1071 state = nfs4_get_open_state(inode, data->owner);
1072 if (state == NULL)
1073 goto err;
1074
1075 ret = nfs_refresh_inode(inode, &data->f_attr);
1076 if (ret)
1077 goto err;
1078
1079 if (data->o_res.delegation_type != 0)
1080 nfs4_opendata_check_deleg(data, state);
1081 update_open_stateid(state, &data->o_res.stateid, NULL,
1082 data->o_arg.fmode);
1083
1084 return state;
1085err:
1086 return ERR_PTR(ret);
1087
1088}
1089
1090static struct nfs4_state *
1091_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001092{
1093 struct inode *inode;
1094 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001095 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001096
Trond Myklebustaac00a82007-07-05 19:02:21 -04001097 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001098 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 goto out;
1100 }
1101
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001102 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001103 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001104 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001106 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001107 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001108 goto err;
1109 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 state = nfs4_get_open_state(inode, data->owner);
1111 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001112 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001113 if (data->o_res.delegation_type != 0)
1114 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001115 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001116 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001117 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001119 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001120err_put_inode:
1121 iput(inode);
1122err:
1123 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124}
1125
Andy Adamsone23008e2012-10-02 21:07:32 -04001126static struct nfs4_state *
1127nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1128{
1129 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1130 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1131 return _nfs4_opendata_to_nfs4_state(data);
1132}
1133
Trond Myklebust864472e2006-01-03 09:55:15 +01001134static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1135{
1136 struct nfs_inode *nfsi = NFS_I(state->inode);
1137 struct nfs_open_context *ctx;
1138
1139 spin_lock(&state->inode->i_lock);
1140 list_for_each_entry(ctx, &nfsi->open_files, list) {
1141 if (ctx->state != state)
1142 continue;
1143 get_nfs_open_context(ctx);
1144 spin_unlock(&state->inode->i_lock);
1145 return ctx;
1146 }
1147 spin_unlock(&state->inode->i_lock);
1148 return ERR_PTR(-ENOENT);
1149}
1150
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001151static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1152{
1153 struct nfs4_opendata *opendata;
1154
Al Viro3d4ff432011-06-22 18:40:12 -04001155 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001156 if (opendata == NULL)
1157 return ERR_PTR(-ENOMEM);
1158 opendata->state = state;
1159 atomic_inc(&state->count);
1160 return opendata;
1161}
1162
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001163static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001164{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001165 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 int ret;
1167
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001168 opendata->o_arg.open_flags = 0;
1169 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001170 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1171 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1172 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001173 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001174 if (ret != 0)
1175 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001176 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001177 if (IS_ERR(newstate))
1178 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001179 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 return 0;
1182}
1183
1184static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1185{
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 int ret;
1188
1189 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 smp_rmb();
1192 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001193 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 if (ret != 0)
1196 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 if (newstate != state)
1198 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 }
1200 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001201 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001202 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 if (ret != 0)
1204 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 if (newstate != state)
1206 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 }
1208 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001209 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 if (ret != 0)
1212 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 if (newstate != state)
1214 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001216 /*
1217 * We may have performed cached opens for all three recoveries.
1218 * Check if we need to update the current stateid.
1219 */
1220 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001221 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001222 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001223 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001224 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001225 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001226 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 return 0;
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
1231 * OPEN_RECLAIM:
1232 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001234static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001236 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int status;
1240
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001241 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1242 if (IS_ERR(opendata))
1243 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1245 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001246 rcu_read_lock();
1247 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001248 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001249 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001250 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 opendata->o_arg.u.delegation_type = delegation_type;
1252 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001253 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return status;
1255}
1256
Trond Myklebust539cd032007-06-05 11:46:42 -04001257static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 struct nfs_server *server = NFS_SERVER(state->inode);
1260 struct nfs4_exception exception = { };
1261 int err;
1262 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001263 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001264 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001265 break;
1266 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 } while (exception.retry);
1268 return err;
1269}
1270
Trond Myklebust864472e2006-01-03 09:55:15 +01001271static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1272{
1273 struct nfs_open_context *ctx;
1274 int ret;
1275
1276 ctx = nfs4_state_find_open_context(state);
1277 if (IS_ERR(ctx))
1278 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001279 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 put_nfs_open_context(ctx);
1281 return ret;
1282}
1283
Trond Myklebust13437e12007-07-06 15:10:43 -04001284static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001286 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001289 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1290 if (IS_ERR(opendata))
1291 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001293 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Trond Myklebust13437e12007-07-06 15:10:43 -04001299int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 int err;
1304 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001305 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 switch (err) {
1307 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001308 case -ENOENT:
1309 case -ESTALE:
1310 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001311 case -NFS4ERR_BADSESSION:
1312 case -NFS4ERR_BADSLOT:
1313 case -NFS4ERR_BAD_HIGH_SLOT:
1314 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1315 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001316 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case -NFS4ERR_STALE_CLIENTID:
1319 case -NFS4ERR_STALE_STATEID:
1320 case -NFS4ERR_EXPIRED:
1321 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001322 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001323 goto out;
1324 case -ERESTARTSYS:
1325 /*
1326 * The show must go on: exit, but mark the
1327 * stateid as needing recovery.
1328 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001329 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001330 case -NFS4ERR_ADMIN_REVOKED:
1331 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001332 nfs_inode_find_state_and_recover(state->inode,
1333 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001334 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001335 case -EKEYEXPIRED:
1336 /*
1337 * User RPCSEC_GSS context has expired.
1338 * We cannot recover this stateid now, so
1339 * skip it and allow recovery thread to
1340 * proceed.
1341 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001342 case -ENOMEM:
1343 err = 0;
1344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 err = nfs4_handle_exception(server, err, &exception);
1347 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001348out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return err;
1350}
1351
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001352static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1353{
1354 struct nfs4_opendata *data = calldata;
1355
1356 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001357 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001358 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001359 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001360 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001361 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001362 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363}
1364
1365static void nfs4_open_confirm_release(void *calldata)
1366{
1367 struct nfs4_opendata *data = calldata;
1368 struct nfs4_state *state = NULL;
1369
1370 /* If this request hasn't been cancelled, do nothing */
1371 if (data->cancelled == 0)
1372 goto out_free;
1373 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001374 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001375 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001376 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001377 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001378 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001379out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001380 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001381}
1382
1383static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384 .rpc_call_done = nfs4_open_confirm_done,
1385 .rpc_release = nfs4_open_confirm_release,
1386};
1387
1388/*
1389 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1390 */
1391static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1392{
1393 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1394 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001395 struct rpc_message msg = {
1396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1397 .rpc_argp = &data->c_arg,
1398 .rpc_resp = &data->c_res,
1399 .rpc_cred = data->owner->so_cred,
1400 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001401 struct rpc_task_setup task_setup_data = {
1402 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001403 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001404 .callback_ops = &nfs4_open_confirm_ops,
1405 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001406 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001407 .flags = RPC_TASK_ASYNC,
1408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int status;
1410
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001411 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001412 data->rpc_done = 0;
1413 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001415 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001416 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418 status = nfs4_wait_for_completion_rpc_task(task);
1419 if (status != 0) {
1420 data->cancelled = 1;
1421 smp_wmb();
1422 } else
1423 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001424 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return status;
1426}
1427
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430 struct nfs4_opendata *data = calldata;
1431 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001432
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1434 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001435 /*
1436 * Check if we still need to send an OPEN call, or if we can use
1437 * a delegation instead.
1438 */
1439 if (data->state != NULL) {
1440 struct nfs_delegation *delegation;
1441
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001442 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001443 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001444 rcu_read_lock();
1445 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001446 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1447 can_open_delegated(delegation, data->o_arg.fmode))
1448 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001449 rcu_read_unlock();
1450 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001451 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001452 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001453 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001454 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001455 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001456 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1457 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001458 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001459 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001460 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001461 &data->o_res.seq_res,
1462 task) != 0)
1463 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001464 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001465unlock_no_action:
1466 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001467out_no_action:
1468 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001469 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001470}
1471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472static void nfs4_open_done(struct rpc_task *task, void *calldata)
1473{
1474 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001477
Trond Myklebust14516c32010-07-31 14:29:06 -04001478 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1479 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001482 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1483 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001484 case S_IFREG:
1485 break;
1486 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001488 break;
1489 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001491 break;
1492 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001494 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001495 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001496 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001497 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1498 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001499 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001500 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503static void nfs4_open_release(void *calldata)
1504{
1505 struct nfs4_opendata *data = calldata;
1506 struct nfs4_state *state = NULL;
1507
1508 /* If this request hasn't been cancelled, do nothing */
1509 if (data->cancelled == 0)
1510 goto out_free;
1511 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001512 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 goto out_free;
1514 /* In case we need an open_confirm, no cleanup! */
1515 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1516 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001518 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001519 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001521 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522}
1523
1524static const struct rpc_call_ops nfs4_open_ops = {
1525 .rpc_call_prepare = nfs4_open_prepare,
1526 .rpc_call_done = nfs4_open_done,
1527 .rpc_release = nfs4_open_release,
1528};
1529
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001530static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531{
1532 struct inode *dir = data->dir->d_inode;
1533 struct nfs_server *server = NFS_SERVER(dir);
1534 struct nfs_openargs *o_arg = &data->o_arg;
1535 struct nfs_openres *o_res = &data->o_res;
1536 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001537 struct rpc_message msg = {
1538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1539 .rpc_argp = o_arg,
1540 .rpc_resp = o_res,
1541 .rpc_cred = data->owner->so_cred,
1542 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 struct rpc_task_setup task_setup_data = {
1544 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001546 .callback_ops = &nfs4_open_ops,
1547 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001548 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001549 .flags = RPC_TASK_ASYNC,
1550 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551 int status;
1552
Trond Myklebust9d12b212012-01-17 22:04:25 -05001553 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001554 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001555 data->rpc_done = 0;
1556 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001557 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001558 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001559 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001560 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001561 if (IS_ERR(task))
1562 return PTR_ERR(task);
1563 status = nfs4_wait_for_completion_rpc_task(task);
1564 if (status != 0) {
1565 data->cancelled = 1;
1566 smp_wmb();
1567 } else
1568 status = data->rpc_status;
1569 rpc_put_task(task);
1570
1571 return status;
1572}
1573
1574static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1575{
1576 struct inode *dir = data->dir->d_inode;
1577 struct nfs_openres *o_res = &data->o_res;
1578 int status;
1579
1580 status = nfs4_run_open_task(data, 1);
1581 if (status != 0 || !data->rpc_done)
1582 return status;
1583
Trond Myklebust6926afd2012-01-07 13:22:46 -05001584 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1585
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001586 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1587 status = _nfs4_proc_open_confirm(data);
1588 if (status != 0)
1589 return status;
1590 }
1591
1592 return status;
1593}
1594
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001595static int nfs4_opendata_access(struct rpc_cred *cred,
1596 struct nfs4_opendata *opendata,
1597 struct nfs4_state *state, fmode_t fmode)
1598{
1599 struct nfs_access_entry cache;
1600 u32 mask;
1601
1602 /* access call failed or for some reason the server doesn't
1603 * support any access modes -- defer access call until later */
1604 if (opendata->o_res.access_supported == 0)
1605 return 0;
1606
1607 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001608 /* don't check MAY_WRITE - a newly created file may not have
1609 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001610 if (fmode & FMODE_READ)
1611 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001612 if (fmode & FMODE_EXEC)
1613 mask |= MAY_EXEC;
1614
1615 cache.cred = cred;
1616 cache.jiffies = jiffies;
1617 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1618 nfs_access_add_cache(state->inode, &cache);
1619
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001620 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001621 return 0;
1622
1623 /* even though OPEN succeeded, access is denied. Close the file */
1624 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001625 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001626}
1627
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001628/*
1629 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1630 */
1631static int _nfs4_proc_open(struct nfs4_opendata *data)
1632{
1633 struct inode *dir = data->dir->d_inode;
1634 struct nfs_server *server = NFS_SERVER(dir);
1635 struct nfs_openargs *o_arg = &data->o_arg;
1636 struct nfs_openres *o_res = &data->o_res;
1637 int status;
1638
1639 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001640 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001642 if (status != 0) {
1643 if (status == -NFS4ERR_BADNAME &&
1644 !(o_arg->open_flags & O_CREAT))
1645 return -ENOENT;
1646 return status;
1647 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001648
Trond Myklebust6926afd2012-01-07 13:22:46 -05001649 nfs_fattr_map_and_free_names(server, &data->f_attr);
1650
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001651 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001652 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001653 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1654 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001656 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001658 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001661 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Andy Adamsond83217c2011-03-01 01:34:17 +00001665static int nfs4_recover_expired_lease(struct nfs_server *server)
1666{
1667 return nfs4_client_recover_expired_lease(server->nfs_client);
1668}
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670/*
1671 * OPEN_EXPIRED:
1672 * reclaim state on the server after a network partition.
1673 * Assumes caller holds the appropriate lock
1674 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001675static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001677 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001678 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001680 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1681 if (IS_ERR(opendata))
1682 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001683 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001684 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001685 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001686 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001687 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001690static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001691{
Trond Myklebust539cd032007-06-05 11:46:42 -04001692 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001693 struct nfs4_exception exception = { };
1694 int err;
1695
1696 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001697 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001698 switch (err) {
1699 default:
1700 goto out;
1701 case -NFS4ERR_GRACE:
1702 case -NFS4ERR_DELAY:
1703 nfs4_handle_exception(server, err, &exception);
1704 err = 0;
1705 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001706 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001707out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001708 return err;
1709}
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Trond Myklebust864472e2006-01-03 09:55:15 +01001716 ctx = nfs4_state_find_open_context(state);
1717 if (IS_ERR(ctx))
1718 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001719 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001720 put_nfs_open_context(ctx);
1721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001724#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001725static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001726{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001727 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001728 nfs4_stateid *stateid = &state->stateid;
1729 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001730
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001731 /* If a state reset has been done, test_stateid is unneeded */
1732 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1733 return;
1734
1735 status = nfs41_test_stateid(server, stateid);
1736 if (status != NFS_OK) {
1737 /* Free the stateid unless the server explicitly
1738 * informs us the stateid is unrecognized. */
1739 if (status != -NFS4ERR_BAD_STATEID)
1740 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001741 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001742
Bryan Schumaker69388672012-09-26 15:25:52 -04001743 write_seqlock(&state->seqlock);
1744 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1745 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001746 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1747 }
1748}
1749
1750/**
1751 * nfs41_check_open_stateid - possibly free an open stateid
1752 *
1753 * @state: NFSv4 state for an inode
1754 *
1755 * Returns NFS_OK if recovery for this stateid is now finished.
1756 * Otherwise a negative NFS4ERR value is returned.
1757 */
1758static int nfs41_check_open_stateid(struct nfs4_state *state)
1759{
1760 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001761 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001762 int status;
1763
1764 /* If a state reset has been done, test_stateid is unneeded */
1765 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1766 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1767 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1768 return -NFS4ERR_BAD_STATEID;
1769
1770 status = nfs41_test_stateid(server, stateid);
1771 if (status != NFS_OK) {
1772 /* Free the stateid unless the server explicitly
1773 * informs us the stateid is unrecognized. */
1774 if (status != -NFS4ERR_BAD_STATEID)
1775 nfs41_free_stateid(server, stateid);
1776
1777 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1778 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1779 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001780 }
1781 return status;
1782}
1783
1784static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1785{
Chuck Levereb64cf92012-07-11 16:30:05 -04001786 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001787
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001788 nfs41_clear_delegation_stateid(state);
1789 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001790 if (status != NFS_OK)
1791 status = nfs4_open_expired(sp, state);
1792 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001793}
1794#endif
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001797 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1798 * fields corresponding to attributes that were used to store the verifier.
1799 * Make sure we clobber those fields in the later setattr call
1800 */
1801static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1802{
1803 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1804 !(sattr->ia_valid & ATTR_ATIME_SET))
1805 sattr->ia_valid |= ATTR_ATIME;
1806
1807 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1808 !(sattr->ia_valid & ATTR_MTIME_SET))
1809 sattr->ia_valid |= ATTR_MTIME;
1810}
1811
1812/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Andy Adamson82be4172012-05-23 05:02:35 -04001815static int _nfs4_do_open(struct inode *dir,
1816 struct dentry *dentry,
1817 fmode_t fmode,
1818 int flags,
1819 struct iattr *sattr,
1820 struct rpc_cred *cred,
1821 struct nfs4_state **res,
1822 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 struct nfs4_state_owner *sp;
1825 struct nfs4_state *state = NULL;
1826 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001827 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001828 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001832 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1833 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1835 goto out_err;
1836 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001837 status = nfs4_recover_expired_lease(server);
1838 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001839 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001840 if (dentry->d_inode != NULL)
1841 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001842 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001843 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001844 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001845 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Andy Adamson82be4172012-05-23 05:02:35 -04001847 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1848 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1849 if (!opendata->f_attr.mdsthreshold)
1850 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001851 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001852 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001853 if (dentry->d_inode != NULL)
1854 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001855
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001858 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001860 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001861 status = PTR_ERR(state);
1862 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001863 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001864 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001865 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001866
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001867 status = nfs4_opendata_access(cred, opendata, state, fmode);
1868 if (status != 0)
1869 goto err_opendata_put;
1870
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001871 if (opendata->o_arg.open_flags & O_EXCL) {
1872 nfs4_exclusive_attrset(opendata, sattr);
1873
1874 nfs_fattr_init(opendata->o_res.f_attr);
1875 status = nfs4_do_setattr(state->inode, cred,
1876 opendata->o_res.f_attr, sattr,
1877 state);
1878 if (status == 0)
1879 nfs_setattr_update_inode(state->inode, sattr);
1880 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1881 }
Andy Adamson82be4172012-05-23 05:02:35 -04001882
1883 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1884 *ctx_th = opendata->f_attr.mdsthreshold;
1885 else
1886 kfree(opendata->f_attr.mdsthreshold);
1887 opendata->f_attr.mdsthreshold = NULL;
1888
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001889 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 *res = state;
1892 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001893err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001894 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001895 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001896err_put_state_owner:
1897 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 *res = NULL;
1900 return status;
1901}
1902
1903
Andy Adamson82be4172012-05-23 05:02:35 -04001904static struct nfs4_state *nfs4_do_open(struct inode *dir,
1905 struct dentry *dentry,
1906 fmode_t fmode,
1907 int flags,
1908 struct iattr *sattr,
1909 struct rpc_cred *cred,
1910 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 struct nfs4_exception exception = { };
1913 struct nfs4_state *res;
1914 int status;
1915
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001916 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001918 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1919 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (status == 0)
1921 break;
1922 /* NOTE: BAD_SEQID means the server and client disagree about the
1923 * book-keeping w.r.t. state-changing operations
1924 * (OPEN/CLOSE/LOCK/LOCKU...)
1925 * It is actually a sign of a bug on the client or on the server.
1926 *
1927 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001928 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * have unhashed the old state_owner for us, and that we can
1930 * therefore safely retry using a new one. We should still warn
1931 * the user though...
1932 */
1933 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001934 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001935 " returned a bad sequence-id error!\n",
1936 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 exception.retry = 1;
1938 continue;
1939 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001940 /*
1941 * BAD_STATEID on OPEN means that the server cancelled our
1942 * state before it received the OPEN_CONFIRM.
1943 * Recover by retrying the request as per the discussion
1944 * on Page 181 of RFC3530.
1945 */
1946 if (status == -NFS4ERR_BAD_STATEID) {
1947 exception.retry = 1;
1948 continue;
1949 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001950 if (status == -EAGAIN) {
1951 /* We must have found a delegation */
1952 exception.retry = 1;
1953 continue;
1954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1956 status, &exception));
1957 } while (exception.retry);
1958 return res;
1959}
1960
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001961static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1962 struct nfs_fattr *fattr, struct iattr *sattr,
1963 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001965 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001967 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 .iap = sattr,
1969 .server = server,
1970 .bitmask = server->attr_bitmask,
1971 };
1972 struct nfs_setattrres res = {
1973 .fattr = fattr,
1974 .server = server,
1975 };
1976 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1978 .rpc_argp = &arg,
1979 .rpc_resp = &res,
1980 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001982 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001983 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Trond Myklebust0e574af2005-10-27 22:12:38 -04001985 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Trond Myklebust4fc87962012-03-08 17:42:01 -05001987 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04001988 struct nfs_lockowner lockowner = {
1989 .l_owner = current->files,
1990 .l_pid = current->tgid,
1991 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05001992 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04001993 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05001994 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1995 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001996 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001997 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001998 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Bryan Schumaker7c513052011-03-24 17:12:24 +00002000 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002001 if (status == 0 && state != NULL)
2002 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002003 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002006static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2007 struct nfs_fattr *fattr, struct iattr *sattr,
2008 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002010 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002011 struct nfs4_exception exception = {
2012 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002013 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002014 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 int err;
2016 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002017 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2018 switch (err) {
2019 case -NFS4ERR_OPENMODE:
2020 if (state && !(state->state & FMODE_WRITE)) {
2021 err = -EBADF;
2022 if (sattr->ia_valid & ATTR_OPEN)
2023 err = -EACCES;
2024 goto out;
2025 }
2026 }
2027 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002029out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return err;
2031}
2032
2033struct nfs4_closedata {
2034 struct inode *inode;
2035 struct nfs4_state *state;
2036 struct nfs_closeargs arg;
2037 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002038 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002039 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002040 bool roc;
2041 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042};
2043
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002044static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002045{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002046 struct nfs4_closedata *calldata = data;
2047 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002048 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002049
Fred Isamanf7e89172011-01-06 11:36:32 +00002050 if (calldata->roc)
2051 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002052 nfs4_put_open_state(calldata->state);
2053 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002054 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002055 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002056 kfree(calldata);
2057}
2058
Trond Myklebust88069f72009-12-08 08:33:16 -05002059static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2060 fmode_t fmode)
2061{
2062 spin_lock(&state->owner->so_lock);
2063 if (!(fmode & FMODE_READ))
2064 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2065 if (!(fmode & FMODE_WRITE))
2066 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2067 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2068 spin_unlock(&state->owner->so_lock);
2069}
2070
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002071static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002073 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct nfs_server *server = NFS_SERVER(calldata->inode);
2076
Chuck Levera3ca5652012-03-01 17:00:40 -05002077 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002078 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2079 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* hmm. we are done with the inode, and in the process of freeing
2081 * the state_owner. we keep this around to process errors
2082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 switch (task->tk_status) {
2084 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002085 if (calldata->roc)
2086 pnfs_roc_set_barrier(state->inode,
2087 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002088 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002089 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002090 nfs4_close_clear_stateid_flags(state,
2091 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 break;
2093 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002094 case -NFS4ERR_OLD_STATEID:
2095 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002097 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002100 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2101 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002103 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002104 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002105 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002108static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002110 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002111 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002112 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002113 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002114
Chuck Levera3ca5652012-03-01 17:00:40 -05002115 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002116 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002117 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002118
Trond Myklebust88069f72009-12-08 08:33:16 -05002119 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2120 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002121 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002122 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002123 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002124 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002125 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2126 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2127 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002128 }
2129 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002130 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2131 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2132 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002133 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002134 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002135 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002136
2137 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002138 /* Note: exit _without_ calling nfs4_close_done */
2139 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002140 nfs4_sequence_done(task, &calldata->res.seq_res);
Chuck Levera3ca5652012-03-01 17:00:40 -05002141 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002142 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002143
Fred Isamanf7e89172011-01-06 11:36:32 +00002144 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002145 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002146 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002147 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002148 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002149 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002150
Trond Myklebust516a6af2005-10-27 22:12:41 -04002151 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002152 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002153 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002154 &calldata->arg.seq_args,
2155 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002156 task) != 0)
2157 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002158out:
2159 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002162static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002163 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002164 .rpc_call_done = nfs4_close_done,
2165 .rpc_release = nfs4_free_closedata,
2166};
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168/*
2169 * It is possible for data to be read/written from a mem-mapped file
2170 * after the sys_close call (which hits the vfs layer as a flush).
2171 * This means that we can't safely call nfsv4 close on a file until
2172 * the inode is cleared. This in turn means that we are not good
2173 * NFSv4 citizens - we do not indicate to the server to update the file's
2174 * share state even when we are done with one of the three share
2175 * stateid's in the inode.
2176 *
2177 * NOTE: Caller must be holding the sp->so_owner semaphore!
2178 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002179int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002181 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002183 struct nfs4_state_owner *sp = state->owner;
2184 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 struct rpc_message msg = {
2186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2187 .rpc_cred = state->owner->so_cred,
2188 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 struct rpc_task_setup task_setup_data = {
2190 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002191 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002192 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002193 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002194 .flags = RPC_TASK_ASYNC,
2195 };
Trond Myklebust95121352005-10-18 14:20:12 -07002196 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002198 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002200 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002201 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002202 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002204 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002205 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002207 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002208 if (calldata->arg.seqid == NULL)
2209 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002210 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002211 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002212 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002213 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002214 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002215 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002216 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002217
Trond Myklebust1174dd12010-12-21 10:52:24 -05002218 msg.rpc_argp = &calldata->arg;
2219 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 task_setup_data.callback_data = calldata;
2221 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002222 if (IS_ERR(task))
2223 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002224 status = 0;
2225 if (wait)
2226 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002227 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002228 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002229out_free_calldata:
2230 kfree(calldata);
2231out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002232 nfs4_put_open_state(state);
2233 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002234 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Trond Myklebust2b484292010-09-17 10:56:51 -04002237static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002238nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 struct nfs4_state *state;
2241
Trond Myklebust565277f2007-10-15 18:17:53 -04002242 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002243 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2244 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002245 if (IS_ERR(state))
2246 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002247 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002248 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Trond Myklebust1185a552009-12-03 15:54:02 -05002251static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002252{
2253 if (ctx->state == NULL)
2254 return;
2255 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002256 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002257 else
Al Viro643168c2011-06-22 18:20:23 -04002258 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2262{
Benny Halevy43652ad2009-04-01 09:21:54 -04002263 struct nfs4_server_caps_arg args = {
2264 .fhandle = fhandle,
2265 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 struct nfs4_server_caps_res res = {};
2267 struct rpc_message msg = {
2268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002269 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 .rpc_resp = &res,
2271 };
2272 int status;
2273
Bryan Schumaker7c513052011-03-24 17:12:24 +00002274 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (status == 0) {
2276 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002277 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2278 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2279 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2280 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2281 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2283 server->caps |= NFS_CAP_ACLS;
2284 if (res.has_links != 0)
2285 server->caps |= NFS_CAP_HARDLINKS;
2286 if (res.has_symlinks != 0)
2287 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002288 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2289 server->caps |= NFS_CAP_FILEID;
2290 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2291 server->caps |= NFS_CAP_MODE;
2292 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2293 server->caps |= NFS_CAP_NLINK;
2294 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2295 server->caps |= NFS_CAP_OWNER;
2296 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2297 server->caps |= NFS_CAP_OWNER_GROUP;
2298 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2299 server->caps |= NFS_CAP_ATIME;
2300 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2301 server->caps |= NFS_CAP_CTIME;
2302 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2303 server->caps |= NFS_CAP_MTIME;
2304
Trond Myklebusta65318b2009-03-11 14:10:28 -04002305 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2306 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2307 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002309 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return status;
2313}
2314
Trond Myklebust55a97592006-06-09 09:34:19 -04002315int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317 struct nfs4_exception exception = { };
2318 int err;
2319 do {
2320 err = nfs4_handle_exception(server,
2321 _nfs4_server_capabilities(server, fhandle),
2322 &exception);
2323 } while (exception.retry);
2324 return err;
2325}
2326
2327static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2328 struct nfs_fsinfo *info)
2329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 struct nfs4_lookup_root_arg args = {
2331 .bitmask = nfs4_fattr_bitmap,
2332 };
2333 struct nfs4_lookup_res res = {
2334 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002335 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 .fh = fhandle,
2337 };
2338 struct rpc_message msg = {
2339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2340 .rpc_argp = &args,
2341 .rpc_resp = &res,
2342 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002343
Trond Myklebust0e574af2005-10-27 22:12:38 -04002344 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2349 struct nfs_fsinfo *info)
2350{
2351 struct nfs4_exception exception = { };
2352 int err;
2353 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002354 err = _nfs4_lookup_root(server, fhandle, info);
2355 switch (err) {
2356 case 0:
2357 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002358 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002359 default:
2360 err = nfs4_handle_exception(server, err, &exception);
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002363out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return err;
2365}
2366
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002367static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2368 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2369{
2370 struct rpc_auth *auth;
2371 int ret;
2372
2373 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002374 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002375 ret = -EIO;
2376 goto out;
2377 }
2378 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002379out:
2380 return ret;
2381}
2382
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002383static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002384 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002386 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002387 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002389 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002390 if (len < 0)
2391 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002392
2393 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002394 /* AUTH_UNIX is the default flavor if none was specified,
2395 * thus has already been tried. */
2396 if (flav_array[i] == RPC_AUTH_UNIX)
2397 continue;
2398
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002399 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002400 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002401 continue;
2402 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002403 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002404 /*
2405 * -EACCESS could mean that the user doesn't have correct permissions
2406 * to access the mount. It could also mean that we tried to mount
2407 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2408 * existing mount programs don't handle -EACCES very well so it should
2409 * be mapped to -EPERM instead.
2410 */
2411 if (status == -EACCES)
2412 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002413 return status;
2414}
2415
2416/*
2417 * get the file handle for the "/" directory on the server
2418 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002419int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2420 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002421{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002422 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002423 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002424 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2425 /*
2426 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2427 * by nfs4_map_errors() as this function exits.
2428 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002429 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (status == 0)
2431 status = nfs4_server_capabilities(server, fhandle);
2432 if (status == 0)
2433 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002434 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Bryan Schumakerbae36242012-05-10 15:07:31 -04002437static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2438 struct nfs_fsinfo *info)
2439{
2440 int error;
2441 struct nfs_fattr *fattr = info->fattr;
2442
2443 error = nfs4_server_capabilities(server, mntfh);
2444 if (error < 0) {
2445 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2446 return error;
2447 }
2448
2449 error = nfs4_proc_getattr(server, mntfh, fattr);
2450 if (error < 0) {
2451 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2452 return error;
2453 }
2454
2455 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2456 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2457 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2458
2459 return error;
2460}
2461
Manoj Naik6b97fd32006-06-09 09:34:29 -04002462/*
2463 * Get locations and (maybe) other attributes of a referral.
2464 * Note that we'll actually follow the referral later when
2465 * we detect fsid mismatch in inode revalidation
2466 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002467static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2468 const struct qstr *name, struct nfs_fattr *fattr,
2469 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002470{
2471 int status = -ENOMEM;
2472 struct page *page = NULL;
2473 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002474
2475 page = alloc_page(GFP_KERNEL);
2476 if (page == NULL)
2477 goto out;
2478 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2479 if (locations == NULL)
2480 goto out;
2481
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002482 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002483 if (status != 0)
2484 goto out;
2485 /* Make sure server returned a different fsid for the referral */
2486 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002487 dprintk("%s: server did not return a different fsid for"
2488 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002489 status = -EIO;
2490 goto out;
2491 }
Andy Adamson533eb462011-06-13 18:25:56 -04002492 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2493 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002494
Andy Adamson533eb462011-06-13 18:25:56 -04002495 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002496 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002497 memset(fhandle, 0, sizeof(struct nfs_fh));
2498out:
2499 if (page)
2500 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002501 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002502 return status;
2503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2506{
2507 struct nfs4_getattr_arg args = {
2508 .fh = fhandle,
2509 .bitmask = server->attr_bitmask,
2510 };
2511 struct nfs4_getattr_res res = {
2512 .fattr = fattr,
2513 .server = server,
2514 };
2515 struct rpc_message msg = {
2516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2517 .rpc_argp = &args,
2518 .rpc_resp = &res,
2519 };
2520
Trond Myklebust0e574af2005-10-27 22:12:38 -04002521 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002522 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
2525static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2526{
2527 struct nfs4_exception exception = { };
2528 int err;
2529 do {
2530 err = nfs4_handle_exception(server,
2531 _nfs4_proc_getattr(server, fhandle, fattr),
2532 &exception);
2533 } while (exception.retry);
2534 return err;
2535}
2536
2537/*
2538 * The file is not closed if it is opened due to the a request to change
2539 * the size of the file. The open call will not be needed once the
2540 * VFS layer lookup-intents are implemented.
2541 *
2542 * Close is called when the inode is destroyed.
2543 * If we haven't opened the file for O_WRONLY, we
2544 * need to in the size_change case to obtain a stateid.
2545 *
2546 * Got race?
2547 * Because OPEN is always done by name in nfsv4, it is
2548 * possible that we opened a different file by the same
2549 * name. We can recognize this race condition, but we
2550 * can't do anything about it besides returning an error.
2551 *
2552 * This will be fixed with VFS changes (lookup-intent).
2553 */
2554static int
2555nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2556 struct iattr *sattr)
2557{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002558 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002559 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002560 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 int status;
2562
Benny Halevy8a1636c2010-07-14 15:43:57 -04002563 if (pnfs_ld_layoutret_on_setattr(inode))
2564 pnfs_return_layout(inode);
2565
Trond Myklebust0e574af2005-10-27 22:12:38 -04002566 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Andy Adamson26699402012-05-30 16:12:24 -04002568 /* Deal with open(O_TRUNC) */
2569 if (sattr->ia_valid & ATTR_OPEN)
2570 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2571
2572 /* Optimization: if the end result is no change, don't RPC */
2573 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2574 return 0;
2575
Trond Myklebustd5308382005-11-04 15:33:38 -05002576 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002577 if (sattr->ia_valid & ATTR_FILE) {
2578 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002579
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002580 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002581 if (ctx) {
2582 cred = ctx->cred;
2583 state = ctx->state;
2584 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002585 }
2586
2587 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002588 if (status == 0)
2589 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return status;
2591}
2592
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002593static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2594 const struct qstr *name, struct nfs_fh *fhandle,
2595 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002596{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002597 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002598 int status;
2599 struct nfs4_lookup_arg args = {
2600 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002601 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002602 .name = name,
2603 };
2604 struct nfs4_lookup_res res = {
2605 .server = server,
2606 .fattr = fattr,
2607 .fh = fhandle,
2608 };
2609 struct rpc_message msg = {
2610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2611 .rpc_argp = &args,
2612 .rpc_resp = &res,
2613 };
2614
2615 nfs_fattr_init(fattr);
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002618 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 dprintk("NFS reply lookup: %d\n", status);
2620 return status;
2621}
2622
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002623static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002624{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002625 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002626 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002627 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2628 fattr->nlink = 2;
2629}
2630
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002631static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2632 struct qstr *name, struct nfs_fh *fhandle,
2633 struct nfs_fattr *fattr)
2634{
2635 struct nfs4_exception exception = { };
2636 struct rpc_clnt *client = *clnt;
2637 int err;
2638 do {
2639 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2640 switch (err) {
2641 case -NFS4ERR_BADNAME:
2642 err = -ENOENT;
2643 goto out;
2644 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002645 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002646 goto out;
2647 case -NFS4ERR_WRONGSEC:
2648 err = -EPERM;
2649 if (client != *clnt)
2650 goto out;
2651
2652 client = nfs4_create_sec_client(client, dir, name);
2653 if (IS_ERR(client))
2654 return PTR_ERR(client);
2655
2656 exception.retry = 1;
2657 break;
2658 default:
2659 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2660 }
2661 } while (exception.retry);
2662
2663out:
2664 if (err == 0)
2665 *clnt = client;
2666 else if (client != *clnt)
2667 rpc_shutdown_client(client);
2668
2669 return err;
2670}
2671
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002672static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002673 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002675 int status;
2676 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002677
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002678 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2679 if (client != NFS_CLIENT(dir)) {
2680 rpc_shutdown_client(client);
2681 nfs_fixup_secinfo_attributes(fattr);
2682 }
2683 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002686struct rpc_clnt *
2687nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2688 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2689{
2690 int status;
2691 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2692
2693 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2694 if (status < 0) {
2695 rpc_shutdown_client(client);
2696 return ERR_PTR(status);
2697 }
2698 return client;
2699}
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2702{
Trond Myklebust76b32992007-08-10 17:45:11 -04002703 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 struct nfs4_accessargs args = {
2705 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002706 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002708 struct nfs4_accessres res = {
2709 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002710 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 struct rpc_message msg = {
2712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2713 .rpc_argp = &args,
2714 .rpc_resp = &res,
2715 .rpc_cred = entry->cred,
2716 };
2717 int mode = entry->mask;
2718 int status;
2719
2720 /*
2721 * Determine which access bits we want to ask for...
2722 */
2723 if (mode & MAY_READ)
2724 args.access |= NFS4_ACCESS_READ;
2725 if (S_ISDIR(inode->i_mode)) {
2726 if (mode & MAY_WRITE)
2727 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2728 if (mode & MAY_EXEC)
2729 args.access |= NFS4_ACCESS_LOOKUP;
2730 } else {
2731 if (mode & MAY_WRITE)
2732 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2733 if (mode & MAY_EXEC)
2734 args.access |= NFS4_ACCESS_EXECUTE;
2735 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002736
2737 res.fattr = nfs_alloc_fattr();
2738 if (res.fattr == NULL)
2739 return -ENOMEM;
2740
Bryan Schumaker7c513052011-03-24 17:12:24 +00002741 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002743 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002744 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002746 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return status;
2748}
2749
2750static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2751{
2752 struct nfs4_exception exception = { };
2753 int err;
2754 do {
2755 err = nfs4_handle_exception(NFS_SERVER(inode),
2756 _nfs4_proc_access(inode, entry),
2757 &exception);
2758 } while (exception.retry);
2759 return err;
2760}
2761
2762/*
2763 * TODO: For the time being, we don't try to get any attributes
2764 * along with any of the zero-copy operations READ, READDIR,
2765 * READLINK, WRITE.
2766 *
2767 * In the case of the first three, we want to put the GETATTR
2768 * after the read-type operation -- this is because it is hard
2769 * to predict the length of a GETATTR response in v4, and thus
2770 * align the READ data correctly. This means that the GETATTR
2771 * may end up partially falling into the page cache, and we should
2772 * shift it into the 'tail' of the xdr_buf before processing.
2773 * To do this efficiently, we need to know the total length
2774 * of data received, which doesn't seem to be available outside
2775 * of the RPC layer.
2776 *
2777 * In the case of WRITE, we also want to put the GETATTR after
2778 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002779 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 *
2781 * Both of these changes to the XDR layer would in fact be quite
2782 * minor, but I decided to leave them for a subsequent patch.
2783 */
2784static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2785 unsigned int pgbase, unsigned int pglen)
2786{
2787 struct nfs4_readlink args = {
2788 .fh = NFS_FH(inode),
2789 .pgbase = pgbase,
2790 .pglen = pglen,
2791 .pages = &page,
2792 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002793 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct rpc_message msg = {
2795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2796 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002797 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 };
2799
Bryan Schumaker7c513052011-03-24 17:12:24 +00002800 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
2803static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2804 unsigned int pgbase, unsigned int pglen)
2805{
2806 struct nfs4_exception exception = { };
2807 int err;
2808 do {
2809 err = nfs4_handle_exception(NFS_SERVER(inode),
2810 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2811 &exception);
2812 } while (exception.retry);
2813 return err;
2814}
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002817 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819static int
2820nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002821 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002823 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 int status = 0;
2826
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002827 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2828 if (IS_ERR(ctx))
2829 return PTR_ERR(ctx);
2830
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002831 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002832 state = nfs4_do_open(dir, dentry, ctx->mode,
2833 flags, sattr, ctx->cred,
2834 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002835 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (IS_ERR(state)) {
2837 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002840 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002841 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002842 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002844 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return status;
2846}
2847
2848static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2849{
Trond Myklebust16e42952005-10-27 22:12:44 -04002850 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002851 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002853 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002855 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002856 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002857 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2860 .rpc_argp = &args,
2861 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002863 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002864
Bryan Schumaker7c513052011-03-24 17:12:24 +00002865 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002866 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002867 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return status;
2869}
2870
2871static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2872{
2873 struct nfs4_exception exception = { };
2874 int err;
2875 do {
2876 err = nfs4_handle_exception(NFS_SERVER(dir),
2877 _nfs4_proc_remove(dir, name),
2878 &exception);
2879 } while (exception.retry);
2880 return err;
2881}
2882
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002883static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002885 struct nfs_server *server = NFS_SERVER(dir);
2886 struct nfs_removeargs *args = msg->rpc_argp;
2887 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002889 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002891 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002894static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2895{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002896 nfs4_setup_sequence(NFS_SERVER(data->dir),
2897 &data->args.seq_args,
2898 &data->res.seq_res,
2899 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002902static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002904 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2905
Trond Myklebust14516c32010-07-31 14:29:06 -04002906 if (!nfs4_sequence_done(task, &res->seq_res))
2907 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002908 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002909 return 0;
2910 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002911 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
Jeff Laytond3d41522010-09-17 17:31:57 -04002914static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2915{
2916 struct nfs_server *server = NFS_SERVER(dir);
2917 struct nfs_renameargs *arg = msg->rpc_argp;
2918 struct nfs_renameres *res = msg->rpc_resp;
2919
2920 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002921 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002922 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002923}
2924
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002925static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2926{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002927 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2928 &data->args.seq_args,
2929 &data->res.seq_res,
2930 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002931}
2932
2933static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2934 struct inode *new_dir)
2935{
2936 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2937
2938 if (!nfs4_sequence_done(task, &res->seq_res))
2939 return 0;
2940 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2941 return 0;
2942
2943 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002944 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002945 return 1;
2946}
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2949 struct inode *new_dir, struct qstr *new_name)
2950{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002951 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002952 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 .old_dir = NFS_FH(old_dir),
2954 .new_dir = NFS_FH(new_dir),
2955 .old_name = old_name,
2956 .new_name = new_name,
2957 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002958 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002959 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002960 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 struct rpc_message msg = {
2962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2963 .rpc_argp = &arg,
2964 .rpc_resp = &res,
2965 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002966 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Bryan Schumaker7c513052011-03-24 17:12:24 +00002968 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (!status) {
2970 update_changeattr(old_dir, &res.old_cinfo);
2971 update_changeattr(new_dir, &res.new_cinfo);
2972 }
2973 return status;
2974}
2975
2976static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2977 struct inode *new_dir, struct qstr *new_name)
2978{
2979 struct nfs4_exception exception = { };
2980 int err;
2981 do {
2982 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2983 _nfs4_proc_rename(old_dir, old_name,
2984 new_dir, new_name),
2985 &exception);
2986 } while (exception.retry);
2987 return err;
2988}
2989
2990static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2991{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002992 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct nfs4_link_arg arg = {
2994 .fh = NFS_FH(inode),
2995 .dir_fh = NFS_FH(dir),
2996 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002997 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002999 struct nfs4_link_res res = {
3000 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003001 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 struct rpc_message msg = {
3003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3004 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003005 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003007 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Trond Myklebust136f2622010-04-16 16:22:49 -04003009 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003010 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003011 goto out;
3012
Bryan Schumaker7c513052011-03-24 17:12:24 +00003013 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003014 if (!status) {
3015 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003016 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003017 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003018out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003019 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return status;
3021}
3022
3023static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3024{
3025 struct nfs4_exception exception = { };
3026 int err;
3027 do {
3028 err = nfs4_handle_exception(NFS_SERVER(inode),
3029 _nfs4_proc_link(inode, dir, name),
3030 &exception);
3031 } while (exception.retry);
3032 return err;
3033}
3034
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003035struct nfs4_createdata {
3036 struct rpc_message msg;
3037 struct nfs4_create_arg arg;
3038 struct nfs4_create_res res;
3039 struct nfs_fh fh;
3040 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003041};
3042
3043static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3044 struct qstr *name, struct iattr *sattr, u32 ftype)
3045{
3046 struct nfs4_createdata *data;
3047
3048 data = kzalloc(sizeof(*data), GFP_KERNEL);
3049 if (data != NULL) {
3050 struct nfs_server *server = NFS_SERVER(dir);
3051
3052 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3053 data->msg.rpc_argp = &data->arg;
3054 data->msg.rpc_resp = &data->res;
3055 data->arg.dir_fh = NFS_FH(dir);
3056 data->arg.server = server;
3057 data->arg.name = name;
3058 data->arg.attrs = sattr;
3059 data->arg.ftype = ftype;
3060 data->arg.bitmask = server->attr_bitmask;
3061 data->res.server = server;
3062 data->res.fh = &data->fh;
3063 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003064 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003065 }
3066 return data;
3067}
3068
3069static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3070{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003071 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003072 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003073 if (status == 0) {
3074 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003075 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3076 }
3077 return status;
3078}
3079
3080static void nfs4_free_createdata(struct nfs4_createdata *data)
3081{
3082 kfree(data);
3083}
3084
Chuck Lever4f390c12006-08-22 20:06:22 -04003085static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003086 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003088 struct nfs4_createdata *data;
3089 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Chuck Lever94a6d752006-08-22 20:06:23 -04003091 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003092 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003093
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003094 status = -ENOMEM;
3095 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3096 if (data == NULL)
3097 goto out;
3098
3099 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3100 data->arg.u.symlink.pages = &page;
3101 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 status = nfs4_do_create(dir, dentry, data);
3104
3105 nfs4_free_createdata(data);
3106out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return status;
3108}
3109
Chuck Lever4f390c12006-08-22 20:06:22 -04003110static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003111 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 struct nfs4_exception exception = { };
3114 int err;
3115 do {
3116 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003117 _nfs4_proc_symlink(dir, dentry, page,
3118 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 &exception);
3120 } while (exception.retry);
3121 return err;
3122}
3123
3124static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3125 struct iattr *sattr)
3126{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003127 struct nfs4_createdata *data;
3128 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003130 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3131 if (data == NULL)
3132 goto out;
3133
3134 status = nfs4_do_create(dir, dentry, data);
3135
3136 nfs4_free_createdata(data);
3137out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return status;
3139}
3140
3141static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3142 struct iattr *sattr)
3143{
3144 struct nfs4_exception exception = { };
3145 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003146
3147 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 do {
3149 err = nfs4_handle_exception(NFS_SERVER(dir),
3150 _nfs4_proc_mkdir(dir, dentry, sattr),
3151 &exception);
3152 } while (exception.retry);
3153 return err;
3154}
3155
3156static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003157 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
3159 struct inode *dir = dentry->d_inode;
3160 struct nfs4_readdir_arg args = {
3161 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003162 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 .pgbase = 0,
3164 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003165 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003166 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 };
3168 struct nfs4_readdir_res res;
3169 struct rpc_message msg = {
3170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3171 .rpc_argp = &args,
3172 .rpc_resp = &res,
3173 .rpc_cred = cred,
3174 };
3175 int status;
3176
Harvey Harrison3110ff82008-05-02 13:42:44 -07003177 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003178 dentry->d_parent->d_name.name,
3179 dentry->d_name.name,
3180 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003181 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003183 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003184 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003185 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003186 status += args.pgbase;
3187 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003188
3189 nfs_invalidate_atime(dir);
3190
Harvey Harrison3110ff82008-05-02 13:42:44 -07003191 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return status;
3193}
3194
3195static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003196 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct nfs4_exception exception = { };
3199 int err;
3200 do {
3201 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3202 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003203 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 &exception);
3205 } while (exception.retry);
3206 return err;
3207}
3208
3209static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3210 struct iattr *sattr, dev_t rdev)
3211{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003212 struct nfs4_createdata *data;
3213 int mode = sattr->ia_mode;
3214 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3217 if (data == NULL)
3218 goto out;
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003221 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003223 data->arg.ftype = NF4BLK;
3224 data->arg.u.device.specdata1 = MAJOR(rdev);
3225 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003228 data->arg.ftype = NF4CHR;
3229 data->arg.u.device.specdata1 = MAJOR(rdev);
3230 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003231 } else if (!S_ISSOCK(mode)) {
3232 status = -EINVAL;
3233 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003236 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003237out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003238 nfs4_free_createdata(data);
3239out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return status;
3241}
3242
3243static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3244 struct iattr *sattr, dev_t rdev)
3245{
3246 struct nfs4_exception exception = { };
3247 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003248
3249 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 do {
3251 err = nfs4_handle_exception(NFS_SERVER(dir),
3252 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3253 &exception);
3254 } while (exception.retry);
3255 return err;
3256}
3257
3258static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3259 struct nfs_fsstat *fsstat)
3260{
3261 struct nfs4_statfs_arg args = {
3262 .fh = fhandle,
3263 .bitmask = server->attr_bitmask,
3264 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003265 struct nfs4_statfs_res res = {
3266 .fsstat = fsstat,
3267 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct rpc_message msg = {
3269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3270 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003271 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 };
3273
Trond Myklebust0e574af2005-10-27 22:12:38 -04003274 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003275 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
3278static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3279{
3280 struct nfs4_exception exception = { };
3281 int err;
3282 do {
3283 err = nfs4_handle_exception(server,
3284 _nfs4_proc_statfs(server, fhandle, fsstat),
3285 &exception);
3286 } while (exception.retry);
3287 return err;
3288}
3289
3290static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3291 struct nfs_fsinfo *fsinfo)
3292{
3293 struct nfs4_fsinfo_arg args = {
3294 .fh = fhandle,
3295 .bitmask = server->attr_bitmask,
3296 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003297 struct nfs4_fsinfo_res res = {
3298 .fsinfo = fsinfo,
3299 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct rpc_message msg = {
3301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3302 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003303 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 };
3305
Bryan Schumaker7c513052011-03-24 17:12:24 +00003306 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
3309static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3310{
3311 struct nfs4_exception exception = { };
3312 int err;
3313
3314 do {
3315 err = nfs4_handle_exception(server,
3316 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3317 &exception);
3318 } while (exception.retry);
3319 return err;
3320}
3321
3322static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3323{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003324 int error;
3325
Trond Myklebust0e574af2005-10-27 22:12:38 -04003326 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003327 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003328 if (error == 0) {
3329 /* block layout checks this! */
3330 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003331 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003332 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003333
3334 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
3337static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3338 struct nfs_pathconf *pathconf)
3339{
3340 struct nfs4_pathconf_arg args = {
3341 .fh = fhandle,
3342 .bitmask = server->attr_bitmask,
3343 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003344 struct nfs4_pathconf_res res = {
3345 .pathconf = pathconf,
3346 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 struct rpc_message msg = {
3348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3349 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003350 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 };
3352
3353 /* None of the pathconf attributes are mandatory to implement */
3354 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3355 memset(pathconf, 0, sizeof(*pathconf));
3356 return 0;
3357 }
3358
Trond Myklebust0e574af2005-10-27 22:12:38 -04003359 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003360 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
3363static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3364 struct nfs_pathconf *pathconf)
3365{
3366 struct nfs4_exception exception = { };
3367 int err;
3368
3369 do {
3370 err = nfs4_handle_exception(server,
3371 _nfs4_proc_pathconf(server, fhandle, pathconf),
3372 &exception);
3373 } while (exception.retry);
3374 return err;
3375}
3376
Benny Halevyd20581a2011-05-22 19:52:03 +03003377void __nfs4_read_done_cb(struct nfs_read_data *data)
3378{
Fred Isamancd841602012-04-20 14:47:44 -04003379 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003380}
3381
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003382static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Fred Isamancd841602012-04-20 14:47:44 -04003384 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003386 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003387 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003388 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003390
Benny Halevyd20581a2011-05-22 19:52:03 +03003391 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003393 renew_lease(server, data->timestamp);
3394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003397static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3398{
3399
3400 dprintk("--> %s\n", __func__);
3401
3402 if (!nfs4_sequence_done(task, &data->res.seq_res))
3403 return -EAGAIN;
3404
Benny Halevyd20581a2011-05-22 19:52:03 +03003405 return data->read_done_cb ? data->read_done_cb(task, data) :
3406 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003407}
3408
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003409static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003412 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003413 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003414 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003417static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3418{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003419 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3420 &data->args.seq_args,
3421 &data->res.seq_res,
3422 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Fred Isamanb029bc92011-03-03 15:13:42 +00003425static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Fred Isamancd841602012-04-20 14:47:44 -04003427 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003429 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003430 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003431 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003433 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003435 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003436 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
Fred Isamanb029bc92011-03-03 15:13:42 +00003440static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3441{
3442 if (!nfs4_sequence_done(task, &data->res.seq_res))
3443 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003444 return data->write_done_cb ? data->write_done_cb(task, data) :
3445 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003446}
3447
Trond Myklebust5a37f852012-04-28 14:55:16 -04003448static
3449bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003450{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003451 const struct nfs_pgio_header *hdr = data->header;
3452
3453 /* Don't request attributes for pNFS or O_DIRECT writes */
3454 if (data->ds_clp != NULL || hdr->dreq != NULL)
3455 return false;
3456 /* Otherwise, request attributes if and only if we don't hold
3457 * a delegation
3458 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003459 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003460}
Fred Isamana69aef12011-03-03 15:13:47 +00003461
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003462static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
Fred Isamancd841602012-04-20 14:47:44 -04003464 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003465
Trond Myklebust5a37f852012-04-28 14:55:16 -04003466 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003467 data->args.bitmask = NULL;
3468 data->res.fattr = NULL;
3469 } else
3470 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003471
Fred Isamanb029bc92011-03-03 15:13:42 +00003472 if (!data->write_done_cb)
3473 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003474 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 data->timestamp = jiffies;
3476
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003477 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003478 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003481static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3482{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003483 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3484 &data->args.seq_args,
3485 &data->res.seq_res,
3486 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
Fred Isaman0b7c0152012-04-20 14:47:39 -04003489static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3490{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003491 nfs4_setup_sequence(NFS_SERVER(data->inode),
3492 &data->args.seq_args,
3493 &data->res.seq_res,
3494 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
Fred Isaman0b7c0152012-04-20 14:47:39 -04003497static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003500
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003501 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003502 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003503 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
Fred Isaman0b7c0152012-04-20 14:47:39 -04003508static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003509{
3510 if (!nfs4_sequence_done(task, &data->res.seq_res))
3511 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003512 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003513}
3514
Fred Isaman0b7c0152012-04-20 14:47:39 -04003515static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003517 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003518
Fred Isaman0b7c0152012-04-20 14:47:39 -04003519 if (data->commit_done_cb == NULL)
3520 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003521 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003522 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003523 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003526struct nfs4_renewdata {
3527 struct nfs_client *client;
3528 unsigned long timestamp;
3529};
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531/*
3532 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3533 * standalone procedure for queueing an asynchronous RENEW.
3534 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003535static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003536{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003537 struct nfs4_renewdata *data = calldata;
3538 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003539
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003540 if (atomic_read(&clp->cl_count) > 1)
3541 nfs4_schedule_state_renewal(clp);
3542 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003543 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003544}
3545
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003546static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003548 struct nfs4_renewdata *data = calldata;
3549 struct nfs_client *clp = data->client;
3550 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003553 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003554 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3555 return;
3556 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003557 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003558 return;
3559 }
3560 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003562 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003565static const struct rpc_call_ops nfs4_renew_ops = {
3566 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003567 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003568};
3569
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003570static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 struct rpc_message msg = {
3573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3574 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003575 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003577 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003579 if (renew_flags == 0)
3580 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003581 if (!atomic_inc_not_zero(&clp->cl_count))
3582 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003583 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003584 if (data == NULL)
3585 return -ENOMEM;
3586 data->client = clp;
3587 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003589 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003592static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
3594 struct rpc_message msg = {
3595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3596 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003597 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 };
3599 unsigned long now = jiffies;
3600 int status;
3601
3602 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3603 if (status < 0)
3604 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003605 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 return 0;
3607}
3608
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003609static inline int nfs4_server_supports_acls(struct nfs_server *server)
3610{
3611 return (server->caps & NFS_CAP_ACLS)
3612 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3613 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3614}
3615
Trond Myklebust21f498c2012-08-24 10:59:25 -04003616/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3617 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003618 * the stack.
3619 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003620#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003621
Neil Hormane9e3d722011-03-04 19:26:03 -05003622static int buf_to_pages_noslab(const void *buf, size_t buflen,
3623 struct page **pages, unsigned int *pgbase)
3624{
3625 struct page *newpage, **spages;
3626 int rc = 0;
3627 size_t len;
3628 spages = pages;
3629
3630 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003631 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003632 newpage = alloc_page(GFP_KERNEL);
3633
3634 if (newpage == NULL)
3635 goto unwind;
3636 memcpy(page_address(newpage), buf, len);
3637 buf += len;
3638 buflen -= len;
3639 *pages++ = newpage;
3640 rc++;
3641 } while (buflen != 0);
3642
3643 return rc;
3644
3645unwind:
3646 for(; rc > 0; rc--)
3647 __free_page(spages[rc-1]);
3648 return -ENOMEM;
3649}
3650
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003651struct nfs4_cached_acl {
3652 int cached;
3653 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003654 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003655};
3656
3657static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003658{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003659 struct nfs_inode *nfsi = NFS_I(inode);
3660
3661 spin_lock(&inode->i_lock);
3662 kfree(nfsi->nfs4_acl);
3663 nfsi->nfs4_acl = acl;
3664 spin_unlock(&inode->i_lock);
3665}
3666
3667static void nfs4_zap_acl_attr(struct inode *inode)
3668{
3669 nfs4_set_cached_acl(inode, NULL);
3670}
3671
3672static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3673{
3674 struct nfs_inode *nfsi = NFS_I(inode);
3675 struct nfs4_cached_acl *acl;
3676 int ret = -ENOENT;
3677
3678 spin_lock(&inode->i_lock);
3679 acl = nfsi->nfs4_acl;
3680 if (acl == NULL)
3681 goto out;
3682 if (buf == NULL) /* user is just asking for length */
3683 goto out_len;
3684 if (acl->cached == 0)
3685 goto out;
3686 ret = -ERANGE; /* see getxattr(2) man page */
3687 if (acl->len > buflen)
3688 goto out;
3689 memcpy(buf, acl->data, acl->len);
3690out_len:
3691 ret = acl->len;
3692out:
3693 spin_unlock(&inode->i_lock);
3694 return ret;
3695}
3696
Sachin Prabhu5794d212012-04-17 14:36:40 +01003697static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003698{
3699 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003700 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003701
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003702 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003703 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003704 if (acl == NULL)
3705 goto out;
3706 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003707 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003708 } else {
3709 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3710 if (acl == NULL)
3711 goto out;
3712 acl->cached = 0;
3713 }
3714 acl->len = acl_len;
3715out:
3716 nfs4_set_cached_acl(inode, acl);
3717}
3718
Andy Adamsonbf118a32011-12-07 11:55:27 -05003719/*
3720 * The getxattr API returns the required buffer length when called with a
3721 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3722 * the required buf. On a NULL buf, we send a page of data to the server
3723 * guessing that the ACL request can be serviced by a page. If so, we cache
3724 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3725 * the cache. If not so, we throw away the page, and cache the required
3726 * length. The next getxattr call will then produce another round trip to
3727 * the server, this time with the input buf of the required size.
3728 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003729static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003730{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003731 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003732 struct nfs_getaclargs args = {
3733 .fh = NFS_FH(inode),
3734 .acl_pages = pages,
3735 .acl_len = buflen,
3736 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003737 struct nfs_getaclres res = {
3738 .acl_len = buflen,
3739 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003740 struct rpc_message msg = {
3741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3742 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003743 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003744 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003745 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3746 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003747
Andy Adamsonbf118a32011-12-07 11:55:27 -05003748 /* As long as we're doing a round trip to the server anyway,
3749 * let's be prepared for a page of acl data. */
3750 if (npages == 0)
3751 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003752 if (npages > ARRAY_SIZE(pages))
3753 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003754
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755 for (i = 0; i < npages; i++) {
3756 pages[i] = alloc_page(GFP_KERNEL);
3757 if (!pages[i])
3758 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003759 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003760
3761 /* for decoding across pages */
3762 res.acl_scratch = alloc_page(GFP_KERNEL);
3763 if (!res.acl_scratch)
3764 goto out_free;
3765
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766 args.acl_len = npages * PAGE_SIZE;
3767 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003768
Peng Taode040be2012-01-10 22:42:47 +08003769 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003770 __func__, buf, buflen, npages, args.acl_len);
3771 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3772 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 if (ret)
3774 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003775
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003776 /* Handle the case where the passed-in buffer is too short */
3777 if (res.acl_flags & NFS4_ACL_TRUNC) {
3778 /* Did the user only issue a request for the acl length? */
3779 if (buf == NULL)
3780 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003782 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003783 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003784 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3785 if (buf)
3786 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3787out_ok:
3788 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003789out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003790 for (i = 0; i < npages; i++)
3791 if (pages[i])
3792 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003793 if (res.acl_scratch)
3794 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003795 return ret;
3796}
3797
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003798static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3799{
3800 struct nfs4_exception exception = { };
3801 ssize_t ret;
3802 do {
3803 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3804 if (ret >= 0)
3805 break;
3806 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3807 } while (exception.retry);
3808 return ret;
3809}
3810
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003811static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3812{
3813 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003814 int ret;
3815
3816 if (!nfs4_server_supports_acls(server))
3817 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003818 ret = nfs_revalidate_inode(server, inode);
3819 if (ret < 0)
3820 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003821 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3822 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003823 ret = nfs4_read_cached_acl(inode, buf, buflen);
3824 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003825 /* -ENOENT is returned if there is no ACL or if there is an ACL
3826 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003827 return ret;
3828 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003829}
3830
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003831static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003832{
3833 struct nfs_server *server = NFS_SERVER(inode);
3834 struct page *pages[NFS4ACL_MAXPAGES];
3835 struct nfs_setaclargs arg = {
3836 .fh = NFS_FH(inode),
3837 .acl_pages = pages,
3838 .acl_len = buflen,
3839 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003840 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003841 struct rpc_message msg = {
3842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3843 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003844 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003845 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003846 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003847 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003848
3849 if (!nfs4_server_supports_acls(server))
3850 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003851 if (npages > ARRAY_SIZE(pages))
3852 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003853 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3854 if (i < 0)
3855 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003856 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003857 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003858
3859 /*
3860 * Free each page after tx, so the only ref left is
3861 * held by the network stack
3862 */
3863 for (; i > 0; i--)
3864 put_page(pages[i-1]);
3865
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003866 /*
3867 * Acl update can result in inode attribute update.
3868 * so mark the attribute cache invalid.
3869 */
3870 spin_lock(&inode->i_lock);
3871 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3872 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003873 nfs_access_zap_cache(inode);
3874 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003875 return ret;
3876}
3877
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003878static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3879{
3880 struct nfs4_exception exception = { };
3881 int err;
3882 do {
3883 err = nfs4_handle_exception(NFS_SERVER(inode),
3884 __nfs4_proc_set_acl(inode, buf, buflen),
3885 &exception);
3886 } while (exception.retry);
3887 return err;
3888}
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003891nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003893 struct nfs_client *clp = server->nfs_client;
3894
3895 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 return 0;
3897 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003898 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003899 case -NFS4ERR_ADMIN_REVOKED:
3900 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003901 if (state == NULL)
3902 break;
3903 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003904 case -NFS4ERR_OPENMODE:
3905 if (state == NULL)
3906 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003907 nfs4_schedule_stateid_recovery(server, state);
3908 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003909 case -NFS4ERR_EXPIRED:
3910 if (state != NULL)
3911 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003913 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003914 nfs4_schedule_lease_recovery(clp);
3915 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003916#if defined(CONFIG_NFS_V4_1)
3917 case -NFS4ERR_BADSESSION:
3918 case -NFS4ERR_BADSLOT:
3919 case -NFS4ERR_BAD_HIGH_SLOT:
3920 case -NFS4ERR_DEADSESSION:
3921 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3922 case -NFS4ERR_SEQ_FALSE_RETRY:
3923 case -NFS4ERR_SEQ_MISORDERED:
3924 dprintk("%s ERROR %d, Reset session\n", __func__,
3925 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003926 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003927 task->tk_status = 0;
3928 return -EAGAIN;
3929#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003931 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003932 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003933 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3935 task->tk_status = 0;
3936 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003937 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 case -NFS4ERR_OLD_STATEID:
3939 task->tk_status = 0;
3940 return -EAGAIN;
3941 }
3942 task->tk_status = nfs4_map_errors(task->tk_status);
3943 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003944wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003945 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003946 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3947 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3948 task->tk_status = 0;
3949 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
Chuck Leverf0920752012-05-21 22:45:41 -04003952static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3953 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003954{
3955 __be32 verf[2];
3956
Chuck Lever2c820d92012-05-21 22:45:33 -04003957 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3958 /* An impossible timestamp guarantees this value
3959 * will never match a generated boot time. */
3960 verf[0] = 0;
3961 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3962 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003963 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3964 verf[0] = (__be32)nn->boot_time.tv_sec;
3965 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003966 }
Chuck Levercd937102012-03-02 17:14:31 -05003967 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3968}
3969
Chuck Levere984a552012-09-14 17:24:21 -04003970static unsigned int
3971nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
3972 char *buf, size_t len)
3973{
3974 unsigned int result;
3975
3976 rcu_read_lock();
3977 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
3978 clp->cl_ipaddr,
3979 rpc_peeraddr2str(clp->cl_rpcclient,
3980 RPC_DISPLAY_ADDR),
3981 rpc_peeraddr2str(clp->cl_rpcclient,
3982 RPC_DISPLAY_PROTO));
3983 rcu_read_unlock();
3984 return result;
3985}
3986
3987static unsigned int
3988nfs4_init_uniform_client_string(const struct nfs_client *clp,
3989 char *buf, size_t len)
3990{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04003991 char *nodename = clp->cl_rpcclient->cl_nodename;
3992
3993 if (nfs4_client_id_uniquifier[0] != '\0')
3994 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04003995 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
3996 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04003997 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04003998}
3999
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004000/**
4001 * nfs4_proc_setclientid - Negotiate client ID
4002 * @clp: state data structure
4003 * @program: RPC program for NFSv4 callback service
4004 * @port: IP port number for NFS4 callback service
4005 * @cred: RPC credential to use for this call
4006 * @res: where to place the result
4007 *
4008 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4009 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004010int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4011 unsigned short port, struct rpc_cred *cred,
4012 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
4014 nfs4_verifier sc_verifier;
4015 struct nfs4_setclientid setclientid = {
4016 .sc_verifier = &sc_verifier,
4017 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004018 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 };
4020 struct rpc_message msg = {
4021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4022 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004023 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004024 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004026 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Chuck Leverde734832012-07-11 16:30:50 -04004028 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004029 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004030 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4031 setclientid.sc_name_len =
4032 nfs4_init_uniform_client_string(clp,
4033 setclientid.sc_name,
4034 sizeof(setclientid.sc_name));
4035 else
4036 setclientid.sc_name_len =
4037 nfs4_init_nonuniform_client_string(clp,
4038 setclientid.sc_name,
4039 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004040 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004041 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004042 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004043 sizeof(setclientid.sc_netid),
4044 rpc_peeraddr2str(clp->cl_rpcclient,
4045 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004046 rcu_read_unlock();
4047 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004048 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 clp->cl_ipaddr, port >> 8, port & 255);
4050
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004051 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4052 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4053 setclientid.sc_name_len, setclientid.sc_name);
4054 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4055 dprintk("NFS reply setclientid: %d\n", status);
4056 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057}
4058
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004059/**
4060 * nfs4_proc_setclientid_confirm - Confirm client ID
4061 * @clp: state data structure
4062 * @res: result of a previous SETCLIENTID
4063 * @cred: RPC credential to use for this call
4064 *
4065 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4066 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004067int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004068 struct nfs4_setclientid_res *arg,
4069 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
4071 struct nfs_fsinfo fsinfo;
4072 struct rpc_message msg = {
4073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004074 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004076 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 };
4078 unsigned long now;
4079 int status;
4080
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004081 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4082 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4083 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004085 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 if (status == 0) {
4087 spin_lock(&clp->cl_lock);
4088 clp->cl_lease_time = fsinfo.lease_time * HZ;
4089 clp->cl_last_renewal = now;
4090 spin_unlock(&clp->cl_lock);
4091 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004092 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 return status;
4094}
4095
Trond Myklebustfe650402006-01-03 09:55:18 +01004096struct nfs4_delegreturndata {
4097 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004098 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004099 struct nfs_fh fh;
4100 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004101 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004102 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004103 int rpc_status;
4104};
4105
Trond Myklebustfe650402006-01-03 09:55:18 +01004106static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4107{
4108 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004109
Trond Myklebust14516c32010-07-31 14:29:06 -04004110 if (!nfs4_sequence_done(task, &data->res.seq_res))
4111 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004112
Ricardo Labiaga79708862009-12-07 09:23:21 -05004113 switch (task->tk_status) {
4114 case -NFS4ERR_STALE_STATEID:
4115 case -NFS4ERR_EXPIRED:
4116 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004117 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004118 break;
4119 default:
4120 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4121 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004122 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004123 return;
4124 }
4125 }
4126 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004127}
4128
4129static void nfs4_delegreturn_release(void *calldata)
4130{
Trond Myklebustfe650402006-01-03 09:55:18 +01004131 kfree(calldata);
4132}
4133
Andy Adamson938e1012009-04-01 09:22:28 -04004134#if defined(CONFIG_NFS_V4_1)
4135static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4136{
4137 struct nfs4_delegreturndata *d_data;
4138
4139 d_data = (struct nfs4_delegreturndata *)data;
4140
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004141 nfs4_setup_sequence(d_data->res.server,
4142 &d_data->args.seq_args,
4143 &d_data->res.seq_res,
4144 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004145}
4146#endif /* CONFIG_NFS_V4_1 */
4147
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004148static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004149#if defined(CONFIG_NFS_V4_1)
4150 .rpc_call_prepare = nfs4_delegreturn_prepare,
4151#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004152 .rpc_call_done = nfs4_delegreturn_done,
4153 .rpc_release = nfs4_delegreturn_release,
4154};
4155
Trond Myklebuste6f81072008-01-24 18:14:34 -05004156static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004157{
4158 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004159 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004160 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004161 struct rpc_message msg = {
4162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4163 .rpc_cred = cred,
4164 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004165 struct rpc_task_setup task_setup_data = {
4166 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004167 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004168 .callback_ops = &nfs4_delegreturn_ops,
4169 .flags = RPC_TASK_ASYNC,
4170 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004171 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004172
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004173 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004174 if (data == NULL)
4175 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004176 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004177 data->args.fhandle = &data->fh;
4178 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004179 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004180 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004181 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004182 data->res.fattr = &data->fattr;
4183 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004184 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004185 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004186 data->rpc_status = 0;
4187
Trond Myklebustc970aa82007-07-14 15:39:59 -04004188 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004189 msg.rpc_argp = &data->args;
4190 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004191 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004192 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004193 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004194 if (!issync)
4195 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004196 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004197 if (status != 0)
4198 goto out;
4199 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004200 if (status == 0)
4201 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4202 else
4203 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004204out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004205 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004206 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207}
4208
Trond Myklebuste6f81072008-01-24 18:14:34 -05004209int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
4211 struct nfs_server *server = NFS_SERVER(inode);
4212 struct nfs4_exception exception = { };
4213 int err;
4214 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004215 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 switch (err) {
4217 case -NFS4ERR_STALE_STATEID:
4218 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 case 0:
4220 return 0;
4221 }
4222 err = nfs4_handle_exception(server, err, &exception);
4223 } while (exception.retry);
4224 return err;
4225}
4226
4227#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4228#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4229
4230/*
4231 * sleep, with exponential backoff, and retry the LOCK operation.
4232 */
4233static unsigned long
4234nfs4_set_lock_task_retry(unsigned long timeout)
4235{
Jeff Laytond3103102011-12-01 22:44:39 +01004236 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 timeout <<= 1;
4238 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4239 return NFS4_LOCK_MAXTIMEOUT;
4240 return timeout;
4241}
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4244{
4245 struct inode *inode = state->inode;
4246 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004247 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004248 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004250 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004252 struct nfs_lockt_res res = {
4253 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 };
4255 struct rpc_message msg = {
4256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4257 .rpc_argp = &arg,
4258 .rpc_resp = &res,
4259 .rpc_cred = state->owner->so_cred,
4260 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 struct nfs4_lock_state *lsp;
4262 int status;
4263
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004264 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004265 status = nfs4_set_lock_state(state, request);
4266 if (status != 0)
4267 goto out;
4268 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004269 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004270 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004271 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004272 switch (status) {
4273 case 0:
4274 request->fl_type = F_UNLCK;
4275 break;
4276 case -NFS4ERR_DENIED:
4277 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004279 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004280out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return status;
4282}
4283
4284static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4285{
4286 struct nfs4_exception exception = { };
4287 int err;
4288
4289 do {
4290 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4291 _nfs4_proc_getlk(state, cmd, request),
4292 &exception);
4293 } while (exception.retry);
4294 return err;
4295}
4296
4297static int do_vfs_lock(struct file *file, struct file_lock *fl)
4298{
4299 int res = 0;
4300 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4301 case FL_POSIX:
4302 res = posix_lock_file_wait(file, fl);
4303 break;
4304 case FL_FLOCK:
4305 res = flock_lock_file_wait(file, fl);
4306 break;
4307 default:
4308 BUG();
4309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return res;
4311}
4312
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004313struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004314 struct nfs_locku_args arg;
4315 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316 struct nfs4_lock_state *lsp;
4317 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004318 struct file_lock fl;
4319 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004320 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321};
4322
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004323static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4324 struct nfs_open_context *ctx,
4325 struct nfs4_lock_state *lsp,
4326 struct nfs_seqid *seqid)
4327{
4328 struct nfs4_unlockdata *p;
4329 struct inode *inode = lsp->ls_state->inode;
4330
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004331 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004332 if (p == NULL)
4333 return NULL;
4334 p->arg.fh = NFS_FH(inode);
4335 p->arg.fl = &p->fl;
4336 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004337 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004338 p->arg.stateid = &lsp->ls_stateid;
4339 p->lsp = lsp;
4340 atomic_inc(&lsp->ls_count);
4341 /* Ensure we don't close file until we're done freeing locks! */
4342 p->ctx = get_nfs_open_context(ctx);
4343 memcpy(&p->fl, fl, sizeof(p->fl));
4344 p->server = NFS_SERVER(inode);
4345 return p;
4346}
4347
Trond Myklebust06f814a2006-01-03 09:55:07 +01004348static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004349{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004350 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004351 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004352 nfs4_put_lock_state(calldata->lsp);
4353 put_nfs_open_context(calldata->ctx);
4354 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004355}
4356
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004357static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004358{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004359 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004360
Trond Myklebust14516c32010-07-31 14:29:06 -04004361 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4362 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004363 switch (task->tk_status) {
4364 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004365 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4366 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004367 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004368 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004369 case -NFS4ERR_BAD_STATEID:
4370 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004371 case -NFS4ERR_STALE_STATEID:
4372 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004373 break;
4374 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004375 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004376 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004377 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004378 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004379}
4380
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004381static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004382{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004383 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004385 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004386 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004387 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004388 /* Note: exit _without_ running nfs4_locku_done */
4389 task->tk_action = NULL;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05004390 nfs4_sequence_done(task, &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391 return;
4392 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004393 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004394 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004395 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004396 &calldata->res.seq_res,
4397 task) != 0)
4398 nfs_release_seqid(calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004401static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004402 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004403 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004404 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004405};
4406
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004407static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4408 struct nfs_open_context *ctx,
4409 struct nfs4_lock_state *lsp,
4410 struct nfs_seqid *seqid)
4411{
4412 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004413 struct rpc_message msg = {
4414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4415 .rpc_cred = ctx->cred,
4416 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004417 struct rpc_task_setup task_setup_data = {
4418 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004419 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004420 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004421 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004422 .flags = RPC_TASK_ASYNC,
4423 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004424
Frank Filz137d6ac2007-07-09 15:32:29 -07004425 /* Ensure this is an unlock - when canceling a lock, the
4426 * canceled lock is passed in, and it won't be an unlock.
4427 */
4428 fl->fl_type = F_UNLCK;
4429
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004430 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4431 if (data == NULL) {
4432 nfs_free_seqid(seqid);
4433 return ERR_PTR(-ENOMEM);
4434 }
4435
Trond Myklebust9d12b212012-01-17 22:04:25 -05004436 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004437 msg.rpc_argp = &data->arg;
4438 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004439 task_setup_data.callback_data = data;
4440 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004441}
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4444{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004445 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004446 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004447 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004448 struct rpc_task *task;
4449 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004450 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Trond Myklebust9b073572006-06-29 16:38:34 -04004452 status = nfs4_set_lock_state(state, request);
4453 /* Unlock _before_ we do the RPC call */
4454 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004455 down_read(&nfsi->rwsem);
4456 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4457 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004458 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004459 }
4460 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004461 if (status != 0)
4462 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004463 /* Is this a delegated lock? */
4464 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004465 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004466 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004467 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004468 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004469 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004470 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004471 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004472 status = PTR_ERR(task);
4473 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004474 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004476 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004477out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004478 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004479 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480}
4481
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004482struct nfs4_lockdata {
4483 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004484 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485 struct nfs4_lock_state *lsp;
4486 struct nfs_open_context *ctx;
4487 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004488 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004489 int rpc_status;
4490 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004491 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004492};
4493
4494static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004495 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4496 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004497{
4498 struct nfs4_lockdata *p;
4499 struct inode *inode = lsp->ls_state->inode;
4500 struct nfs_server *server = NFS_SERVER(inode);
4501
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004502 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503 if (p == NULL)
4504 return NULL;
4505
4506 p->arg.fh = NFS_FH(inode);
4507 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004508 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004509 if (p->arg.open_seqid == NULL)
4510 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004511 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004513 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004515 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004516 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004517 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004518 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004520 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004521 atomic_inc(&lsp->ls_count);
4522 p->ctx = get_nfs_open_context(ctx);
4523 memcpy(&p->fl, fl, sizeof(p->fl));
4524 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004525out_free_seqid:
4526 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527out_free:
4528 kfree(p);
4529 return NULL;
4530}
4531
4532static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4533{
4534 struct nfs4_lockdata *data = calldata;
4535 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
Harvey Harrison3110ff82008-05-02 13:42:44 -07004537 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004538 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4539 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 /* Do we need to do an open_to_lock_owner? */
4541 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004542 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004543 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004544 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004545 data->arg.open_stateid = &state->stateid;
4546 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004547 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004548 } else
4549 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004550 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004551 if (nfs4_setup_sequence(data->server,
4552 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004553 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004554 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004555 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004556 nfs_release_seqid(data->arg.open_seqid);
4557out_release_lock_seqid:
4558 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004559 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004560}
4561
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4563{
4564 struct nfs4_lockdata *data = calldata;
4565
Harvey Harrison3110ff82008-05-02 13:42:44 -07004566 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567
Trond Myklebust14516c32010-07-31 14:29:06 -04004568 if (!nfs4_sequence_done(task, &data->res.seq_res))
4569 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004570
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004571 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004572 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004573 if (data->rpc_status == 0)
4574 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4575 else
4576 goto out;
4577 }
4578 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004579 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004580 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004581 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004583out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004584 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585}
4586
4587static void nfs4_lock_release(void *calldata)
4588{
4589 struct nfs4_lockdata *data = calldata;
4590
Harvey Harrison3110ff82008-05-02 13:42:44 -07004591 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004592 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593 if (data->cancelled != 0) {
4594 struct rpc_task *task;
4595 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4596 data->arg.lock_seqid);
4597 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004598 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004599 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004600 } else
4601 nfs_free_seqid(data->arg.lock_seqid);
4602 nfs4_put_lock_state(data->lsp);
4603 put_nfs_open_context(data->ctx);
4604 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004605 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606}
4607
4608static const struct rpc_call_ops nfs4_lock_ops = {
4609 .rpc_call_prepare = nfs4_lock_prepare,
4610 .rpc_call_done = nfs4_lock_done,
4611 .rpc_release = nfs4_lock_release,
4612};
4613
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004614static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4615{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004616 switch (error) {
4617 case -NFS4ERR_ADMIN_REVOKED:
4618 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004619 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004620 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004621 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004622 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004623 break;
4624 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004625 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004626 case -NFS4ERR_EXPIRED:
4627 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004628 };
4629}
4630
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004631static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632{
4633 struct nfs4_lockdata *data;
4634 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004635 struct rpc_message msg = {
4636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4637 .rpc_cred = state->owner->so_cred,
4638 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004639 struct rpc_task_setup task_setup_data = {
4640 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004641 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004642 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004643 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004644 .flags = RPC_TASK_ASYNC,
4645 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646 int ret;
4647
Harvey Harrison3110ff82008-05-02 13:42:44 -07004648 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004649 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004650 fl->fl_u.nfs4_fl.owner,
4651 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652 if (data == NULL)
4653 return -ENOMEM;
4654 if (IS_SETLKW(cmd))
4655 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004656 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004657 msg.rpc_argp = &data->arg;
4658 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004659 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004660 if (recovery_type > NFS_LOCK_NEW) {
4661 if (recovery_type == NFS_LOCK_RECLAIM)
4662 data->arg.reclaim = NFS_LOCK_RECLAIM;
4663 nfs4_set_sequence_privileged(&data->arg.seq_args);
4664 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004665 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004666 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004667 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668 ret = nfs4_wait_for_completion_rpc_task(task);
4669 if (ret == 0) {
4670 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004671 if (ret)
4672 nfs4_handle_setlk_error(data->server, data->lsp,
4673 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004674 } else
4675 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004676 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004677 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679}
4680
4681static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4682{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004683 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004684 struct nfs4_exception exception = {
4685 .inode = state->inode,
4686 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004687 int err;
4688
4689 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004690 /* Cache the lock if possible... */
4691 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4692 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004693 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004694 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004695 break;
4696 nfs4_handle_exception(server, err, &exception);
4697 } while (exception.retry);
4698 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699}
4700
4701static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4702{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004703 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004704 struct nfs4_exception exception = {
4705 .inode = state->inode,
4706 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004707 int err;
4708
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004709 err = nfs4_set_lock_state(state, request);
4710 if (err != 0)
4711 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004712 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004713 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4714 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004715 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004716 switch (err) {
4717 default:
4718 goto out;
4719 case -NFS4ERR_GRACE:
4720 case -NFS4ERR_DELAY:
4721 nfs4_handle_exception(server, err, &exception);
4722 err = 0;
4723 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004724 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004725out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004726 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004729#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004730/**
4731 * nfs41_check_expired_locks - possibly free a lock stateid
4732 *
4733 * @state: NFSv4 state for an inode
4734 *
4735 * Returns NFS_OK if recovery for this stateid is now finished.
4736 * Otherwise a negative NFS4ERR value is returned.
4737 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004738static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004739{
Chuck Levereb64cf92012-07-11 16:30:05 -04004740 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004741 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004742 struct nfs_server *server = NFS_SERVER(state->inode);
4743
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004744 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004745 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004746 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4747 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004748 /* Free the stateid unless the server
4749 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004750 if (status != -NFS4ERR_BAD_STATEID)
4751 nfs41_free_stateid(server,
4752 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004753 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004754 ret = status;
4755 }
4756 }
4757 };
4758
4759 return ret;
4760}
4761
4762static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4763{
4764 int status = NFS_OK;
4765
4766 if (test_bit(LK_STATE_IN_USE, &state->flags))
4767 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004768 if (status != NFS_OK)
4769 status = nfs4_lock_expired(state, request);
4770 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004771}
4772#endif
4773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4775{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004776 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004777 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004778 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004780 if ((fl_flags & FL_POSIX) &&
4781 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4782 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004783 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004784 status = nfs4_set_lock_state(state, request);
4785 if (status != 0)
4786 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004787 request->fl_flags |= FL_ACCESS;
4788 status = do_vfs_lock(request->fl_file, request);
4789 if (status < 0)
4790 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004791 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004792 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004793 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004794 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004795 request->fl_flags = fl_flags & ~FL_SLEEP;
4796 status = do_vfs_lock(request->fl_file, request);
4797 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004798 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004799 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004800 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004801 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004802 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004803 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004804 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004805 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4806 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004807out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004808 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004809out:
4810 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 return status;
4812}
4813
4814static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4815{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004816 struct nfs4_exception exception = {
4817 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004818 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004819 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 int err;
4821
4822 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004823 err = _nfs4_proc_setlk(state, cmd, request);
4824 if (err == -NFS4ERR_DENIED)
4825 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004827 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 } while (exception.retry);
4829 return err;
4830}
4831
4832static int
4833nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4834{
4835 struct nfs_open_context *ctx;
4836 struct nfs4_state *state;
4837 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4838 int status;
4839
4840 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004841 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 state = ctx->state;
4843
4844 if (request->fl_start < 0 || request->fl_end < 0)
4845 return -EINVAL;
4846
Trond Myklebustd9531262009-07-21 19:22:38 -04004847 if (IS_GETLK(cmd)) {
4848 if (state != NULL)
4849 return nfs4_proc_getlk(state, F_GETLK, request);
4850 return 0;
4851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
4853 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4854 return -EINVAL;
4855
Trond Myklebustd9531262009-07-21 19:22:38 -04004856 if (request->fl_type == F_UNLCK) {
4857 if (state != NULL)
4858 return nfs4_proc_unlck(state, cmd, request);
4859 return 0;
4860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Trond Myklebustd9531262009-07-21 19:22:38 -04004862 if (state == NULL)
4863 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004864 /*
4865 * Don't rely on the VFS having checked the file open mode,
4866 * since it won't do this for flock() locks.
4867 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004868 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004869 case F_RDLCK:
4870 if (!(filp->f_mode & FMODE_READ))
4871 return -EBADF;
4872 break;
4873 case F_WRLCK:
4874 if (!(filp->f_mode & FMODE_WRITE))
4875 return -EBADF;
4876 }
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 do {
4879 status = nfs4_proc_setlk(state, cmd, request);
4880 if ((status != -EAGAIN) || IS_SETLK(cmd))
4881 break;
4882 timeout = nfs4_set_lock_task_retry(timeout);
4883 status = -ERESTARTSYS;
4884 if (signalled())
4885 break;
4886 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 return status;
4888}
4889
Trond Myklebust888e6942005-11-04 15:38:11 -05004890int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4891{
4892 struct nfs_server *server = NFS_SERVER(state->inode);
4893 struct nfs4_exception exception = { };
4894 int err;
4895
4896 err = nfs4_set_lock_state(state, fl);
4897 if (err != 0)
4898 goto out;
4899 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004900 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004901 switch (err) {
4902 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004903 printk(KERN_ERR "NFS: %s: unhandled error "
4904 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004905 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004906 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004907 goto out;
4908 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004909 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004910 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004911 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004912 nfs4_schedule_lease_recovery(server->nfs_client);
4913 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004914 case -NFS4ERR_BADSESSION:
4915 case -NFS4ERR_BADSLOT:
4916 case -NFS4ERR_BAD_HIGH_SLOT:
4917 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4918 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004919 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004920 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004921 case -ERESTARTSYS:
4922 /*
4923 * The show must go on: exit, but mark the
4924 * stateid as needing recovery.
4925 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004926 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004927 case -NFS4ERR_ADMIN_REVOKED:
4928 case -NFS4ERR_BAD_STATEID:
4929 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004930 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004931 err = 0;
4932 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004933 case -EKEYEXPIRED:
4934 /*
4935 * User RPCSEC_GSS context has expired.
4936 * We cannot recover this stateid now, so
4937 * skip it and allow recovery thread to
4938 * proceed.
4939 */
4940 err = 0;
4941 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004942 case -ENOMEM:
4943 case -NFS4ERR_DENIED:
4944 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4945 err = 0;
4946 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004947 case -NFS4ERR_DELAY:
4948 break;
4949 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004950 err = nfs4_handle_exception(server, err, &exception);
4951 } while (exception.retry);
4952out:
4953 return err;
4954}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004955
Trond Myklebustcf470c32012-03-07 13:49:12 -05004956struct nfs_release_lockowner_data {
4957 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004958 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004959 struct nfs_release_lockowner_args args;
4960};
4961
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004962static void nfs4_release_lockowner_release(void *calldata)
4963{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004964 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004965 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004966 kfree(calldata);
4967}
4968
Trond Myklebust17280172012-03-11 13:11:00 -04004969static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004970 .rpc_release = nfs4_release_lockowner_release,
4971};
4972
Trond Myklebustcf470c32012-03-07 13:49:12 -05004973int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004974{
4975 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004976 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004977 struct rpc_message msg = {
4978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4979 };
4980
4981 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004982 return -EINVAL;
4983 data = kmalloc(sizeof(*data), GFP_NOFS);
4984 if (!data)
4985 return -ENOMEM;
4986 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004987 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004988 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4989 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4990 data->args.lock_owner.s_dev = server->s_dev;
4991 msg.rpc_argp = &data->args;
4992 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4993 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004994}
4995
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004996#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4997
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004998static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4999 const void *buf, size_t buflen,
5000 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005001{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005002 if (strcmp(key, "") != 0)
5003 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005004
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005005 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005006}
5007
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005008static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5009 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005010{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005011 if (strcmp(key, "") != 0)
5012 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005013
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005014 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005015}
5016
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005017static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5018 size_t list_len, const char *name,
5019 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005020{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005021 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005022
J. Bruce Fields096455a2006-03-20 23:23:42 -05005023 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5024 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005025
5026 if (list && len <= list_len)
5027 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005028 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005029}
5030
Andy Adamson533eb462011-06-13 18:25:56 -04005031/*
5032 * nfs_fhget will use either the mounted_on_fileid or the fileid
5033 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005034static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5035{
Andy Adamson533eb462011-06-13 18:25:56 -04005036 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5037 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5038 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005039 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005040 return;
5041
5042 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005043 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005044 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5045 fattr->nlink = 2;
5046}
5047
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005048static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5049 const struct qstr *name,
5050 struct nfs4_fs_locations *fs_locations,
5051 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005052{
5053 struct nfs_server *server = NFS_SERVER(dir);
5054 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005055 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005056 };
5057 struct nfs4_fs_locations_arg args = {
5058 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005059 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005060 .page = page,
5061 .bitmask = bitmask,
5062 };
Benny Halevy22958462009-04-01 09:22:02 -04005063 struct nfs4_fs_locations_res res = {
5064 .fs_locations = fs_locations,
5065 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005066 struct rpc_message msg = {
5067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5068 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005069 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005070 };
5071 int status;
5072
Harvey Harrison3110ff82008-05-02 13:42:44 -07005073 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005074
5075 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5076 * is not supported */
5077 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5078 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5079 else
5080 bitmask[0] |= FATTR4_WORD0_FILEID;
5081
Trond Myklebustc228fd32007-01-13 02:28:11 -05005082 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005083 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005084 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005085 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005086 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005087 return status;
5088}
5089
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005090int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5091 const struct qstr *name,
5092 struct nfs4_fs_locations *fs_locations,
5093 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005094{
5095 struct nfs4_exception exception = { };
5096 int err;
5097 do {
5098 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005099 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005100 &exception);
5101 } while (exception.retry);
5102 return err;
5103}
5104
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005105static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5106{
5107 int status;
5108 struct nfs4_secinfo_arg args = {
5109 .dir_fh = NFS_FH(dir),
5110 .name = name,
5111 };
5112 struct nfs4_secinfo_res res = {
5113 .flavors = flavors,
5114 };
5115 struct rpc_message msg = {
5116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5117 .rpc_argp = &args,
5118 .rpc_resp = &res,
5119 };
5120
5121 dprintk("NFS call secinfo %s\n", name->name);
5122 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5123 dprintk("NFS reply secinfo: %d\n", status);
5124 return status;
5125}
5126
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005127int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5128 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005129{
5130 struct nfs4_exception exception = { };
5131 int err;
5132 do {
5133 err = nfs4_handle_exception(NFS_SERVER(dir),
5134 _nfs4_proc_secinfo(dir, name, flavors),
5135 &exception);
5136 } while (exception.retry);
5137 return err;
5138}
5139
Andy Adamson557134a2009-04-01 09:21:53 -04005140#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005141/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005142 * Check the exchange flags returned by the server for invalid flags, having
5143 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5144 * DS flags set.
5145 */
5146static int nfs4_check_cl_exchange_flags(u32 flags)
5147{
5148 if (flags & ~EXCHGID4_FLAG_MASK_R)
5149 goto out_inval;
5150 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5151 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5152 goto out_inval;
5153 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5154 goto out_inval;
5155 return NFS_OK;
5156out_inval:
5157 return -NFS4ERR_INVAL;
5158}
5159
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005160static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005161nfs41_same_server_scope(struct nfs41_server_scope *a,
5162 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005163{
5164 if (a->server_scope_sz == b->server_scope_sz &&
5165 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5166 return true;
5167
5168 return false;
5169}
5170
Andy Adamson357f54d2010-12-14 10:11:57 -05005171/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005172 * nfs4_proc_bind_conn_to_session()
5173 *
5174 * The 4.1 client currently uses the same TCP connection for the
5175 * fore and backchannel.
5176 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005177int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005178{
5179 int status;
5180 struct nfs41_bind_conn_to_session_res res;
5181 struct rpc_message msg = {
5182 .rpc_proc =
5183 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5184 .rpc_argp = clp,
5185 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005186 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005187 };
5188
5189 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005190
5191 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5192 if (unlikely(res.session == NULL)) {
5193 status = -ENOMEM;
5194 goto out;
5195 }
5196
5197 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5198 if (status == 0) {
5199 if (memcmp(res.session->sess_id.data,
5200 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5201 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5202 status = -EIO;
5203 goto out_session;
5204 }
5205 if (res.dir != NFS4_CDFS4_BOTH) {
5206 dprintk("NFS: %s: Unexpected direction from server\n",
5207 __func__);
5208 status = -EIO;
5209 goto out_session;
5210 }
5211 if (res.use_conn_in_rdma_mode) {
5212 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5213 __func__);
5214 status = -EIO;
5215 goto out_session;
5216 }
5217 }
5218out_session:
5219 kfree(res.session);
5220out:
5221 dprintk("<-- %s status= %d\n", __func__, status);
5222 return status;
5223}
5224
5225/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005226 * nfs4_proc_exchange_id()
5227 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005228 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5229 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005230 * Since the clientid has expired, all compounds using sessions
5231 * associated with the stale clientid will be returning
5232 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5233 * be in some phase of session reset.
5234 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005235int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005236{
5237 nfs4_verifier verifier;
5238 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005239 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005240 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005241 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005242 };
5243 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005244 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005245 };
5246 int status;
5247 struct rpc_message msg = {
5248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5249 .rpc_argp = &args,
5250 .rpc_resp = &res,
5251 .rpc_cred = cred,
5252 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005253
Chuck Leverf0920752012-05-21 22:45:41 -04005254 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005255 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5256 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005257 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5258 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5259 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005260
Chuck Leveracdeb692012-05-21 22:46:16 -04005261 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005262 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005263 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005264 status = -ENOMEM;
5265 goto out;
5266 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005267
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005268 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005269 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005270 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005271 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005272 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005273 }
5274
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005275 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005276 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005277 status = -ENOMEM;
5278 goto out_server_scope;
5279 }
5280
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005281 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005282 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005283 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005284
Chuck Lever177313f2012-05-21 22:44:58 -04005285 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005286 clp->cl_clientid = res.clientid;
5287 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5288 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5289 clp->cl_seqid = res.seqid;
5290
Chuck Leveracdeb692012-05-21 22:46:16 -04005291 kfree(clp->cl_serverowner);
5292 clp->cl_serverowner = res.server_owner;
5293 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005294
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005295 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005296 kfree(clp->cl_implid);
5297 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005298
Chuck Lever177313f2012-05-21 22:44:58 -04005299 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005300 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005301 res.server_scope)) {
5302 dprintk("%s: server_scope mismatch detected\n",
5303 __func__);
5304 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005305 kfree(clp->cl_serverscope);
5306 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005307 }
5308
Chuck Lever177313f2012-05-21 22:44:58 -04005309 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005310 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005311 goto out;
5312 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005313 } else
5314 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005315
Chuck Leveracdeb692012-05-21 22:46:16 -04005316out_server_owner:
5317 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005318out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005319 kfree(res.server_scope);
5320out:
Chuck Lever177313f2012-05-21 22:44:58 -04005321 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005322 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005323 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005324 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005325 clp->cl_implid->date.seconds,
5326 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005327 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005328 return status;
5329}
5330
Trond Myklebust66245532012-05-25 17:18:09 -04005331static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5332 struct rpc_cred *cred)
5333{
5334 struct rpc_message msg = {
5335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5336 .rpc_argp = clp,
5337 .rpc_cred = cred,
5338 };
5339 int status;
5340
5341 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5342 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005343 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005344 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5345 return status;
5346}
5347
5348static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5349 struct rpc_cred *cred)
5350{
5351 unsigned int loop;
5352 int ret;
5353
5354 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5355 ret = _nfs4_proc_destroy_clientid(clp, cred);
5356 switch (ret) {
5357 case -NFS4ERR_DELAY:
5358 case -NFS4ERR_CLIENTID_BUSY:
5359 ssleep(1);
5360 break;
5361 default:
5362 return ret;
5363 }
5364 }
5365 return 0;
5366}
5367
5368int nfs4_destroy_clientid(struct nfs_client *clp)
5369{
5370 struct rpc_cred *cred;
5371 int ret = 0;
5372
5373 if (clp->cl_mvops->minor_version < 1)
5374 goto out;
5375 if (clp->cl_exchange_flags == 0)
5376 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005377 if (clp->cl_preserve_clid)
5378 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005379 cred = nfs4_get_exchange_id_cred(clp);
5380 ret = nfs4_proc_destroy_clientid(clp, cred);
5381 if (cred)
5382 put_rpccred(cred);
5383 switch (ret) {
5384 case 0:
5385 case -NFS4ERR_STALE_CLIENTID:
5386 clp->cl_exchange_flags = 0;
5387 }
5388out:
5389 return ret;
5390}
5391
Andy Adamson2050f0c2009-04-01 09:22:30 -04005392struct nfs4_get_lease_time_data {
5393 struct nfs4_get_lease_time_args *args;
5394 struct nfs4_get_lease_time_res *res;
5395 struct nfs_client *clp;
5396};
5397
5398static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5399 void *calldata)
5400{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005401 struct nfs4_get_lease_time_data *data =
5402 (struct nfs4_get_lease_time_data *)calldata;
5403
5404 dprintk("--> %s\n", __func__);
5405 /* just setup sequence, do not trigger session recovery
5406 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005407 nfs41_setup_sequence(data->clp->cl_session,
5408 &data->args->la_seq_args,
5409 &data->res->lr_seq_res,
5410 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005411 dprintk("<-- %s\n", __func__);
5412}
5413
5414/*
5415 * Called from nfs4_state_manager thread for session setup, so don't recover
5416 * from sequence operation or clientid errors.
5417 */
5418static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5419{
5420 struct nfs4_get_lease_time_data *data =
5421 (struct nfs4_get_lease_time_data *)calldata;
5422
5423 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005424 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5425 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005426 switch (task->tk_status) {
5427 case -NFS4ERR_DELAY:
5428 case -NFS4ERR_GRACE:
5429 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5430 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5431 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005432 /* fall through */
5433 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005434 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005435 return;
5436 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005437 dprintk("<-- %s\n", __func__);
5438}
5439
Trond Myklebust17280172012-03-11 13:11:00 -04005440static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005441 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5442 .rpc_call_done = nfs4_get_lease_time_done,
5443};
5444
5445int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5446{
5447 struct rpc_task *task;
5448 struct nfs4_get_lease_time_args args;
5449 struct nfs4_get_lease_time_res res = {
5450 .lr_fsinfo = fsinfo,
5451 };
5452 struct nfs4_get_lease_time_data data = {
5453 .args = &args,
5454 .res = &res,
5455 .clp = clp,
5456 };
5457 struct rpc_message msg = {
5458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5459 .rpc_argp = &args,
5460 .rpc_resp = &res,
5461 };
5462 struct rpc_task_setup task_setup = {
5463 .rpc_client = clp->cl_rpcclient,
5464 .rpc_message = &msg,
5465 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005466 .callback_data = &data,
5467 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005468 };
5469 int status;
5470
Trond Myklebust9d12b212012-01-17 22:04:25 -05005471 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005472 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005473 dprintk("--> %s\n", __func__);
5474 task = rpc_run_task(&task_setup);
5475
5476 if (IS_ERR(task))
5477 status = PTR_ERR(task);
5478 else {
5479 status = task->tk_status;
5480 rpc_put_task(task);
5481 }
5482 dprintk("<-- %s return %d\n", __func__, status);
5483
5484 return status;
5485}
5486
Andy Adamsonfc931582009-04-01 09:22:31 -04005487/*
5488 * Initialize the values to be used by the client in CREATE_SESSION
5489 * If nfs4_init_session set the fore channel request and response sizes,
5490 * use them.
5491 *
5492 * Set the back channel max_resp_sz_cached to zero to force the client to
5493 * always set csa_cachethis to FALSE because the current implementation
5494 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5495 */
5496static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5497{
5498 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005499 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5500 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005501
5502 if (mxrqst_sz == 0)
5503 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5504 if (mxresp_sz == 0)
5505 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5506 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005507 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5508 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005509 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005510 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005511
5512 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005513 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005514 __func__,
5515 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005516 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005517
5518 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005519 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5520 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5521 args->bc_attrs.max_resp_sz_cached = 0;
5522 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5523 args->bc_attrs.max_reqs = 1;
5524
5525 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5526 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5527 __func__,
5528 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5529 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5530 args->bc_attrs.max_reqs);
5531}
5532
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005533static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005534{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005535 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5536 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5537
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005538 if (rcvd->max_resp_sz > sent->max_resp_sz)
5539 return -EINVAL;
5540 /*
5541 * Our requested max_ops is the minimum we need; we're not
5542 * prepared to break up compounds into smaller pieces than that.
5543 * So, no point even trying to continue if the server won't
5544 * cooperate:
5545 */
5546 if (rcvd->max_ops < sent->max_ops)
5547 return -EINVAL;
5548 if (rcvd->max_reqs == 0)
5549 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005550 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5551 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005552 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005553}
5554
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005555static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5556{
5557 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5558 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005559
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005560 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5561 return -EINVAL;
5562 if (rcvd->max_resp_sz < sent->max_resp_sz)
5563 return -EINVAL;
5564 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5565 return -EINVAL;
5566 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005567 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005568 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005569 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005570 return -EINVAL;
5571 return 0;
5572}
Andy Adamson8d353012009-04-01 09:22:32 -04005573
Andy Adamson8d353012009-04-01 09:22:32 -04005574static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5575 struct nfs4_session *session)
5576{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005577 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005578
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005579 ret = nfs4_verify_fore_channel_attrs(args, session);
5580 if (ret)
5581 return ret;
5582 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005583}
5584
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005585static int _nfs4_proc_create_session(struct nfs_client *clp,
5586 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005587{
5588 struct nfs4_session *session = clp->cl_session;
5589 struct nfs41_create_session_args args = {
5590 .client = clp,
5591 .cb_program = NFS4_CALLBACK,
5592 };
5593 struct nfs41_create_session_res res = {
5594 .client = clp,
5595 };
5596 struct rpc_message msg = {
5597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5598 .rpc_argp = &args,
5599 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005600 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005601 };
5602 int status;
5603
5604 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005605 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005606
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005607 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005608
Trond Myklebust43095d32012-11-20 11:13:12 -05005609 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005610 /* Verify the session's negotiated channel_attrs values */
5611 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005612 /* Increment the clientid slot sequence id */
5613 clp->cl_seqid++;
5614 }
5615
5616 return status;
5617}
5618
5619/*
5620 * Issues a CREATE_SESSION operation to the server.
5621 * It is the responsibility of the caller to verify the session is
5622 * expired before calling this routine.
5623 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005624int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005625{
5626 int status;
5627 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005628 struct nfs4_session *session = clp->cl_session;
5629
5630 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5631
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005632 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005633 if (status)
5634 goto out;
5635
Andy Adamsonaacd5532011-11-09 13:58:21 -05005636 /* Init or reset the session slot tables */
5637 status = nfs4_setup_session_slot_tables(session);
5638 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005639 if (status)
5640 goto out;
5641
5642 ptr = (unsigned *)&session->sess_id.data[0];
5643 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5644 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005645out:
5646 dprintk("<-- %s\n", __func__);
5647 return status;
5648}
5649
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005650/*
5651 * Issue the over-the-wire RPC DESTROY_SESSION.
5652 * The caller must serialize access to this routine.
5653 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005654int nfs4_proc_destroy_session(struct nfs4_session *session,
5655 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005656{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005657 struct rpc_message msg = {
5658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5659 .rpc_argp = session,
5660 .rpc_cred = cred,
5661 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005662 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005663
5664 dprintk("--> nfs4_proc_destroy_session\n");
5665
5666 /* session is still being setup */
5667 if (session->clp->cl_cons_state != NFS_CS_READY)
5668 return status;
5669
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005670 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005671
5672 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005673 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005674 "Session has been destroyed regardless...\n", status);
5675
5676 dprintk("<-- nfs4_proc_destroy_session\n");
5677 return status;
5678}
5679
Trond Myklebust7b38c362012-05-23 13:23:31 -04005680/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005681 * Renew the cl_session lease.
5682 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005683struct nfs4_sequence_data {
5684 struct nfs_client *clp;
5685 struct nfs4_sequence_args args;
5686 struct nfs4_sequence_res res;
5687};
5688
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005689static void nfs41_sequence_release(void *data)
5690{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005691 struct nfs4_sequence_data *calldata = data;
5692 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005693
Alexandros Batsakis71358402010-02-05 03:45:05 -08005694 if (atomic_read(&clp->cl_count) > 1)
5695 nfs4_schedule_state_renewal(clp);
5696 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005697 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005698}
5699
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005700static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5701{
5702 switch(task->tk_status) {
5703 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005704 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5705 return -EAGAIN;
5706 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005707 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005708 }
5709 return 0;
5710}
5711
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005712static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005713{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005714 struct nfs4_sequence_data *calldata = data;
5715 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005716
Trond Myklebust14516c32010-07-31 14:29:06 -04005717 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5718 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005719
5720 if (task->tk_status < 0) {
5721 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005722 if (atomic_read(&clp->cl_count) == 1)
5723 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005724
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005725 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5726 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005727 return;
5728 }
5729 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005730 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005731out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005732 dprintk("<-- %s\n", __func__);
5733}
5734
5735static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5736{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005737 struct nfs4_sequence_data *calldata = data;
5738 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005739 struct nfs4_sequence_args *args;
5740 struct nfs4_sequence_res *res;
5741
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005742 args = task->tk_msg.rpc_argp;
5743 res = task->tk_msg.rpc_resp;
5744
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005745 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005746}
5747
5748static const struct rpc_call_ops nfs41_sequence_ops = {
5749 .rpc_call_done = nfs41_sequence_call_done,
5750 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005751 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005752};
5753
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005754static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5755 struct rpc_cred *cred,
5756 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005757{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005758 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005759 struct rpc_message msg = {
5760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5761 .rpc_cred = cred,
5762 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005763 struct rpc_task_setup task_setup_data = {
5764 .rpc_client = clp->cl_rpcclient,
5765 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005766 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005767 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5768 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005769
Alexandros Batsakis71358402010-02-05 03:45:05 -08005770 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005771 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005772 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005773 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005774 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005775 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005776 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005777 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005778 if (is_privileged)
5779 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005780 msg.rpc_argp = &calldata->args;
5781 msg.rpc_resp = &calldata->res;
5782 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005783 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005784
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005785 return rpc_run_task(&task_setup_data);
5786}
5787
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005788static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005789{
5790 struct rpc_task *task;
5791 int ret = 0;
5792
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005793 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5794 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005795 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005796 if (IS_ERR(task))
5797 ret = PTR_ERR(task);
5798 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005799 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005800 dprintk("<-- %s status=%d\n", __func__, ret);
5801 return ret;
5802}
5803
5804static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5805{
5806 struct rpc_task *task;
5807 int ret;
5808
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005809 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005810 if (IS_ERR(task)) {
5811 ret = PTR_ERR(task);
5812 goto out;
5813 }
5814 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005815 if (!ret) {
5816 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5817
5818 if (task->tk_status == 0)
5819 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005820 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005821 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005822 rpc_put_task(task);
5823out:
5824 dprintk("<-- %s status=%d\n", __func__, ret);
5825 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005826}
5827
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005828struct nfs4_reclaim_complete_data {
5829 struct nfs_client *clp;
5830 struct nfs41_reclaim_complete_args arg;
5831 struct nfs41_reclaim_complete_res res;
5832};
5833
5834static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5835{
5836 struct nfs4_reclaim_complete_data *calldata = data;
5837
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005838 nfs41_setup_sequence(calldata->clp->cl_session,
5839 &calldata->arg.seq_args,
5840 &calldata->res.seq_res,
5841 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005842}
5843
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005844static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5845{
5846 switch(task->tk_status) {
5847 case 0:
5848 case -NFS4ERR_COMPLETE_ALREADY:
5849 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5850 break;
5851 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005852 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005853 /* fall through */
5854 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005855 return -EAGAIN;
5856 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005857 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005858 }
5859 return 0;
5860}
5861
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005862static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5863{
5864 struct nfs4_reclaim_complete_data *calldata = data;
5865 struct nfs_client *clp = calldata->clp;
5866 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5867
5868 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005869 if (!nfs41_sequence_done(task, res))
5870 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005871
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005872 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5873 rpc_restart_call_prepare(task);
5874 return;
5875 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005876 dprintk("<-- %s\n", __func__);
5877}
5878
5879static void nfs4_free_reclaim_complete_data(void *data)
5880{
5881 struct nfs4_reclaim_complete_data *calldata = data;
5882
5883 kfree(calldata);
5884}
5885
5886static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5887 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5888 .rpc_call_done = nfs4_reclaim_complete_done,
5889 .rpc_release = nfs4_free_reclaim_complete_data,
5890};
5891
5892/*
5893 * Issue a global reclaim complete.
5894 */
5895static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5896{
5897 struct nfs4_reclaim_complete_data *calldata;
5898 struct rpc_task *task;
5899 struct rpc_message msg = {
5900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5901 };
5902 struct rpc_task_setup task_setup_data = {
5903 .rpc_client = clp->cl_rpcclient,
5904 .rpc_message = &msg,
5905 .callback_ops = &nfs4_reclaim_complete_call_ops,
5906 .flags = RPC_TASK_ASYNC,
5907 };
5908 int status = -ENOMEM;
5909
5910 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005911 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005912 if (calldata == NULL)
5913 goto out;
5914 calldata->clp = clp;
5915 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005916
Trond Myklebust9d12b212012-01-17 22:04:25 -05005917 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005918 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005919 msg.rpc_argp = &calldata->arg;
5920 msg.rpc_resp = &calldata->res;
5921 task_setup_data.callback_data = calldata;
5922 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005923 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005925 goto out;
5926 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005927 status = nfs4_wait_for_completion_rpc_task(task);
5928 if (status == 0)
5929 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005930 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005931 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005932out:
5933 dprintk("<-- %s status=%d\n", __func__, status);
5934 return status;
5935}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005936
5937static void
5938nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5939{
5940 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005941 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005942 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005943
5944 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005945 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5946 * right now covering the LAYOUTGET we are about to send.
5947 * However, that is not so catastrophic, and there seems
5948 * to be no way to prevent it completely.
5949 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005950 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005951 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005952 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005953 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5954 NFS_I(lgp->args.inode)->layout,
5955 lgp->args.ctx->state)) {
5956 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00005957 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005958}
5959
5960static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5961{
5962 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07005963 struct inode *inode = lgp->args.inode;
5964 struct nfs_server *server = NFS_SERVER(inode);
5965 struct pnfs_layout_hdr *lo;
5966 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005967
5968 dprintk("--> %s\n", __func__);
5969
Trond Myklebust6ba7db32012-10-22 20:07:20 -04005970 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07005971 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005972
5973 switch (task->tk_status) {
5974 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07005975 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005976 case -NFS4ERR_LAYOUTTRYLATER:
5977 case -NFS4ERR_RECALLCONFLICT:
5978 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07005979 break;
5980 case -NFS4ERR_EXPIRED:
5981 case -NFS4ERR_BAD_STATEID:
5982 spin_lock(&inode->i_lock);
5983 lo = NFS_I(inode)->layout;
5984 if (!lo || list_empty(&lo->plh_segs)) {
5985 spin_unlock(&inode->i_lock);
5986 /* If the open stateid was bad, then recover it. */
5987 state = lgp->args.ctx->state;
5988 } else {
5989 LIST_HEAD(head);
5990
5991 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
5992 spin_unlock(&inode->i_lock);
5993 /* Mark the bad layout state as invalid, then
5994 * retry using the open stateid. */
5995 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005996 }
5997 }
Trond Myklebustee314c22012-10-01 17:25:48 -07005998 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
5999 rpc_restart_call_prepare(task);
6000out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006001 dprintk("<-- %s\n", __func__);
6002}
6003
Idan Kedar85541162012-08-02 11:47:10 +03006004static size_t max_response_pages(struct nfs_server *server)
6005{
6006 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6007 return nfs_page_array_len(0, max_resp_sz);
6008}
6009
6010static void nfs4_free_pages(struct page **pages, size_t size)
6011{
6012 int i;
6013
6014 if (!pages)
6015 return;
6016
6017 for (i = 0; i < size; i++) {
6018 if (!pages[i])
6019 break;
6020 __free_page(pages[i]);
6021 }
6022 kfree(pages);
6023}
6024
6025static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6026{
6027 struct page **pages;
6028 int i;
6029
6030 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6031 if (!pages) {
6032 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6033 return NULL;
6034 }
6035
6036 for (i = 0; i < size; i++) {
6037 pages[i] = alloc_page(gfp_flags);
6038 if (!pages[i]) {
6039 dprintk("%s: failed to allocate page\n", __func__);
6040 nfs4_free_pages(pages, size);
6041 return NULL;
6042 }
6043 }
6044
6045 return pages;
6046}
6047
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006048static void nfs4_layoutget_release(void *calldata)
6049{
6050 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006051 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6052 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006053
6054 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006055 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006056 put_nfs_open_context(lgp->args.ctx);
6057 kfree(calldata);
6058 dprintk("<-- %s\n", __func__);
6059}
6060
6061static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6062 .rpc_call_prepare = nfs4_layoutget_prepare,
6063 .rpc_call_done = nfs4_layoutget_done,
6064 .rpc_release = nfs4_layoutget_release,
6065};
6066
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006067struct pnfs_layout_segment *
6068nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006069{
6070 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006071 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006072 struct rpc_task *task;
6073 struct rpc_message msg = {
6074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6075 .rpc_argp = &lgp->args,
6076 .rpc_resp = &lgp->res,
6077 };
6078 struct rpc_task_setup task_setup_data = {
6079 .rpc_client = server->client,
6080 .rpc_message = &msg,
6081 .callback_ops = &nfs4_layoutget_call_ops,
6082 .callback_data = lgp,
6083 .flags = RPC_TASK_ASYNC,
6084 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006085 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006086 int status = 0;
6087
6088 dprintk("--> %s\n", __func__);
6089
Idan Kedar85541162012-08-02 11:47:10 +03006090 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6091 if (!lgp->args.layout.pages) {
6092 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006093 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006094 }
6095 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6096
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006097 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006098 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006099 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006100 task = rpc_run_task(&task_setup_data);
6101 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006102 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006103 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006104 if (status == 0)
6105 status = task->tk_status;
6106 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006107 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006108 rpc_put_task(task);
6109 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006110 if (status)
6111 return ERR_PTR(status);
6112 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006113}
6114
Benny Halevycbe82602011-05-22 19:52:37 +03006115static void
6116nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6117{
6118 struct nfs4_layoutreturn *lrp = calldata;
6119
6120 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006121 nfs41_setup_sequence(lrp->clp->cl_session,
6122 &lrp->args.seq_args,
6123 &lrp->res.seq_res,
6124 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006125}
6126
6127static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6128{
6129 struct nfs4_layoutreturn *lrp = calldata;
6130 struct nfs_server *server;
6131
6132 dprintk("--> %s\n", __func__);
6133
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006134 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006135 return;
6136
6137 server = NFS_SERVER(lrp->args.inode);
6138 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006139 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006140 return;
6141 }
Benny Halevycbe82602011-05-22 19:52:37 +03006142 dprintk("<-- %s\n", __func__);
6143}
6144
6145static void nfs4_layoutreturn_release(void *calldata)
6146{
6147 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006148 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006149
6150 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006151 spin_lock(&lo->plh_inode->i_lock);
6152 if (lrp->res.lrs_present)
6153 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6154 lo->plh_block_lgets--;
6155 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006156 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006157 kfree(calldata);
6158 dprintk("<-- %s\n", __func__);
6159}
6160
6161static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6162 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6163 .rpc_call_done = nfs4_layoutreturn_done,
6164 .rpc_release = nfs4_layoutreturn_release,
6165};
6166
6167int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6168{
6169 struct rpc_task *task;
6170 struct rpc_message msg = {
6171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6172 .rpc_argp = &lrp->args,
6173 .rpc_resp = &lrp->res,
6174 };
6175 struct rpc_task_setup task_setup_data = {
6176 .rpc_client = lrp->clp->cl_rpcclient,
6177 .rpc_message = &msg,
6178 .callback_ops = &nfs4_layoutreturn_call_ops,
6179 .callback_data = lrp,
6180 };
6181 int status;
6182
6183 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006184 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006185 task = rpc_run_task(&task_setup_data);
6186 if (IS_ERR(task))
6187 return PTR_ERR(task);
6188 status = task->tk_status;
6189 dprintk("<-- %s status=%d\n", __func__, status);
6190 rpc_put_task(task);
6191 return status;
6192}
6193
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006194/*
6195 * Retrieve the list of Data Server devices from the MDS.
6196 */
6197static int _nfs4_getdevicelist(struct nfs_server *server,
6198 const struct nfs_fh *fh,
6199 struct pnfs_devicelist *devlist)
6200{
6201 struct nfs4_getdevicelist_args args = {
6202 .fh = fh,
6203 .layoutclass = server->pnfs_curr_ld->id,
6204 };
6205 struct nfs4_getdevicelist_res res = {
6206 .devlist = devlist,
6207 };
6208 struct rpc_message msg = {
6209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6210 .rpc_argp = &args,
6211 .rpc_resp = &res,
6212 };
6213 int status;
6214
6215 dprintk("--> %s\n", __func__);
6216 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6217 &res.seq_res, 0);
6218 dprintk("<-- %s status=%d\n", __func__, status);
6219 return status;
6220}
6221
6222int nfs4_proc_getdevicelist(struct nfs_server *server,
6223 const struct nfs_fh *fh,
6224 struct pnfs_devicelist *devlist)
6225{
6226 struct nfs4_exception exception = { };
6227 int err;
6228
6229 do {
6230 err = nfs4_handle_exception(server,
6231 _nfs4_getdevicelist(server, fh, devlist),
6232 &exception);
6233 } while (exception.retry);
6234
6235 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6236 err, devlist->num_devs);
6237
6238 return err;
6239}
6240EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6241
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006242static int
6243_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6244{
6245 struct nfs4_getdeviceinfo_args args = {
6246 .pdev = pdev,
6247 };
6248 struct nfs4_getdeviceinfo_res res = {
6249 .pdev = pdev,
6250 };
6251 struct rpc_message msg = {
6252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6253 .rpc_argp = &args,
6254 .rpc_resp = &res,
6255 };
6256 int status;
6257
6258 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006259 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006260 dprintk("<-- %s status=%d\n", __func__, status);
6261
6262 return status;
6263}
6264
6265int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6266{
6267 struct nfs4_exception exception = { };
6268 int err;
6269
6270 do {
6271 err = nfs4_handle_exception(server,
6272 _nfs4_proc_getdeviceinfo(server, pdev),
6273 &exception);
6274 } while (exception.retry);
6275 return err;
6276}
6277EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6278
Andy Adamson863a3c62011-03-23 13:27:54 +00006279static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6280{
6281 struct nfs4_layoutcommit_data *data = calldata;
6282 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006283 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006284
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006285 nfs41_setup_sequence(session,
6286 &data->args.seq_args,
6287 &data->res.seq_res,
6288 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006289}
6290
6291static void
6292nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6293{
6294 struct nfs4_layoutcommit_data *data = calldata;
6295 struct nfs_server *server = NFS_SERVER(data->args.inode);
6296
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006297 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006298 return;
6299
6300 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006301 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6302 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6303 case -NFS4ERR_BADLAYOUT: /* no layout */
6304 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006305 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006306 break;
6307 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006308 nfs_post_op_update_inode_force_wcc(data->args.inode,
6309 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006310 break;
6311 default:
6312 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6313 rpc_restart_call_prepare(task);
6314 return;
6315 }
6316 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006317}
6318
6319static void nfs4_layoutcommit_release(void *calldata)
6320{
6321 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006322 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006323 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006324
Andy Adamsondb29c082011-07-30 20:52:38 -04006325 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006326 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006327 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6328 list_del_init(&lseg->pls_lc_list);
6329 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6330 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006331 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006332 }
Peng Tao92407e72011-10-23 20:21:17 -07006333
6334 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6335 smp_mb__after_clear_bit();
6336 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6337
Andy Adamson863a3c62011-03-23 13:27:54 +00006338 put_rpccred(data->cred);
6339 kfree(data);
6340}
6341
6342static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6343 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6344 .rpc_call_done = nfs4_layoutcommit_done,
6345 .rpc_release = nfs4_layoutcommit_release,
6346};
6347
6348int
Andy Adamsonef311532011-03-12 02:58:10 -05006349nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006350{
6351 struct rpc_message msg = {
6352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6353 .rpc_argp = &data->args,
6354 .rpc_resp = &data->res,
6355 .rpc_cred = data->cred,
6356 };
6357 struct rpc_task_setup task_setup_data = {
6358 .task = &data->task,
6359 .rpc_client = NFS_CLIENT(data->args.inode),
6360 .rpc_message = &msg,
6361 .callback_ops = &nfs4_layoutcommit_ops,
6362 .callback_data = data,
6363 .flags = RPC_TASK_ASYNC,
6364 };
6365 struct rpc_task *task;
6366 int status = 0;
6367
6368 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6369 "lbw: %llu inode %lu\n",
6370 data->task.tk_pid, sync,
6371 data->args.lastbytewritten,
6372 data->args.inode->i_ino);
6373
Trond Myklebust9d12b212012-01-17 22:04:25 -05006374 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006375 task = rpc_run_task(&task_setup_data);
6376 if (IS_ERR(task))
6377 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006378 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006379 goto out;
6380 status = nfs4_wait_for_completion_rpc_task(task);
6381 if (status != 0)
6382 goto out;
6383 status = task->tk_status;
6384out:
6385 dprintk("%s: status %d\n", __func__, status);
6386 rpc_put_task(task);
6387 return status;
6388}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006389
6390static int
6391_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6392 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6393{
6394 struct nfs41_secinfo_no_name_args args = {
6395 .style = SECINFO_STYLE_CURRENT_FH,
6396 };
6397 struct nfs4_secinfo_res res = {
6398 .flavors = flavors,
6399 };
6400 struct rpc_message msg = {
6401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6402 .rpc_argp = &args,
6403 .rpc_resp = &res,
6404 };
6405 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6406}
6407
6408static int
6409nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6410 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6411{
6412 struct nfs4_exception exception = { };
6413 int err;
6414 do {
6415 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6416 switch (err) {
6417 case 0:
6418 case -NFS4ERR_WRONGSEC:
6419 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006420 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006421 default:
6422 err = nfs4_handle_exception(server, err, &exception);
6423 }
6424 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006425out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006426 return err;
6427}
6428
6429static int
6430nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6431 struct nfs_fsinfo *info)
6432{
6433 int err;
6434 struct page *page;
6435 rpc_authflavor_t flavor;
6436 struct nfs4_secinfo_flavors *flavors;
6437
6438 page = alloc_page(GFP_KERNEL);
6439 if (!page) {
6440 err = -ENOMEM;
6441 goto out;
6442 }
6443
6444 flavors = page_address(page);
6445 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6446
6447 /*
6448 * Fall back on "guess and check" method if
6449 * the server doesn't support SECINFO_NO_NAME
6450 */
6451 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6452 err = nfs4_find_root_sec(server, fhandle, info);
6453 goto out_freepage;
6454 }
6455 if (err)
6456 goto out_freepage;
6457
6458 flavor = nfs_find_best_sec(flavors);
6459 if (err == 0)
6460 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6461
6462out_freepage:
6463 put_page(page);
6464 if (err == -EACCES)
6465 return -EPERM;
6466out:
6467 return err;
6468}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006469
6470static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006471{
6472 int status;
6473 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006474 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006475 };
6476 struct nfs41_test_stateid_res res;
6477 struct rpc_message msg = {
6478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6479 .rpc_argp = &args,
6480 .rpc_resp = &res,
6481 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006482
Chuck Lever38527b12012-07-11 16:30:23 -04006483 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006484 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006485 nfs4_set_sequence_privileged(&args.seq_args);
6486 status = nfs4_call_sync_sequence(server->client, server, &msg,
6487 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006488 if (status != NFS_OK) {
6489 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006490 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006491 }
6492 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006493 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006494}
6495
Chuck Lever38527b12012-07-11 16:30:23 -04006496/**
6497 * nfs41_test_stateid - perform a TEST_STATEID operation
6498 *
6499 * @server: server / transport on which to perform the operation
6500 * @stateid: state ID to test
6501 *
6502 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6503 * Otherwise a negative NFS4ERR value is returned if the operation
6504 * failed or the state ID is not currently valid.
6505 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006506static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006507{
6508 struct nfs4_exception exception = { };
6509 int err;
6510 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006511 err = _nfs41_test_stateid(server, stateid);
6512 if (err != -NFS4ERR_DELAY)
6513 break;
6514 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006515 } while (exception.retry);
6516 return err;
6517}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006518
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006519static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006520{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006521 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006522 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006523 };
6524 struct nfs41_free_stateid_res res;
6525 struct rpc_message msg = {
6526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6527 .rpc_argp = &args,
6528 .rpc_resp = &res,
6529 };
Chuck Lever38527b12012-07-11 16:30:23 -04006530 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006531
Chuck Lever38527b12012-07-11 16:30:23 -04006532 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006533 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006534 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006535 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006536 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006537 dprintk("NFS reply free_stateid: %d\n", status);
6538 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006539}
6540
Chuck Lever38527b12012-07-11 16:30:23 -04006541/**
6542 * nfs41_free_stateid - perform a FREE_STATEID operation
6543 *
6544 * @server: server / transport on which to perform the operation
6545 * @stateid: state ID to release
6546 *
6547 * Returns NFS_OK if the server freed "stateid". Otherwise a
6548 * negative NFS4ERR value is returned.
6549 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006550static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006551{
6552 struct nfs4_exception exception = { };
6553 int err;
6554 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006555 err = _nfs4_free_stateid(server, stateid);
6556 if (err != -NFS4ERR_DELAY)
6557 break;
6558 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006559 } while (exception.retry);
6560 return err;
6561}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006562
6563static bool nfs41_match_stateid(const nfs4_stateid *s1,
6564 const nfs4_stateid *s2)
6565{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006566 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006567 return false;
6568
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006569 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006570 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006571 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006572 return true;
6573
6574 return false;
6575}
6576
Andy Adamson557134a2009-04-01 09:21:53 -04006577#endif /* CONFIG_NFS_V4_1 */
6578
Trond Myklebust36281ca2012-03-04 18:13:56 -05006579static bool nfs4_match_stateid(const nfs4_stateid *s1,
6580 const nfs4_stateid *s2)
6581{
Trond Myklebustf597c532012-03-04 18:13:56 -05006582 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006583}
6584
6585
Trond Myklebust17280172012-03-11 13:11:00 -04006586static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006587 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006588 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 .recover_open = nfs4_open_reclaim,
6590 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006591 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006592 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006593 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594};
6595
Andy Adamson591d71c2009-04-01 09:22:47 -04006596#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006597static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006598 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6599 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6600 .recover_open = nfs4_open_reclaim,
6601 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006602 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006603 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006604 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006605 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006606};
6607#endif /* CONFIG_NFS_V4_1 */
6608
Trond Myklebust17280172012-03-11 13:11:00 -04006609static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006610 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006611 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 .recover_open = nfs4_open_expired,
6613 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006614 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006615 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616};
6617
Andy Adamson591d71c2009-04-01 09:22:47 -04006618#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006619static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006620 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6621 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006622 .recover_open = nfs41_open_expired,
6623 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006624 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006625 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006626};
6627#endif /* CONFIG_NFS_V4_1 */
6628
Trond Myklebust17280172012-03-11 13:11:00 -04006629static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006630 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006631 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006632 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006633};
6634
6635#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006636static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006637 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006638 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006639 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006640};
6641#endif
6642
Trond Myklebust97dc1352010-06-16 09:52:26 -04006643static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6644 .minor_version = 0,
6645 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006646 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006647 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006648 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6649 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6650 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006651};
6652
6653#if defined(CONFIG_NFS_V4_1)
6654static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6655 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006656 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006657 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006658 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006659 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6660 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6661 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006662};
6663#endif
6664
Trond Myklebust97dc1352010-06-16 09:52:26 -04006665const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6666 [0] = &nfs_v4_0_minor_ops,
6667#if defined(CONFIG_NFS_V4_1)
6668 [1] = &nfs_v4_1_minor_ops,
6669#endif
6670};
6671
Bryan Schumaker73a79702012-07-16 16:39:12 -04006672const struct inode_operations nfs4_dir_inode_operations = {
6673 .create = nfs_create,
6674 .lookup = nfs_lookup,
6675 .atomic_open = nfs_atomic_open,
6676 .link = nfs_link,
6677 .unlink = nfs_unlink,
6678 .symlink = nfs_symlink,
6679 .mkdir = nfs_mkdir,
6680 .rmdir = nfs_rmdir,
6681 .mknod = nfs_mknod,
6682 .rename = nfs_rename,
6683 .permission = nfs_permission,
6684 .getattr = nfs_getattr,
6685 .setattr = nfs_setattr,
6686 .getxattr = generic_getxattr,
6687 .setxattr = generic_setxattr,
6688 .listxattr = generic_listxattr,
6689 .removexattr = generic_removexattr,
6690};
6691
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006692static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006693 .permission = nfs_permission,
6694 .getattr = nfs_getattr,
6695 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006696 .getxattr = generic_getxattr,
6697 .setxattr = generic_setxattr,
6698 .listxattr = generic_listxattr,
6699 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006700};
6701
David Howells509de812006-08-22 20:06:11 -04006702const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 .version = 4, /* protocol version */
6704 .dentry_ops = &nfs4_dentry_operations,
6705 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006706 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006707 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006709 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006710 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 .getattr = nfs4_proc_getattr,
6712 .setattr = nfs4_proc_setattr,
6713 .lookup = nfs4_proc_lookup,
6714 .access = nfs4_proc_access,
6715 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 .create = nfs4_proc_create,
6717 .remove = nfs4_proc_remove,
6718 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006719 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 .unlink_done = nfs4_proc_unlink_done,
6721 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006722 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006723 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006724 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 .link = nfs4_proc_link,
6726 .symlink = nfs4_proc_symlink,
6727 .mkdir = nfs4_proc_mkdir,
6728 .rmdir = nfs4_proc_remove,
6729 .readdir = nfs4_proc_readdir,
6730 .mknod = nfs4_proc_mknod,
6731 .statfs = nfs4_proc_statfs,
6732 .fsinfo = nfs4_proc_fsinfo,
6733 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006734 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 .decode_dirent = nfs4_decode_dirent,
6736 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006737 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006738 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006739 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006741 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006742 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006743 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006745 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006746 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006748 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006749 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006750 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006751 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006752 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006753 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006754 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006755 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006756 .create_server = nfs4_create_server,
6757 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758};
6759
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006760static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6761 .prefix = XATTR_NAME_NFSV4_ACL,
6762 .list = nfs4_xattr_list_nfs4_acl,
6763 .get = nfs4_xattr_get_nfs4_acl,
6764 .set = nfs4_xattr_set_nfs4_acl,
6765};
6766
6767const struct xattr_handler *nfs4_xattr_handlers[] = {
6768 &nfs4_xattr_nfs4_acl_handler,
6769 NULL
6770};
6771
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772/*
6773 * Local variables:
6774 * c-basic-offset: 8
6775 * End:
6776 */