blob: 39d24158f97f3a2ebb2e82f85f389c5b739ef67a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd12012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000097 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080099 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400106 case -NFS4ERR_ACCESS:
107 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700110 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * This is our standard bitmap for GETATTR requests.
118 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400119const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
134};
135
Trond Myklebust1549210f2012-06-05 09:16:47 -0400136static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
152};
153
Andy Adamsone23008e2012-10-02 21:07:32 -0400154static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
167};
168
Trond Myklebust4ce79712005-06-22 17:16:21 +0000169const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
172 0
173};
174
Fred Isamandae100c2011-07-30 20:52:37 -0400175const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700179 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Manoj Naik830b8e32006-06-09 09:34:25 -0400184const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
201};
202
Al Virobc4785c2006-10-19 23:28:51 -0700203static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nfs4_readdir_arg *readdir)
205{
Al Viro0dbb4c62006-10-19 23:28:49 -0700206 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000209 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
212 }
213
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
218
219 /*
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
225 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800226 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800254 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Trond Myklebust65de8722008-12-23 15:21:44 -0500257static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258{
259 int res = 0;
260
261 might_sleep();
262
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100267 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
272}
273
274/* This is the error handling routine for processes that are allowed
275 * to sleep.
276 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800277static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500278{
279 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500280 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500281 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500282 int ret = errorcode;
283
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400290 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500291 exception->retry = 1;
292 return 0;
293 }
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500298 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 if (state == NULL)
302 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400303 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500313#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
330 */
331 ret = -EBUSY;
332 break;
333 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500336 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 ret = nfs4_delay(server->client, &exception->timeout);
338 if (ret != 0)
339 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400340 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_OLD_STATEID:
342 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800343 break;
344 case -NFS4ERR_BADOWNER:
345 /* The following works around a Linux server bug! */
346 case -NFS4ERR_BADNAME:
347 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
348 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
349 exception->retry = 1;
350 printk(KERN_WARNING "NFS: v4 server %s "
351 "does not accept raw "
352 "uid/gids. "
353 "Reenabling the idmapper.\n",
354 server->nfs_client->cl_hostname);
355 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 }
357 /* We failed to handle the error */
358 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500359wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500360 ret = nfs4_wait_clnt_recover(clp);
361 if (ret == 0)
362 exception->retry = 1;
363 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500364}
365
366
Trond Myklebust452e9352010-07-31 14:29:06 -0400367static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock(&clp->cl_lock);
370 if (time_before(clp->cl_last_renewal,timestamp))
371 clp->cl_last_renewal = timestamp;
372 spin_unlock(&clp->cl_lock);
373}
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376{
377 do_renew_lease(server->nfs_client, timestamp);
378}
379
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380#if defined(CONFIG_NFS_V4_1)
381
Trond Myklebust961a8282012-01-17 22:57:37 -0500382bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
383{
384 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
385 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386}
387
Trond Myklebustd185a332010-06-16 09:52:25 -0400388static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400389{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500390 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400391 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500392 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400393
Benny Halevydfb4f3092010-09-24 09:17:01 -0400394 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400395 /* just wake up the next guy waiting since
396 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500397 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500398 return;
Andy Adamson13615872009-04-01 09:22:17 -0400399 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500400 tbl = res->sr_slot->table;
401 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500402
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500403 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500404 /* Be nice to the server: try to ensure that the last transmitted
405 * value for highest_user_slotid <= target_highest_slotid
406 */
407 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
408 send_new_highest_used_slotid = true;
409
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500410 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500411
412 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
413 send_new_highest_used_slotid = false;
414 if (!nfs4_session_draining(session)) {
415 if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
416 nfs4_set_task_privileged, NULL) != NULL)
417 send_new_highest_used_slotid = false;
418 }
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500419 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400420 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500421 if (send_new_highest_used_slotid)
422 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400423}
424
Trond Myklebust14516c32010-07-31 14:29:06 -0400425static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500427 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500428 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400429 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400430 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431
432 /*
433 * sr_status remains 1 if an RPC level error occurred. The server
434 * may or may not have processed the sequence operation..
435 * Proceed as if the server received and processed the sequence
436 * operation.
437 */
438 if (res->sr_status == 1)
439 res->sr_status = NFS_OK;
440
Bryan Schumaker468f8612011-04-18 15:57:32 -0400441 /* don't increment the sequence number if the task wasn't sent */
442 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 goto out;
444
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500446 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500447
Andy Adamson691daf32009-12-04 15:55:39 -0500448 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 switch (res->sr_status) {
450 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400451 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500452 ++slot->seq_nr;
453 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500454 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400455 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500456 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500459 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 break;
461 case -NFS4ERR_DELAY:
462 /* The server detected a resend of the RPC call and
463 * returned NFS4ERR_DELAY as per Section 2.10.6.2
464 * of RFC5661.
465 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500466 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400467 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500468 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500469 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 goto out_retry;
471 default:
472 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500473 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400474 }
475out:
476 /* The session may be reset by one of the error handlers. */
477 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400478 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 return 1;
480out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400481 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 goto out;
483 rpc_delay(task, NFS4_POLL_RETRY_MAX);
484 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485}
486
Trond Myklebust14516c32010-07-31 14:29:06 -0400487static int nfs4_sequence_done(struct rpc_task *task,
488 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400489{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500490 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 return 1;
492 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400493}
494
Trond Myklebust9d12b212012-01-17 22:04:25 -0500495static void nfs41_init_sequence(struct nfs4_sequence_args *args,
496 struct nfs4_sequence_res *res, int cache_reply)
497{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500498 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500499 args->sa_cache_this = 0;
500 if (cache_reply)
501 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500502 res->sr_slot = NULL;
503}
504
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000505int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 struct rpc_task *task)
509{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 struct nfs4_slot *slot;
511 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512
513 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400514 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400515 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400516 return 0;
517
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 tbl = &session->fc_slot_table;
519
Trond Myklebust69d206b2012-11-22 13:21:02 -0500520 task->tk_timeout = 0;
521
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400523 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800524 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500525 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500526 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400527 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500528 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500529 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400530 }
531
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800532 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
533 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
534 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
535 spin_unlock(&tbl->slot_tbl_lock);
536 dprintk("%s enforce FIFO order\n", __func__);
537 return -EAGAIN;
538 }
539
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500540 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500541 if (IS_ERR(slot)) {
542 /* If out of memory, try again in 1/4 second */
543 if (slot == ERR_PTR(-ENOMEM))
544 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400545 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
546 spin_unlock(&tbl->slot_tbl_lock);
547 dprintk("<-- %s: no free slots\n", __func__);
548 return -EAGAIN;
549 }
550 spin_unlock(&tbl->slot_tbl_lock);
551
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800552 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500553
554 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500556 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
557 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558
Benny Halevydfb4f3092010-09-24 09:17:01 -0400559 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400560 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400561 /*
562 * sr_status is only set in decode_sequence, and so will remain
563 * set to 1 if an rpc level failure occurs.
564 */
565 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 return 0;
567}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000568EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569
Trond Myklebust035168ab2010-06-16 09:52:26 -0400570int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571 struct nfs4_sequence_args *args,
572 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 struct rpc_task *task)
574{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400575 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 int ret = 0;
577
Trond Myklebust9d12b212012-01-17 22:04:25 -0500578 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400579 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400580
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500581 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400582 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500583 res->sr_slot->slot_nr : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400584
Trond Myklebust9d12b212012-01-17 22:04:25 -0500585 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400586out:
587 dprintk("<-- %s status=%d\n", __func__, ret);
588 return ret;
589}
590
591struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400592 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *seq_args;
594 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595};
596
597static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
598{
599 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400600 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601
Trond Myklebust035168ab2010-06-16 09:52:26 -0400602 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
603
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400604 if (nfs41_setup_sequence(session, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500605 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 return;
607 rpc_call_start(task);
608}
609
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800610static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
611{
612 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
613 nfs41_call_sync_prepare(task, calldata);
614}
615
Andy Adamson69ab40c2009-04-01 09:22:19 -0400616static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
617{
618 struct nfs41_call_sync_data *data = calldata;
619
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400621}
622
Trond Myklebust17280172012-03-11 13:11:00 -0400623static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400625 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626};
627
Trond Myklebust17280172012-03-11 13:11:00 -0400628static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800629 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
630 .rpc_call_done = nfs41_call_sync_done,
631};
632
Bryan Schumaker7c513052011-03-24 17:12:24 +0000633static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
634 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400635 struct rpc_message *msg,
636 struct nfs4_sequence_args *args,
637 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800638 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639{
640 int ret;
641 struct rpc_task *task;
642 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400643 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 .seq_args = args,
645 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400646 };
647 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000648 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649 .rpc_message = msg,
650 .callback_ops = &nfs41_call_sync_ops,
651 .callback_data = &data
652 };
653
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800654 if (privileged)
655 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 task = rpc_run_task(&task_setup);
657 if (IS_ERR(task))
658 ret = PTR_ERR(task);
659 else {
660 ret = task->tk_status;
661 rpc_put_task(task);
662 }
663 return ret;
664}
665
Bryan Schumaker7c513052011-03-24 17:12:24 +0000666int _nfs4_call_sync_session(struct rpc_clnt *clnt,
667 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400668 struct rpc_message *msg,
669 struct nfs4_sequence_args *args,
670 struct nfs4_sequence_res *res,
671 int cache_reply)
672{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500673 nfs41_init_sequence(args, res, cache_reply);
674 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400675}
676
Trond Myklebustdf896452010-06-16 09:52:26 -0400677#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500678static inline
679void nfs41_init_sequence(struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res, int cache_reply)
681{
682}
683
Trond Myklebust14516c32010-07-31 14:29:06 -0400684static int nfs4_sequence_done(struct rpc_task *task,
685 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400686{
Trond Myklebust14516c32010-07-31 14:29:06 -0400687 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400688}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689#endif /* CONFIG_NFS_V4_1 */
690
Bryan Schumaker7c513052011-03-24 17:12:24 +0000691int _nfs4_call_sync(struct rpc_clnt *clnt,
692 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400693 struct rpc_message *msg,
694 struct nfs4_sequence_args *args,
695 struct nfs4_sequence_res *res,
696 int cache_reply)
697{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500698 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000699 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400700}
701
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000702static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000703int nfs4_call_sync(struct rpc_clnt *clnt,
704 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000705 struct rpc_message *msg,
706 struct nfs4_sequence_args *args,
707 struct nfs4_sequence_res *res,
708 int cache_reply)
709{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000710 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
711 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000712}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713
Trond Myklebust38478b22006-05-25 01:40:57 -0400714static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Trond Myklebust38478b22006-05-25 01:40:57 -0400716 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Trond Myklebust38478b22006-05-25 01:40:57 -0400718 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400719 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700720 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400721 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700722 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400723 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100726struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400727 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 struct nfs_openargs o_arg;
729 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100730 struct nfs_open_confirmargs c_arg;
731 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -0500732 struct nfs4_string owner_name;
733 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100734 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100735 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400736 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100737 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400738 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100740 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400741 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100742 int rpc_status;
743 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744};
745
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400746
747static void nfs4_init_opendata_res(struct nfs4_opendata *p)
748{
749 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400750 p->o_res.seqid = p->o_arg.seqid;
751 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400752 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400753 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400754 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -0500755 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400756}
757
Al Viro82a2c1b2011-06-22 18:30:55 -0400758static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500759 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400760 const struct iattr *attrs,
761 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762{
Al Viro82a2c1b2011-06-22 18:30:55 -0400763 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct inode *dir = parent->d_inode;
765 struct nfs_server *server = NFS_SERVER(dir);
766 struct nfs4_opendata *p;
767
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400768 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 if (p == NULL)
770 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400771 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 if (p->o_arg.seqid == NULL)
773 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400774 nfs_sb_active(dentry->d_sb);
775 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 p->dir = parent;
777 p->owner = sp;
778 atomic_inc(&sp->so_count);
779 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500780 p->o_arg.open_flags = flags;
781 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700782 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
783 * will return permission denied for all bits until close */
784 if (!(flags & O_EXCL)) {
785 /* ask server to check for all possible rights as results
786 * are cached */
787 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
788 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700789 }
David Howells7539bba2006-08-22 20:06:09 -0400790 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400791 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
792 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400793 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 p->o_arg.server = server;
795 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400796 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500798 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500799 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400800
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801 p->o_arg.u.attrs = &p->attrs;
802 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500803
804 verf[0] = jiffies;
805 verf[1] = current->pid;
806 memcpy(p->o_arg.u.verifier.data, verf,
807 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100809 p->c_arg.fh = &p->o_res.fh;
810 p->c_arg.stateid = &p->o_res.stateid;
811 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400813 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 return p;
815err_free:
816 kfree(p);
817err:
818 dput(parent);
819 return NULL;
820}
821
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400822static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400824 struct nfs4_opendata *p = container_of(kref,
825 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400826 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400827
828 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 if (p->state != NULL)
830 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400831 nfs4_put_state_owner(p->owner);
832 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400833 dput(p->dentry);
834 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -0500835 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836 kfree(p);
837}
838
839static void nfs4_opendata_put(struct nfs4_opendata *p)
840{
841 if (p != NULL)
842 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843}
844
Trond Myklebust06f814a2006-01-03 09:55:07 +0100845static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
846{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100847 int ret;
848
Trond Myklebust06f814a2006-01-03 09:55:07 +0100849 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100850 return ret;
851}
852
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400854{
855 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856
Trond Myklebust536e43d2012-01-17 22:04:26 -0500857 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 goto out;
859 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400860 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500861 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
862 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400863 break;
864 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500865 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
866 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400867 break;
868 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500869 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
870 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400873 return ret;
874}
875
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400877{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500878 if (delegation == NULL)
879 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400881 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500882 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400883 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500884 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400885 return 1;
886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100889{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100891 case FMODE_WRITE:
892 state->n_wronly++;
893 break;
894 case FMODE_READ:
895 state->n_rdonly++;
896 break;
897 case FMODE_READ|FMODE_WRITE:
898 state->n_rdwr++;
899 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400904{
905 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500906 nfs4_stateid_copy(&state->stateid, stateid);
907 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400909 case FMODE_READ:
910 set_bit(NFS_O_RDONLY_STATE, &state->flags);
911 break;
912 case FMODE_WRITE:
913 set_bit(NFS_O_WRONLY_STATE, &state->flags);
914 break;
915 case FMODE_READ|FMODE_WRITE:
916 set_bit(NFS_O_RDWR_STATE, &state->flags);
917 }
918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400922 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400924 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400929 /*
930 * Protect the call to nfs4_state_set_mode_locked and
931 * serialise the stateid update
932 */
933 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400934 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500935 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400936 set_bit(NFS_DELEGATED_STATE, &state->flags);
937 }
938 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400940 write_sequnlock(&state->seqlock);
941 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700943 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500947{
948 struct nfs_inode *nfsi = NFS_I(state->inode);
949 struct nfs_delegation *deleg_cur;
950 int ret = 0;
951
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500953
954 rcu_read_lock();
955 deleg_cur = rcu_dereference(nfsi->delegation);
956 if (deleg_cur == NULL)
957 goto no_delegation;
958
959 spin_lock(&deleg_cur->lock);
960 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500962 goto no_delegation_unlock;
963
964 if (delegation == NULL)
965 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500966 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500967 goto no_delegation_unlock;
968
Trond Myklebustb7391f42008-12-23 15:21:52 -0500969 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500971 ret = 1;
972no_delegation_unlock:
973 spin_unlock(&deleg_cur->lock);
974no_delegation:
975 rcu_read_unlock();
976
977 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500979 ret = 1;
980 }
981
982 return ret;
983}
984
985
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400987{
988 struct nfs_delegation *delegation;
989
990 rcu_read_lock();
991 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400993 rcu_read_unlock();
994 return;
995 }
996 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400997 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400998}
999
Trond Myklebust6ee41262007-07-08 14:11:36 -04001000static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001{
1002 struct nfs4_state *state = opendata->state;
1003 struct nfs_inode *nfsi = NFS_I(state->inode);
1004 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001005 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007 nfs4_stateid stateid;
1008 int ret = -EAGAIN;
1009
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001012 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 if (can_open_cached(state, fmode, open_mode)) {
1014 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 goto out_return_state;
1017 }
1018 spin_unlock(&state->owner->so_lock);
1019 }
Trond Myklebust34310432008-12-23 15:21:38 -05001020 rcu_read_lock();
1021 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001022 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001023 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001024 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001025 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001027 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001029 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 if (ret != 0)
1031 goto out;
1032 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001033
1034 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001036 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038out:
1039 return ERR_PTR(ret);
1040out_return_state:
1041 atomic_inc(&state->count);
1042 return state;
1043}
1044
Andy Adamsone23008e2012-10-02 21:07:32 -04001045static void
1046nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1047{
1048 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1049 struct nfs_delegation *delegation;
1050 int delegation_flags = 0;
1051
1052 rcu_read_lock();
1053 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1054 if (delegation)
1055 delegation_flags = delegation->flags;
1056 rcu_read_unlock();
1057 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1058 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1059 "returning a delegation for "
1060 "OPEN(CLAIM_DELEGATE_CUR)\n",
1061 clp->cl_hostname);
1062 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1063 nfs_inode_set_delegation(state->inode,
1064 data->owner->so_cred,
1065 &data->o_res);
1066 else
1067 nfs_inode_reclaim_delegation(state->inode,
1068 data->owner->so_cred,
1069 &data->o_res);
1070}
1071
1072/*
1073 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1074 * and update the nfs4_state.
1075 */
1076static struct nfs4_state *
1077_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1078{
1079 struct inode *inode = data->state->inode;
1080 struct nfs4_state *state = data->state;
1081 int ret;
1082
1083 if (!data->rpc_done) {
1084 ret = data->rpc_status;
1085 goto err;
1086 }
1087
1088 ret = -ESTALE;
1089 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1090 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1091 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1092 goto err;
1093
1094 ret = -ENOMEM;
1095 state = nfs4_get_open_state(inode, data->owner);
1096 if (state == NULL)
1097 goto err;
1098
1099 ret = nfs_refresh_inode(inode, &data->f_attr);
1100 if (ret)
1101 goto err;
1102
1103 if (data->o_res.delegation_type != 0)
1104 nfs4_opendata_check_deleg(data, state);
1105 update_open_stateid(state, &data->o_res.stateid, NULL,
1106 data->o_arg.fmode);
1107
1108 return state;
1109err:
1110 return ERR_PTR(ret);
1111
1112}
1113
1114static struct nfs4_state *
1115_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116{
1117 struct inode *inode;
1118 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001120
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001122 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001123 goto out;
1124 }
1125
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001126 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001131 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132 goto err;
1133 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134 state = nfs4_get_open_state(inode, data->owner);
1135 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001136 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001137 if (data->o_res.delegation_type != 0)
1138 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001139 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001142out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001144err_put_inode:
1145 iput(inode);
1146err:
1147 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001148}
1149
Andy Adamsone23008e2012-10-02 21:07:32 -04001150static struct nfs4_state *
1151nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1152{
1153 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1154 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1155 return _nfs4_opendata_to_nfs4_state(data);
1156}
1157
Trond Myklebust864472e2006-01-03 09:55:15 +01001158static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1159{
1160 struct nfs_inode *nfsi = NFS_I(state->inode);
1161 struct nfs_open_context *ctx;
1162
1163 spin_lock(&state->inode->i_lock);
1164 list_for_each_entry(ctx, &nfsi->open_files, list) {
1165 if (ctx->state != state)
1166 continue;
1167 get_nfs_open_context(ctx);
1168 spin_unlock(&state->inode->i_lock);
1169 return ctx;
1170 }
1171 spin_unlock(&state->inode->i_lock);
1172 return ERR_PTR(-ENOENT);
1173}
1174
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001175static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1176{
1177 struct nfs4_opendata *opendata;
1178
Al Viro3d4ff432011-06-22 18:40:12 -04001179 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001180 if (opendata == NULL)
1181 return ERR_PTR(-ENOMEM);
1182 opendata->state = state;
1183 atomic_inc(&state->count);
1184 return opendata;
1185}
1186
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001188{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 int ret;
1191
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001192 opendata->o_arg.open_flags = 0;
1193 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1195 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1196 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001197 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 if (ret != 0)
1199 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001201 if (IS_ERR(newstate))
1202 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001203 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 return 0;
1206}
1207
1208static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1209{
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 int ret;
1212
1213 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 smp_rmb();
1216 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001217 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 if (ret != 0)
1220 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 if (newstate != state)
1222 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 }
1224 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001225 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001226 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 if (ret != 0)
1228 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 if (newstate != state)
1230 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 }
1232 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001233 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001234 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 if (ret != 0)
1236 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001237 if (newstate != state)
1238 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001240 /*
1241 * We may have performed cached opens for all three recoveries.
1242 * Check if we need to update the current stateid.
1243 */
1244 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001245 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001246 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001247 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001248 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001249 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001250 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 return 0;
1252}
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/*
1255 * OPEN_RECLAIM:
1256 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001258static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001260 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001262 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int status;
1264
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001265 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1266 if (IS_ERR(opendata))
1267 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1269 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001270 rcu_read_lock();
1271 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001272 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001273 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001274 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 opendata->o_arg.u.delegation_type = delegation_type;
1276 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return status;
1279}
1280
Trond Myklebust539cd032007-06-05 11:46:42 -04001281static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct nfs_server *server = NFS_SERVER(state->inode);
1284 struct nfs4_exception exception = { };
1285 int err;
1286 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001287 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001288 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001289 break;
1290 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 } while (exception.retry);
1292 return err;
1293}
1294
Trond Myklebust864472e2006-01-03 09:55:15 +01001295static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1296{
1297 struct nfs_open_context *ctx;
1298 int ret;
1299
1300 ctx = nfs4_state_find_open_context(state);
1301 if (IS_ERR(ctx))
1302 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001303 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 put_nfs_open_context(ctx);
1305 return ret;
1306}
1307
Trond Myklebust13437e12007-07-06 15:10:43 -04001308static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001311 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001313 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1314 if (IS_ERR(opendata))
1315 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001316 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001317 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001319 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Trond Myklebust13437e12007-07-06 15:10:43 -04001323int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001326 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int err;
1328 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001329 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 switch (err) {
1331 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001332 case -ENOENT:
1333 case -ESTALE:
1334 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001335 case -NFS4ERR_BADSESSION:
1336 case -NFS4ERR_BADSLOT:
1337 case -NFS4ERR_BAD_HIGH_SLOT:
1338 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1339 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001340 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 case -NFS4ERR_STALE_CLIENTID:
1343 case -NFS4ERR_STALE_STATEID:
1344 case -NFS4ERR_EXPIRED:
1345 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001346 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347 goto out;
1348 case -ERESTARTSYS:
1349 /*
1350 * The show must go on: exit, but mark the
1351 * stateid as needing recovery.
1352 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001353 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001354 case -NFS4ERR_ADMIN_REVOKED:
1355 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001356 nfs_inode_find_state_and_recover(state->inode,
1357 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001358 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001359 case -EKEYEXPIRED:
1360 /*
1361 * User RPCSEC_GSS context has expired.
1362 * We cannot recover this stateid now, so
1363 * skip it and allow recovery thread to
1364 * proceed.
1365 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001366 case -ENOMEM:
1367 err = 0;
1368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370 err = nfs4_handle_exception(server, err, &exception);
1371 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001372out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return err;
1374}
1375
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001376static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1377{
1378 struct nfs4_opendata *data = calldata;
1379
1380 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001381 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001382 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001383 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001384 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001385 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001386 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001387}
1388
1389static void nfs4_open_confirm_release(void *calldata)
1390{
1391 struct nfs4_opendata *data = calldata;
1392 struct nfs4_state *state = NULL;
1393
1394 /* If this request hasn't been cancelled, do nothing */
1395 if (data->cancelled == 0)
1396 goto out_free;
1397 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001398 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001401 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001402 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001404 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405}
1406
1407static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001408 .rpc_call_done = nfs4_open_confirm_done,
1409 .rpc_release = nfs4_open_confirm_release,
1410};
1411
1412/*
1413 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1414 */
1415static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1416{
1417 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1418 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001419 struct rpc_message msg = {
1420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1421 .rpc_argp = &data->c_arg,
1422 .rpc_resp = &data->c_res,
1423 .rpc_cred = data->owner->so_cred,
1424 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001425 struct rpc_task_setup task_setup_data = {
1426 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001427 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001428 .callback_ops = &nfs4_open_confirm_ops,
1429 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001430 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001431 .flags = RPC_TASK_ASYNC,
1432 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int status;
1434
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001435 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001436 data->rpc_done = 0;
1437 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001438 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001440 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001441 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001442 status = nfs4_wait_for_completion_rpc_task(task);
1443 if (status != 0) {
1444 data->cancelled = 1;
1445 smp_wmb();
1446 } else
1447 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001448 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return status;
1450}
1451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454 struct nfs4_opendata *data = calldata;
1455 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1458 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001459 /*
1460 * Check if we still need to send an OPEN call, or if we can use
1461 * a delegation instead.
1462 */
1463 if (data->state != NULL) {
1464 struct nfs_delegation *delegation;
1465
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001466 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001467 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001468 rcu_read_lock();
1469 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001470 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1471 can_open_delegated(delegation, data->o_arg.fmode))
1472 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001473 rcu_read_unlock();
1474 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001475 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001476 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001477 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001478 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001479 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001480 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1481 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001482 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001483 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001484 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001485 &data->o_res.seq_res,
1486 task) != 0)
1487 nfs_release_seqid(data->o_arg.seqid);
1488 else
1489 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001490 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001491unlock_no_action:
1492 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001493out_no_action:
1494 task->tk_action = NULL;
1495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001498static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1499{
1500 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1501 nfs4_open_prepare(task, calldata);
1502}
1503
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504static void nfs4_open_done(struct rpc_task *task, void *calldata)
1505{
1506 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001509
Trond Myklebust14516c32010-07-31 14:29:06 -04001510 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1511 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001512
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001514 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1515 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001516 case S_IFREG:
1517 break;
1518 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001520 break;
1521 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 break;
1524 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001526 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001527 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001528 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001529 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1530 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001531 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001532 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535static void nfs4_open_release(void *calldata)
1536{
1537 struct nfs4_opendata *data = calldata;
1538 struct nfs4_state *state = NULL;
1539
1540 /* If this request hasn't been cancelled, do nothing */
1541 if (data->cancelled == 0)
1542 goto out_free;
1543 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001544 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 goto out_free;
1546 /* In case we need an open_confirm, no cleanup! */
1547 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1548 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001550 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001551 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001553 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554}
1555
1556static const struct rpc_call_ops nfs4_open_ops = {
1557 .rpc_call_prepare = nfs4_open_prepare,
1558 .rpc_call_done = nfs4_open_done,
1559 .rpc_release = nfs4_open_release,
1560};
1561
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001562static const struct rpc_call_ops nfs4_recover_open_ops = {
1563 .rpc_call_prepare = nfs4_recover_open_prepare,
1564 .rpc_call_done = nfs4_open_done,
1565 .rpc_release = nfs4_open_release,
1566};
1567
1568static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569{
1570 struct inode *dir = data->dir->d_inode;
1571 struct nfs_server *server = NFS_SERVER(dir);
1572 struct nfs_openargs *o_arg = &data->o_arg;
1573 struct nfs_openres *o_res = &data->o_res;
1574 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001575 struct rpc_message msg = {
1576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1577 .rpc_argp = o_arg,
1578 .rpc_resp = o_res,
1579 .rpc_cred = data->owner->so_cred,
1580 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 struct rpc_task_setup task_setup_data = {
1582 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001583 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001584 .callback_ops = &nfs4_open_ops,
1585 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001586 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001587 .flags = RPC_TASK_ASYNC,
1588 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001589 int status;
1590
Trond Myklebust9d12b212012-01-17 22:04:25 -05001591 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001592 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001593 data->rpc_done = 0;
1594 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001595 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001596 if (isrecover)
1597 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001598 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001599 if (IS_ERR(task))
1600 return PTR_ERR(task);
1601 status = nfs4_wait_for_completion_rpc_task(task);
1602 if (status != 0) {
1603 data->cancelled = 1;
1604 smp_wmb();
1605 } else
1606 status = data->rpc_status;
1607 rpc_put_task(task);
1608
1609 return status;
1610}
1611
1612static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1613{
1614 struct inode *dir = data->dir->d_inode;
1615 struct nfs_openres *o_res = &data->o_res;
1616 int status;
1617
1618 status = nfs4_run_open_task(data, 1);
1619 if (status != 0 || !data->rpc_done)
1620 return status;
1621
Trond Myklebust6926afd12012-01-07 13:22:46 -05001622 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1623
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001624 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1625 status = _nfs4_proc_open_confirm(data);
1626 if (status != 0)
1627 return status;
1628 }
1629
1630 return status;
1631}
1632
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001633static int nfs4_opendata_access(struct rpc_cred *cred,
1634 struct nfs4_opendata *opendata,
1635 struct nfs4_state *state, fmode_t fmode)
1636{
1637 struct nfs_access_entry cache;
1638 u32 mask;
1639
1640 /* access call failed or for some reason the server doesn't
1641 * support any access modes -- defer access call until later */
1642 if (opendata->o_res.access_supported == 0)
1643 return 0;
1644
1645 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001646 /* don't check MAY_WRITE - a newly created file may not have
1647 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001648 if (fmode & FMODE_READ)
1649 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001650 if (fmode & FMODE_EXEC)
1651 mask |= MAY_EXEC;
1652
1653 cache.cred = cred;
1654 cache.jiffies = jiffies;
1655 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1656 nfs_access_add_cache(state->inode, &cache);
1657
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001658 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001659 return 0;
1660
1661 /* even though OPEN succeeded, access is denied. Close the file */
1662 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001663 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001664}
1665
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001666/*
1667 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1668 */
1669static int _nfs4_proc_open(struct nfs4_opendata *data)
1670{
1671 struct inode *dir = data->dir->d_inode;
1672 struct nfs_server *server = NFS_SERVER(dir);
1673 struct nfs_openargs *o_arg = &data->o_arg;
1674 struct nfs_openres *o_res = &data->o_res;
1675 int status;
1676
1677 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001678 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001680 if (status != 0) {
1681 if (status == -NFS4ERR_BADNAME &&
1682 !(o_arg->open_flags & O_CREAT))
1683 return -ENOENT;
1684 return status;
1685 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686
Trond Myklebust6926afd12012-01-07 13:22:46 -05001687 nfs_fattr_map_and_free_names(server, &data->f_attr);
1688
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001689 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001690 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001691 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1692 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001694 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001699 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Andy Adamsond83217c2011-03-01 01:34:17 +00001703static int nfs4_recover_expired_lease(struct nfs_server *server)
1704{
1705 return nfs4_client_recover_expired_lease(server->nfs_client);
1706}
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708/*
1709 * OPEN_EXPIRED:
1710 * reclaim state on the server after a network partition.
1711 * Assumes caller holds the appropriate lock
1712 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001713static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001715 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001716 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001718 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1719 if (IS_ERR(opendata))
1720 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001721 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001722 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001723 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001724 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001728static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729{
Trond Myklebust539cd032007-06-05 11:46:42 -04001730 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001731 struct nfs4_exception exception = { };
1732 int err;
1733
1734 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001735 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001736 switch (err) {
1737 default:
1738 goto out;
1739 case -NFS4ERR_GRACE:
1740 case -NFS4ERR_DELAY:
1741 nfs4_handle_exception(server, err, &exception);
1742 err = 0;
1743 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001744 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001745out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001746 return err;
1747}
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Trond Myklebust864472e2006-01-03 09:55:15 +01001754 ctx = nfs4_state_find_open_context(state);
1755 if (IS_ERR(ctx))
1756 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001757 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001758 put_nfs_open_context(ctx);
1759 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001763static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001764{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001765 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001766 nfs4_stateid *stateid = &state->stateid;
1767 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001768
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001769 /* If a state reset has been done, test_stateid is unneeded */
1770 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1771 return;
1772
1773 status = nfs41_test_stateid(server, stateid);
1774 if (status != NFS_OK) {
1775 /* Free the stateid unless the server explicitly
1776 * informs us the stateid is unrecognized. */
1777 if (status != -NFS4ERR_BAD_STATEID)
1778 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001779 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001780
Bryan Schumaker69388672012-09-26 15:25:52 -04001781 write_seqlock(&state->seqlock);
1782 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1783 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001784 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1785 }
1786}
1787
1788/**
1789 * nfs41_check_open_stateid - possibly free an open stateid
1790 *
1791 * @state: NFSv4 state for an inode
1792 *
1793 * Returns NFS_OK if recovery for this stateid is now finished.
1794 * Otherwise a negative NFS4ERR value is returned.
1795 */
1796static int nfs41_check_open_stateid(struct nfs4_state *state)
1797{
1798 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001799 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001800 int status;
1801
1802 /* If a state reset has been done, test_stateid is unneeded */
1803 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1804 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1805 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1806 return -NFS4ERR_BAD_STATEID;
1807
1808 status = nfs41_test_stateid(server, stateid);
1809 if (status != NFS_OK) {
1810 /* Free the stateid unless the server explicitly
1811 * informs us the stateid is unrecognized. */
1812 if (status != -NFS4ERR_BAD_STATEID)
1813 nfs41_free_stateid(server, stateid);
1814
1815 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1816 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1817 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001818 }
1819 return status;
1820}
1821
1822static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1823{
Chuck Levereb64cf92012-07-11 16:30:05 -04001824 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001825
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001826 nfs41_clear_delegation_stateid(state);
1827 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001828 if (status != NFS_OK)
1829 status = nfs4_open_expired(sp, state);
1830 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001831}
1832#endif
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001835 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1836 * fields corresponding to attributes that were used to store the verifier.
1837 * Make sure we clobber those fields in the later setattr call
1838 */
1839static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1840{
1841 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1842 !(sattr->ia_valid & ATTR_ATIME_SET))
1843 sattr->ia_valid |= ATTR_ATIME;
1844
1845 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1846 !(sattr->ia_valid & ATTR_MTIME_SET))
1847 sattr->ia_valid |= ATTR_MTIME;
1848}
1849
1850/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001851 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
Andy Adamson82be4172012-05-23 05:02:35 -04001853static int _nfs4_do_open(struct inode *dir,
1854 struct dentry *dentry,
1855 fmode_t fmode,
1856 int flags,
1857 struct iattr *sattr,
1858 struct rpc_cred *cred,
1859 struct nfs4_state **res,
1860 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 struct nfs4_state_owner *sp;
1863 struct nfs4_state *state = NULL;
1864 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001865 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001866 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001870 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1871 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1873 goto out_err;
1874 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001875 status = nfs4_recover_expired_lease(server);
1876 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001877 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001878 if (dentry->d_inode != NULL)
1879 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001880 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001881 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001882 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001883 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Andy Adamson82be4172012-05-23 05:02:35 -04001885 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1886 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1887 if (!opendata->f_attr.mdsthreshold)
1888 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001889 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001890 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001891 if (dentry->d_inode != NULL)
1892 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001893
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001894 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001896 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001898 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001899 status = PTR_ERR(state);
1900 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001901 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001902 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001903 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001904
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001905 status = nfs4_opendata_access(cred, opendata, state, fmode);
1906 if (status != 0)
1907 goto err_opendata_put;
1908
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001909 if (opendata->o_arg.open_flags & O_EXCL) {
1910 nfs4_exclusive_attrset(opendata, sattr);
1911
1912 nfs_fattr_init(opendata->o_res.f_attr);
1913 status = nfs4_do_setattr(state->inode, cred,
1914 opendata->o_res.f_attr, sattr,
1915 state);
1916 if (status == 0)
1917 nfs_setattr_update_inode(state->inode, sattr);
1918 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1919 }
Andy Adamson82be4172012-05-23 05:02:35 -04001920
1921 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1922 *ctx_th = opendata->f_attr.mdsthreshold;
1923 else
1924 kfree(opendata->f_attr.mdsthreshold);
1925 opendata->f_attr.mdsthreshold = NULL;
1926
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001927 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 *res = state;
1930 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001931err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001932 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001933 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001934err_put_state_owner:
1935 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 *res = NULL;
1938 return status;
1939}
1940
1941
Andy Adamson82be4172012-05-23 05:02:35 -04001942static struct nfs4_state *nfs4_do_open(struct inode *dir,
1943 struct dentry *dentry,
1944 fmode_t fmode,
1945 int flags,
1946 struct iattr *sattr,
1947 struct rpc_cred *cred,
1948 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 struct nfs4_exception exception = { };
1951 struct nfs4_state *res;
1952 int status;
1953
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001954 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001956 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1957 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (status == 0)
1959 break;
1960 /* NOTE: BAD_SEQID means the server and client disagree about the
1961 * book-keeping w.r.t. state-changing operations
1962 * (OPEN/CLOSE/LOCK/LOCKU...)
1963 * It is actually a sign of a bug on the client or on the server.
1964 *
1965 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001966 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * have unhashed the old state_owner for us, and that we can
1968 * therefore safely retry using a new one. We should still warn
1969 * the user though...
1970 */
1971 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001972 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001973 " returned a bad sequence-id error!\n",
1974 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 exception.retry = 1;
1976 continue;
1977 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001978 /*
1979 * BAD_STATEID on OPEN means that the server cancelled our
1980 * state before it received the OPEN_CONFIRM.
1981 * Recover by retrying the request as per the discussion
1982 * on Page 181 of RFC3530.
1983 */
1984 if (status == -NFS4ERR_BAD_STATEID) {
1985 exception.retry = 1;
1986 continue;
1987 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001988 if (status == -EAGAIN) {
1989 /* We must have found a delegation */
1990 exception.retry = 1;
1991 continue;
1992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1994 status, &exception));
1995 } while (exception.retry);
1996 return res;
1997}
1998
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001999static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2000 struct nfs_fattr *fattr, struct iattr *sattr,
2001 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002003 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002005 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 .iap = sattr,
2007 .server = server,
2008 .bitmask = server->attr_bitmask,
2009 };
2010 struct nfs_setattrres res = {
2011 .fattr = fattr,
2012 .server = server,
2013 };
2014 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2016 .rpc_argp = &arg,
2017 .rpc_resp = &res,
2018 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002020 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002021 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Trond Myklebust0e574af2005-10-27 22:12:38 -04002023 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Trond Myklebust4fc87962012-03-08 17:42:01 -05002025 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002026 struct nfs_lockowner lockowner = {
2027 .l_owner = current->files,
2028 .l_pid = current->tgid,
2029 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002030 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002031 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002032 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2033 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002034 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002035 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002036 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Bryan Schumaker7c513052011-03-24 17:12:24 +00002038 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002039 if (status == 0 && state != NULL)
2040 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002041 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002044static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2045 struct nfs_fattr *fattr, struct iattr *sattr,
2046 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002048 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002049 struct nfs4_exception exception = {
2050 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002051 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002052 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int err;
2054 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002055 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2056 switch (err) {
2057 case -NFS4ERR_OPENMODE:
2058 if (state && !(state->state & FMODE_WRITE)) {
2059 err = -EBADF;
2060 if (sattr->ia_valid & ATTR_OPEN)
2061 err = -EACCES;
2062 goto out;
2063 }
2064 }
2065 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002067out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return err;
2069}
2070
2071struct nfs4_closedata {
2072 struct inode *inode;
2073 struct nfs4_state *state;
2074 struct nfs_closeargs arg;
2075 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002076 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002077 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002078 bool roc;
2079 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080};
2081
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002082static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002083{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002084 struct nfs4_closedata *calldata = data;
2085 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002086 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002087
Fred Isamanf7e89172011-01-06 11:36:32 +00002088 if (calldata->roc)
2089 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002090 nfs4_put_open_state(calldata->state);
2091 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002092 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002093 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002094 kfree(calldata);
2095}
2096
Trond Myklebust88069f72009-12-08 08:33:16 -05002097static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2098 fmode_t fmode)
2099{
2100 spin_lock(&state->owner->so_lock);
2101 if (!(fmode & FMODE_READ))
2102 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2103 if (!(fmode & FMODE_WRITE))
2104 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2105 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2106 spin_unlock(&state->owner->so_lock);
2107}
2108
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002109static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002111 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 struct nfs_server *server = NFS_SERVER(calldata->inode);
2114
Chuck Levera3ca5652012-03-01 17:00:40 -05002115 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002116 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2117 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 /* hmm. we are done with the inode, and in the process of freeing
2119 * the state_owner. we keep this around to process errors
2120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 switch (task->tk_status) {
2122 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002123 if (calldata->roc)
2124 pnfs_roc_set_barrier(state->inode,
2125 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002126 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002127 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002128 nfs4_close_clear_stateid_flags(state,
2129 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 break;
2131 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002132 case -NFS4ERR_OLD_STATEID:
2133 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002135 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002138 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2139 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002141 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002142 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002143 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002146static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002148 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002149 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002150 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002151 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002152
Chuck Levera3ca5652012-03-01 17:00:40 -05002153 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002154 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002155 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002156
Trond Myklebust88069f72009-12-08 08:33:16 -05002157 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2158 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002159 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002160 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002161 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002162 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002163 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2164 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2165 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002166 }
2167 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002168 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2169 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2170 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002171 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002172 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002173 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002174
2175 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002176 /* Note: exit _without_ calling nfs4_close_done */
2177 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002178 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002179 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002180
Fred Isamanf7e89172011-01-06 11:36:32 +00002181 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002182 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002183 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002184 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002185 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002186 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002187
Trond Myklebust516a6af2005-10-27 22:12:41 -04002188 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002189 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002190 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002191 &calldata->arg.seq_args,
2192 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002193 task) != 0)
2194 nfs_release_seqid(calldata->arg.seqid);
2195 else
2196 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002197out:
2198 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002201static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002202 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002203 .rpc_call_done = nfs4_close_done,
2204 .rpc_release = nfs4_free_closedata,
2205};
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/*
2208 * It is possible for data to be read/written from a mem-mapped file
2209 * after the sys_close call (which hits the vfs layer as a flush).
2210 * This means that we can't safely call nfsv4 close on a file until
2211 * the inode is cleared. This in turn means that we are not good
2212 * NFSv4 citizens - we do not indicate to the server to update the file's
2213 * share state even when we are done with one of the three share
2214 * stateid's in the inode.
2215 *
2216 * NOTE: Caller must be holding the sp->so_owner semaphore!
2217 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002218int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002220 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002222 struct nfs4_state_owner *sp = state->owner;
2223 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2226 .rpc_cred = state->owner->so_cred,
2227 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002228 struct rpc_task_setup task_setup_data = {
2229 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002230 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002231 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002232 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002233 .flags = RPC_TASK_ASYNC,
2234 };
Trond Myklebust95121352005-10-18 14:20:12 -07002235 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002237 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002239 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002240 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002241 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002243 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002244 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002246 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002247 if (calldata->arg.seqid == NULL)
2248 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002249 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002250 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002251 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002252 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002253 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002254 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002255 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002256
Trond Myklebust1174dd12010-12-21 10:52:24 -05002257 msg.rpc_argp = &calldata->arg;
2258 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002259 task_setup_data.callback_data = calldata;
2260 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002261 if (IS_ERR(task))
2262 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002263 status = 0;
2264 if (wait)
2265 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002266 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002267 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002268out_free_calldata:
2269 kfree(calldata);
2270out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002271 nfs4_put_open_state(state);
2272 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002273 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Trond Myklebust2b484292010-09-17 10:56:51 -04002276static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002277nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct nfs4_state *state;
2280
Trond Myklebust565277f2007-10-15 18:17:53 -04002281 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002282 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2283 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002284 if (IS_ERR(state))
2285 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002286 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002287 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Trond Myklebust1185a552009-12-03 15:54:02 -05002290static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002291{
2292 if (ctx->state == NULL)
2293 return;
2294 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002295 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002296 else
Al Viro643168c2011-06-22 18:20:23 -04002297 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2301{
Benny Halevy43652ad2009-04-01 09:21:54 -04002302 struct nfs4_server_caps_arg args = {
2303 .fhandle = fhandle,
2304 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct nfs4_server_caps_res res = {};
2306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002308 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 .rpc_resp = &res,
2310 };
2311 int status;
2312
Bryan Schumaker7c513052011-03-24 17:12:24 +00002313 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (status == 0) {
2315 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002316 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2317 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2318 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2319 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2320 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2322 server->caps |= NFS_CAP_ACLS;
2323 if (res.has_links != 0)
2324 server->caps |= NFS_CAP_HARDLINKS;
2325 if (res.has_symlinks != 0)
2326 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002327 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2328 server->caps |= NFS_CAP_FILEID;
2329 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2330 server->caps |= NFS_CAP_MODE;
2331 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2332 server->caps |= NFS_CAP_NLINK;
2333 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2334 server->caps |= NFS_CAP_OWNER;
2335 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2336 server->caps |= NFS_CAP_OWNER_GROUP;
2337 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2338 server->caps |= NFS_CAP_ATIME;
2339 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2340 server->caps |= NFS_CAP_CTIME;
2341 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2342 server->caps |= NFS_CAP_MTIME;
2343
Trond Myklebusta65318b2009-03-11 14:10:28 -04002344 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2345 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2346 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002348 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return status;
2352}
2353
Trond Myklebust55a97592006-06-09 09:34:19 -04002354int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 struct nfs4_exception exception = { };
2357 int err;
2358 do {
2359 err = nfs4_handle_exception(server,
2360 _nfs4_server_capabilities(server, fhandle),
2361 &exception);
2362 } while (exception.retry);
2363 return err;
2364}
2365
2366static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2367 struct nfs_fsinfo *info)
2368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 struct nfs4_lookup_root_arg args = {
2370 .bitmask = nfs4_fattr_bitmap,
2371 };
2372 struct nfs4_lookup_res res = {
2373 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002374 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 .fh = fhandle,
2376 };
2377 struct rpc_message msg = {
2378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2379 .rpc_argp = &args,
2380 .rpc_resp = &res,
2381 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002382
Trond Myklebust0e574af2005-10-27 22:12:38 -04002383 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002384 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
2387static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2388 struct nfs_fsinfo *info)
2389{
2390 struct nfs4_exception exception = { };
2391 int err;
2392 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002393 err = _nfs4_lookup_root(server, fhandle, info);
2394 switch (err) {
2395 case 0:
2396 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002397 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002398 default:
2399 err = nfs4_handle_exception(server, err, &exception);
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002402out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return err;
2404}
2405
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002406static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2407 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2408{
2409 struct rpc_auth *auth;
2410 int ret;
2411
2412 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002413 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002414 ret = -EIO;
2415 goto out;
2416 }
2417 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002418out:
2419 return ret;
2420}
2421
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002422static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002423 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002425 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002426 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002428 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002429 if (len < 0)
2430 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002431
2432 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002433 /* AUTH_UNIX is the default flavor if none was specified,
2434 * thus has already been tried. */
2435 if (flav_array[i] == RPC_AUTH_UNIX)
2436 continue;
2437
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002438 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002439 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002440 continue;
2441 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002442 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002443 /*
2444 * -EACCESS could mean that the user doesn't have correct permissions
2445 * to access the mount. It could also mean that we tried to mount
2446 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2447 * existing mount programs don't handle -EACCES very well so it should
2448 * be mapped to -EPERM instead.
2449 */
2450 if (status == -EACCES)
2451 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002452 return status;
2453}
2454
2455/*
2456 * get the file handle for the "/" directory on the server
2457 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002458int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2459 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002460{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002461 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002462 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002463 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2464 /*
2465 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2466 * by nfs4_map_errors() as this function exits.
2467 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002468 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (status == 0)
2470 status = nfs4_server_capabilities(server, fhandle);
2471 if (status == 0)
2472 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002473 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Bryan Schumakerbae36242012-05-10 15:07:31 -04002476static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2477 struct nfs_fsinfo *info)
2478{
2479 int error;
2480 struct nfs_fattr *fattr = info->fattr;
2481
2482 error = nfs4_server_capabilities(server, mntfh);
2483 if (error < 0) {
2484 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2485 return error;
2486 }
2487
2488 error = nfs4_proc_getattr(server, mntfh, fattr);
2489 if (error < 0) {
2490 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2491 return error;
2492 }
2493
2494 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2495 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2496 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2497
2498 return error;
2499}
2500
Manoj Naik6b97fd32006-06-09 09:34:29 -04002501/*
2502 * Get locations and (maybe) other attributes of a referral.
2503 * Note that we'll actually follow the referral later when
2504 * we detect fsid mismatch in inode revalidation
2505 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002506static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2507 const struct qstr *name, struct nfs_fattr *fattr,
2508 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002509{
2510 int status = -ENOMEM;
2511 struct page *page = NULL;
2512 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002513
2514 page = alloc_page(GFP_KERNEL);
2515 if (page == NULL)
2516 goto out;
2517 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2518 if (locations == NULL)
2519 goto out;
2520
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002521 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002522 if (status != 0)
2523 goto out;
2524 /* Make sure server returned a different fsid for the referral */
2525 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002526 dprintk("%s: server did not return a different fsid for"
2527 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002528 status = -EIO;
2529 goto out;
2530 }
Andy Adamson533eb462011-06-13 18:25:56 -04002531 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2532 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002533
Andy Adamson533eb462011-06-13 18:25:56 -04002534 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002535 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002536 memset(fhandle, 0, sizeof(struct nfs_fh));
2537out:
2538 if (page)
2539 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002540 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002541 return status;
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2545{
2546 struct nfs4_getattr_arg args = {
2547 .fh = fhandle,
2548 .bitmask = server->attr_bitmask,
2549 };
2550 struct nfs4_getattr_res res = {
2551 .fattr = fattr,
2552 .server = server,
2553 };
2554 struct rpc_message msg = {
2555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2556 .rpc_argp = &args,
2557 .rpc_resp = &res,
2558 };
2559
Trond Myklebust0e574af2005-10-27 22:12:38 -04002560 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002561 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
2564static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2565{
2566 struct nfs4_exception exception = { };
2567 int err;
2568 do {
2569 err = nfs4_handle_exception(server,
2570 _nfs4_proc_getattr(server, fhandle, fattr),
2571 &exception);
2572 } while (exception.retry);
2573 return err;
2574}
2575
2576/*
2577 * The file is not closed if it is opened due to the a request to change
2578 * the size of the file. The open call will not be needed once the
2579 * VFS layer lookup-intents are implemented.
2580 *
2581 * Close is called when the inode is destroyed.
2582 * If we haven't opened the file for O_WRONLY, we
2583 * need to in the size_change case to obtain a stateid.
2584 *
2585 * Got race?
2586 * Because OPEN is always done by name in nfsv4, it is
2587 * possible that we opened a different file by the same
2588 * name. We can recognize this race condition, but we
2589 * can't do anything about it besides returning an error.
2590 *
2591 * This will be fixed with VFS changes (lookup-intent).
2592 */
2593static int
2594nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2595 struct iattr *sattr)
2596{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002597 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002598 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002599 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 int status;
2601
Benny Halevy8a1636c2010-07-14 15:43:57 -04002602 if (pnfs_ld_layoutret_on_setattr(inode))
2603 pnfs_return_layout(inode);
2604
Trond Myklebust0e574af2005-10-27 22:12:38 -04002605 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Andy Adamson26699402012-05-30 16:12:24 -04002607 /* Deal with open(O_TRUNC) */
2608 if (sattr->ia_valid & ATTR_OPEN)
2609 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2610
2611 /* Optimization: if the end result is no change, don't RPC */
2612 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2613 return 0;
2614
Trond Myklebustd5308382005-11-04 15:33:38 -05002615 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002616 if (sattr->ia_valid & ATTR_FILE) {
2617 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002618
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002619 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002620 if (ctx) {
2621 cred = ctx->cred;
2622 state = ctx->state;
2623 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002624 }
2625
2626 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002627 if (status == 0)
2628 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return status;
2630}
2631
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002632static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2633 const struct qstr *name, struct nfs_fh *fhandle,
2634 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002635{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002636 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002637 int status;
2638 struct nfs4_lookup_arg args = {
2639 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002640 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002641 .name = name,
2642 };
2643 struct nfs4_lookup_res res = {
2644 .server = server,
2645 .fattr = fattr,
2646 .fh = fhandle,
2647 };
2648 struct rpc_message msg = {
2649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2650 .rpc_argp = &args,
2651 .rpc_resp = &res,
2652 };
2653
2654 nfs_fattr_init(fattr);
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002657 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 dprintk("NFS reply lookup: %d\n", status);
2659 return status;
2660}
2661
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002662static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002663{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002664 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002665 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002666 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2667 fattr->nlink = 2;
2668}
2669
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002670static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2671 struct qstr *name, struct nfs_fh *fhandle,
2672 struct nfs_fattr *fattr)
2673{
2674 struct nfs4_exception exception = { };
2675 struct rpc_clnt *client = *clnt;
2676 int err;
2677 do {
2678 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2679 switch (err) {
2680 case -NFS4ERR_BADNAME:
2681 err = -ENOENT;
2682 goto out;
2683 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002684 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002685 goto out;
2686 case -NFS4ERR_WRONGSEC:
2687 err = -EPERM;
2688 if (client != *clnt)
2689 goto out;
2690
2691 client = nfs4_create_sec_client(client, dir, name);
2692 if (IS_ERR(client))
2693 return PTR_ERR(client);
2694
2695 exception.retry = 1;
2696 break;
2697 default:
2698 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2699 }
2700 } while (exception.retry);
2701
2702out:
2703 if (err == 0)
2704 *clnt = client;
2705 else if (client != *clnt)
2706 rpc_shutdown_client(client);
2707
2708 return err;
2709}
2710
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002711static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002712 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002714 int status;
2715 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002716
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002717 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2718 if (client != NFS_CLIENT(dir)) {
2719 rpc_shutdown_client(client);
2720 nfs_fixup_secinfo_attributes(fattr);
2721 }
2722 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002725struct rpc_clnt *
2726nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2727 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2728{
2729 int status;
2730 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2731
2732 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2733 if (status < 0) {
2734 rpc_shutdown_client(client);
2735 return ERR_PTR(status);
2736 }
2737 return client;
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2741{
Trond Myklebust76b32992007-08-10 17:45:11 -04002742 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct nfs4_accessargs args = {
2744 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002745 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002747 struct nfs4_accessres res = {
2748 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 struct rpc_message msg = {
2751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2752 .rpc_argp = &args,
2753 .rpc_resp = &res,
2754 .rpc_cred = entry->cred,
2755 };
2756 int mode = entry->mask;
2757 int status;
2758
2759 /*
2760 * Determine which access bits we want to ask for...
2761 */
2762 if (mode & MAY_READ)
2763 args.access |= NFS4_ACCESS_READ;
2764 if (S_ISDIR(inode->i_mode)) {
2765 if (mode & MAY_WRITE)
2766 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2767 if (mode & MAY_EXEC)
2768 args.access |= NFS4_ACCESS_LOOKUP;
2769 } else {
2770 if (mode & MAY_WRITE)
2771 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2772 if (mode & MAY_EXEC)
2773 args.access |= NFS4_ACCESS_EXECUTE;
2774 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002775
2776 res.fattr = nfs_alloc_fattr();
2777 if (res.fattr == NULL)
2778 return -ENOMEM;
2779
Bryan Schumaker7c513052011-03-24 17:12:24 +00002780 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002782 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002783 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002785 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return status;
2787}
2788
2789static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2790{
2791 struct nfs4_exception exception = { };
2792 int err;
2793 do {
2794 err = nfs4_handle_exception(NFS_SERVER(inode),
2795 _nfs4_proc_access(inode, entry),
2796 &exception);
2797 } while (exception.retry);
2798 return err;
2799}
2800
2801/*
2802 * TODO: For the time being, we don't try to get any attributes
2803 * along with any of the zero-copy operations READ, READDIR,
2804 * READLINK, WRITE.
2805 *
2806 * In the case of the first three, we want to put the GETATTR
2807 * after the read-type operation -- this is because it is hard
2808 * to predict the length of a GETATTR response in v4, and thus
2809 * align the READ data correctly. This means that the GETATTR
2810 * may end up partially falling into the page cache, and we should
2811 * shift it into the 'tail' of the xdr_buf before processing.
2812 * To do this efficiently, we need to know the total length
2813 * of data received, which doesn't seem to be available outside
2814 * of the RPC layer.
2815 *
2816 * In the case of WRITE, we also want to put the GETATTR after
2817 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002818 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 *
2820 * Both of these changes to the XDR layer would in fact be quite
2821 * minor, but I decided to leave them for a subsequent patch.
2822 */
2823static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2824 unsigned int pgbase, unsigned int pglen)
2825{
2826 struct nfs4_readlink args = {
2827 .fh = NFS_FH(inode),
2828 .pgbase = pgbase,
2829 .pglen = pglen,
2830 .pages = &page,
2831 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002832 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct rpc_message msg = {
2834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2835 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002836 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 };
2838
Bryan Schumaker7c513052011-03-24 17:12:24 +00002839 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2843 unsigned int pgbase, unsigned int pglen)
2844{
2845 struct nfs4_exception exception = { };
2846 int err;
2847 do {
2848 err = nfs4_handle_exception(NFS_SERVER(inode),
2849 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2850 &exception);
2851 } while (exception.retry);
2852 return err;
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002856 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858static int
2859nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002860 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002862 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 int status = 0;
2865
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002866 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2867 if (IS_ERR(ctx))
2868 return PTR_ERR(ctx);
2869
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002870 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002871 state = nfs4_do_open(dir, dentry, ctx->mode,
2872 flags, sattr, ctx->cred,
2873 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002874 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (IS_ERR(state)) {
2876 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002879 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002880 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002881 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002883 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return status;
2885}
2886
2887static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2888{
Trond Myklebust16e42952005-10-27 22:12:44 -04002889 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002890 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002892 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002894 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002895 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002896 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2899 .rpc_argp = &args,
2900 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002902 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002903
Bryan Schumaker7c513052011-03-24 17:12:24 +00002904 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002905 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002906 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 return status;
2908}
2909
2910static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2911{
2912 struct nfs4_exception exception = { };
2913 int err;
2914 do {
2915 err = nfs4_handle_exception(NFS_SERVER(dir),
2916 _nfs4_proc_remove(dir, name),
2917 &exception);
2918 } while (exception.retry);
2919 return err;
2920}
2921
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002922static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002924 struct nfs_server *server = NFS_SERVER(dir);
2925 struct nfs_removeargs *args = msg->rpc_argp;
2926 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002928 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002930 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002933static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2934{
2935 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2936 &data->args.seq_args,
2937 &data->res.seq_res,
2938 task))
2939 return;
2940 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941}
2942
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002943static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002945 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2946
Trond Myklebust14516c32010-07-31 14:29:06 -04002947 if (!nfs4_sequence_done(task, &res->seq_res))
2948 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002949 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002950 return 0;
2951 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002952 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
2954
Jeff Laytond3d41522010-09-17 17:31:57 -04002955static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2956{
2957 struct nfs_server *server = NFS_SERVER(dir);
2958 struct nfs_renameargs *arg = msg->rpc_argp;
2959 struct nfs_renameres *res = msg->rpc_resp;
2960
2961 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002962 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002963 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002964}
2965
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002966static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2967{
2968 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2969 &data->args.seq_args,
2970 &data->res.seq_res,
2971 task))
2972 return;
2973 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002974}
2975
2976static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2977 struct inode *new_dir)
2978{
2979 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2980
2981 if (!nfs4_sequence_done(task, &res->seq_res))
2982 return 0;
2983 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2984 return 0;
2985
2986 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002987 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002988 return 1;
2989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2992 struct inode *new_dir, struct qstr *new_name)
2993{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002994 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002995 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 .old_dir = NFS_FH(old_dir),
2997 .new_dir = NFS_FH(new_dir),
2998 .old_name = old_name,
2999 .new_name = new_name,
3000 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003001 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003002 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003003 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct rpc_message msg = {
3005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3006 .rpc_argp = &arg,
3007 .rpc_resp = &res,
3008 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003009 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Bryan Schumaker7c513052011-03-24 17:12:24 +00003011 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 if (!status) {
3013 update_changeattr(old_dir, &res.old_cinfo);
3014 update_changeattr(new_dir, &res.new_cinfo);
3015 }
3016 return status;
3017}
3018
3019static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3020 struct inode *new_dir, struct qstr *new_name)
3021{
3022 struct nfs4_exception exception = { };
3023 int err;
3024 do {
3025 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3026 _nfs4_proc_rename(old_dir, old_name,
3027 new_dir, new_name),
3028 &exception);
3029 } while (exception.retry);
3030 return err;
3031}
3032
3033static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3034{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003035 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct nfs4_link_arg arg = {
3037 .fh = NFS_FH(inode),
3038 .dir_fh = NFS_FH(dir),
3039 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003040 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003042 struct nfs4_link_res res = {
3043 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003044 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 struct rpc_message msg = {
3046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3047 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003048 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003050 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Trond Myklebust136f2622010-04-16 16:22:49 -04003052 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003053 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003054 goto out;
3055
Bryan Schumaker7c513052011-03-24 17:12:24 +00003056 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003057 if (!status) {
3058 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003059 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003060 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003061out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003062 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return status;
3064}
3065
3066static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3067{
3068 struct nfs4_exception exception = { };
3069 int err;
3070 do {
3071 err = nfs4_handle_exception(NFS_SERVER(inode),
3072 _nfs4_proc_link(inode, dir, name),
3073 &exception);
3074 } while (exception.retry);
3075 return err;
3076}
3077
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003078struct nfs4_createdata {
3079 struct rpc_message msg;
3080 struct nfs4_create_arg arg;
3081 struct nfs4_create_res res;
3082 struct nfs_fh fh;
3083 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003084};
3085
3086static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3087 struct qstr *name, struct iattr *sattr, u32 ftype)
3088{
3089 struct nfs4_createdata *data;
3090
3091 data = kzalloc(sizeof(*data), GFP_KERNEL);
3092 if (data != NULL) {
3093 struct nfs_server *server = NFS_SERVER(dir);
3094
3095 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3096 data->msg.rpc_argp = &data->arg;
3097 data->msg.rpc_resp = &data->res;
3098 data->arg.dir_fh = NFS_FH(dir);
3099 data->arg.server = server;
3100 data->arg.name = name;
3101 data->arg.attrs = sattr;
3102 data->arg.ftype = ftype;
3103 data->arg.bitmask = server->attr_bitmask;
3104 data->res.server = server;
3105 data->res.fh = &data->fh;
3106 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003108 }
3109 return data;
3110}
3111
3112static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3113{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003114 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003115 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003116 if (status == 0) {
3117 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003118 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3119 }
3120 return status;
3121}
3122
3123static void nfs4_free_createdata(struct nfs4_createdata *data)
3124{
3125 kfree(data);
3126}
3127
Chuck Lever4f390c12006-08-22 20:06:22 -04003128static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003129 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131 struct nfs4_createdata *data;
3132 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Chuck Lever94a6d752006-08-22 20:06:23 -04003134 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003135 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003136
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003137 status = -ENOMEM;
3138 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3139 if (data == NULL)
3140 goto out;
3141
3142 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3143 data->arg.u.symlink.pages = &page;
3144 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003146 status = nfs4_do_create(dir, dentry, data);
3147
3148 nfs4_free_createdata(data);
3149out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return status;
3151}
3152
Chuck Lever4f390c12006-08-22 20:06:22 -04003153static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003154 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 struct nfs4_exception exception = { };
3157 int err;
3158 do {
3159 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003160 _nfs4_proc_symlink(dir, dentry, page,
3161 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 &exception);
3163 } while (exception.retry);
3164 return err;
3165}
3166
3167static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3168 struct iattr *sattr)
3169{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003170 struct nfs4_createdata *data;
3171 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003173 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3174 if (data == NULL)
3175 goto out;
3176
3177 status = nfs4_do_create(dir, dentry, data);
3178
3179 nfs4_free_createdata(data);
3180out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return status;
3182}
3183
3184static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3185 struct iattr *sattr)
3186{
3187 struct nfs4_exception exception = { };
3188 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003189
3190 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 do {
3192 err = nfs4_handle_exception(NFS_SERVER(dir),
3193 _nfs4_proc_mkdir(dir, dentry, sattr),
3194 &exception);
3195 } while (exception.retry);
3196 return err;
3197}
3198
3199static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003200 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 struct inode *dir = dentry->d_inode;
3203 struct nfs4_readdir_arg args = {
3204 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003205 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 .pgbase = 0,
3207 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003208 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003209 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 };
3211 struct nfs4_readdir_res res;
3212 struct rpc_message msg = {
3213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3214 .rpc_argp = &args,
3215 .rpc_resp = &res,
3216 .rpc_cred = cred,
3217 };
3218 int status;
3219
Harvey Harrison3110ff82008-05-02 13:42:44 -07003220 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003221 dentry->d_parent->d_name.name,
3222 dentry->d_name.name,
3223 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003224 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003226 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 -05003227 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003228 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003229 status += args.pgbase;
3230 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003231
3232 nfs_invalidate_atime(dir);
3233
Harvey Harrison3110ff82008-05-02 13:42:44 -07003234 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return status;
3236}
3237
3238static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003239 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
3241 struct nfs4_exception exception = { };
3242 int err;
3243 do {
3244 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3245 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003246 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 &exception);
3248 } while (exception.retry);
3249 return err;
3250}
3251
3252static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3253 struct iattr *sattr, dev_t rdev)
3254{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003255 struct nfs4_createdata *data;
3256 int mode = sattr->ia_mode;
3257 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003259 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3260 if (data == NULL)
3261 goto out;
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003264 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003266 data->arg.ftype = NF4BLK;
3267 data->arg.u.device.specdata1 = MAJOR(rdev);
3268 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 }
3270 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003271 data->arg.ftype = NF4CHR;
3272 data->arg.u.device.specdata1 = MAJOR(rdev);
3273 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003274 } else if (!S_ISSOCK(mode)) {
3275 status = -EINVAL;
3276 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003279 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003280out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003281 nfs4_free_createdata(data);
3282out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return status;
3284}
3285
3286static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3287 struct iattr *sattr, dev_t rdev)
3288{
3289 struct nfs4_exception exception = { };
3290 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003291
3292 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 do {
3294 err = nfs4_handle_exception(NFS_SERVER(dir),
3295 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3296 &exception);
3297 } while (exception.retry);
3298 return err;
3299}
3300
3301static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3302 struct nfs_fsstat *fsstat)
3303{
3304 struct nfs4_statfs_arg args = {
3305 .fh = fhandle,
3306 .bitmask = server->attr_bitmask,
3307 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003308 struct nfs4_statfs_res res = {
3309 .fsstat = fsstat,
3310 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 struct rpc_message msg = {
3312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3313 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003314 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 };
3316
Trond Myklebust0e574af2005-10-27 22:12:38 -04003317 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003318 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
3321static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3322{
3323 struct nfs4_exception exception = { };
3324 int err;
3325 do {
3326 err = nfs4_handle_exception(server,
3327 _nfs4_proc_statfs(server, fhandle, fsstat),
3328 &exception);
3329 } while (exception.retry);
3330 return err;
3331}
3332
3333static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3334 struct nfs_fsinfo *fsinfo)
3335{
3336 struct nfs4_fsinfo_arg args = {
3337 .fh = fhandle,
3338 .bitmask = server->attr_bitmask,
3339 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003340 struct nfs4_fsinfo_res res = {
3341 .fsinfo = fsinfo,
3342 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 struct rpc_message msg = {
3344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3345 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003346 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 };
3348
Bryan Schumaker7c513052011-03-24 17:12:24 +00003349 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
3352static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3353{
3354 struct nfs4_exception exception = { };
3355 int err;
3356
3357 do {
3358 err = nfs4_handle_exception(server,
3359 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3360 &exception);
3361 } while (exception.retry);
3362 return err;
3363}
3364
3365static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3366{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003367 int error;
3368
Trond Myklebust0e574af2005-10-27 22:12:38 -04003369 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003370 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003371 if (error == 0) {
3372 /* block layout checks this! */
3373 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003374 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003375 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003376
3377 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
3380static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3381 struct nfs_pathconf *pathconf)
3382{
3383 struct nfs4_pathconf_arg args = {
3384 .fh = fhandle,
3385 .bitmask = server->attr_bitmask,
3386 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003387 struct nfs4_pathconf_res res = {
3388 .pathconf = pathconf,
3389 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 struct rpc_message msg = {
3391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3392 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003393 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 };
3395
3396 /* None of the pathconf attributes are mandatory to implement */
3397 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3398 memset(pathconf, 0, sizeof(*pathconf));
3399 return 0;
3400 }
3401
Trond Myklebust0e574af2005-10-27 22:12:38 -04003402 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003403 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
3406static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3407 struct nfs_pathconf *pathconf)
3408{
3409 struct nfs4_exception exception = { };
3410 int err;
3411
3412 do {
3413 err = nfs4_handle_exception(server,
3414 _nfs4_proc_pathconf(server, fhandle, pathconf),
3415 &exception);
3416 } while (exception.retry);
3417 return err;
3418}
3419
Benny Halevyd20581a2011-05-22 19:52:03 +03003420void __nfs4_read_done_cb(struct nfs_read_data *data)
3421{
Fred Isamancd841602012-04-20 14:47:44 -04003422 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003423}
3424
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003425static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Fred Isamancd841602012-04-20 14:47:44 -04003427 struct nfs_server *server = NFS_SERVER(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, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003430 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003431 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003433
Benny Halevyd20581a2011-05-22 19:52:03 +03003434 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003436 renew_lease(server, data->timestamp);
3437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003440static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3441{
3442
3443 dprintk("--> %s\n", __func__);
3444
3445 if (!nfs4_sequence_done(task, &data->res.seq_res))
3446 return -EAGAIN;
3447
Benny Halevyd20581a2011-05-22 19:52:03 +03003448 return data->read_done_cb ? data->read_done_cb(task, data) :
3449 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003450}
3451
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003452static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003455 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003456 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003457 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003460static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3461{
Fred Isamancd841602012-04-20 14:47:44 -04003462 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003463 &data->args.seq_args,
3464 &data->res.seq_res,
3465 task))
3466 return;
3467 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
Fred Isamanb029bc92011-03-03 15:13:42 +00003470static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
Fred Isamancd841602012-04-20 14:47:44 -04003472 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003474 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003475 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003476 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003478 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003480 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003481 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Fred Isamanb029bc92011-03-03 15:13:42 +00003485static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3486{
3487 if (!nfs4_sequence_done(task, &data->res.seq_res))
3488 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003489 return data->write_done_cb ? data->write_done_cb(task, data) :
3490 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003491}
3492
Trond Myklebust5a37f852012-04-28 14:55:16 -04003493static
3494bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003495{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003496 const struct nfs_pgio_header *hdr = data->header;
3497
3498 /* Don't request attributes for pNFS or O_DIRECT writes */
3499 if (data->ds_clp != NULL || hdr->dreq != NULL)
3500 return false;
3501 /* Otherwise, request attributes if and only if we don't hold
3502 * a delegation
3503 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003504 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003505}
Fred Isamana69aef12011-03-03 15:13:47 +00003506
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003507static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Fred Isamancd841602012-04-20 14:47:44 -04003509 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003510
Trond Myklebust5a37f852012-04-28 14:55:16 -04003511 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003512 data->args.bitmask = NULL;
3513 data->res.fattr = NULL;
3514 } else
3515 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003516
Fred Isamanb029bc92011-03-03 15:13:42 +00003517 if (!data->write_done_cb)
3518 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003519 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 data->timestamp = jiffies;
3521
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003522 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
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
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003526static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3527{
Fred Isamancd841602012-04-20 14:47:44 -04003528 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003529 &data->args.seq_args,
3530 &data->res.seq_res,
3531 task))
3532 return;
3533 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Fred Isaman0b7c0152012-04-20 14:47:39 -04003536static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3539 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003540 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return;
3543 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003544}
3545
Fred Isaman0b7c0152012-04-20 14:47:39 -04003546static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
3548 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003549
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003550 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003551 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003552 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
3556
Fred Isaman0b7c0152012-04-20 14:47:39 -04003557static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003558{
3559 if (!nfs4_sequence_done(task, &data->res.seq_res))
3560 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003561 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003562}
3563
Fred Isaman0b7c0152012-04-20 14:47:39 -04003564static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003566 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003567
Fred Isaman0b7c0152012-04-20 14:47:39 -04003568 if (data->commit_done_cb == NULL)
3569 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003570 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003571 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003572 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003575struct nfs4_renewdata {
3576 struct nfs_client *client;
3577 unsigned long timestamp;
3578};
3579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580/*
3581 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3582 * standalone procedure for queueing an asynchronous RENEW.
3583 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003584static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003585{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003586 struct nfs4_renewdata *data = calldata;
3587 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003588
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003589 if (atomic_read(&clp->cl_count) > 1)
3590 nfs4_schedule_state_renewal(clp);
3591 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003592 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003593}
3594
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003595static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003597 struct nfs4_renewdata *data = calldata;
3598 struct nfs_client *clp = data->client;
3599 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
3601 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003602 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003603 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3604 return;
3605 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003606 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003607 return;
3608 }
3609 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003611 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
3613
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003614static const struct rpc_call_ops nfs4_renew_ops = {
3615 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003616 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003617};
3618
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003619static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 struct rpc_message msg = {
3622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3623 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003624 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003626 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003628 if (renew_flags == 0)
3629 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003630 if (!atomic_inc_not_zero(&clp->cl_count))
3631 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003632 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003633 if (data == NULL)
3634 return -ENOMEM;
3635 data->client = clp;
3636 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003638 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003641static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
3643 struct rpc_message msg = {
3644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3645 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003646 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 };
3648 unsigned long now = jiffies;
3649 int status;
3650
3651 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3652 if (status < 0)
3653 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003654 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return 0;
3656}
3657
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003658static inline int nfs4_server_supports_acls(struct nfs_server *server)
3659{
3660 return (server->caps & NFS_CAP_ACLS)
3661 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3662 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3663}
3664
Trond Myklebust21f498c2012-08-24 10:59:25 -04003665/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3666 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003667 * the stack.
3668 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003669#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003670
Neil Hormane9e3d722011-03-04 19:26:03 -05003671static int buf_to_pages_noslab(const void *buf, size_t buflen,
3672 struct page **pages, unsigned int *pgbase)
3673{
3674 struct page *newpage, **spages;
3675 int rc = 0;
3676 size_t len;
3677 spages = pages;
3678
3679 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003680 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003681 newpage = alloc_page(GFP_KERNEL);
3682
3683 if (newpage == NULL)
3684 goto unwind;
3685 memcpy(page_address(newpage), buf, len);
3686 buf += len;
3687 buflen -= len;
3688 *pages++ = newpage;
3689 rc++;
3690 } while (buflen != 0);
3691
3692 return rc;
3693
3694unwind:
3695 for(; rc > 0; rc--)
3696 __free_page(spages[rc-1]);
3697 return -ENOMEM;
3698}
3699
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003700struct nfs4_cached_acl {
3701 int cached;
3702 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003703 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003704};
3705
3706static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003707{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003708 struct nfs_inode *nfsi = NFS_I(inode);
3709
3710 spin_lock(&inode->i_lock);
3711 kfree(nfsi->nfs4_acl);
3712 nfsi->nfs4_acl = acl;
3713 spin_unlock(&inode->i_lock);
3714}
3715
3716static void nfs4_zap_acl_attr(struct inode *inode)
3717{
3718 nfs4_set_cached_acl(inode, NULL);
3719}
3720
3721static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3722{
3723 struct nfs_inode *nfsi = NFS_I(inode);
3724 struct nfs4_cached_acl *acl;
3725 int ret = -ENOENT;
3726
3727 spin_lock(&inode->i_lock);
3728 acl = nfsi->nfs4_acl;
3729 if (acl == NULL)
3730 goto out;
3731 if (buf == NULL) /* user is just asking for length */
3732 goto out_len;
3733 if (acl->cached == 0)
3734 goto out;
3735 ret = -ERANGE; /* see getxattr(2) man page */
3736 if (acl->len > buflen)
3737 goto out;
3738 memcpy(buf, acl->data, acl->len);
3739out_len:
3740 ret = acl->len;
3741out:
3742 spin_unlock(&inode->i_lock);
3743 return ret;
3744}
3745
Sachin Prabhu5794d212012-04-17 14:36:40 +01003746static 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 +00003747{
3748 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003749 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003750
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003751 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003752 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003753 if (acl == NULL)
3754 goto out;
3755 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003756 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003757 } else {
3758 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3759 if (acl == NULL)
3760 goto out;
3761 acl->cached = 0;
3762 }
3763 acl->len = acl_len;
3764out:
3765 nfs4_set_cached_acl(inode, acl);
3766}
3767
Andy Adamsonbf118a32011-12-07 11:55:27 -05003768/*
3769 * The getxattr API returns the required buffer length when called with a
3770 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3771 * the required buf. On a NULL buf, we send a page of data to the server
3772 * guessing that the ACL request can be serviced by a page. If so, we cache
3773 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3774 * the cache. If not so, we throw away the page, and cache the required
3775 * length. The next getxattr call will then produce another round trip to
3776 * the server, this time with the input buf of the required size.
3777 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003778static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003779{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003780 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003781 struct nfs_getaclargs args = {
3782 .fh = NFS_FH(inode),
3783 .acl_pages = pages,
3784 .acl_len = buflen,
3785 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003786 struct nfs_getaclres res = {
3787 .acl_len = buflen,
3788 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003789 struct rpc_message msg = {
3790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3791 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003792 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003793 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003794 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3795 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003796
Andy Adamsonbf118a32011-12-07 11:55:27 -05003797 /* As long as we're doing a round trip to the server anyway,
3798 * let's be prepared for a page of acl data. */
3799 if (npages == 0)
3800 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003801 if (npages > ARRAY_SIZE(pages))
3802 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003803
Andy Adamsonbf118a32011-12-07 11:55:27 -05003804 for (i = 0; i < npages; i++) {
3805 pages[i] = alloc_page(GFP_KERNEL);
3806 if (!pages[i])
3807 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003808 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003809
3810 /* for decoding across pages */
3811 res.acl_scratch = alloc_page(GFP_KERNEL);
3812 if (!res.acl_scratch)
3813 goto out_free;
3814
Andy Adamsonbf118a32011-12-07 11:55:27 -05003815 args.acl_len = npages * PAGE_SIZE;
3816 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003817
Peng Taode040be2012-01-10 22:42:47 +08003818 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003819 __func__, buf, buflen, npages, args.acl_len);
3820 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3821 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003822 if (ret)
3823 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003824
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003825 /* Handle the case where the passed-in buffer is too short */
3826 if (res.acl_flags & NFS4_ACL_TRUNC) {
3827 /* Did the user only issue a request for the acl length? */
3828 if (buf == NULL)
3829 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003830 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003831 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003832 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003833 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3834 if (buf)
3835 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3836out_ok:
3837 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003838out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003839 for (i = 0; i < npages; i++)
3840 if (pages[i])
3841 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003842 if (res.acl_scratch)
3843 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003844 return ret;
3845}
3846
Trond Myklebust16b42892006-08-24 12:27:15 -04003847static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3848{
3849 struct nfs4_exception exception = { };
3850 ssize_t ret;
3851 do {
3852 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3853 if (ret >= 0)
3854 break;
3855 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3856 } while (exception.retry);
3857 return ret;
3858}
3859
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3861{
3862 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003863 int ret;
3864
3865 if (!nfs4_server_supports_acls(server))
3866 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867 ret = nfs_revalidate_inode(server, inode);
3868 if (ret < 0)
3869 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003870 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3871 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003872 ret = nfs4_read_cached_acl(inode, buf, buflen);
3873 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003874 /* -ENOENT is returned if there is no ACL or if there is an ACL
3875 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003876 return ret;
3877 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003878}
3879
Trond Myklebust16b42892006-08-24 12:27:15 -04003880static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003881{
3882 struct nfs_server *server = NFS_SERVER(inode);
3883 struct page *pages[NFS4ACL_MAXPAGES];
3884 struct nfs_setaclargs arg = {
3885 .fh = NFS_FH(inode),
3886 .acl_pages = pages,
3887 .acl_len = buflen,
3888 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003889 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003890 struct rpc_message msg = {
3891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3892 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003893 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003894 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003895 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003896 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003897
3898 if (!nfs4_server_supports_acls(server))
3899 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003900 if (npages > ARRAY_SIZE(pages))
3901 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003902 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3903 if (i < 0)
3904 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003905 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003906 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003907
3908 /*
3909 * Free each page after tx, so the only ref left is
3910 * held by the network stack
3911 */
3912 for (; i > 0; i--)
3913 put_page(pages[i-1]);
3914
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003915 /*
3916 * Acl update can result in inode attribute update.
3917 * so mark the attribute cache invalid.
3918 */
3919 spin_lock(&inode->i_lock);
3920 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3921 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003922 nfs_access_zap_cache(inode);
3923 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003924 return ret;
3925}
3926
Trond Myklebust16b42892006-08-24 12:27:15 -04003927static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3928{
3929 struct nfs4_exception exception = { };
3930 int err;
3931 do {
3932 err = nfs4_handle_exception(NFS_SERVER(inode),
3933 __nfs4_proc_set_acl(inode, buf, buflen),
3934 &exception);
3935 } while (exception.retry);
3936 return err;
3937}
3938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003940nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003942 struct nfs_client *clp = server->nfs_client;
3943
3944 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 return 0;
3946 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003947 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003948 case -NFS4ERR_ADMIN_REVOKED:
3949 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003950 if (state == NULL)
3951 break;
3952 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003953 case -NFS4ERR_OPENMODE:
3954 if (state == NULL)
3955 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003956 nfs4_schedule_stateid_recovery(server, state);
3957 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003958 case -NFS4ERR_EXPIRED:
3959 if (state != NULL)
3960 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003962 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003963 nfs4_schedule_lease_recovery(clp);
3964 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003965#if defined(CONFIG_NFS_V4_1)
3966 case -NFS4ERR_BADSESSION:
3967 case -NFS4ERR_BADSLOT:
3968 case -NFS4ERR_BAD_HIGH_SLOT:
3969 case -NFS4ERR_DEADSESSION:
3970 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3971 case -NFS4ERR_SEQ_FALSE_RETRY:
3972 case -NFS4ERR_SEQ_MISORDERED:
3973 dprintk("%s ERROR %d, Reset session\n", __func__,
3974 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003975 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003976 task->tk_status = 0;
3977 return -EAGAIN;
3978#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003980 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003981 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003982 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3984 task->tk_status = 0;
3985 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003986 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 case -NFS4ERR_OLD_STATEID:
3988 task->tk_status = 0;
3989 return -EAGAIN;
3990 }
3991 task->tk_status = nfs4_map_errors(task->tk_status);
3992 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003993wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003994 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003995 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3996 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3997 task->tk_status = 0;
3998 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
Chuck Leverf0920752012-05-21 22:45:41 -04004001static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4002 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004003{
4004 __be32 verf[2];
4005
Chuck Lever2c820d92012-05-21 22:45:33 -04004006 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4007 /* An impossible timestamp guarantees this value
4008 * will never match a generated boot time. */
4009 verf[0] = 0;
4010 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4011 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004012 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4013 verf[0] = (__be32)nn->boot_time.tv_sec;
4014 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004015 }
Chuck Levercd937102012-03-02 17:14:31 -05004016 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4017}
4018
Chuck Levere984a552012-09-14 17:24:21 -04004019static unsigned int
4020nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4021 char *buf, size_t len)
4022{
4023 unsigned int result;
4024
4025 rcu_read_lock();
4026 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4027 clp->cl_ipaddr,
4028 rpc_peeraddr2str(clp->cl_rpcclient,
4029 RPC_DISPLAY_ADDR),
4030 rpc_peeraddr2str(clp->cl_rpcclient,
4031 RPC_DISPLAY_PROTO));
4032 rcu_read_unlock();
4033 return result;
4034}
4035
4036static unsigned int
4037nfs4_init_uniform_client_string(const struct nfs_client *clp,
4038 char *buf, size_t len)
4039{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004040 char *nodename = clp->cl_rpcclient->cl_nodename;
4041
4042 if (nfs4_client_id_uniquifier[0] != '\0')
4043 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004044 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4045 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004046 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004047}
4048
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004049/**
4050 * nfs4_proc_setclientid - Negotiate client ID
4051 * @clp: state data structure
4052 * @program: RPC program for NFSv4 callback service
4053 * @port: IP port number for NFS4 callback service
4054 * @cred: RPC credential to use for this call
4055 * @res: where to place the result
4056 *
4057 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4058 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004059int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4060 unsigned short port, struct rpc_cred *cred,
4061 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
4063 nfs4_verifier sc_verifier;
4064 struct nfs4_setclientid setclientid = {
4065 .sc_verifier = &sc_verifier,
4066 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004067 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 };
4069 struct rpc_message msg = {
4070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4071 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004072 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004073 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004075 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Chuck Leverde734832012-07-11 16:30:50 -04004077 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004078 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004079 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4080 setclientid.sc_name_len =
4081 nfs4_init_uniform_client_string(clp,
4082 setclientid.sc_name,
4083 sizeof(setclientid.sc_name));
4084 else
4085 setclientid.sc_name_len =
4086 nfs4_init_nonuniform_client_string(clp,
4087 setclientid.sc_name,
4088 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004089 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004090 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004091 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004092 sizeof(setclientid.sc_netid),
4093 rpc_peeraddr2str(clp->cl_rpcclient,
4094 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004095 rcu_read_unlock();
4096 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004097 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 clp->cl_ipaddr, port >> 8, port & 255);
4099
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004100 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4101 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4102 setclientid.sc_name_len, setclientid.sc_name);
4103 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4104 dprintk("NFS reply setclientid: %d\n", status);
4105 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004108/**
4109 * nfs4_proc_setclientid_confirm - Confirm client ID
4110 * @clp: state data structure
4111 * @res: result of a previous SETCLIENTID
4112 * @cred: RPC credential to use for this call
4113 *
4114 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4115 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004116int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004117 struct nfs4_setclientid_res *arg,
4118 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
4120 struct nfs_fsinfo fsinfo;
4121 struct rpc_message msg = {
4122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004123 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004125 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 };
4127 unsigned long now;
4128 int status;
4129
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004130 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4131 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4132 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004134 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 if (status == 0) {
4136 spin_lock(&clp->cl_lock);
4137 clp->cl_lease_time = fsinfo.lease_time * HZ;
4138 clp->cl_last_renewal = now;
4139 spin_unlock(&clp->cl_lock);
4140 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004141 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return status;
4143}
4144
Trond Myklebustfe650402006-01-03 09:55:18 +01004145struct nfs4_delegreturndata {
4146 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004147 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004148 struct nfs_fh fh;
4149 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004150 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004151 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004152 int rpc_status;
4153};
4154
Trond Myklebustfe650402006-01-03 09:55:18 +01004155static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4156{
4157 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004158
Trond Myklebust14516c32010-07-31 14:29:06 -04004159 if (!nfs4_sequence_done(task, &data->res.seq_res))
4160 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004161
Ricardo Labiaga79708862009-12-07 09:23:21 -05004162 switch (task->tk_status) {
4163 case -NFS4ERR_STALE_STATEID:
4164 case -NFS4ERR_EXPIRED:
4165 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004166 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004167 break;
4168 default:
4169 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4170 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004171 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004172 return;
4173 }
4174 }
4175 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004176}
4177
4178static void nfs4_delegreturn_release(void *calldata)
4179{
Trond Myklebustfe650402006-01-03 09:55:18 +01004180 kfree(calldata);
4181}
4182
Andy Adamson938e1012009-04-01 09:22:28 -04004183#if defined(CONFIG_NFS_V4_1)
4184static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4185{
4186 struct nfs4_delegreturndata *d_data;
4187
4188 d_data = (struct nfs4_delegreturndata *)data;
4189
Trond Myklebust035168ab2010-06-16 09:52:26 -04004190 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004191 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004192 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004193 return;
4194 rpc_call_start(task);
4195}
4196#endif /* CONFIG_NFS_V4_1 */
4197
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004198static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004199#if defined(CONFIG_NFS_V4_1)
4200 .rpc_call_prepare = nfs4_delegreturn_prepare,
4201#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004202 .rpc_call_done = nfs4_delegreturn_done,
4203 .rpc_release = nfs4_delegreturn_release,
4204};
4205
Trond Myklebuste6f81072008-01-24 18:14:34 -05004206static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004207{
4208 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004209 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004210 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004211 struct rpc_message msg = {
4212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4213 .rpc_cred = cred,
4214 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004215 struct rpc_task_setup task_setup_data = {
4216 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004217 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004218 .callback_ops = &nfs4_delegreturn_ops,
4219 .flags = RPC_TASK_ASYNC,
4220 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004221 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004222
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004223 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004224 if (data == NULL)
4225 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004226 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004227 data->args.fhandle = &data->fh;
4228 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004229 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004230 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004231 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004232 data->res.fattr = &data->fattr;
4233 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004234 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004235 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004236 data->rpc_status = 0;
4237
Trond Myklebustc970aa82007-07-14 15:39:59 -04004238 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004239 msg.rpc_argp = &data->args;
4240 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004241 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004242 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004243 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004244 if (!issync)
4245 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004246 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004247 if (status != 0)
4248 goto out;
4249 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004250 if (status == 0)
4251 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4252 else
4253 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004254out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004255 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004256 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257}
4258
Trond Myklebuste6f81072008-01-24 18:14:34 -05004259int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
4261 struct nfs_server *server = NFS_SERVER(inode);
4262 struct nfs4_exception exception = { };
4263 int err;
4264 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004265 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 switch (err) {
4267 case -NFS4ERR_STALE_STATEID:
4268 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 case 0:
4270 return 0;
4271 }
4272 err = nfs4_handle_exception(server, err, &exception);
4273 } while (exception.retry);
4274 return err;
4275}
4276
4277#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4278#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4279
4280/*
4281 * sleep, with exponential backoff, and retry the LOCK operation.
4282 */
4283static unsigned long
4284nfs4_set_lock_task_retry(unsigned long timeout)
4285{
Jeff Laytond3103102011-12-01 22:44:39 +01004286 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 timeout <<= 1;
4288 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4289 return NFS4_LOCK_MAXTIMEOUT;
4290 return timeout;
4291}
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4294{
4295 struct inode *inode = state->inode;
4296 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004297 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004298 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004300 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004302 struct nfs_lockt_res res = {
4303 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 };
4305 struct rpc_message msg = {
4306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4307 .rpc_argp = &arg,
4308 .rpc_resp = &res,
4309 .rpc_cred = state->owner->so_cred,
4310 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 struct nfs4_lock_state *lsp;
4312 int status;
4313
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004314 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004315 status = nfs4_set_lock_state(state, request);
4316 if (status != 0)
4317 goto out;
4318 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004319 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004320 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004321 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004322 switch (status) {
4323 case 0:
4324 request->fl_type = F_UNLCK;
4325 break;
4326 case -NFS4ERR_DENIED:
4327 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004329 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004330out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return status;
4332}
4333
4334static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4335{
4336 struct nfs4_exception exception = { };
4337 int err;
4338
4339 do {
4340 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4341 _nfs4_proc_getlk(state, cmd, request),
4342 &exception);
4343 } while (exception.retry);
4344 return err;
4345}
4346
4347static int do_vfs_lock(struct file *file, struct file_lock *fl)
4348{
4349 int res = 0;
4350 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4351 case FL_POSIX:
4352 res = posix_lock_file_wait(file, fl);
4353 break;
4354 case FL_FLOCK:
4355 res = flock_lock_file_wait(file, fl);
4356 break;
4357 default:
4358 BUG();
4359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return res;
4361}
4362
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004363struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004364 struct nfs_locku_args arg;
4365 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004366 struct nfs4_lock_state *lsp;
4367 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004368 struct file_lock fl;
4369 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004370 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004371};
4372
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004373static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4374 struct nfs_open_context *ctx,
4375 struct nfs4_lock_state *lsp,
4376 struct nfs_seqid *seqid)
4377{
4378 struct nfs4_unlockdata *p;
4379 struct inode *inode = lsp->ls_state->inode;
4380
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004381 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004382 if (p == NULL)
4383 return NULL;
4384 p->arg.fh = NFS_FH(inode);
4385 p->arg.fl = &p->fl;
4386 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004387 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004388 p->arg.stateid = &lsp->ls_stateid;
4389 p->lsp = lsp;
4390 atomic_inc(&lsp->ls_count);
4391 /* Ensure we don't close file until we're done freeing locks! */
4392 p->ctx = get_nfs_open_context(ctx);
4393 memcpy(&p->fl, fl, sizeof(p->fl));
4394 p->server = NFS_SERVER(inode);
4395 return p;
4396}
4397
Trond Myklebust06f814a2006-01-03 09:55:07 +01004398static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004399{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004400 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004401 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004402 nfs4_put_lock_state(calldata->lsp);
4403 put_nfs_open_context(calldata->ctx);
4404 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004405}
4406
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004407static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004408{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004409 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004410
Trond Myklebust14516c32010-07-31 14:29:06 -04004411 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4412 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004413 switch (task->tk_status) {
4414 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004415 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4416 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004417 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004418 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004419 case -NFS4ERR_BAD_STATEID:
4420 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004421 case -NFS4ERR_STALE_STATEID:
4422 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004423 break;
4424 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004425 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004426 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004427 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004428 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004429}
4430
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004431static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004432{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004433 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004435 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004436 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004437 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004438 /* Note: exit _without_ running nfs4_locku_done */
4439 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004440 return;
4441 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004442 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004443 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004444 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004445 &calldata->res.seq_res,
4446 task) != 0)
4447 nfs_release_seqid(calldata->arg.seqid);
4448 else
4449 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450}
4451
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004452static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004453 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004454 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004455 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004456};
4457
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004458static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4459 struct nfs_open_context *ctx,
4460 struct nfs4_lock_state *lsp,
4461 struct nfs_seqid *seqid)
4462{
4463 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004464 struct rpc_message msg = {
4465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4466 .rpc_cred = ctx->cred,
4467 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004468 struct rpc_task_setup task_setup_data = {
4469 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004470 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004471 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004472 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004473 .flags = RPC_TASK_ASYNC,
4474 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475
Frank Filz137d6ac2007-07-09 15:32:29 -07004476 /* Ensure this is an unlock - when canceling a lock, the
4477 * canceled lock is passed in, and it won't be an unlock.
4478 */
4479 fl->fl_type = F_UNLCK;
4480
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4482 if (data == NULL) {
4483 nfs_free_seqid(seqid);
4484 return ERR_PTR(-ENOMEM);
4485 }
4486
Trond Myklebust9d12b212012-01-17 22:04:25 -05004487 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004488 msg.rpc_argp = &data->arg;
4489 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004490 task_setup_data.callback_data = data;
4491 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004492}
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4495{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004496 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004497 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004498 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004499 struct rpc_task *task;
4500 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004501 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Trond Myklebust9b073572006-06-29 16:38:34 -04004503 status = nfs4_set_lock_state(state, request);
4504 /* Unlock _before_ we do the RPC call */
4505 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004506 down_read(&nfsi->rwsem);
4507 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4508 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004509 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004510 }
4511 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004512 if (status != 0)
4513 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004514 /* Is this a delegated lock? */
4515 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004516 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004517 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004518 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004519 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004520 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004521 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004522 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523 status = PTR_ERR(task);
4524 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004525 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004526 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004527 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004528out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004529 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004530 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531}
4532
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533struct nfs4_lockdata {
4534 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004535 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004536 struct nfs4_lock_state *lsp;
4537 struct nfs_open_context *ctx;
4538 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004539 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 int rpc_status;
4541 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004542 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004543};
4544
4545static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004546 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4547 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004548{
4549 struct nfs4_lockdata *p;
4550 struct inode *inode = lsp->ls_state->inode;
4551 struct nfs_server *server = NFS_SERVER(inode);
4552
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004553 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554 if (p == NULL)
4555 return NULL;
4556
4557 p->arg.fh = NFS_FH(inode);
4558 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004559 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004560 if (p->arg.open_seqid == NULL)
4561 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004562 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004563 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004564 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004565 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004566 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004567 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004568 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004569 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004570 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004571 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004572 atomic_inc(&lsp->ls_count);
4573 p->ctx = get_nfs_open_context(ctx);
4574 memcpy(&p->fl, fl, sizeof(p->fl));
4575 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004576out_free_seqid:
4577 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578out_free:
4579 kfree(p);
4580 return NULL;
4581}
4582
4583static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4584{
4585 struct nfs4_lockdata *data = calldata;
4586 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Harvey Harrison3110ff82008-05-02 13:42:44 -07004588 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004589 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4590 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591 /* Do we need to do an open_to_lock_owner? */
4592 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004593 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004594 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 data->arg.open_stateid = &state->stateid;
4596 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004597 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004598 } else
4599 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004600 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004601 if (nfs4_setup_sequence(data->server,
4602 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004603 &data->res.seq_res,
4604 task) == 0) {
4605 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004606 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004607 }
4608 nfs_release_seqid(data->arg.open_seqid);
4609out_release_lock_seqid:
4610 nfs_release_seqid(data->arg.lock_seqid);
4611 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612}
4613
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004614static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4615{
4616 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4617 nfs4_lock_prepare(task, calldata);
4618}
4619
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004620static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4621{
4622 struct nfs4_lockdata *data = calldata;
4623
Harvey Harrison3110ff82008-05-02 13:42:44 -07004624 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625
Trond Myklebust14516c32010-07-31 14:29:06 -04004626 if (!nfs4_sequence_done(task, &data->res.seq_res))
4627 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004628
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004629 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004630 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004631 if (data->rpc_status == 0)
4632 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4633 else
4634 goto out;
4635 }
4636 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004637 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004638 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004639 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004641out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004642 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643}
4644
4645static void nfs4_lock_release(void *calldata)
4646{
4647 struct nfs4_lockdata *data = calldata;
4648
Harvey Harrison3110ff82008-05-02 13:42:44 -07004649 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004650 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651 if (data->cancelled != 0) {
4652 struct rpc_task *task;
4653 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4654 data->arg.lock_seqid);
4655 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004656 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004657 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004658 } else
4659 nfs_free_seqid(data->arg.lock_seqid);
4660 nfs4_put_lock_state(data->lsp);
4661 put_nfs_open_context(data->ctx);
4662 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004663 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004664}
4665
4666static const struct rpc_call_ops nfs4_lock_ops = {
4667 .rpc_call_prepare = nfs4_lock_prepare,
4668 .rpc_call_done = nfs4_lock_done,
4669 .rpc_release = nfs4_lock_release,
4670};
4671
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004672static const struct rpc_call_ops nfs4_recover_lock_ops = {
4673 .rpc_call_prepare = nfs4_recover_lock_prepare,
4674 .rpc_call_done = nfs4_lock_done,
4675 .rpc_release = nfs4_lock_release,
4676};
4677
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004678static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4679{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004680 switch (error) {
4681 case -NFS4ERR_ADMIN_REVOKED:
4682 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004683 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004684 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004685 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004686 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004687 break;
4688 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004689 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004690 case -NFS4ERR_EXPIRED:
4691 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004692 };
4693}
4694
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004695static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004696{
4697 struct nfs4_lockdata *data;
4698 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004699 struct rpc_message msg = {
4700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4701 .rpc_cred = state->owner->so_cred,
4702 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004703 struct rpc_task_setup task_setup_data = {
4704 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004705 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004706 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004707 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004708 .flags = RPC_TASK_ASYNC,
4709 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004710 int ret;
4711
Harvey Harrison3110ff82008-05-02 13:42:44 -07004712 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004713 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004714 fl->fl_u.nfs4_fl.owner,
4715 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004716 if (data == NULL)
4717 return -ENOMEM;
4718 if (IS_SETLKW(cmd))
4719 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004720 if (recovery_type > NFS_LOCK_NEW) {
4721 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004722 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004723 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4724 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004725 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004726 msg.rpc_argp = &data->arg;
4727 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004728 task_setup_data.callback_data = data;
4729 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004730 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004731 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004732 ret = nfs4_wait_for_completion_rpc_task(task);
4733 if (ret == 0) {
4734 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004735 if (ret)
4736 nfs4_handle_setlk_error(data->server, data->lsp,
4737 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004738 } else
4739 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004740 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004741 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
4745static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4746{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004747 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004748 struct nfs4_exception exception = {
4749 .inode = state->inode,
4750 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004751 int err;
4752
4753 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004754 /* Cache the lock if possible... */
4755 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4756 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004757 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004758 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004759 break;
4760 nfs4_handle_exception(server, err, &exception);
4761 } while (exception.retry);
4762 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763}
4764
4765static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4766{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004767 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004768 struct nfs4_exception exception = {
4769 .inode = state->inode,
4770 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004771 int err;
4772
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004773 err = nfs4_set_lock_state(state, request);
4774 if (err != 0)
4775 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004776 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004777 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4778 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004779 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004780 switch (err) {
4781 default:
4782 goto out;
4783 case -NFS4ERR_GRACE:
4784 case -NFS4ERR_DELAY:
4785 nfs4_handle_exception(server, err, &exception);
4786 err = 0;
4787 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004788 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004789out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004790 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791}
4792
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004793#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004794/**
4795 * nfs41_check_expired_locks - possibly free a lock stateid
4796 *
4797 * @state: NFSv4 state for an inode
4798 *
4799 * Returns NFS_OK if recovery for this stateid is now finished.
4800 * Otherwise a negative NFS4ERR value is returned.
4801 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004802static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004803{
Chuck Levereb64cf92012-07-11 16:30:05 -04004804 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004805 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004806 struct nfs_server *server = NFS_SERVER(state->inode);
4807
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004808 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004809 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004810 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4811 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004812 /* Free the stateid unless the server
4813 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004814 if (status != -NFS4ERR_BAD_STATEID)
4815 nfs41_free_stateid(server,
4816 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004817 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004818 ret = status;
4819 }
4820 }
4821 };
4822
4823 return ret;
4824}
4825
4826static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4827{
4828 int status = NFS_OK;
4829
4830 if (test_bit(LK_STATE_IN_USE, &state->flags))
4831 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004832 if (status != NFS_OK)
4833 status = nfs4_lock_expired(state, request);
4834 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004835}
4836#endif
4837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4839{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004840 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004841 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004842 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004844 if ((fl_flags & FL_POSIX) &&
4845 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4846 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004847 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004848 status = nfs4_set_lock_state(state, request);
4849 if (status != 0)
4850 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004851 request->fl_flags |= FL_ACCESS;
4852 status = do_vfs_lock(request->fl_file, request);
4853 if (status < 0)
4854 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004855 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004856 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004857 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004858 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004859 request->fl_flags = fl_flags & ~FL_SLEEP;
4860 status = do_vfs_lock(request->fl_file, request);
4861 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004862 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004863 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004864 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004865 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004866 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004867 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004868 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004869 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4870 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004871out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004872 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004873out:
4874 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 return status;
4876}
4877
4878static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4879{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004880 struct nfs4_exception exception = {
4881 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004882 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004883 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 int err;
4885
4886 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004887 err = _nfs4_proc_setlk(state, cmd, request);
4888 if (err == -NFS4ERR_DENIED)
4889 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004891 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 } while (exception.retry);
4893 return err;
4894}
4895
4896static int
4897nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4898{
4899 struct nfs_open_context *ctx;
4900 struct nfs4_state *state;
4901 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4902 int status;
4903
4904 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004905 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 state = ctx->state;
4907
4908 if (request->fl_start < 0 || request->fl_end < 0)
4909 return -EINVAL;
4910
Trond Myklebustd9531262009-07-21 19:22:38 -04004911 if (IS_GETLK(cmd)) {
4912 if (state != NULL)
4913 return nfs4_proc_getlk(state, F_GETLK, request);
4914 return 0;
4915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
4917 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4918 return -EINVAL;
4919
Trond Myklebustd9531262009-07-21 19:22:38 -04004920 if (request->fl_type == F_UNLCK) {
4921 if (state != NULL)
4922 return nfs4_proc_unlck(state, cmd, request);
4923 return 0;
4924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Trond Myklebustd9531262009-07-21 19:22:38 -04004926 if (state == NULL)
4927 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004928 /*
4929 * Don't rely on the VFS having checked the file open mode,
4930 * since it won't do this for flock() locks.
4931 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004932 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004933 case F_RDLCK:
4934 if (!(filp->f_mode & FMODE_READ))
4935 return -EBADF;
4936 break;
4937 case F_WRLCK:
4938 if (!(filp->f_mode & FMODE_WRITE))
4939 return -EBADF;
4940 }
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 do {
4943 status = nfs4_proc_setlk(state, cmd, request);
4944 if ((status != -EAGAIN) || IS_SETLK(cmd))
4945 break;
4946 timeout = nfs4_set_lock_task_retry(timeout);
4947 status = -ERESTARTSYS;
4948 if (signalled())
4949 break;
4950 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 return status;
4952}
4953
Trond Myklebust888e6942005-11-04 15:38:11 -05004954int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4955{
4956 struct nfs_server *server = NFS_SERVER(state->inode);
4957 struct nfs4_exception exception = { };
4958 int err;
4959
4960 err = nfs4_set_lock_state(state, fl);
4961 if (err != 0)
4962 goto out;
4963 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004964 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004965 switch (err) {
4966 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004967 printk(KERN_ERR "NFS: %s: unhandled error "
4968 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004969 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004970 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004971 goto out;
4972 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004973 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004974 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004975 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004976 nfs4_schedule_lease_recovery(server->nfs_client);
4977 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004978 case -NFS4ERR_BADSESSION:
4979 case -NFS4ERR_BADSLOT:
4980 case -NFS4ERR_BAD_HIGH_SLOT:
4981 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4982 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004983 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004984 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004985 case -ERESTARTSYS:
4986 /*
4987 * The show must go on: exit, but mark the
4988 * stateid as needing recovery.
4989 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004990 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004991 case -NFS4ERR_ADMIN_REVOKED:
4992 case -NFS4ERR_BAD_STATEID:
4993 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004994 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004995 err = 0;
4996 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004997 case -EKEYEXPIRED:
4998 /*
4999 * User RPCSEC_GSS context has expired.
5000 * We cannot recover this stateid now, so
5001 * skip it and allow recovery thread to
5002 * proceed.
5003 */
5004 err = 0;
5005 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005006 case -ENOMEM:
5007 case -NFS4ERR_DENIED:
5008 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5009 err = 0;
5010 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005011 case -NFS4ERR_DELAY:
5012 break;
5013 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005014 err = nfs4_handle_exception(server, err, &exception);
5015 } while (exception.retry);
5016out:
5017 return err;
5018}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005019
Trond Myklebustcf470c32012-03-07 13:49:12 -05005020struct nfs_release_lockowner_data {
5021 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005022 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005023 struct nfs_release_lockowner_args args;
5024};
5025
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005026static void nfs4_release_lockowner_release(void *calldata)
5027{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005028 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005029 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005030 kfree(calldata);
5031}
5032
Trond Myklebust17280172012-03-11 13:11:00 -04005033static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005034 .rpc_release = nfs4_release_lockowner_release,
5035};
5036
Trond Myklebustcf470c32012-03-07 13:49:12 -05005037int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005038{
5039 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005040 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005041 struct rpc_message msg = {
5042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5043 };
5044
5045 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005046 return -EINVAL;
5047 data = kmalloc(sizeof(*data), GFP_NOFS);
5048 if (!data)
5049 return -ENOMEM;
5050 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005051 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005052 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5053 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5054 data->args.lock_owner.s_dev = server->s_dev;
5055 msg.rpc_argp = &data->args;
5056 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5057 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005058}
5059
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005060#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5061
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005062static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5063 const void *buf, size_t buflen,
5064 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005065{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005066 if (strcmp(key, "") != 0)
5067 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005068
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005069 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005070}
5071
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005072static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5073 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005074{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005075 if (strcmp(key, "") != 0)
5076 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005077
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005078 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005079}
5080
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005081static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5082 size_t list_len, const char *name,
5083 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005084{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005085 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005086
J. Bruce Fields096455a2006-03-20 23:23:42 -05005087 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5088 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005089
5090 if (list && len <= list_len)
5091 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005092 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005093}
5094
Andy Adamson533eb462011-06-13 18:25:56 -04005095/*
5096 * nfs_fhget will use either the mounted_on_fileid or the fileid
5097 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005098static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5099{
Andy Adamson533eb462011-06-13 18:25:56 -04005100 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5101 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5102 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005103 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005104 return;
5105
5106 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005107 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005108 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5109 fattr->nlink = 2;
5110}
5111
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005112static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5113 const struct qstr *name,
5114 struct nfs4_fs_locations *fs_locations,
5115 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005116{
5117 struct nfs_server *server = NFS_SERVER(dir);
5118 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005119 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005120 };
5121 struct nfs4_fs_locations_arg args = {
5122 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005123 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005124 .page = page,
5125 .bitmask = bitmask,
5126 };
Benny Halevy22958462009-04-01 09:22:02 -04005127 struct nfs4_fs_locations_res res = {
5128 .fs_locations = fs_locations,
5129 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005130 struct rpc_message msg = {
5131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5132 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005133 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005134 };
5135 int status;
5136
Harvey Harrison3110ff82008-05-02 13:42:44 -07005137 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005138
5139 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5140 * is not supported */
5141 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5142 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5143 else
5144 bitmask[0] |= FATTR4_WORD0_FILEID;
5145
Trond Myklebustc228fd32007-01-13 02:28:11 -05005146 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005147 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005148 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005149 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005150 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005151 return status;
5152}
5153
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005154int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5155 const struct qstr *name,
5156 struct nfs4_fs_locations *fs_locations,
5157 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005158{
5159 struct nfs4_exception exception = { };
5160 int err;
5161 do {
5162 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005163 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005164 &exception);
5165 } while (exception.retry);
5166 return err;
5167}
5168
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005169static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5170{
5171 int status;
5172 struct nfs4_secinfo_arg args = {
5173 .dir_fh = NFS_FH(dir),
5174 .name = name,
5175 };
5176 struct nfs4_secinfo_res res = {
5177 .flavors = flavors,
5178 };
5179 struct rpc_message msg = {
5180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5181 .rpc_argp = &args,
5182 .rpc_resp = &res,
5183 };
5184
5185 dprintk("NFS call secinfo %s\n", name->name);
5186 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5187 dprintk("NFS reply secinfo: %d\n", status);
5188 return status;
5189}
5190
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005191int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5192 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005193{
5194 struct nfs4_exception exception = { };
5195 int err;
5196 do {
5197 err = nfs4_handle_exception(NFS_SERVER(dir),
5198 _nfs4_proc_secinfo(dir, name, flavors),
5199 &exception);
5200 } while (exception.retry);
5201 return err;
5202}
5203
Andy Adamson557134a2009-04-01 09:21:53 -04005204#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005205/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005206 * Check the exchange flags returned by the server for invalid flags, having
5207 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5208 * DS flags set.
5209 */
5210static int nfs4_check_cl_exchange_flags(u32 flags)
5211{
5212 if (flags & ~EXCHGID4_FLAG_MASK_R)
5213 goto out_inval;
5214 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5215 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5216 goto out_inval;
5217 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5218 goto out_inval;
5219 return NFS_OK;
5220out_inval:
5221 return -NFS4ERR_INVAL;
5222}
5223
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005224static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005225nfs41_same_server_scope(struct nfs41_server_scope *a,
5226 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005227{
5228 if (a->server_scope_sz == b->server_scope_sz &&
5229 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5230 return true;
5231
5232 return false;
5233}
5234
Andy Adamson357f54d2010-12-14 10:11:57 -05005235/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005236 * nfs4_proc_bind_conn_to_session()
5237 *
5238 * The 4.1 client currently uses the same TCP connection for the
5239 * fore and backchannel.
5240 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005241int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005242{
5243 int status;
5244 struct nfs41_bind_conn_to_session_res res;
5245 struct rpc_message msg = {
5246 .rpc_proc =
5247 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5248 .rpc_argp = clp,
5249 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005250 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005251 };
5252
5253 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005254
5255 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5256 if (unlikely(res.session == NULL)) {
5257 status = -ENOMEM;
5258 goto out;
5259 }
5260
5261 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5262 if (status == 0) {
5263 if (memcmp(res.session->sess_id.data,
5264 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5265 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5266 status = -EIO;
5267 goto out_session;
5268 }
5269 if (res.dir != NFS4_CDFS4_BOTH) {
5270 dprintk("NFS: %s: Unexpected direction from server\n",
5271 __func__);
5272 status = -EIO;
5273 goto out_session;
5274 }
5275 if (res.use_conn_in_rdma_mode) {
5276 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5277 __func__);
5278 status = -EIO;
5279 goto out_session;
5280 }
5281 }
5282out_session:
5283 kfree(res.session);
5284out:
5285 dprintk("<-- %s status= %d\n", __func__, status);
5286 return status;
5287}
5288
5289/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005290 * nfs4_proc_exchange_id()
5291 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005292 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5293 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005294 * Since the clientid has expired, all compounds using sessions
5295 * associated with the stale clientid will be returning
5296 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5297 * be in some phase of session reset.
5298 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005299int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005300{
5301 nfs4_verifier verifier;
5302 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005303 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005304 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005305 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005306 };
5307 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005308 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005309 };
5310 int status;
5311 struct rpc_message msg = {
5312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5313 .rpc_argp = &args,
5314 .rpc_resp = &res,
5315 .rpc_cred = cred,
5316 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005317
Chuck Leverf0920752012-05-21 22:45:41 -04005318 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005319 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5320 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005321 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5322 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5323 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005324
Chuck Leveracdeb692012-05-21 22:46:16 -04005325 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005326 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005327 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005328 status = -ENOMEM;
5329 goto out;
5330 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005331
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005332 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005333 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005334 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005335 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005336 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005337 }
5338
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005339 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005340 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005341 status = -ENOMEM;
5342 goto out_server_scope;
5343 }
5344
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005345 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005346 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005347 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005348
Chuck Lever177313f2012-05-21 22:44:58 -04005349 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005350 clp->cl_clientid = res.clientid;
5351 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5352 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5353 clp->cl_seqid = res.seqid;
5354
Chuck Leveracdeb692012-05-21 22:46:16 -04005355 kfree(clp->cl_serverowner);
5356 clp->cl_serverowner = res.server_owner;
5357 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005358
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005359 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005360 kfree(clp->cl_implid);
5361 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005362
Chuck Lever177313f2012-05-21 22:44:58 -04005363 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005364 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005365 res.server_scope)) {
5366 dprintk("%s: server_scope mismatch detected\n",
5367 __func__);
5368 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005369 kfree(clp->cl_serverscope);
5370 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005371 }
5372
Chuck Lever177313f2012-05-21 22:44:58 -04005373 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005374 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005375 goto out;
5376 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005377 } else
5378 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005379
Chuck Leveracdeb692012-05-21 22:46:16 -04005380out_server_owner:
5381 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005382out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005383 kfree(res.server_scope);
5384out:
Chuck Lever177313f2012-05-21 22:44:58 -04005385 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005386 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005387 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005388 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005389 clp->cl_implid->date.seconds,
5390 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005391 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005392 return status;
5393}
5394
Trond Myklebust66245532012-05-25 17:18:09 -04005395static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5396 struct rpc_cred *cred)
5397{
5398 struct rpc_message msg = {
5399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5400 .rpc_argp = clp,
5401 .rpc_cred = cred,
5402 };
5403 int status;
5404
5405 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5406 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005407 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005408 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5409 return status;
5410}
5411
5412static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5413 struct rpc_cred *cred)
5414{
5415 unsigned int loop;
5416 int ret;
5417
5418 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5419 ret = _nfs4_proc_destroy_clientid(clp, cred);
5420 switch (ret) {
5421 case -NFS4ERR_DELAY:
5422 case -NFS4ERR_CLIENTID_BUSY:
5423 ssleep(1);
5424 break;
5425 default:
5426 return ret;
5427 }
5428 }
5429 return 0;
5430}
5431
5432int nfs4_destroy_clientid(struct nfs_client *clp)
5433{
5434 struct rpc_cred *cred;
5435 int ret = 0;
5436
5437 if (clp->cl_mvops->minor_version < 1)
5438 goto out;
5439 if (clp->cl_exchange_flags == 0)
5440 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005441 if (clp->cl_preserve_clid)
5442 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005443 cred = nfs4_get_exchange_id_cred(clp);
5444 ret = nfs4_proc_destroy_clientid(clp, cred);
5445 if (cred)
5446 put_rpccred(cred);
5447 switch (ret) {
5448 case 0:
5449 case -NFS4ERR_STALE_CLIENTID:
5450 clp->cl_exchange_flags = 0;
5451 }
5452out:
5453 return ret;
5454}
5455
Andy Adamson2050f0c2009-04-01 09:22:30 -04005456struct nfs4_get_lease_time_data {
5457 struct nfs4_get_lease_time_args *args;
5458 struct nfs4_get_lease_time_res *res;
5459 struct nfs_client *clp;
5460};
5461
5462static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5463 void *calldata)
5464{
5465 int ret;
5466 struct nfs4_get_lease_time_data *data =
5467 (struct nfs4_get_lease_time_data *)calldata;
5468
5469 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005470 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005471 /* just setup sequence, do not trigger session recovery
5472 since we're invoked within one */
5473 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005474 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005475 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005476
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005477 if (ret != -EAGAIN)
5478 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005479 dprintk("<-- %s\n", __func__);
5480}
5481
5482/*
5483 * Called from nfs4_state_manager thread for session setup, so don't recover
5484 * from sequence operation or clientid errors.
5485 */
5486static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5487{
5488 struct nfs4_get_lease_time_data *data =
5489 (struct nfs4_get_lease_time_data *)calldata;
5490
5491 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005492 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5493 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005494 switch (task->tk_status) {
5495 case -NFS4ERR_DELAY:
5496 case -NFS4ERR_GRACE:
5497 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5498 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5499 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005500 /* fall through */
5501 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005502 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005503 return;
5504 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005505 dprintk("<-- %s\n", __func__);
5506}
5507
Trond Myklebust17280172012-03-11 13:11:00 -04005508static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005509 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5510 .rpc_call_done = nfs4_get_lease_time_done,
5511};
5512
5513int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5514{
5515 struct rpc_task *task;
5516 struct nfs4_get_lease_time_args args;
5517 struct nfs4_get_lease_time_res res = {
5518 .lr_fsinfo = fsinfo,
5519 };
5520 struct nfs4_get_lease_time_data data = {
5521 .args = &args,
5522 .res = &res,
5523 .clp = clp,
5524 };
5525 struct rpc_message msg = {
5526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5527 .rpc_argp = &args,
5528 .rpc_resp = &res,
5529 };
5530 struct rpc_task_setup task_setup = {
5531 .rpc_client = clp->cl_rpcclient,
5532 .rpc_message = &msg,
5533 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005534 .callback_data = &data,
5535 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005536 };
5537 int status;
5538
Trond Myklebust9d12b212012-01-17 22:04:25 -05005539 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005540 dprintk("--> %s\n", __func__);
5541 task = rpc_run_task(&task_setup);
5542
5543 if (IS_ERR(task))
5544 status = PTR_ERR(task);
5545 else {
5546 status = task->tk_status;
5547 rpc_put_task(task);
5548 }
5549 dprintk("<-- %s return %d\n", __func__, status);
5550
5551 return status;
5552}
5553
Andy Adamsonfc931582009-04-01 09:22:31 -04005554/*
5555 * Initialize the values to be used by the client in CREATE_SESSION
5556 * If nfs4_init_session set the fore channel request and response sizes,
5557 * use them.
5558 *
5559 * Set the back channel max_resp_sz_cached to zero to force the client to
5560 * always set csa_cachethis to FALSE because the current implementation
5561 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5562 */
5563static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5564{
5565 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005566 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5567 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005568
5569 if (mxrqst_sz == 0)
5570 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5571 if (mxresp_sz == 0)
5572 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5573 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005574 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5575 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005576 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005577 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005578
5579 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005580 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005581 __func__,
5582 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005583 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005584
5585 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005586 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5587 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5588 args->bc_attrs.max_resp_sz_cached = 0;
5589 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5590 args->bc_attrs.max_reqs = 1;
5591
5592 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5593 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5594 __func__,
5595 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5596 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5597 args->bc_attrs.max_reqs);
5598}
5599
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005600static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005601{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005602 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5603 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5604
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005605 if (rcvd->max_resp_sz > sent->max_resp_sz)
5606 return -EINVAL;
5607 /*
5608 * Our requested max_ops is the minimum we need; we're not
5609 * prepared to break up compounds into smaller pieces than that.
5610 * So, no point even trying to continue if the server won't
5611 * cooperate:
5612 */
5613 if (rcvd->max_ops < sent->max_ops)
5614 return -EINVAL;
5615 if (rcvd->max_reqs == 0)
5616 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005617 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5618 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005619 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005620}
5621
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005622static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5623{
5624 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5625 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005626
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005627 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5628 return -EINVAL;
5629 if (rcvd->max_resp_sz < sent->max_resp_sz)
5630 return -EINVAL;
5631 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5632 return -EINVAL;
5633 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005634 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005635 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005636 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005637 return -EINVAL;
5638 return 0;
5639}
Andy Adamson8d353012009-04-01 09:22:32 -04005640
Andy Adamson8d353012009-04-01 09:22:32 -04005641static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5642 struct nfs4_session *session)
5643{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005644 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005645
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005646 ret = nfs4_verify_fore_channel_attrs(args, session);
5647 if (ret)
5648 return ret;
5649 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005650}
5651
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005652static int _nfs4_proc_create_session(struct nfs_client *clp,
5653 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005654{
5655 struct nfs4_session *session = clp->cl_session;
5656 struct nfs41_create_session_args args = {
5657 .client = clp,
5658 .cb_program = NFS4_CALLBACK,
5659 };
5660 struct nfs41_create_session_res res = {
5661 .client = clp,
5662 };
5663 struct rpc_message msg = {
5664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5665 .rpc_argp = &args,
5666 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005667 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005668 };
5669 int status;
5670
5671 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005672 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005673
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005674 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005675
Trond Myklebust43095d32012-11-20 11:13:12 -05005676 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005677 /* Verify the session's negotiated channel_attrs values */
5678 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005679 /* Increment the clientid slot sequence id */
5680 clp->cl_seqid++;
5681 }
5682
5683 return status;
5684}
5685
5686/*
5687 * Issues a CREATE_SESSION operation to the server.
5688 * It is the responsibility of the caller to verify the session is
5689 * expired before calling this routine.
5690 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005691int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005692{
5693 int status;
5694 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005695 struct nfs4_session *session = clp->cl_session;
5696
5697 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5698
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005699 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005700 if (status)
5701 goto out;
5702
Andy Adamsonaacd5532011-11-09 13:58:21 -05005703 /* Init or reset the session slot tables */
5704 status = nfs4_setup_session_slot_tables(session);
5705 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005706 if (status)
5707 goto out;
5708
5709 ptr = (unsigned *)&session->sess_id.data[0];
5710 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5711 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005712out:
5713 dprintk("<-- %s\n", __func__);
5714 return status;
5715}
5716
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005717/*
5718 * Issue the over-the-wire RPC DESTROY_SESSION.
5719 * The caller must serialize access to this routine.
5720 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005721int nfs4_proc_destroy_session(struct nfs4_session *session,
5722 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005723{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005724 struct rpc_message msg = {
5725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5726 .rpc_argp = session,
5727 .rpc_cred = cred,
5728 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005729 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005730
5731 dprintk("--> nfs4_proc_destroy_session\n");
5732
5733 /* session is still being setup */
5734 if (session->clp->cl_cons_state != NFS_CS_READY)
5735 return status;
5736
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005737 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005738
5739 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005740 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005741 "Session has been destroyed regardless...\n", status);
5742
5743 dprintk("<-- nfs4_proc_destroy_session\n");
5744 return status;
5745}
5746
Trond Myklebust7b38c362012-05-23 13:23:31 -04005747/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005748 * Renew the cl_session lease.
5749 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005750struct nfs4_sequence_data {
5751 struct nfs_client *clp;
5752 struct nfs4_sequence_args args;
5753 struct nfs4_sequence_res res;
5754};
5755
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005756static void nfs41_sequence_release(void *data)
5757{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005758 struct nfs4_sequence_data *calldata = data;
5759 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005760
Alexandros Batsakis71358402010-02-05 03:45:05 -08005761 if (atomic_read(&clp->cl_count) > 1)
5762 nfs4_schedule_state_renewal(clp);
5763 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005764 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005765}
5766
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005767static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5768{
5769 switch(task->tk_status) {
5770 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005771 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5772 return -EAGAIN;
5773 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005774 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005775 }
5776 return 0;
5777}
5778
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005779static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005780{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005781 struct nfs4_sequence_data *calldata = data;
5782 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005783
Trond Myklebust14516c32010-07-31 14:29:06 -04005784 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5785 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005786
5787 if (task->tk_status < 0) {
5788 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005789 if (atomic_read(&clp->cl_count) == 1)
5790 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005791
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005792 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5793 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005794 return;
5795 }
5796 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005797 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005798out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005799 dprintk("<-- %s\n", __func__);
5800}
5801
5802static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5803{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005804 struct nfs4_sequence_data *calldata = data;
5805 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005806 struct nfs4_sequence_args *args;
5807 struct nfs4_sequence_res *res;
5808
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005809 args = task->tk_msg.rpc_argp;
5810 res = task->tk_msg.rpc_resp;
5811
Trond Myklebust9d12b212012-01-17 22:04:25 -05005812 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005813 return;
5814 rpc_call_start(task);
5815}
5816
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005817static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5818{
5819 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5820 nfs41_sequence_prepare(task, data);
5821}
5822
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005823static const struct rpc_call_ops nfs41_sequence_ops = {
5824 .rpc_call_done = nfs41_sequence_call_done,
5825 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005826 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005827};
5828
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005829static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5830 .rpc_call_done = nfs41_sequence_call_done,
5831 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5832 .rpc_release = nfs41_sequence_release,
5833};
5834
5835static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5836 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005837{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005838 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005839 struct rpc_message msg = {
5840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5841 .rpc_cred = cred,
5842 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005843 struct rpc_task_setup task_setup_data = {
5844 .rpc_client = clp->cl_rpcclient,
5845 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005846 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005847 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5848 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005849
Alexandros Batsakis71358402010-02-05 03:45:05 -08005850 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005851 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005852 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005853 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005854 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005855 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005856 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005857 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005858 msg.rpc_argp = &calldata->args;
5859 msg.rpc_resp = &calldata->res;
5860 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005861 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005862
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005863 return rpc_run_task(&task_setup_data);
5864}
5865
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005866static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005867{
5868 struct rpc_task *task;
5869 int ret = 0;
5870
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005871 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5872 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005873 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005874 if (IS_ERR(task))
5875 ret = PTR_ERR(task);
5876 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005877 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005878 dprintk("<-- %s status=%d\n", __func__, ret);
5879 return ret;
5880}
5881
5882static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5883{
5884 struct rpc_task *task;
5885 int ret;
5886
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05005887 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005888 if (IS_ERR(task)) {
5889 ret = PTR_ERR(task);
5890 goto out;
5891 }
5892 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005893 if (!ret) {
5894 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5895
5896 if (task->tk_status == 0)
5897 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005898 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005899 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005900 rpc_put_task(task);
5901out:
5902 dprintk("<-- %s status=%d\n", __func__, ret);
5903 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005904}
5905
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005906struct nfs4_reclaim_complete_data {
5907 struct nfs_client *clp;
5908 struct nfs41_reclaim_complete_args arg;
5909 struct nfs41_reclaim_complete_res res;
5910};
5911
5912static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5913{
5914 struct nfs4_reclaim_complete_data *calldata = data;
5915
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005916 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005917 if (nfs41_setup_sequence(calldata->clp->cl_session,
5918 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005919 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005920 return;
5921
5922 rpc_call_start(task);
5923}
5924
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005925static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5926{
5927 switch(task->tk_status) {
5928 case 0:
5929 case -NFS4ERR_COMPLETE_ALREADY:
5930 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5931 break;
5932 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005933 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005934 /* fall through */
5935 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005936 return -EAGAIN;
5937 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005938 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005939 }
5940 return 0;
5941}
5942
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005943static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5944{
5945 struct nfs4_reclaim_complete_data *calldata = data;
5946 struct nfs_client *clp = calldata->clp;
5947 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5948
5949 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005950 if (!nfs41_sequence_done(task, res))
5951 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005952
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005953 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5954 rpc_restart_call_prepare(task);
5955 return;
5956 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005957 dprintk("<-- %s\n", __func__);
5958}
5959
5960static void nfs4_free_reclaim_complete_data(void *data)
5961{
5962 struct nfs4_reclaim_complete_data *calldata = data;
5963
5964 kfree(calldata);
5965}
5966
5967static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5968 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5969 .rpc_call_done = nfs4_reclaim_complete_done,
5970 .rpc_release = nfs4_free_reclaim_complete_data,
5971};
5972
5973/*
5974 * Issue a global reclaim complete.
5975 */
5976static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5977{
5978 struct nfs4_reclaim_complete_data *calldata;
5979 struct rpc_task *task;
5980 struct rpc_message msg = {
5981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5982 };
5983 struct rpc_task_setup task_setup_data = {
5984 .rpc_client = clp->cl_rpcclient,
5985 .rpc_message = &msg,
5986 .callback_ops = &nfs4_reclaim_complete_call_ops,
5987 .flags = RPC_TASK_ASYNC,
5988 };
5989 int status = -ENOMEM;
5990
5991 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005992 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005993 if (calldata == NULL)
5994 goto out;
5995 calldata->clp = clp;
5996 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005997
Trond Myklebust9d12b212012-01-17 22:04:25 -05005998 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005999 msg.rpc_argp = &calldata->arg;
6000 msg.rpc_resp = &calldata->res;
6001 task_setup_data.callback_data = calldata;
6002 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006003 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006004 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006005 goto out;
6006 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006007 status = nfs4_wait_for_completion_rpc_task(task);
6008 if (status == 0)
6009 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006010 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006011 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006012out:
6013 dprintk("<-- %s status=%d\n", __func__, status);
6014 return status;
6015}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006016
6017static void
6018nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6019{
6020 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006021 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006022 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006023
6024 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006025 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6026 * right now covering the LAYOUTGET we are about to send.
6027 * However, that is not so catastrophic, and there seems
6028 * to be no way to prevent it completely.
6029 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006030 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006031 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006032 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006033 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6034 NFS_I(lgp->args.inode)->layout,
6035 lgp->args.ctx->state)) {
6036 rpc_exit(task, NFS4_OK);
6037 return;
6038 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006039 rpc_call_start(task);
6040}
6041
6042static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6043{
6044 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006045 struct inode *inode = lgp->args.inode;
6046 struct nfs_server *server = NFS_SERVER(inode);
6047 struct pnfs_layout_hdr *lo;
6048 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006049
6050 dprintk("--> %s\n", __func__);
6051
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006052 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006053 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006054
6055 switch (task->tk_status) {
6056 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006057 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006058 case -NFS4ERR_LAYOUTTRYLATER:
6059 case -NFS4ERR_RECALLCONFLICT:
6060 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006061 break;
6062 case -NFS4ERR_EXPIRED:
6063 case -NFS4ERR_BAD_STATEID:
6064 spin_lock(&inode->i_lock);
6065 lo = NFS_I(inode)->layout;
6066 if (!lo || list_empty(&lo->plh_segs)) {
6067 spin_unlock(&inode->i_lock);
6068 /* If the open stateid was bad, then recover it. */
6069 state = lgp->args.ctx->state;
6070 } else {
6071 LIST_HEAD(head);
6072
6073 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6074 spin_unlock(&inode->i_lock);
6075 /* Mark the bad layout state as invalid, then
6076 * retry using the open stateid. */
6077 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006078 }
6079 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006080 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6081 rpc_restart_call_prepare(task);
6082out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006083 dprintk("<-- %s\n", __func__);
6084}
6085
Idan Kedar85541162012-08-02 11:47:10 +03006086static size_t max_response_pages(struct nfs_server *server)
6087{
6088 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6089 return nfs_page_array_len(0, max_resp_sz);
6090}
6091
6092static void nfs4_free_pages(struct page **pages, size_t size)
6093{
6094 int i;
6095
6096 if (!pages)
6097 return;
6098
6099 for (i = 0; i < size; i++) {
6100 if (!pages[i])
6101 break;
6102 __free_page(pages[i]);
6103 }
6104 kfree(pages);
6105}
6106
6107static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6108{
6109 struct page **pages;
6110 int i;
6111
6112 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6113 if (!pages) {
6114 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6115 return NULL;
6116 }
6117
6118 for (i = 0; i < size; i++) {
6119 pages[i] = alloc_page(gfp_flags);
6120 if (!pages[i]) {
6121 dprintk("%s: failed to allocate page\n", __func__);
6122 nfs4_free_pages(pages, size);
6123 return NULL;
6124 }
6125 }
6126
6127 return pages;
6128}
6129
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006130static void nfs4_layoutget_release(void *calldata)
6131{
6132 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006133 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6134 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006135
6136 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006137 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006138 put_nfs_open_context(lgp->args.ctx);
6139 kfree(calldata);
6140 dprintk("<-- %s\n", __func__);
6141}
6142
6143static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6144 .rpc_call_prepare = nfs4_layoutget_prepare,
6145 .rpc_call_done = nfs4_layoutget_done,
6146 .rpc_release = nfs4_layoutget_release,
6147};
6148
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006149struct pnfs_layout_segment *
6150nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006151{
6152 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006153 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006154 struct rpc_task *task;
6155 struct rpc_message msg = {
6156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6157 .rpc_argp = &lgp->args,
6158 .rpc_resp = &lgp->res,
6159 };
6160 struct rpc_task_setup task_setup_data = {
6161 .rpc_client = server->client,
6162 .rpc_message = &msg,
6163 .callback_ops = &nfs4_layoutget_call_ops,
6164 .callback_data = lgp,
6165 .flags = RPC_TASK_ASYNC,
6166 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006167 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006168 int status = 0;
6169
6170 dprintk("--> %s\n", __func__);
6171
Idan Kedar85541162012-08-02 11:47:10 +03006172 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6173 if (!lgp->args.layout.pages) {
6174 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006175 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006176 }
6177 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6178
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006179 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006180 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006181 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006182 task = rpc_run_task(&task_setup_data);
6183 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006184 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006185 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006186 if (status == 0)
6187 status = task->tk_status;
6188 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006189 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006190 rpc_put_task(task);
6191 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006192 if (status)
6193 return ERR_PTR(status);
6194 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006195}
6196
Benny Halevycbe82602011-05-22 19:52:37 +03006197static void
6198nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6199{
6200 struct nfs4_layoutreturn *lrp = calldata;
6201
6202 dprintk("--> %s\n", __func__);
6203 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006204 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006205 return;
6206 rpc_call_start(task);
6207}
6208
6209static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6210{
6211 struct nfs4_layoutreturn *lrp = calldata;
6212 struct nfs_server *server;
6213
6214 dprintk("--> %s\n", __func__);
6215
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006216 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006217 return;
6218
6219 server = NFS_SERVER(lrp->args.inode);
6220 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006221 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006222 return;
6223 }
Benny Halevycbe82602011-05-22 19:52:37 +03006224 dprintk("<-- %s\n", __func__);
6225}
6226
6227static void nfs4_layoutreturn_release(void *calldata)
6228{
6229 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006230 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006231
6232 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006233 spin_lock(&lo->plh_inode->i_lock);
6234 if (lrp->res.lrs_present)
6235 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6236 lo->plh_block_lgets--;
6237 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006238 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006239 kfree(calldata);
6240 dprintk("<-- %s\n", __func__);
6241}
6242
6243static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6244 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6245 .rpc_call_done = nfs4_layoutreturn_done,
6246 .rpc_release = nfs4_layoutreturn_release,
6247};
6248
6249int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6250{
6251 struct rpc_task *task;
6252 struct rpc_message msg = {
6253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6254 .rpc_argp = &lrp->args,
6255 .rpc_resp = &lrp->res,
6256 };
6257 struct rpc_task_setup task_setup_data = {
6258 .rpc_client = lrp->clp->cl_rpcclient,
6259 .rpc_message = &msg,
6260 .callback_ops = &nfs4_layoutreturn_call_ops,
6261 .callback_data = lrp,
6262 };
6263 int status;
6264
6265 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006266 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006267 task = rpc_run_task(&task_setup_data);
6268 if (IS_ERR(task))
6269 return PTR_ERR(task);
6270 status = task->tk_status;
6271 dprintk("<-- %s status=%d\n", __func__, status);
6272 rpc_put_task(task);
6273 return status;
6274}
6275
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006276/*
6277 * Retrieve the list of Data Server devices from the MDS.
6278 */
6279static int _nfs4_getdevicelist(struct nfs_server *server,
6280 const struct nfs_fh *fh,
6281 struct pnfs_devicelist *devlist)
6282{
6283 struct nfs4_getdevicelist_args args = {
6284 .fh = fh,
6285 .layoutclass = server->pnfs_curr_ld->id,
6286 };
6287 struct nfs4_getdevicelist_res res = {
6288 .devlist = devlist,
6289 };
6290 struct rpc_message msg = {
6291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6292 .rpc_argp = &args,
6293 .rpc_resp = &res,
6294 };
6295 int status;
6296
6297 dprintk("--> %s\n", __func__);
6298 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6299 &res.seq_res, 0);
6300 dprintk("<-- %s status=%d\n", __func__, status);
6301 return status;
6302}
6303
6304int nfs4_proc_getdevicelist(struct nfs_server *server,
6305 const struct nfs_fh *fh,
6306 struct pnfs_devicelist *devlist)
6307{
6308 struct nfs4_exception exception = { };
6309 int err;
6310
6311 do {
6312 err = nfs4_handle_exception(server,
6313 _nfs4_getdevicelist(server, fh, devlist),
6314 &exception);
6315 } while (exception.retry);
6316
6317 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6318 err, devlist->num_devs);
6319
6320 return err;
6321}
6322EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6323
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006324static int
6325_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6326{
6327 struct nfs4_getdeviceinfo_args args = {
6328 .pdev = pdev,
6329 };
6330 struct nfs4_getdeviceinfo_res res = {
6331 .pdev = pdev,
6332 };
6333 struct rpc_message msg = {
6334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6335 .rpc_argp = &args,
6336 .rpc_resp = &res,
6337 };
6338 int status;
6339
6340 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006341 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006342 dprintk("<-- %s status=%d\n", __func__, status);
6343
6344 return status;
6345}
6346
6347int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6348{
6349 struct nfs4_exception exception = { };
6350 int err;
6351
6352 do {
6353 err = nfs4_handle_exception(server,
6354 _nfs4_proc_getdeviceinfo(server, pdev),
6355 &exception);
6356 } while (exception.retry);
6357 return err;
6358}
6359EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6360
Andy Adamson863a3c62011-03-23 13:27:54 +00006361static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6362{
6363 struct nfs4_layoutcommit_data *data = calldata;
6364 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006365 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006366
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006367 if (nfs41_setup_sequence(session, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006368 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006369 return;
6370 rpc_call_start(task);
6371}
6372
6373static void
6374nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6375{
6376 struct nfs4_layoutcommit_data *data = calldata;
6377 struct nfs_server *server = NFS_SERVER(data->args.inode);
6378
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006379 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006380 return;
6381
6382 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006383 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6384 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6385 case -NFS4ERR_BADLAYOUT: /* no layout */
6386 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006387 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006388 break;
6389 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006390 nfs_post_op_update_inode_force_wcc(data->args.inode,
6391 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006392 break;
6393 default:
6394 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6395 rpc_restart_call_prepare(task);
6396 return;
6397 }
6398 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006399}
6400
6401static void nfs4_layoutcommit_release(void *calldata)
6402{
6403 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006404 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006405 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006406
Andy Adamsondb29c082011-07-30 20:52:38 -04006407 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006408 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006409 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6410 list_del_init(&lseg->pls_lc_list);
6411 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6412 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006413 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006414 }
Peng Tao92407e72011-10-23 20:21:17 -07006415
6416 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6417 smp_mb__after_clear_bit();
6418 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6419
Andy Adamson863a3c62011-03-23 13:27:54 +00006420 put_rpccred(data->cred);
6421 kfree(data);
6422}
6423
6424static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6425 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6426 .rpc_call_done = nfs4_layoutcommit_done,
6427 .rpc_release = nfs4_layoutcommit_release,
6428};
6429
6430int
Andy Adamsonef311532011-03-12 02:58:10 -05006431nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006432{
6433 struct rpc_message msg = {
6434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6435 .rpc_argp = &data->args,
6436 .rpc_resp = &data->res,
6437 .rpc_cred = data->cred,
6438 };
6439 struct rpc_task_setup task_setup_data = {
6440 .task = &data->task,
6441 .rpc_client = NFS_CLIENT(data->args.inode),
6442 .rpc_message = &msg,
6443 .callback_ops = &nfs4_layoutcommit_ops,
6444 .callback_data = data,
6445 .flags = RPC_TASK_ASYNC,
6446 };
6447 struct rpc_task *task;
6448 int status = 0;
6449
6450 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6451 "lbw: %llu inode %lu\n",
6452 data->task.tk_pid, sync,
6453 data->args.lastbytewritten,
6454 data->args.inode->i_ino);
6455
Trond Myklebust9d12b212012-01-17 22:04:25 -05006456 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006457 task = rpc_run_task(&task_setup_data);
6458 if (IS_ERR(task))
6459 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006460 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006461 goto out;
6462 status = nfs4_wait_for_completion_rpc_task(task);
6463 if (status != 0)
6464 goto out;
6465 status = task->tk_status;
6466out:
6467 dprintk("%s: status %d\n", __func__, status);
6468 rpc_put_task(task);
6469 return status;
6470}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006471
6472static int
6473_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6474 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6475{
6476 struct nfs41_secinfo_no_name_args args = {
6477 .style = SECINFO_STYLE_CURRENT_FH,
6478 };
6479 struct nfs4_secinfo_res res = {
6480 .flavors = flavors,
6481 };
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6484 .rpc_argp = &args,
6485 .rpc_resp = &res,
6486 };
6487 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6488}
6489
6490static int
6491nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6492 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6493{
6494 struct nfs4_exception exception = { };
6495 int err;
6496 do {
6497 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6498 switch (err) {
6499 case 0:
6500 case -NFS4ERR_WRONGSEC:
6501 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006502 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006503 default:
6504 err = nfs4_handle_exception(server, err, &exception);
6505 }
6506 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006507out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006508 return err;
6509}
6510
6511static int
6512nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6513 struct nfs_fsinfo *info)
6514{
6515 int err;
6516 struct page *page;
6517 rpc_authflavor_t flavor;
6518 struct nfs4_secinfo_flavors *flavors;
6519
6520 page = alloc_page(GFP_KERNEL);
6521 if (!page) {
6522 err = -ENOMEM;
6523 goto out;
6524 }
6525
6526 flavors = page_address(page);
6527 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6528
6529 /*
6530 * Fall back on "guess and check" method if
6531 * the server doesn't support SECINFO_NO_NAME
6532 */
6533 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6534 err = nfs4_find_root_sec(server, fhandle, info);
6535 goto out_freepage;
6536 }
6537 if (err)
6538 goto out_freepage;
6539
6540 flavor = nfs_find_best_sec(flavors);
6541 if (err == 0)
6542 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6543
6544out_freepage:
6545 put_page(page);
6546 if (err == -EACCES)
6547 return -EPERM;
6548out:
6549 return err;
6550}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006551
6552static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006553{
6554 int status;
6555 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006556 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006557 };
6558 struct nfs41_test_stateid_res res;
6559 struct rpc_message msg = {
6560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6561 .rpc_argp = &args,
6562 .rpc_resp = &res,
6563 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006564
Chuck Lever38527b12012-07-11 16:30:23 -04006565 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006566 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006567 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006568 if (status != NFS_OK) {
6569 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006570 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006571 }
6572 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006573 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006574}
6575
Chuck Lever38527b12012-07-11 16:30:23 -04006576/**
6577 * nfs41_test_stateid - perform a TEST_STATEID operation
6578 *
6579 * @server: server / transport on which to perform the operation
6580 * @stateid: state ID to test
6581 *
6582 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6583 * Otherwise a negative NFS4ERR value is returned if the operation
6584 * failed or the state ID is not currently valid.
6585 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006586static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006587{
6588 struct nfs4_exception exception = { };
6589 int err;
6590 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006591 err = _nfs41_test_stateid(server, stateid);
6592 if (err != -NFS4ERR_DELAY)
6593 break;
6594 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006595 } while (exception.retry);
6596 return err;
6597}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006598
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006599static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006600{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006601 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006602 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006603 };
6604 struct nfs41_free_stateid_res res;
6605 struct rpc_message msg = {
6606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6607 .rpc_argp = &args,
6608 .rpc_resp = &res,
6609 };
Chuck Lever38527b12012-07-11 16:30:23 -04006610 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006611
Chuck Lever38527b12012-07-11 16:30:23 -04006612 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006613 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006614 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006615 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006616 dprintk("NFS reply free_stateid: %d\n", status);
6617 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006618}
6619
Chuck Lever38527b12012-07-11 16:30:23 -04006620/**
6621 * nfs41_free_stateid - perform a FREE_STATEID operation
6622 *
6623 * @server: server / transport on which to perform the operation
6624 * @stateid: state ID to release
6625 *
6626 * Returns NFS_OK if the server freed "stateid". Otherwise a
6627 * negative NFS4ERR value is returned.
6628 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006629static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006630{
6631 struct nfs4_exception exception = { };
6632 int err;
6633 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006634 err = _nfs4_free_stateid(server, stateid);
6635 if (err != -NFS4ERR_DELAY)
6636 break;
6637 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006638 } while (exception.retry);
6639 return err;
6640}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006641
6642static bool nfs41_match_stateid(const nfs4_stateid *s1,
6643 const nfs4_stateid *s2)
6644{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006645 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006646 return false;
6647
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006648 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006649 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006650 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006651 return true;
6652
6653 return false;
6654}
6655
Andy Adamson557134a2009-04-01 09:21:53 -04006656#endif /* CONFIG_NFS_V4_1 */
6657
Trond Myklebust36281ca2012-03-04 18:13:56 -05006658static bool nfs4_match_stateid(const nfs4_stateid *s1,
6659 const nfs4_stateid *s2)
6660{
Trond Myklebustf597c532012-03-04 18:13:56 -05006661 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006662}
6663
6664
Trond Myklebust17280172012-03-11 13:11:00 -04006665static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006666 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006667 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 .recover_open = nfs4_open_reclaim,
6669 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006670 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006671 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006672 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673};
6674
Andy Adamson591d71c2009-04-01 09:22:47 -04006675#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006676static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006677 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6678 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6679 .recover_open = nfs4_open_reclaim,
6680 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006681 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006682 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006683 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006684 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006685};
6686#endif /* CONFIG_NFS_V4_1 */
6687
Trond Myklebust17280172012-03-11 13:11:00 -04006688static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006689 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006690 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 .recover_open = nfs4_open_expired,
6692 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006693 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006694 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695};
6696
Andy Adamson591d71c2009-04-01 09:22:47 -04006697#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006698static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006699 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6700 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006701 .recover_open = nfs41_open_expired,
6702 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006703 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006704 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006705};
6706#endif /* CONFIG_NFS_V4_1 */
6707
Trond Myklebust17280172012-03-11 13:11:00 -04006708static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006709 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006710 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006711 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006712};
6713
6714#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006715static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006716 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006717 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006718 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006719};
6720#endif
6721
Trond Myklebust97dc1352010-06-16 09:52:26 -04006722static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6723 .minor_version = 0,
6724 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006725 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006726 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006727 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6728 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6729 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006730};
6731
6732#if defined(CONFIG_NFS_V4_1)
6733static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6734 .minor_version = 1,
6735 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006736 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006737 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006738 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6739 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6740 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006741};
6742#endif
6743
Trond Myklebust97dc1352010-06-16 09:52:26 -04006744const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6745 [0] = &nfs_v4_0_minor_ops,
6746#if defined(CONFIG_NFS_V4_1)
6747 [1] = &nfs_v4_1_minor_ops,
6748#endif
6749};
6750
Bryan Schumaker73a79702012-07-16 16:39:12 -04006751const struct inode_operations nfs4_dir_inode_operations = {
6752 .create = nfs_create,
6753 .lookup = nfs_lookup,
6754 .atomic_open = nfs_atomic_open,
6755 .link = nfs_link,
6756 .unlink = nfs_unlink,
6757 .symlink = nfs_symlink,
6758 .mkdir = nfs_mkdir,
6759 .rmdir = nfs_rmdir,
6760 .mknod = nfs_mknod,
6761 .rename = nfs_rename,
6762 .permission = nfs_permission,
6763 .getattr = nfs_getattr,
6764 .setattr = nfs_setattr,
6765 .getxattr = generic_getxattr,
6766 .setxattr = generic_setxattr,
6767 .listxattr = generic_listxattr,
6768 .removexattr = generic_removexattr,
6769};
6770
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006771static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006772 .permission = nfs_permission,
6773 .getattr = nfs_getattr,
6774 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006775 .getxattr = generic_getxattr,
6776 .setxattr = generic_setxattr,
6777 .listxattr = generic_listxattr,
6778 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006779};
6780
David Howells509de812006-08-22 20:06:11 -04006781const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 .version = 4, /* protocol version */
6783 .dentry_ops = &nfs4_dentry_operations,
6784 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006785 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006786 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006788 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006789 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 .getattr = nfs4_proc_getattr,
6791 .setattr = nfs4_proc_setattr,
6792 .lookup = nfs4_proc_lookup,
6793 .access = nfs4_proc_access,
6794 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 .create = nfs4_proc_create,
6796 .remove = nfs4_proc_remove,
6797 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006798 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 .unlink_done = nfs4_proc_unlink_done,
6800 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006801 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006802 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006803 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 .link = nfs4_proc_link,
6805 .symlink = nfs4_proc_symlink,
6806 .mkdir = nfs4_proc_mkdir,
6807 .rmdir = nfs4_proc_remove,
6808 .readdir = nfs4_proc_readdir,
6809 .mknod = nfs4_proc_mknod,
6810 .statfs = nfs4_proc_statfs,
6811 .fsinfo = nfs4_proc_fsinfo,
6812 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006813 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 .decode_dirent = nfs4_decode_dirent,
6815 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006816 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006817 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006818 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006820 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006821 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006822 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006824 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006825 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006827 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006828 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006829 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006830 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006831 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006832 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006833 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006834 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006835 .create_server = nfs4_create_server,
6836 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837};
6838
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006839static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6840 .prefix = XATTR_NAME_NFSV4_ACL,
6841 .list = nfs4_xattr_list_nfs4_acl,
6842 .get = nfs4_xattr_get_nfs4_acl,
6843 .set = nfs4_xattr_set_nfs4_acl,
6844};
6845
6846const struct xattr_handler *nfs4_xattr_handlers[] = {
6847 &nfs4_xattr_nfs4_acl_handler,
6848 NULL
6849};
6850
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851/*
6852 * Local variables:
6853 * c-basic-offset: 8
6854 * End:
6855 */